2022-05-06 12:09:28 +03:00
|
|
|
/*
|
2024-09-09 16:57:16 +03:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-05-06 12:09:28 +03:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 16:57:16 +03:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2022-05-06 12:09:28 +03:00
|
|
|
*/
|
|
|
|
|
2022-01-06 13:09:26 +03:00
|
|
|
import React from "react";
|
2022-03-22 13:32:35 +03:00
|
|
|
import { mocked } from "jest-mock";
|
2023-04-21 12:48:48 +03:00
|
|
|
import { randomString } from "matrix-js-sdk/src/randomstring";
|
2024-10-14 19:11:58 +03:00
|
|
|
import { act, fireEvent, render, RenderResult } from "jest-matrix-react";
|
2023-08-08 13:12:12 +03:00
|
|
|
import { EventType, MatrixClient, Room, GuestAccess, HistoryVisibility, JoinRule } from "matrix-js-sdk/src/matrix";
|
2022-01-06 13:09:26 +03:00
|
|
|
|
|
|
|
import _SpaceSettingsVisibilityTab from "../../../../src/components/views/spaces/SpaceSettingsVisibilityTab";
|
2023-08-03 15:56:30 +03:00
|
|
|
import {
|
|
|
|
createTestClient,
|
|
|
|
mkEvent,
|
|
|
|
wrapInMatrixClientContext,
|
|
|
|
mkSpace,
|
|
|
|
mockStateEventImplementation,
|
|
|
|
} from "../../../test-utils";
|
2022-01-06 13:09:26 +03:00
|
|
|
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
|
|
|
|
|
|
|
|
const SpaceSettingsVisibilityTab = wrapInMatrixClientContext(_SpaceSettingsVisibilityTab);
|
|
|
|
|
|
|
|
jest.useFakeTimers();
|
|
|
|
|
|
|
|
describe("<SpaceSettingsVisibilityTab />", () => {
|
|
|
|
const mockMatrixClient = createTestClient() as MatrixClient;
|
|
|
|
|
|
|
|
const makeJoinEvent = (rule: JoinRule = JoinRule.Invite) =>
|
|
|
|
mkEvent({
|
|
|
|
type: EventType.RoomJoinRules,
|
|
|
|
event: true,
|
|
|
|
content: {
|
|
|
|
join_rule: rule,
|
|
|
|
},
|
|
|
|
} as any);
|
|
|
|
const makeGuestAccessEvent = (rule: GuestAccess = GuestAccess.CanJoin) =>
|
|
|
|
mkEvent({
|
|
|
|
type: EventType.RoomGuestAccess,
|
|
|
|
event: true,
|
|
|
|
content: {
|
|
|
|
guest_access: rule,
|
|
|
|
},
|
|
|
|
} as any);
|
|
|
|
const makeHistoryEvent = (rule: HistoryVisibility = HistoryVisibility.Shared) =>
|
|
|
|
mkEvent({
|
|
|
|
type: EventType.RoomHistoryVisibility,
|
|
|
|
event: true,
|
|
|
|
content: {
|
|
|
|
history_visibility: rule,
|
|
|
|
},
|
|
|
|
} as any);
|
2022-12-12 14:24:14 +03:00
|
|
|
|
2022-01-06 13:09:26 +03:00
|
|
|
const mockSpaceId = "mock-space";
|
|
|
|
|
|
|
|
// TODO case for canonical
|
|
|
|
const makeMockSpace = (
|
|
|
|
client: MatrixClient,
|
|
|
|
joinRule: JoinRule = JoinRule.Invite,
|
|
|
|
guestRule: GuestAccess = GuestAccess.CanJoin,
|
|
|
|
historyRule: HistoryVisibility = HistoryVisibility.WorldReadable,
|
|
|
|
): Room => {
|
|
|
|
const events = [makeJoinEvent(joinRule), makeGuestAccessEvent(guestRule), makeHistoryEvent(historyRule)];
|
|
|
|
const space = mkSpace(client, mockSpaceId);
|
|
|
|
const getStateEvents = mockStateEventImplementation(events);
|
2022-03-22 13:32:35 +03:00
|
|
|
mocked(space.currentState).getStateEvents.mockImplementation(getStateEvents);
|
|
|
|
mocked(space.currentState).mayClientSendStateEvent.mockReturnValue(false);
|
|
|
|
space.getJoinRule.mockReturnValue(joinRule);
|
|
|
|
mocked(space.currentState).getJoinRule.mockReturnValue(joinRule);
|
2022-01-06 13:09:26 +03:00
|
|
|
return space as unknown as Room;
|
|
|
|
};
|
|
|
|
const defaultProps = {
|
|
|
|
matrixClient: mockMatrixClient,
|
|
|
|
space: makeMockSpace(mockMatrixClient),
|
|
|
|
closeSettingsFn: jest.fn(),
|
|
|
|
};
|
|
|
|
|
|
|
|
const getComponent = (props = {}) => {
|
2023-02-22 13:52:55 +03:00
|
|
|
return render(<SpaceSettingsVisibilityTab {...defaultProps} {...props} />);
|
2022-01-06 13:09:26 +03:00
|
|
|
};
|
|
|
|
|
2023-02-22 13:52:55 +03:00
|
|
|
const toggleGuestAccessSection = async ({ getByTestId }: RenderResult) => {
|
|
|
|
const toggleButton = getByTestId("toggle-guest-access-btn")!;
|
|
|
|
fireEvent.click(toggleButton);
|
2022-01-06 13:09:26 +03:00
|
|
|
};
|
2023-04-21 12:48:48 +03:00
|
|
|
const getGuestAccessToggle = ({ getByLabelText }: RenderResult) => getByLabelText("Enable guest access");
|
|
|
|
const getHistoryVisibilityToggle = ({ getByLabelText }: RenderResult) => getByLabelText("Preview Space");
|
2023-02-22 13:52:55 +03:00
|
|
|
const getErrorMessage = ({ getByTestId }: RenderResult) => getByTestId("space-settings-error")?.textContent;
|
2022-01-06 13:09:26 +03:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2023-04-21 12:48:48 +03:00
|
|
|
let i = 0;
|
|
|
|
mocked(randomString).mockImplementation(() => {
|
|
|
|
return "testid_" + i++;
|
|
|
|
});
|
|
|
|
|
2022-01-06 13:09:26 +03:00
|
|
|
(mockMatrixClient.sendStateEvent as jest.Mock).mockClear().mockResolvedValue({});
|
|
|
|
MatrixClientPeg.get = jest.fn().mockReturnValue(mockMatrixClient);
|
2023-06-05 20:12:23 +03:00
|
|
|
MatrixClientPeg.safeGet = jest.fn().mockReturnValue(mockMatrixClient);
|
2022-01-06 13:09:26 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("renders container", () => {
|
2023-02-22 13:52:55 +03:00
|
|
|
const { asFragment } = getComponent();
|
|
|
|
expect(asFragment()).toMatchSnapshot();
|
2022-01-06 13:09:26 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("for a private space", () => {
|
|
|
|
const joinRule = JoinRule.Invite;
|
|
|
|
it("does not render addresses section", () => {
|
|
|
|
const space = makeMockSpace(mockMatrixClient, joinRule);
|
2023-02-22 13:52:55 +03:00
|
|
|
const { queryByTestId } = getComponent({ space });
|
2022-01-06 13:09:26 +03:00
|
|
|
|
2023-02-22 13:52:55 +03:00
|
|
|
expect(queryByTestId("published-address-fieldset")).toBeFalsy();
|
|
|
|
expect(queryByTestId("local-address-fieldset")).toBeFalsy();
|
2022-01-06 13:09:26 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("for a public space", () => {
|
|
|
|
const joinRule = JoinRule.Public;
|
|
|
|
const guestRule = GuestAccess.CanJoin;
|
|
|
|
const historyRule = HistoryVisibility.Joined;
|
|
|
|
|
|
|
|
describe("Access", () => {
|
|
|
|
it("renders guest access section toggle", async () => {
|
|
|
|
const space = makeMockSpace(mockMatrixClient, joinRule, guestRule);
|
|
|
|
const component = getComponent({ space });
|
|
|
|
|
|
|
|
await toggleGuestAccessSection(component);
|
|
|
|
|
|
|
|
expect(getGuestAccessToggle(component)).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("send guest access event on toggle", async () => {
|
|
|
|
const space = makeMockSpace(mockMatrixClient, joinRule, guestRule);
|
|
|
|
|
|
|
|
const component = getComponent({ space });
|
|
|
|
await toggleGuestAccessSection(component);
|
|
|
|
const guestAccessInput = getGuestAccessToggle(component);
|
|
|
|
|
2023-02-15 16:36:22 +03:00
|
|
|
expect(guestAccessInput?.getAttribute("aria-checked")).toEqual("true");
|
2022-01-06 13:09:26 +03:00
|
|
|
|
2023-02-22 13:52:55 +03:00
|
|
|
fireEvent.click(guestAccessInput!);
|
2022-01-06 13:09:26 +03:00
|
|
|
expect(mockMatrixClient.sendStateEvent).toHaveBeenCalledWith(
|
|
|
|
mockSpaceId,
|
|
|
|
EventType.RoomGuestAccess,
|
|
|
|
// toggled off
|
|
|
|
{ guest_access: GuestAccess.Forbidden },
|
|
|
|
"",
|
|
|
|
);
|
|
|
|
|
|
|
|
// toggled off
|
2023-02-15 16:36:22 +03:00
|
|
|
expect(guestAccessInput?.getAttribute("aria-checked")).toEqual("false");
|
2022-01-06 13:09:26 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it("renders error message when update fails", async () => {
|
|
|
|
const space = makeMockSpace(mockMatrixClient, joinRule, guestRule);
|
|
|
|
(mockMatrixClient.sendStateEvent as jest.Mock).mockRejectedValue({});
|
|
|
|
const component = getComponent({ space });
|
|
|
|
await toggleGuestAccessSection(component);
|
2023-02-28 11:58:23 +03:00
|
|
|
await act(() => {
|
|
|
|
fireEvent.click(getGuestAccessToggle(component)!);
|
2022-01-06 13:09:26 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(getErrorMessage(component)).toEqual("Failed to update the guest access of this space");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("disables guest access toggle when setting guest access is not allowed", async () => {
|
|
|
|
const space = makeMockSpace(mockMatrixClient, joinRule, guestRule);
|
|
|
|
(space.currentState.maySendStateEvent as jest.Mock).mockReturnValue(false);
|
|
|
|
const component = getComponent({ space });
|
|
|
|
|
|
|
|
await toggleGuestAccessSection(component);
|
|
|
|
|
2023-02-15 16:36:22 +03:00
|
|
|
expect(getGuestAccessToggle(component)?.getAttribute("aria-disabled")).toEqual("true");
|
2022-01-06 13:09:26 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("Preview", () => {
|
|
|
|
it("renders preview space toggle", () => {
|
|
|
|
const space = makeMockSpace(mockMatrixClient, joinRule, guestRule, historyRule);
|
|
|
|
const component = getComponent({ space });
|
|
|
|
|
|
|
|
// toggle off because space settings is != WorldReadable
|
2023-02-15 16:36:22 +03:00
|
|
|
expect(getHistoryVisibilityToggle(component)?.getAttribute("aria-checked")).toEqual("false");
|
2022-01-06 13:09:26 +03:00
|
|
|
});
|
|
|
|
|
2023-02-22 13:52:55 +03:00
|
|
|
it("updates history visibility on toggle", () => {
|
2022-01-06 13:09:26 +03:00
|
|
|
const space = makeMockSpace(mockMatrixClient, joinRule, guestRule, historyRule);
|
|
|
|
const component = getComponent({ space });
|
|
|
|
|
|
|
|
// toggle off because space settings is != WorldReadable
|
2023-02-15 16:36:22 +03:00
|
|
|
expect(getHistoryVisibilityToggle(component)?.getAttribute("aria-checked")).toEqual("false");
|
2022-01-06 13:09:26 +03:00
|
|
|
|
2023-02-22 13:52:55 +03:00
|
|
|
fireEvent.click(getHistoryVisibilityToggle(component)!);
|
2022-01-06 13:09:26 +03:00
|
|
|
expect(mockMatrixClient.sendStateEvent).toHaveBeenCalledWith(
|
|
|
|
mockSpaceId,
|
|
|
|
EventType.RoomHistoryVisibility,
|
|
|
|
{ history_visibility: HistoryVisibility.WorldReadable },
|
|
|
|
"",
|
|
|
|
);
|
|
|
|
|
2023-02-15 16:36:22 +03:00
|
|
|
expect(getHistoryVisibilityToggle(component)?.getAttribute("aria-checked")).toEqual("true");
|
2022-01-06 13:09:26 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it("renders error message when history update fails", async () => {
|
|
|
|
const space = makeMockSpace(mockMatrixClient, joinRule, guestRule, historyRule);
|
|
|
|
(mockMatrixClient.sendStateEvent as jest.Mock).mockRejectedValue({});
|
|
|
|
const component = getComponent({ space });
|
|
|
|
|
2023-02-28 11:58:23 +03:00
|
|
|
await act(() => {
|
|
|
|
fireEvent.click(getHistoryVisibilityToggle(component)!);
|
2022-01-06 13:09:26 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(getErrorMessage(component)).toEqual("Failed to update the history visibility of this space");
|
|
|
|
});
|
|
|
|
|
2023-02-28 11:58:23 +03:00
|
|
|
it("disables room preview toggle when history visibility changes are not allowed", () => {
|
2022-01-06 13:09:26 +03:00
|
|
|
const space = makeMockSpace(mockMatrixClient, joinRule, guestRule, historyRule);
|
|
|
|
(space.currentState.maySendStateEvent as jest.Mock).mockReturnValue(false);
|
|
|
|
const component = getComponent({ space });
|
2023-02-15 16:36:22 +03:00
|
|
|
expect(getHistoryVisibilityToggle(component)?.getAttribute("aria-disabled")).toEqual("true");
|
2022-01-06 13:09:26 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("renders addresses section", () => {
|
|
|
|
const space = makeMockSpace(mockMatrixClient, joinRule, guestRule);
|
2023-02-22 13:52:55 +03:00
|
|
|
const { getByTestId } = getComponent({ space });
|
2022-01-06 13:09:26 +03:00
|
|
|
|
2023-02-22 13:52:55 +03:00
|
|
|
expect(getByTestId("published-address-fieldset")).toBeTruthy();
|
|
|
|
expect(getByTestId("local-address-fieldset")).toBeTruthy();
|
2022-01-06 13:09:26 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|