add-privileged-users-in-room (#9596)

This commit is contained in:
Marco Bartelt 2022-12-08 12:40:31 +01:00 committed by GitHub
parent 982c83d2a8
commit 95ac957fa4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 927 additions and 3 deletions

View file

@ -46,6 +46,7 @@
@import "./components/views/typography/_Caption.pcss"; @import "./components/views/typography/_Caption.pcss";
@import "./compound/_Icon.pcss"; @import "./compound/_Icon.pcss";
@import "./structures/_AutoHideScrollbar.pcss"; @import "./structures/_AutoHideScrollbar.pcss";
@import "./structures/_AutocompleteInput.pcss";
@import "./structures/_BackdropPanel.pcss"; @import "./structures/_BackdropPanel.pcss";
@import "./structures/_CompatibilityPage.pcss"; @import "./structures/_CompatibilityPage.pcss";
@import "./structures/_ContextualMenu.pcss"; @import "./structures/_ContextualMenu.pcss";

View file

@ -0,0 +1,129 @@
/*
Copyright 2022 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
.mx_AutocompleteInput {
position: relative;
}
.mx_AutocompleteInput_search_icon {
margin-left: $spacing-8;
fill: $secondary-content;
}
.mx_AutocompleteInput_editor {
flex: 1;
display: flex;
flex-wrap: wrap;
align-items: center;
overflow-x: hidden;
overflow-y: auto;
border: 1px solid $input-border-color;
border-radius: 4px;
transition: border-color 0.25s;
> input {
flex: 1;
min-width: 40%;
resize: none;
// `!important` is required to bypass global input styles.
margin: 0 !important;
padding: $spacing-8 9px;
border: none !important;
color: $primary-content !important;
font-weight: normal !important;
&::placeholder {
color: $primary-content !important;
font-weight: normal !important;
}
}
}
.mx_AutocompleteInput_editor--focused {
border-color: $links;
}
.mx_AutocompleteInput_editor--has-suggestions {
border-bottom-left-radius: 0;
border-bottom-right-radius: 0;
}
.mx_AutocompleteInput_editor_selection {
display: flex;
margin-left: $spacing-8;
}
.mx_AutocompleteInput_editor_selection_pill {
display: flex;
align-items: center;
border-radius: 12px;
padding-left: $spacing-8;
padding-right: $spacing-8;
background-color: $username-variant1-color;
color: #ffffff;
font-size: $font-12px;
}
.mx_AutocompleteInput_editor_selection_remove_button {
padding: 0 $spacing-4;
}
.mx_AutocompleteInput_matches {
position: absolute;
left: 0;
right: 0;
background-color: $background;
border: 1px solid $links;
border-top-color: $input-border-color;
border-bottom-left-radius: 4px;
border-bottom-right-radius: 4px;
z-index: 1000;
}
.mx_AutocompleteInput_suggestion {
display: flex;
align-items: center;
padding: $spacing-8;
cursor: pointer;
> * {
user-select: none;
}
&:hover {
background-color: $quinary-content;
border-bottom-left-radius: 4px;
border-bottom-right-radius: 4px;
}
}
.mx_AutocompleteInput_suggestion--selected {
background-color: $quinary-content;
&:last-child {
border-bottom-left-radius: 4px;
border-bottom-right-radius: 4px;
}
}
.mx_AutocompleteInput_suggestion_title {
margin-right: $spacing-8;
}
.mx_AutocompleteInput_suggestion_description {
color: $secondary-content;
font-size: $font-12px;
}

View file

@ -1,3 +1,3 @@
<svg width="18" height="18" viewBox="0 0 18 18" fill="none" xmlns="http://www.w3.org/2000/svg"> <svg width="18" height="18" viewBox="0 0 18 18" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd" d="M12.1333 8.06667C12.1333 10.3126 10.3126 12.1333 8.06667 12.1333C5.82071 12.1333 4 10.3126 4 8.06667C4 5.82071 5.82071 4 8.06667 4C10.3126 4 12.1333 5.82071 12.1333 8.06667ZM12.9992 11.5994C13.7131 10.6044 14.1333 9.38463 14.1333 8.06667C14.1333 4.71614 11.4172 2 8.06667 2C4.71614 2 2 4.71614 2 8.06667C2 11.4172 4.71614 14.1333 8.06667 14.1333C9.38457 14.1333 10.6043 13.7131 11.5992 12.9993C11.6274 13.0369 11.6586 13.0729 11.6928 13.1071L14.2928 15.7071C14.6833 16.0977 15.3165 16.0977 15.707 15.7071C16.0975 15.3166 16.0975 14.6834 15.707 14.2929L13.107 11.6929C13.0728 11.6587 13.0368 11.6276 12.9992 11.5994Z" fill="black"/> <path fill-rule="evenodd" clip-rule="evenodd" d="M12.1333 8.06667C12.1333 10.3126 10.3126 12.1333 8.06667 12.1333C5.82071 12.1333 4 10.3126 4 8.06667C4 5.82071 5.82071 4 8.06667 4C10.3126 4 12.1333 5.82071 12.1333 8.06667ZM12.9992 11.5994C13.7131 10.6044 14.1333 9.38463 14.1333 8.06667C14.1333 4.71614 11.4172 2 8.06667 2C4.71614 2 2 4.71614 2 8.06667C2 11.4172 4.71614 14.1333 8.06667 14.1333C9.38457 14.1333 10.6043 13.7131 11.5992 12.9993C11.6274 13.0369 11.6586 13.0729 11.6928 13.1071L14.2928 15.7071C14.6833 16.0977 15.3165 16.0977 15.707 15.7071C16.0975 15.3166 16.0975 14.6834 15.707 14.2929L13.107 11.6929C13.0728 11.6587 13.0368 11.6276 12.9992 11.5994Z" fill="black"/>
</svg> </svg>

Before

Width:  |  Height:  |  Size: 784 B

After

Width:  |  Height:  |  Size: 772 B

View file

@ -0,0 +1,248 @@
/*
Copyright 2022 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import React, { useState, ReactNode, ChangeEvent, KeyboardEvent, useRef, ReactElement } from 'react';
import classNames from 'classnames';
import Autocompleter from "../../autocomplete/AutocompleteProvider";
import { Key } from '../../Keyboard';
import { ICompletion } from '../../autocomplete/Autocompleter';
import AccessibleButton from '../../components/views/elements/AccessibleButton';
import { Icon as PillRemoveIcon } from '../../../res/img/icon-pill-remove.svg';
import { Icon as SearchIcon } from '../../../res/img/element-icons/roomlist/search.svg';
import useFocus from "../../hooks/useFocus";
interface AutocompleteInputProps {
provider: Autocompleter;
placeholder: string;
selection: ICompletion[];
onSelectionChange: (selection: ICompletion[]) => void;
maxSuggestions?: number;
renderSuggestion?: (s: ICompletion) => ReactElement;
renderSelection?: (m: ICompletion) => ReactElement;
additionalFilter?: (suggestion: ICompletion) => boolean;
}
export const AutocompleteInput: React.FC<AutocompleteInputProps> = ({
provider,
renderSuggestion,
renderSelection,
maxSuggestions = 5,
placeholder,
onSelectionChange,
selection,
additionalFilter,
}) => {
const [query, setQuery] = useState<string>('');
const [suggestions, setSuggestions] = useState<ICompletion[]>([]);
const [isFocused, onFocusChangeHandlerFunctions] = useFocus();
const editorContainerRef = useRef<HTMLDivElement>(null);
const editorRef = useRef<HTMLInputElement>(null);
const focusEditor = () => {
editorRef?.current?.focus();
};
const onQueryChange = async (e: ChangeEvent<HTMLInputElement>) => {
const value = e.target.value.trim();
setQuery(value);
let matches = await provider.getCompletions(
query,
{ start: query.length, end: query.length },
true,
maxSuggestions,
);
if (additionalFilter) {
matches = matches.filter(additionalFilter);
}
setSuggestions(matches);
};
const onClickInputArea = () => {
focusEditor();
};
const onKeyDown = (e: KeyboardEvent) => {
const hasModifiers = e.ctrlKey || e.shiftKey || e.metaKey;
// when the field is empty and the user hits backspace remove the right-most target
if (!query && selection.length > 0 && e.key === Key.BACKSPACE && !hasModifiers) {
removeSelection(selection[selection.length - 1]);
}
};
const toggleSelection = (completion: ICompletion) => {
const newSelection = [...selection];
const index = selection.findIndex(selection => selection.completionId === completion.completionId);
if (index >= 0) {
newSelection.splice(index, 1);
} else {
newSelection.push(completion);
}
onSelectionChange(newSelection);
focusEditor();
};
const removeSelection = (completion: ICompletion) => {
const newSelection = [...selection];
const index = selection.findIndex(selection => selection.completionId === completion.completionId);
if (index >= 0) {
newSelection.splice(index, 1);
onSelectionChange(newSelection);
}
};
const hasPlaceholder = (): boolean => selection.length === 0 && query.length === 0;
return (
<div className="mx_AutocompleteInput">
<div
ref={editorContainerRef}
className={classNames({
'mx_AutocompleteInput_editor': true,
'mx_AutocompleteInput_editor--focused': isFocused,
'mx_AutocompleteInput_editor--has-suggestions': suggestions.length > 0,
})}
onClick={onClickInputArea}
data-testid="autocomplete-editor"
>
<SearchIcon className="mx_AutocompleteInput_search_icon" width={16} height={16} />
{
selection.map(item => (
<SelectionItem
key={item.completionId}
item={item}
onClick={removeSelection}
render={renderSelection}
/>
))
}
<input
ref={editorRef}
type="text"
onKeyDown={onKeyDown}
onChange={onQueryChange}
value={query}
autoComplete="off"
placeholder={hasPlaceholder() ? placeholder : undefined}
data-testid="autocomplete-input"
{...onFocusChangeHandlerFunctions}
/>
</div>
{
(isFocused && suggestions.length) ? (
<div
className="mx_AutocompleteInput_matches"
style={{ top: editorContainerRef.current?.clientHeight }}
data-testid="autocomplete-matches"
>
{
suggestions.map((item) => (
<SuggestionItem
key={item.completionId}
item={item}
selection={selection}
onClick={toggleSelection}
render={renderSuggestion}
/>
))
}
</div>
) : null
}
</div>
);
};
type SelectionItemProps = {
item: ICompletion;
onClick: (completion: ICompletion) => void;
render?: (completion: ICompletion) => ReactElement;
};
const SelectionItem: React.FC<SelectionItemProps> = ({ item, onClick, render }) => {
const withContainer = (children: ReactNode): ReactElement => (
<span
className='mx_AutocompleteInput_editor_selection'
data-testid={`autocomplete-selection-item-${item.completionId}`}
>
<span className='mx_AutocompleteInput_editor_selection_pill'>
{ children }
</span>
<AccessibleButton
className='mx_AutocompleteInput_editor_selection_remove_button'
onClick={() => onClick(item)}
data-testid={`autocomplete-selection-remove-button-${item.completionId}`}
>
<PillRemoveIcon width={8} height={8} />
</AccessibleButton>
</span>
);
if (render) {
return withContainer(render(item));
}
return withContainer(
<span className='mx_AutocompleteInput_editor_selection_text'>{ item.completion }</span>,
);
};
type SuggestionItemProps = {
item: ICompletion;
selection: ICompletion[];
onClick: (completion: ICompletion) => void;
render?: (completion: ICompletion) => ReactElement;
};
const SuggestionItem: React.FC<SuggestionItemProps> = ({ item, selection, onClick, render }) => {
const isSelected = selection.some(selection => selection.completionId === item.completionId);
const classes = classNames({
'mx_AutocompleteInput_suggestion': true,
'mx_AutocompleteInput_suggestion--selected': isSelected,
});
const withContainer = (children: ReactNode): ReactElement => (
<div
className={classes}
// `onClick` cannot be used here as it would lead to focus loss and closing the suggestion list.
onMouseDown={(event) => {
event.preventDefault();
onClick(item);
}}
data-testid={`autocomplete-suggestion-item-${item.completionId}`}
>
{ children }
</div>
);
if (render) {
return withContainer(render(item));
}
return withContainer(
<>
<span className='mx_AutocompleteInput_suggestion_title'>{ item.completion }</span>
<span className='mx_AutocompleteInput_suggestion_description'>{ item.completionId }</span>
</>,
);
};

View file

@ -174,7 +174,15 @@ export default class PowerSelector extends React.Component<IProps, IState> {
}); });
options.push({ value: CUSTOM_VALUE, text: _t("Custom level") }); options.push({ value: CUSTOM_VALUE, text: _t("Custom level") });
const optionsElements = options.map((op) => { const optionsElements = options.map((op) => {
return <option value={op.value} key={op.value}>{ op.text }</option>; return (
<option
value={op.value}
key={op.value}
data-testid={`power-level-option-${op.value}`}
>
{ op.text }
</option>
);
}); });
picker = ( picker = (
@ -184,6 +192,7 @@ export default class PowerSelector extends React.Component<IProps, IState> {
onChange={this.onSelectChange} onChange={this.onSelectChange}
value={String(this.state.selectValue)} value={String(this.state.selectValue)}
disabled={this.props.disabled} disabled={this.props.disabled}
data-testid='power-level-select-element'
> >
{ optionsElements } { optionsElements }
</Field> </Field>

View file

@ -0,0 +1,132 @@
/*
Copyright 2022 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import React, { FormEvent, useCallback, useContext, useRef, useState } from 'react';
import { Room } from 'matrix-js-sdk/src/models/room';
import { EventType } from "matrix-js-sdk/src/@types/event";
import { _t } from "../../../languageHandler";
import { ICompletion } from '../../../autocomplete/Autocompleter';
import UserProvider from "../../../autocomplete/UserProvider";
import { AutocompleteInput } from "../../structures/AutocompleteInput";
import PowerSelector from "../elements/PowerSelector";
import MatrixClientContext from "../../../contexts/MatrixClientContext";
import AccessibleButton from "../elements/AccessibleButton";
import Modal from "../../../Modal";
import ErrorDialog from "../dialogs/ErrorDialog";
import SettingsFieldset from "./SettingsFieldset";
interface AddPrivilegedUsersProps {
room: Room;
defaultUserLevel: number;
}
export const AddPrivilegedUsers: React.FC<AddPrivilegedUsersProps> = ({ room, defaultUserLevel }) => {
const client = useContext(MatrixClientContext);
const userProvider = useRef(new UserProvider(room));
const [isLoading, setIsLoading] = useState<boolean>(false);
const [powerLevel, setPowerLevel] = useState<number>(defaultUserLevel);
const [selectedUsers, setSelectedUsers] = useState<ICompletion[]>([]);
const hasLowerOrEqualLevelThanDefaultLevelFilter = useCallback(
(user: ICompletion) => hasLowerOrEqualLevelThanDefaultLevel(room, user, defaultUserLevel),
[room, defaultUserLevel],
);
const onSubmit = async (event: FormEvent) => {
event.preventDefault();
setIsLoading(true);
const userIds = getUserIdsFromCompletions(selectedUsers);
const powerLevelEvent = room.currentState.getStateEvents(EventType.RoomPowerLevels, "");
// `RoomPowerLevels` event should exist, but technically it is not guaranteed.
if (powerLevelEvent === null) {
Modal.createDialog(ErrorDialog, {
title: _t("Error"),
description: _t("Failed to change power level"),
});
return;
}
try {
await client.setPowerLevel(room.roomId, userIds, powerLevel, powerLevelEvent);
setSelectedUsers([]);
setPowerLevel(defaultUserLevel);
} catch (error) {
Modal.createDialog(ErrorDialog, {
title: _t("Error"),
description: _t("Failed to change power level"),
});
} finally {
setIsLoading(false);
}
};
return (
<form style={{ display: 'flex' }} onSubmit={onSubmit}>
<SettingsFieldset
legend={_t('Add privileged users')}
description={_t('Give one or multiple users in this room more privileges')}
style={{ flexGrow: 1 }}
>
<AutocompleteInput
provider={userProvider.current}
placeholder={_t("Search users in this room…")}
onSelectionChange={setSelectedUsers}
selection={selectedUsers}
additionalFilter={hasLowerOrEqualLevelThanDefaultLevelFilter}
/>
<PowerSelector value={powerLevel} onChange={setPowerLevel} />
<AccessibleButton
type='submit'
element='button'
kind='primary'
disabled={!selectedUsers.length || isLoading}
onClick={null}
data-testid='add-privileged-users-submit-button'
>
{ _t('Apply') }
</AccessibleButton>
</SettingsFieldset>
</form>
);
};
export const hasLowerOrEqualLevelThanDefaultLevel = (
room: Room,
user: ICompletion,
defaultUserLevel: number,
) => {
if (user.completionId === undefined) {
return false;
}
const member = room.getMember(user.completionId);
if (member === null) {
return false;
}
return member.powerLevel <= defaultUserLevel;
};
export const getUserIdsFromCompletions = (completions: ICompletion[]) => {
const completionsWithId = completions.filter(completion => completion.completionId !== undefined);
// undefined completionId's are filtered out above but TypeScript does not seem to understand.
return completionsWithId.map(completion => completion.completionId!);
};

View file

@ -33,6 +33,7 @@ import SettingsStore from "../../../../../settings/SettingsStore";
import { VoiceBroadcastInfoEventType } from '../../../../../voice-broadcast'; import { VoiceBroadcastInfoEventType } from '../../../../../voice-broadcast';
import { ElementCall } from "../../../../../models/Call"; import { ElementCall } from "../../../../../models/Call";
import SdkConfig, { DEFAULTS } from "../../../../../SdkConfig"; import SdkConfig, { DEFAULTS } from "../../../../../SdkConfig";
import { AddPrivilegedUsers } from "../../AddPrivilegedUsers";
interface IEventShowOpts { interface IEventShowOpts {
isState?: boolean; isState?: boolean;
@ -470,6 +471,11 @@ export default class RolesRoomSettingsTab extends React.Component<IProps> {
<div className="mx_SettingsTab mx_RolesRoomSettingsTab"> <div className="mx_SettingsTab mx_RolesRoomSettingsTab">
<div className="mx_SettingsTab_heading">{ _t("Roles & Permissions") }</div> <div className="mx_SettingsTab_heading">{ _t("Roles & Permissions") }</div>
{ privilegedUsersSection } { privilegedUsersSection }
{
(canChangeLevels && room !== null) && (
<AddPrivilegedUsers room={room} defaultUserLevel={defaultUserLevel} />
)
}
{ mutedUsersSection } { mutedUsersSection }
{ bannedUsersSection } { bannedUsersSection }
<SettingsFieldset <SettingsFieldset

View file

@ -1298,6 +1298,11 @@
"Jump to first unread room.": "Jump to first unread room.", "Jump to first unread room.": "Jump to first unread room.",
"Jump to first invite.": "Jump to first invite.", "Jump to first invite.": "Jump to first invite.",
"Space options": "Space options", "Space options": "Space options",
"Failed to change power level": "Failed to change power level",
"Add privileged users": "Add privileged users",
"Give one or multiple users in this room more privileges": "Give one or multiple users in this room more privileges",
"Search users in this room…": "Search users in this room…",
"Apply": "Apply",
"Remove": "Remove", "Remove": "Remove",
"This bridge was provisioned by <user />.": "This bridge was provisioned by <user />.", "This bridge was provisioned by <user />.": "This bridge was provisioned by <user />.",
"This bridge is managed by <user />.": "This bridge is managed by <user />.", "This bridge is managed by <user />.": "This bridge is managed by <user />.",
@ -2227,7 +2232,6 @@
"Failed to mute user": "Failed to mute user", "Failed to mute user": "Failed to mute user",
"Unmute": "Unmute", "Unmute": "Unmute",
"Mute": "Mute", "Mute": "Mute",
"Failed to change power level": "Failed to change power level",
"You will not be able to undo this change as you are promoting the user to have the same power level as yourself.": "You will not be able to undo this change as you are promoting the user to have the same power level as yourself.", "You will not be able to undo this change as you are promoting the user to have the same power level as yourself.": "You will not be able to undo this change as you are promoting the user to have the same power level as yourself.",
"Are you sure?": "Are you sure?", "Are you sure?": "Are you sure?",
"Deactivate user?": "Deactivate user?", "Deactivate user?": "Deactivate user?",

View file

@ -0,0 +1,244 @@
/*
Copyright 2022 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import React from 'react';
import { screen, render, fireEvent, waitFor, within, act } from '@testing-library/react';
import * as TestUtils from '../../test-utils';
import AutocompleteProvider from '../../../src/autocomplete/AutocompleteProvider';
import { ICompletion } from '../../../src/autocomplete/Autocompleter';
import { AutocompleteInput } from "../../../src/components/structures/AutocompleteInput";
describe('AutocompleteInput', () => {
const mockCompletion: ICompletion[] = [
{ type: 'user', completion: 'user_1', completionId: '@user_1:host.local', range: { start: 1, end: 1 } },
{ type: 'user', completion: 'user_2', completionId: '@user_2:host.local', range: { start: 1, end: 1 } },
];
const constructMockProvider = (data: ICompletion[]) => ({
getCompletions: jest.fn().mockImplementation(async () => data),
}) as unknown as AutocompleteProvider;
beforeEach(() => {
TestUtils.stubClient();
});
const getEditorInput = () => {
const input = screen.getByTestId('autocomplete-input');
expect(input).toBeDefined();
return input;
};
it('should render suggestions when a query is set', async () => {
const mockProvider = constructMockProvider(mockCompletion);
const onSelectionChangeMock = jest.fn();
render(
<AutocompleteInput
provider={mockProvider}
placeholder='Search ...'
selection={[]}
onSelectionChange={onSelectionChangeMock}
/>,
);
const input = getEditorInput();
act(() => {
fireEvent.focus(input);
fireEvent.change(input, { target: { value: 'user' } });
});
await waitFor(() => expect(mockProvider.getCompletions).toHaveBeenCalledTimes(1));
expect(screen.getByTestId('autocomplete-matches').childNodes).toHaveLength(mockCompletion.length);
});
it('should render selected items passed in via props', () => {
const mockProvider = constructMockProvider(mockCompletion);
const onSelectionChangeMock = jest.fn();
render(
<AutocompleteInput
provider={mockProvider}
placeholder='Search ...'
selection={mockCompletion}
onSelectionChange={onSelectionChangeMock}
/>,
);
const editor = screen.getByTestId('autocomplete-editor');
const selection = within(editor).getAllByTestId("autocomplete-selection-item", { exact: false });
expect(selection).toHaveLength(mockCompletion.length);
});
it('should call onSelectionChange() when an item is removed from selection', () => {
const mockProvider = constructMockProvider(mockCompletion);
const onSelectionChangeMock = jest.fn();
render(
<AutocompleteInput
provider={mockProvider}
placeholder='Search ...'
selection={mockCompletion}
onSelectionChange={onSelectionChangeMock}
/>,
);
const editor = screen.getByTestId('autocomplete-editor');
const removeButtons = within(editor).getAllByTestId("autocomplete-selection-remove-button", { exact: false });
expect(removeButtons).toHaveLength(mockCompletion.length);
act(() => {
fireEvent.click(removeButtons[0]);
});
expect(onSelectionChangeMock).toHaveBeenCalledTimes(1);
expect(onSelectionChangeMock).toHaveBeenCalledWith([mockCompletion[1]]);
});
it('should render custom selection element when renderSelection() is defined', () => {
const mockProvider = constructMockProvider(mockCompletion);
const onSelectionChangeMock = jest.fn();
const renderSelection = () => (
<span data-testid='custom-selection-element'>custom selection element</span>
);
render(
<AutocompleteInput
provider={mockProvider}
placeholder='Search ...'
selection={mockCompletion}
onSelectionChange={onSelectionChangeMock}
renderSelection={renderSelection}
/>,
);
expect(screen.getAllByTestId('custom-selection-element')).toHaveLength(mockCompletion.length);
});
it('should render custom suggestion element when renderSuggestion() is defined', async () => {
const mockProvider = constructMockProvider(mockCompletion);
const onSelectionChangeMock = jest.fn();
const renderSuggestion = () => (
<span data-testid='custom-suggestion-element'>custom suggestion element</span>
);
render(
<AutocompleteInput
provider={mockProvider}
placeholder='Search ...'
selection={mockCompletion}
onSelectionChange={onSelectionChangeMock}
renderSuggestion={renderSuggestion}
/>,
);
const input = getEditorInput();
act(() => {
fireEvent.focus(input);
fireEvent.change(input, { target: { value: 'user' } });
});
await waitFor(() => expect(mockProvider.getCompletions).toHaveBeenCalledTimes(1));
expect(screen.getAllByTestId('custom-suggestion-element')).toHaveLength(mockCompletion.length);
});
it('should mark selected suggestions as selected', async () => {
const mockProvider = constructMockProvider(mockCompletion);
const onSelectionChangeMock = jest.fn();
const { container } = render(
<AutocompleteInput
provider={mockProvider}
placeholder='Search ...'
selection={mockCompletion}
onSelectionChange={onSelectionChangeMock}
/>,
);
const input = getEditorInput();
act(() => {
fireEvent.focus(input);
fireEvent.change(input, { target: { value: 'user' } });
});
await waitFor(() => expect(mockProvider.getCompletions).toHaveBeenCalledTimes(1));
const suggestions = await within(container).findAllByTestId('autocomplete-suggestion-item', { exact: false });
expect(suggestions).toHaveLength(mockCompletion.length);
suggestions.map(suggestion => expect(suggestion).toHaveClass('mx_AutocompleteInput_suggestion--selected'));
});
it('should remove the last added selection when backspace is pressed in empty input', () => {
const mockProvider = constructMockProvider(mockCompletion);
const onSelectionChangeMock = jest.fn();
render(
<AutocompleteInput
provider={mockProvider}
placeholder='Search ...'
selection={mockCompletion}
onSelectionChange={onSelectionChangeMock}
/>,
);
const input = getEditorInput();
act(() => {
fireEvent.keyDown(input, { key: 'Backspace' });
});
expect(onSelectionChangeMock).toHaveBeenCalledWith([mockCompletion[0]]);
});
it('should toggle a selected item when a suggestion is clicked', async () => {
const mockProvider = constructMockProvider(mockCompletion);
const onSelectionChangeMock = jest.fn();
const { container } = render(
<AutocompleteInput
provider={mockProvider}
placeholder='Search ...'
selection={[]}
onSelectionChange={onSelectionChangeMock}
/>,
);
const input = getEditorInput();
act(() => {
fireEvent.focus(input);
fireEvent.change(input, { target: { value: 'user' } });
});
const suggestions = await within(container).findAllByTestId('autocomplete-suggestion-item', { exact: false });
act(() => {
fireEvent.mouseDown(suggestions[0]);
});
expect(onSelectionChangeMock).toHaveBeenCalledWith([mockCompletion[0]]);
});
afterAll(() => {
jest.clearAllMocks();
jest.resetModules();
});
});

View file

@ -0,0 +1,151 @@
/*
Copyright 2022 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import React from 'react';
import { act, fireEvent, render, waitFor } from '@testing-library/react';
import userEvent from "@testing-library/user-event";
import { mocked } from "jest-mock";
import { RoomMember, EventType } from "matrix-js-sdk/src/matrix";
import {
getMockClientWithEventEmitter,
makeRoomWithStateEvents,
mkEvent,
} from "../../../test-utils";
import MatrixClientContext from '../../../../src/contexts/MatrixClientContext';
import {
AddPrivilegedUsers,
getUserIdsFromCompletions, hasLowerOrEqualLevelThanDefaultLevel,
} from "../../../../src/components/views/settings/AddPrivilegedUsers";
import UserProvider from "../../../../src/autocomplete/UserProvider";
import { ICompletion } from "../../../../src/autocomplete/Autocompleter";
jest.mock('../../../../src/autocomplete/UserProvider');
const completions: ICompletion[] = [
{ type: 'user', completion: 'user_1', completionId: '@user_1:host.local', range: { start: 1, end: 1 } },
{ type: 'user', completion: 'user_2', completionId: '@user_2:host.local', range: { start: 1, end: 1 } },
{ type: 'user', completion: 'user_without_completion_id', range: { start: 1, end: 1 } },
];
describe('<AddPrivilegedUsers />', () => {
const provider = mocked(UserProvider, { shallow: true });
provider.prototype.getCompletions.mockResolvedValue(completions);
const mockClient = getMockClientWithEventEmitter({
// `makeRoomWithStateEvents` only work's if `getRoom` is present.
getRoom: jest.fn(),
setPowerLevel: jest.fn(),
});
const room = makeRoomWithStateEvents([], { roomId: 'room_id', mockClient: mockClient });
room.getMember = (userId: string) => {
const member = new RoomMember('room_id', userId);
member.powerLevel = 0;
return member;
};
(room.currentState.getStateEvents as unknown) = (_eventType: string, _stateKey: string) => {
return mkEvent({
type: EventType.RoomPowerLevels,
content: {},
user: 'user_id',
});
};
const getComponent = () =>
<MatrixClientContext.Provider value={mockClient}>
<AddPrivilegedUsers
room={room}
defaultUserLevel={0}
/>
</MatrixClientContext.Provider>;
it('checks whether form submit works as intended', async () => {
const { getByTestId, queryAllByTestId } = render(getComponent());
// Verify that the submit button is disabled initially.
const submitButton = getByTestId('add-privileged-users-submit-button');
expect(submitButton).toBeDisabled();
// Find some suggestions and select them.
const autocompleteInput = getByTestId('autocomplete-input');
act(() => {
fireEvent.focus(autocompleteInput);
fireEvent.change(autocompleteInput, { target: { value: 'u' } });
});
await waitFor(() => expect(provider.mock.instances[0].getCompletions).toHaveBeenCalledTimes(1));
const matchOne = getByTestId('autocomplete-suggestion-item-@user_1:host.local');
const matchTwo = getByTestId('autocomplete-suggestion-item-@user_2:host.local');
act(() => {
fireEvent.mouseDown(matchOne);
});
act(() => {
fireEvent.mouseDown(matchTwo);
});
// Check that `defaultUserLevel` is initially set and select a higher power level.
expect((getByTestId('power-level-option-0') as HTMLOptionElement).selected).toBeTruthy();
expect((getByTestId('power-level-option-50') as HTMLOptionElement).selected).toBeFalsy();
expect((getByTestId('power-level-option-100') as HTMLOptionElement).selected).toBeFalsy();
const powerLevelSelect = getByTestId('power-level-select-element');
await userEvent.selectOptions(powerLevelSelect, "100");
expect((getByTestId('power-level-option-0') as HTMLOptionElement).selected).toBeFalsy();
expect((getByTestId('power-level-option-50') as HTMLOptionElement).selected).toBeFalsy();
expect((getByTestId('power-level-option-100') as HTMLOptionElement).selected).toBeTruthy();
// The submit button should be enabled now.
expect(submitButton).toBeEnabled();
// Submit the form.
act(() => {
fireEvent.submit(submitButton);
});
await waitFor(() => expect(mockClient.setPowerLevel).toHaveBeenCalledTimes(1));
// Verify that the submit button is disabled again.
expect(submitButton).toBeDisabled();
// Verify that previously selected items are reset.
const selectionItems = queryAllByTestId('autocomplete-selection-item', { exact: false });
expect(selectionItems).toHaveLength(0);
// Verify that power level select is reset to `defaultUserLevel`.
expect((getByTestId('power-level-option-0') as HTMLOptionElement).selected).toBeTruthy();
expect((getByTestId('power-level-option-50') as HTMLOptionElement).selected).toBeFalsy();
expect((getByTestId('power-level-option-100') as HTMLOptionElement).selected).toBeFalsy();
});
it('getUserIdsFromCompletions() should map completions to user id\'s', () => {
expect(getUserIdsFromCompletions(completions)).toStrictEqual(['@user_1:host.local', '@user_2:host.local']);
});
it.each([
{ defaultUserLevel: -50, expectation: false },
{ defaultUserLevel: 0, expectation: true },
{ defaultUserLevel: 50, expectation: true },
])('hasLowerOrEqualLevelThanDefaultLevel() should return $expectation for default level $defaultUserLevel',
({ defaultUserLevel, expectation }) => {
expect(hasLowerOrEqualLevelThanDefaultLevel(room, completions[0], defaultUserLevel)).toBe(expectation);
},
);
});