forked from matrix-org/matrix-react-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJoinRuleSettings-test.tsx
350 lines (294 loc) · 14.3 KB
/
JoinRuleSettings-test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
Copyright 2023 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, screen, within } from "@testing-library/react";
import {
EventType,
GuestAccess,
HistoryVisibility,
JoinRule,
MatrixEvent,
Room,
ClientEvent,
RoomMember,
MatrixError,
Visibility,
} from "matrix-js-sdk/src/matrix";
import { defer, IDeferred } from "matrix-js-sdk/src/utils";
import {
clearAllModals,
flushPromises,
getMockClientWithEventEmitter,
mockClientMethodsUser,
} from "../../../test-utils";
import { filterBoolean } from "../../../../src/utils/arrays";
import JoinRuleSettings, { JoinRuleSettingsProps } from "../../../../src/components/views/settings/JoinRuleSettings";
import { PreferredRoomVersions } from "../../../../src/utils/PreferredRoomVersions";
import SpaceStore from "../../../../src/stores/spaces/SpaceStore";
import SettingsStore from "../../../../src/settings/SettingsStore";
describe("<JoinRuleSettings />", () => {
const userId = "@alice:server.org";
const client = getMockClientWithEventEmitter({
...mockClientMethodsUser(userId),
getRoom: jest.fn(),
getLocalAliases: jest.fn().mockReturnValue([]),
sendStateEvent: jest.fn(),
upgradeRoom: jest.fn(),
getProfileInfo: jest.fn(),
invite: jest.fn().mockResolvedValue(undefined),
isRoomEncrypted: jest.fn().mockReturnValue(false),
getRoomDirectoryVisibility: jest.fn(),
setRoomDirectoryVisibility: jest.fn(),
});
const roomId = "!room:server.org";
const newRoomId = "!roomUpgraded:server.org";
const defaultProps = {
room: new Room(roomId, client, userId),
closeSettingsFn: jest.fn(),
onError: jest.fn(),
};
const getComponent = (props: Partial<JoinRuleSettingsProps> = {}) =>
render(<JoinRuleSettings {...defaultProps} {...props} />);
const setRoomStateEvents = (
room: Room,
roomVersion: string,
joinRule?: JoinRule,
guestAccess?: GuestAccess,
history?: HistoryVisibility,
): void => {
const events = filterBoolean<MatrixEvent>([
new MatrixEvent({
type: EventType.RoomCreate,
content: { room_version: roomVersion },
sender: userId,
state_key: "",
room_id: room.roomId,
}),
guestAccess &&
new MatrixEvent({
type: EventType.RoomGuestAccess,
content: { guest_access: guestAccess },
sender: userId,
state_key: "",
room_id: room.roomId,
}),
history &&
new MatrixEvent({
type: EventType.RoomHistoryVisibility,
content: { history_visibility: history },
sender: userId,
state_key: "",
room_id: room.roomId,
}),
joinRule &&
new MatrixEvent({
type: EventType.RoomJoinRules,
content: { join_rule: joinRule },
sender: userId,
state_key: "",
room_id: room.roomId,
}),
]);
room.currentState.setStateEvents(events);
};
beforeEach(() => {
client.sendStateEvent.mockReset().mockResolvedValue({ event_id: "test" });
client.isRoomEncrypted.mockReturnValue(false);
client.upgradeRoom.mockResolvedValue({ replacement_room: newRoomId });
client.getRoom.mockReturnValue(null);
jest.spyOn(SettingsStore, "getValue").mockImplementation((setting) => setting === "feature_ask_to_join");
});
type TestCase = [string, { label: string; unsupportedRoomVersion: string; preferredRoomVersion: string }];
const testCases: TestCase[] = [
[
JoinRule.Knock,
{
label: "Ask to join",
unsupportedRoomVersion: "6",
preferredRoomVersion: PreferredRoomVersions.KnockRooms,
},
],
[
JoinRule.Restricted,
{
label: "Space members",
unsupportedRoomVersion: "8",
preferredRoomVersion: PreferredRoomVersions.RestrictedRooms,
},
],
];
describe.each(testCases)("%s rooms", (joinRule, { label, unsupportedRoomVersion, preferredRoomVersion }) => {
afterEach(async () => {
await clearAllModals();
});
describe(`when room does not support join rule ${joinRule}`, () => {
it(`should not show ${joinRule} room join rule when upgrade is disabled`, () => {
// room that doesn't support the join rule
const room = new Room(roomId, client, userId);
setRoomStateEvents(room, unsupportedRoomVersion);
getComponent({ room: room, promptUpgrade: false });
expect(screen.queryByText(label)).not.toBeInTheDocument();
});
it(`should show ${joinRule} room join rule when upgrade is enabled`, () => {
// room that doesn't support the join rule
const room = new Room(roomId, client, userId);
setRoomStateEvents(room, unsupportedRoomVersion);
getComponent({ room: room, promptUpgrade: true });
expect(within(screen.getByText(label)).getByText("Upgrade required")).toBeInTheDocument();
});
it(`upgrades room when changing join rule to ${joinRule}`, async () => {
const deferredInvites: IDeferred<any>[] = [];
// room that doesn't support the join rule
const room = new Room(roomId, client, userId);
const parentSpace = new Room("!parentSpace:server.org", client, userId);
jest.spyOn(SpaceStore.instance, "getKnownParents").mockReturnValue(new Set([parentSpace.roomId]));
setRoomStateEvents(room, unsupportedRoomVersion);
const memberAlice = new RoomMember(roomId, "@alice:server.org");
const memberBob = new RoomMember(roomId, "@bob:server.org");
const memberCharlie = new RoomMember(roomId, "@charlie:server.org");
jest.spyOn(room, "getMembersWithMembership").mockImplementation((membership) =>
membership === "join" ? [memberAlice, memberBob] : [memberCharlie],
);
const upgradedRoom = new Room(newRoomId, client, userId);
setRoomStateEvents(upgradedRoom, preferredRoomVersion);
client.getRoom.mockImplementation((id) => {
if (roomId === id) return room;
if (parentSpace.roomId === id) return parentSpace;
return null;
});
// resolve invites by hand
// flushPromises is too blunt to test reliably
client.invite.mockImplementation(() => {
const p = defer<{}>();
deferredInvites.push(p);
return p.promise;
});
getComponent({ room: room, promptUpgrade: true });
fireEvent.click(screen.getByText(label));
const dialog = await screen.findByRole("dialog");
fireEvent.click(within(dialog).getByText("Upgrade"));
expect(client.upgradeRoom).toHaveBeenCalledWith(roomId, preferredRoomVersion);
expect(within(dialog).getByText("Upgrading room")).toBeInTheDocument();
await flushPromises();
expect(within(dialog).getByText("Loading new room")).toBeInTheDocument();
// "create" our new room, have it come thru sync
client.getRoom.mockImplementation((id) => {
if (roomId === id) return room;
if (newRoomId === id) return upgradedRoom;
if (parentSpace.roomId === id) return parentSpace;
return null;
});
client.emit(ClientEvent.Room, upgradedRoom);
// invite users
expect(await screen.findByText("Sending invites... (0 out of 2)")).toBeInTheDocument();
deferredInvites.pop()!.resolve({});
expect(await screen.findByText("Sending invites... (1 out of 2)")).toBeInTheDocument();
deferredInvites.pop()!.resolve({});
// update spaces
expect(await screen.findByText("Updating space...")).toBeInTheDocument();
await flushPromises();
// done, modal closed
expect(screen.queryByRole("dialog")).not.toBeInTheDocument();
});
it(`upgrades room with no parent spaces or members when changing join rule to ${joinRule}`, async () => {
// room that doesn't support the join rule
const room = new Room(roomId, client, userId);
setRoomStateEvents(room, unsupportedRoomVersion);
const upgradedRoom = new Room(newRoomId, client, userId);
setRoomStateEvents(upgradedRoom, preferredRoomVersion);
getComponent({ room: room, promptUpgrade: true });
fireEvent.click(screen.getByText(label));
const dialog = await screen.findByRole("dialog");
fireEvent.click(within(dialog).getByText("Upgrade"));
expect(client.upgradeRoom).toHaveBeenCalledWith(roomId, preferredRoomVersion);
expect(within(dialog).getByText("Upgrading room")).toBeInTheDocument();
await flushPromises();
expect(within(dialog).getByText("Loading new room")).toBeInTheDocument();
// "create" our new room, have it come thru sync
client.getRoom.mockImplementation((id) => {
if (roomId === id) return room;
if (newRoomId === id) return upgradedRoom;
return null;
});
client.emit(ClientEvent.Room, upgradedRoom);
await flushPromises();
await flushPromises();
// done, modal closed
expect(screen.queryByRole("dialog")).not.toBeInTheDocument();
});
});
});
describe("knock rooms directory visibility", () => {
const getCheckbox = () => screen.getByRole("checkbox");
let room: Room;
beforeEach(() => (room = new Room(roomId, client, userId)));
describe("when join rule is knock", () => {
beforeEach(() => setRoomStateEvents(room, PreferredRoomVersions.KnockRooms, JoinRule.Knock));
it("should set the visibility to public", async () => {
jest.spyOn(client, "getRoomDirectoryVisibility").mockResolvedValue({ visibility: Visibility.Private });
jest.spyOn(client, "setRoomDirectoryVisibility").mockResolvedValue({});
getComponent({ room });
fireEvent.click(getCheckbox());
await act(async () => await flushPromises());
expect(client.setRoomDirectoryVisibility).toHaveBeenCalledWith(roomId, Visibility.Public);
expect(getCheckbox()).toBeChecked();
});
it("should set the visibility to private", async () => {
jest.spyOn(client, "getRoomDirectoryVisibility").mockResolvedValue({ visibility: Visibility.Public });
jest.spyOn(client, "setRoomDirectoryVisibility").mockResolvedValue({});
getComponent({ room });
await act(async () => await flushPromises());
fireEvent.click(getCheckbox());
await act(async () => await flushPromises());
expect(client.setRoomDirectoryVisibility).toHaveBeenCalledWith(roomId, Visibility.Private);
expect(getCheckbox()).not.toBeChecked();
});
it("should call onError if setting visibility fails", async () => {
const error = new MatrixError();
jest.spyOn(client, "getRoomDirectoryVisibility").mockResolvedValue({ visibility: Visibility.Private });
jest.spyOn(client, "setRoomDirectoryVisibility").mockRejectedValue(error);
getComponent({ room });
fireEvent.click(getCheckbox());
await act(async () => await flushPromises());
expect(getCheckbox()).not.toBeChecked();
expect(defaultProps.onError).toHaveBeenCalledWith(error);
});
});
describe("when the room version is unsupported and upgrade is enabled", () => {
it("should disable the checkbox", () => {
setRoomStateEvents(room, "6", JoinRule.Invite);
getComponent({ promptUpgrade: true, room });
expect(getCheckbox()).toBeDisabled();
});
});
describe("when join rule is not knock", () => {
beforeEach(() => {
setRoomStateEvents(room, PreferredRoomVersions.KnockRooms, JoinRule.Invite);
getComponent({ room });
});
it("should disable the checkbox", () => {
expect(getCheckbox()).toBeDisabled();
});
it("should set the visibility to private by default", () => {
expect(getCheckbox()).not.toBeChecked();
});
});
});
it("should not show knock room join rule", async () => {
jest.spyOn(SettingsStore, "getValue").mockReturnValue(false);
const room = new Room(newRoomId, client, userId);
setRoomStateEvents(room, PreferredRoomVersions.KnockRooms);
getComponent({ room });
expect(screen.queryByText("Ask to join")).not.toBeInTheDocument();
});
});