2022-05-06 17:09:28 +08: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-01-06 18:09:26 +08:00
|
|
|
import React from "react";
|
2022-12-12 19:24:14 +08:00
|
|
|
import { mocked } from "jest-mock";
|
|
|
|
import { renderIntoDocument, Simulate } from "react-dom/test-utils";
|
2022-01-06 18:09:26 +08:00
|
|
|
import { act } from "react-dom/test-utils";
|
2022-12-12 19:24:14 +08:00
|
|
|
import { EventType, MatrixClient, Room } from "matrix-js-sdk/src/matrix";
|
|
|
|
import { GuestAccess, HistoryVisibility, JoinRule } from "matrix-js-sdk/src/@types/partials";
|
2022-01-06 18:09:26 +08:00
|
|
|
|
|
|
|
import _SpaceSettingsVisibilityTab from "../../../../src/components/views/spaces/SpaceSettingsVisibilityTab";
|
2022-12-12 19:24:14 +08:00
|
|
|
import { createTestClient, mkEvent, wrapInMatrixClientContext } from "../../../test-utils";
|
|
|
|
import { mkSpace, mockStateEventImplementation } from "../../../test-utils";
|
|
|
|
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
|
2022-01-06 18:09:26 +08:00
|
|
|
|
|
|
|
const SpaceSettingsVisibilityTab = wrapInMatrixClientContext(_SpaceSettingsVisibilityTab);
|
|
|
|
|
|
|
|
jest.useFakeTimers();
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
describe("<SpaceSettingsVisibilityTab />", () => {
|
2022-01-06 18:09:26 +08:00
|
|
|
const mockMatrixClient = createTestClient() as MatrixClient;
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
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);
|
|
|
|
|
|
|
|
const mockSpaceId = "mock-space";
|
2022-01-06 18:09:26 +08:00
|
|
|
|
|
|
|
// TODO case for canonical
|
|
|
|
const makeMockSpace = (
|
|
|
|
client: MatrixClient,
|
|
|
|
joinRule: JoinRule = JoinRule.Invite,
|
|
|
|
guestRule: GuestAccess = GuestAccess.CanJoin,
|
|
|
|
historyRule: HistoryVisibility = HistoryVisibility.WorldReadable,
|
|
|
|
): Room => {
|
2022-12-12 19:24:14 +08:00
|
|
|
const events = [makeJoinEvent(joinRule), makeGuestAccessEvent(guestRule), makeHistoryEvent(historyRule)];
|
2022-01-06 18:09:26 +08:00
|
|
|
const space = mkSpace(client, mockSpaceId);
|
|
|
|
const getStateEvents = mockStateEventImplementation(events);
|
2022-03-22 18:32:35 +08: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 18:09:26 +08:00
|
|
|
return space as unknown as Room;
|
|
|
|
};
|
|
|
|
const defaultProps = {
|
|
|
|
matrixClient: mockMatrixClient,
|
|
|
|
space: makeMockSpace(mockMatrixClient),
|
|
|
|
closeSettingsFn: jest.fn(),
|
|
|
|
};
|
|
|
|
|
|
|
|
const getComponent = (props = {}) => {
|
|
|
|
const wrapper = renderIntoDocument<HTMLSpanElement>(
|
|
|
|
// wrap in element so renderIntoDocument can render functional component
|
|
|
|
<span>
|
|
|
|
<SpaceSettingsVisibilityTab {...defaultProps} {...props} />
|
|
|
|
</span>,
|
|
|
|
) as HTMLSpanElement;
|
|
|
|
return wrapper.children[0];
|
|
|
|
};
|
|
|
|
|
|
|
|
const getByTestId = (container: Element, id: string) => container.querySelector(`[data-test-id=${id}]`);
|
2023-02-13 19:39:16 +08:00
|
|
|
const toggleGuestAccessSection = async (component: Element) => {
|
2022-12-12 19:24:14 +08:00
|
|
|
const toggleButton = getByTestId(component, "toggle-guest-access-btn");
|
2022-01-06 18:09:26 +08:00
|
|
|
await act(async () => {
|
|
|
|
Simulate.click(toggleButton);
|
|
|
|
});
|
|
|
|
};
|
2023-02-13 19:39:16 +08:00
|
|
|
const getGuestAccessToggle = (component: Element) => component.querySelector('[aria-label="Enable guest access"');
|
|
|
|
const getHistoryVisibilityToggle = (component: Element) => component.querySelector('[aria-label="Preview Space"');
|
|
|
|
const getErrorMessage = (component: Element) => getByTestId(component, "space-settings-error")?.textContent;
|
2022-01-06 18:09:26 +08:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
(mockMatrixClient.sendStateEvent as jest.Mock).mockClear().mockResolvedValue({});
|
|
|
|
MatrixClientPeg.get = jest.fn().mockReturnValue(mockMatrixClient);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("renders container", () => {
|
2022-01-06 18:09:26 +08:00
|
|
|
const component = getComponent();
|
|
|
|
expect(component).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
describe("for a private space", () => {
|
2022-01-06 18:09:26 +08:00
|
|
|
const joinRule = JoinRule.Invite;
|
2022-12-12 19:24:14 +08:00
|
|
|
it("does not render addresses section", () => {
|
2022-01-06 18:09:26 +08:00
|
|
|
const space = makeMockSpace(mockMatrixClient, joinRule);
|
|
|
|
const component = getComponent({ space });
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
expect(getByTestId(component, "published-address-fieldset")).toBeFalsy();
|
|
|
|
expect(getByTestId(component, "local-address-fieldset")).toBeFalsy();
|
2022-01-06 18:09:26 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
describe("for a public space", () => {
|
2022-01-06 18:09:26 +08:00
|
|
|
const joinRule = JoinRule.Public;
|
|
|
|
const guestRule = GuestAccess.CanJoin;
|
|
|
|
const historyRule = HistoryVisibility.Joined;
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
describe("Access", () => {
|
|
|
|
it("renders guest access section toggle", async () => {
|
2022-01-06 18:09:26 +08:00
|
|
|
const space = makeMockSpace(mockMatrixClient, joinRule, guestRule);
|
|
|
|
const component = getComponent({ space });
|
|
|
|
|
|
|
|
await toggleGuestAccessSection(component);
|
|
|
|
|
|
|
|
expect(getGuestAccessToggle(component)).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("send guest access event on toggle", async () => {
|
2022-01-06 18:09:26 +08:00
|
|
|
const space = makeMockSpace(mockMatrixClient, joinRule, guestRule);
|
|
|
|
|
|
|
|
const component = getComponent({ space });
|
|
|
|
await toggleGuestAccessSection(component);
|
|
|
|
const guestAccessInput = getGuestAccessToggle(component);
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
expect(guestAccessInput.getAttribute("aria-checked")).toEqual("true");
|
2022-01-06 18:09:26 +08:00
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
Simulate.click(guestAccessInput);
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(mockMatrixClient.sendStateEvent).toHaveBeenCalledWith(
|
|
|
|
mockSpaceId,
|
|
|
|
EventType.RoomGuestAccess,
|
|
|
|
// toggled off
|
|
|
|
{ guest_access: GuestAccess.Forbidden },
|
|
|
|
"",
|
|
|
|
);
|
|
|
|
|
|
|
|
// toggled off
|
2022-12-12 19:24:14 +08:00
|
|
|
expect(guestAccessInput.getAttribute("aria-checked")).toEqual("false");
|
2022-01-06 18:09:26 +08:00
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("renders error message when update fails", async () => {
|
2022-01-06 18:09:26 +08:00
|
|
|
const space = makeMockSpace(mockMatrixClient, joinRule, guestRule);
|
|
|
|
(mockMatrixClient.sendStateEvent as jest.Mock).mockRejectedValue({});
|
|
|
|
const component = getComponent({ space });
|
|
|
|
await toggleGuestAccessSection(component);
|
|
|
|
await act(async () => {
|
|
|
|
Simulate.click(getGuestAccessToggle(component));
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(getErrorMessage(component)).toEqual("Failed to update the guest access of this space");
|
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("disables guest access toggle when setting guest access is not allowed", async () => {
|
2022-01-06 18:09:26 +08:00
|
|
|
const space = makeMockSpace(mockMatrixClient, joinRule, guestRule);
|
|
|
|
(space.currentState.maySendStateEvent as jest.Mock).mockReturnValue(false);
|
|
|
|
const component = getComponent({ space });
|
|
|
|
|
|
|
|
await toggleGuestAccessSection(component);
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
expect(getGuestAccessToggle(component).getAttribute("aria-disabled")).toEqual("true");
|
2022-01-06 18:09:26 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
describe("Preview", () => {
|
|
|
|
it("renders preview space toggle", () => {
|
2022-01-06 18:09:26 +08:00
|
|
|
const space = makeMockSpace(mockMatrixClient, joinRule, guestRule, historyRule);
|
|
|
|
const component = getComponent({ space });
|
|
|
|
|
|
|
|
// toggle off because space settings is != WorldReadable
|
2022-12-12 19:24:14 +08:00
|
|
|
expect(getHistoryVisibilityToggle(component).getAttribute("aria-checked")).toEqual("false");
|
2022-01-06 18:09:26 +08:00
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("updates history visibility on toggle", async () => {
|
2022-01-06 18:09:26 +08:00
|
|
|
const space = makeMockSpace(mockMatrixClient, joinRule, guestRule, historyRule);
|
|
|
|
const component = getComponent({ space });
|
|
|
|
|
|
|
|
// toggle off because space settings is != WorldReadable
|
2022-12-12 19:24:14 +08:00
|
|
|
expect(getHistoryVisibilityToggle(component).getAttribute("aria-checked")).toEqual("false");
|
2022-01-06 18:09:26 +08:00
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
Simulate.click(getHistoryVisibilityToggle(component));
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(mockMatrixClient.sendStateEvent).toHaveBeenCalledWith(
|
|
|
|
mockSpaceId,
|
|
|
|
EventType.RoomHistoryVisibility,
|
|
|
|
{ history_visibility: HistoryVisibility.WorldReadable },
|
|
|
|
"",
|
|
|
|
);
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
expect(getHistoryVisibilityToggle(component).getAttribute("aria-checked")).toEqual("true");
|
2022-01-06 18:09:26 +08:00
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("renders error message when history update fails", async () => {
|
2022-01-06 18:09:26 +08:00
|
|
|
const space = makeMockSpace(mockMatrixClient, joinRule, guestRule, historyRule);
|
|
|
|
(mockMatrixClient.sendStateEvent as jest.Mock).mockRejectedValue({});
|
|
|
|
const component = getComponent({ space });
|
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
Simulate.click(getHistoryVisibilityToggle(component));
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(getErrorMessage(component)).toEqual("Failed to update the history visibility of this space");
|
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("disables room preview toggle when history visability changes are not allowed", () => {
|
2022-01-06 18:09:26 +08:00
|
|
|
const space = makeMockSpace(mockMatrixClient, joinRule, guestRule, historyRule);
|
|
|
|
(space.currentState.maySendStateEvent as jest.Mock).mockReturnValue(false);
|
|
|
|
const component = getComponent({ space });
|
2022-12-12 19:24:14 +08:00
|
|
|
expect(getHistoryVisibilityToggle(component).getAttribute("aria-disabled")).toEqual("true");
|
2022-01-06 18:09:26 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("renders addresses section", () => {
|
2022-01-06 18:09:26 +08:00
|
|
|
const space = makeMockSpace(mockMatrixClient, joinRule, guestRule);
|
|
|
|
const component = getComponent({ space });
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
expect(getByTestId(component, "published-address-fieldset")).toBeTruthy();
|
|
|
|
expect(getByTestId(component, "local-address-fieldset")).toBeTruthy();
|
2022-01-06 18:09:26 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|