element-web/test/components/views/settings/AddPrivilegedUsers-test.tsx

162 lines
6.5 KiB
TypeScript
Raw Normal View History

2022-12-08 11:40:31 +00:00
/*
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.
*/
2022-12-12 11:24:14 +00:00
import React from "react";
import { act, fireEvent, render, waitFor } from "@testing-library/react";
2022-12-08 11:40:31 +00:00
import userEvent from "@testing-library/user-event";
import { mocked } from "jest-mock";
import { RoomMember, EventType } from "matrix-js-sdk/src/matrix";
2022-12-12 11:24:14 +00:00
import { getMockClientWithEventEmitter, makeRoomWithStateEvents, mkEvent } from "../../../test-utils";
import MatrixClientContext from "../../../../src/contexts/MatrixClientContext";
2022-12-08 11:40:31 +00:00
import {
AddPrivilegedUsers,
2022-12-12 11:24:14 +00:00
getUserIdsFromCompletions,
hasLowerOrEqualLevelThanDefaultLevel,
2022-12-08 11:40:31 +00:00
} from "../../../../src/components/views/settings/AddPrivilegedUsers";
import UserProvider from "../../../../src/autocomplete/UserProvider";
import { ICompletion } from "../../../../src/autocomplete/Autocompleter";
2022-12-12 11:24:14 +00:00
jest.mock("../../../../src/autocomplete/UserProvider");
jest.mock("../../../../src/stores/WidgetStore");
jest.mock("../../../../src/stores/widgets/WidgetLayoutStore");
2022-12-08 11:40:31 +00:00
const completions: ICompletion[] = [
{
component: <div />,
type: "user",
completion: "user_1",
completionId: "@user_1:host.local",
range: { start: 1, end: 1 },
},
{
component: <div />,
type: "user",
completion: "user_2",
completionId: "@user_2:host.local",
range: { start: 1, end: 1 },
},
{ component: <div />, type: "user", completion: "user_without_completion_id", range: { start: 1, end: 1 } },
2022-12-08 11:40:31 +00:00
];
2022-12-12 11:24:14 +00:00
describe("<AddPrivilegedUsers />", () => {
2022-12-08 11:40:31 +00:00
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(),
});
2022-12-12 11:24:14 +00:00
const room = makeRoomWithStateEvents([], { roomId: "room_id", mockClient: mockClient });
2022-12-08 11:40:31 +00:00
room.getMember = (userId: string) => {
2022-12-12 11:24:14 +00:00
const member = new RoomMember("room_id", userId);
2022-12-08 11:40:31 +00:00
member.powerLevel = 0;
return member;
};
(room.currentState.getStateEvents as unknown) = (_eventType: string, _stateKey: string) => {
return mkEvent({
type: EventType.RoomPowerLevels,
content: {},
2022-12-12 11:24:14 +00:00
user: "user_id",
2022-12-08 11:40:31 +00:00
});
};
2022-12-12 11:24:14 +00:00
const getComponent = () => (
2022-12-08 11:40:31 +00:00
<MatrixClientContext.Provider value={mockClient}>
2022-12-12 11:24:14 +00:00
<AddPrivilegedUsers room={room} defaultUserLevel={0} />
</MatrixClientContext.Provider>
);
2022-12-08 11:40:31 +00:00
2022-12-12 11:24:14 +00:00
it("checks whether form submit works as intended", async () => {
2022-12-08 11:40:31 +00:00
const { getByTestId, queryAllByTestId } = render(getComponent());
// Verify that the submit button is disabled initially.
2022-12-12 11:24:14 +00:00
const submitButton = getByTestId("add-privileged-users-submit-button");
2022-12-08 11:40:31 +00:00
expect(submitButton).toBeDisabled();
// Find some suggestions and select them.
2022-12-12 11:24:14 +00:00
const autocompleteInput = getByTestId("autocomplete-input");
2022-12-08 11:40:31 +00:00
act(() => {
fireEvent.focus(autocompleteInput);
2022-12-12 11:24:14 +00:00
fireEvent.change(autocompleteInput, { target: { value: "u" } });
2022-12-08 11:40:31 +00:00
});
await waitFor(() => expect(provider.mock.instances[0].getCompletions).toHaveBeenCalledTimes(1));
2022-12-12 11:24:14 +00:00
const matchOne = getByTestId("autocomplete-suggestion-item-@user_1:host.local");
const matchTwo = getByTestId("autocomplete-suggestion-item-@user_2:host.local");
2022-12-08 11:40:31 +00:00
act(() => {
fireEvent.mouseDown(matchOne);
});
act(() => {
fireEvent.mouseDown(matchTwo);
});
// Check that `defaultUserLevel` is initially set and select a higher power level.
2022-12-12 11:24:14 +00:00
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();
2022-12-08 11:40:31 +00:00
2022-12-12 11:24:14 +00:00
const powerLevelSelect = getByTestId("power-level-select-element");
2022-12-08 11:40:31 +00:00
await userEvent.selectOptions(powerLevelSelect, "100");
2022-12-12 11:24:14 +00:00
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();
2022-12-08 11:40:31 +00:00
// 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.
2022-12-12 11:24:14 +00:00
const selectionItems = queryAllByTestId("autocomplete-selection-item", { exact: false });
2022-12-08 11:40:31 +00:00
expect(selectionItems).toHaveLength(0);
// Verify that power level select is reset to `defaultUserLevel`.
2022-12-12 11:24:14 +00:00
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();
2022-12-08 11:40:31 +00:00
});
2022-12-12 11:24:14 +00:00
it("getUserIdsFromCompletions() should map completions to user id's", () => {
expect(getUserIdsFromCompletions(completions)).toStrictEqual(["@user_1:host.local", "@user_2:host.local"]);
2022-12-08 11:40:31 +00:00
});
it.each([
{ defaultUserLevel: -50, expectation: false },
{ defaultUserLevel: 0, expectation: true },
{ defaultUserLevel: 50, expectation: true },
2022-12-12 11:24:14 +00:00
])(
"hasLowerOrEqualLevelThanDefaultLevel() should return $expectation for default level $defaultUserLevel",
2022-12-08 11:40:31 +00:00
({ defaultUserLevel, expectation }) => {
expect(hasLowerOrEqualLevelThanDefaultLevel(room, completions[0], defaultUserLevel)).toBe(expectation);
},
);
});