forked from matrix-org/matrix-react-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoomPreviewBar-test.tsx
419 lines (352 loc) · 17 KB
/
RoomPreviewBar-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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*
Copyright 2021 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, { ComponentProps } from "react";
import { render, fireEvent, RenderResult, waitFor } from "@testing-library/react";
import { Room, RoomMember, MatrixError, IContent } from "matrix-js-sdk/src/matrix";
import { stubClient } from "../../../test-utils";
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
import DMRoomMap from "../../../../src/utils/DMRoomMap";
import RoomPreviewBar from "../../../../src/components/views/rooms/RoomPreviewBar";
import defaultDispatcher from "../../../../src/dispatcher/dispatcher";
jest.mock("../../../../src/IdentityAuthClient", () => {
return jest.fn().mockImplementation(() => {
return { getAccessToken: jest.fn().mockResolvedValue("mock-token") };
});
});
jest.useRealTimers();
const createRoom = (roomId: string, userId: string): Room => {
const newRoom = new Room(roomId, MatrixClientPeg.get(), userId, {});
DMRoomMap.makeShared().start();
return newRoom;
};
const makeMockRoomMember = ({
userId,
isKicked,
membership,
content,
memberContent,
}: {
userId?: string;
isKicked?: boolean;
membership?: "invite" | "ban";
content?: Partial<IContent>;
memberContent?: Partial<IContent>;
}) =>
({
userId,
rawDisplayName: `${userId} name`,
isKicked: jest.fn().mockReturnValue(!!isKicked),
getContent: jest.fn().mockReturnValue(content || {}),
membership,
events: {
member: {
getSender: jest.fn().mockReturnValue("@kicker:test.com"),
getContent: jest.fn().mockReturnValue({ reason: "test reason", ...memberContent }),
},
},
} as unknown as RoomMember);
describe("<RoomPreviewBar />", () => {
const roomId = "RoomPreviewBar-test-room";
const userId = "@tester:test.com";
const inviterUserId = "@inviter:test.com";
const otherUserId = "@othertester:test.com";
const getComponent = (props: Omit<ComponentProps<typeof RoomPreviewBar>, 'roomId'> = {}) => {
const defaultProps = {
roomId,
room: createRoom(roomId, userId),
};
return render(<RoomPreviewBar {...defaultProps} {...props} />);
};
const isSpinnerRendered = (wrapper: RenderResult) => !!wrapper.container.querySelector(".mx_Spinner");
const getMessage = (wrapper: RenderResult) =>
wrapper.container.querySelector<HTMLDivElement>(".mx_RoomPreviewBar_message");
const getActions = (wrapper: RenderResult) =>
wrapper.container.querySelector<HTMLDivElement>(".mx_RoomPreviewBar_actions");
const getPrimaryActionButton = (wrapper: RenderResult) =>
getActions(wrapper).querySelector(".mx_AccessibleButton_kind_primary");
const getSecondaryActionButton = (wrapper: RenderResult) =>
getActions(wrapper).querySelector(".mx_AccessibleButton_kind_secondary");
beforeEach(() => {
stubClient();
MatrixClientPeg.get().getUserId = jest.fn().mockReturnValue(userId);
});
afterEach(() => {
const container = document.body.firstChild;
container && document.body.removeChild(container);
});
it("renders joining message", () => {
const component = getComponent({ joining: true });
expect(isSpinnerRendered(component)).toBeTruthy();
expect(getMessage(component).textContent).toEqual("Joining …");
});
it("renders rejecting message", () => {
const component = getComponent({ rejecting: true });
expect(isSpinnerRendered(component)).toBeTruthy();
expect(getMessage(component).textContent).toEqual("Rejecting invite …");
});
it("renders loading message", () => {
const component = getComponent({ loading: true });
expect(isSpinnerRendered(component)).toBeTruthy();
expect(getMessage(component).textContent).toEqual("Loading …");
});
it("renders not logged in message", () => {
MatrixClientPeg.get().isGuest = jest.fn().mockReturnValue(true);
const component = getComponent({ loading: true });
expect(isSpinnerRendered(component)).toBeFalsy();
expect(getMessage(component).textContent).toEqual("Join the conversation with an account");
});
it("should send room oob data to start login", async () => {
MatrixClientPeg.get().isGuest = jest.fn().mockReturnValue(true);
const component = getComponent({
oobData: {
name: "Room Name",
avatarUrl: "mxc://foo/bar",
inviterName: "Charlie",
},
});
const dispatcherSpy = jest.fn();
const dispatcherRef = defaultDispatcher.register(dispatcherSpy);
expect(getMessage(component).textContent).toEqual("Join the conversation with an account");
fireEvent.click(getPrimaryActionButton(component));
await waitFor(() =>
expect(dispatcherSpy).toHaveBeenCalledWith(
expect.objectContaining({
screenAfterLogin: {
screen: "room",
params: expect.objectContaining({
room_name: "Room Name",
room_avatar_url: "mxc://foo/bar",
inviter_name: "Charlie",
}),
},
}),
),
);
defaultDispatcher.unregister(dispatcherRef);
});
it("renders kicked message", () => {
const room = createRoom(roomId, otherUserId);
jest.spyOn(room, "getMember").mockReturnValue(makeMockRoomMember({ isKicked: true }));
const component = getComponent({ loading: true, room });
expect(getMessage(component)).toMatchSnapshot();
});
it("renders banned message", () => {
const room = createRoom(roomId, otherUserId);
jest.spyOn(room, "getMember").mockReturnValue(makeMockRoomMember({ membership: "ban" }));
const component = getComponent({ loading: true, room });
expect(getMessage(component)).toMatchSnapshot();
});
describe("with an error", () => {
it("renders room not found error", () => {
const error = new MatrixError({
errcode: "M_NOT_FOUND",
error: "Room not found",
});
const component = getComponent({ error });
expect(getMessage(component)).toMatchSnapshot();
});
it("renders other errors", () => {
const error = new MatrixError({
errcode: "Something_else",
});
const component = getComponent({ error });
expect(getMessage(component)).toMatchSnapshot();
});
});
it("renders viewing room message when room an be previewed", () => {
const component = getComponent({ canPreview: true });
expect(getMessage(component)).toMatchSnapshot();
});
it("renders viewing room message when room can not be previewed", () => {
const component = getComponent({ canPreview: false });
expect(getMessage(component)).toMatchSnapshot();
});
describe("with an invite", () => {
const inviterName = inviterUserId;
const userMember = makeMockRoomMember({ userId });
const userMemberWithDmInvite = makeMockRoomMember({
userId,
membership: "invite",
memberContent: { is_direct: true },
});
const inviterMember = makeMockRoomMember({
userId: inviterUserId,
content: {
"reason": "test",
"io.element.html_reason": "<h3>hello</h3>",
},
});
describe("without an invited email", () => {
describe("for a non-dm room", () => {
const mockGetMember = (id: string) => {
if (id === userId) return userMember;
return inviterMember;
};
const onJoinClick = jest.fn();
const onRejectClick = jest.fn();
let room: Room;
beforeEach(() => {
room = createRoom(roomId, userId);
jest.spyOn(room, "getMember").mockImplementation(mockGetMember);
jest.spyOn(room.currentState, "getMember").mockImplementation(mockGetMember);
onJoinClick.mockClear();
onRejectClick.mockClear();
});
it("renders invite message", () => {
const component = getComponent({ inviterName, room });
expect(getMessage(component)).toMatchSnapshot();
});
it("renders join and reject action buttons correctly", () => {
const component = getComponent({ inviterName, room, onJoinClick, onRejectClick });
expect(getActions(component)).toMatchSnapshot();
});
it("renders reject and ignore action buttons when handler is provided", () => {
const onRejectAndIgnoreClick = jest.fn();
const component = getComponent({
inviterName,
room,
onJoinClick,
onRejectClick,
onRejectAndIgnoreClick,
});
expect(getActions(component)).toMatchSnapshot();
});
it("renders join and reject action buttons in reverse order when room can previewed", () => {
// when room is previewed action buttons are rendered left to right, with primary on the right
const component = getComponent({ inviterName, room, onJoinClick, onRejectClick, canPreview: true });
expect(getActions(component)).toMatchSnapshot();
});
it("joins room on primary button click", () => {
const component = getComponent({ inviterName, room, onJoinClick, onRejectClick });
fireEvent.click(getPrimaryActionButton(component));
expect(onJoinClick).toHaveBeenCalled();
});
it("rejects invite on secondary button click", () => {
const component = getComponent({ inviterName, room, onJoinClick, onRejectClick });
fireEvent.click(getSecondaryActionButton(component));
expect(onRejectClick).toHaveBeenCalled();
});
});
describe("for a dm room", () => {
const mockGetMember = (id: string) => {
if (id === userId) return userMemberWithDmInvite;
return inviterMember;
};
const onJoinClick = jest.fn();
const onRejectClick = jest.fn();
let room: Room;
beforeEach(() => {
room = createRoom(roomId, userId);
jest.spyOn(room, "getMember").mockImplementation(mockGetMember);
jest.spyOn(room.currentState, "getMember").mockImplementation(mockGetMember);
onJoinClick.mockClear();
onRejectClick.mockClear();
});
it("renders invite message to a non-dm room", () => {
const component = getComponent({ inviterName, room });
expect(getMessage(component)).toMatchSnapshot();
});
it("renders join and reject action buttons with correct labels", () => {
const onRejectAndIgnoreClick = jest.fn();
const component = getComponent({
inviterName,
room,
onJoinClick,
onRejectAndIgnoreClick,
onRejectClick,
});
expect(getActions(component)).toMatchSnapshot();
});
});
});
describe("with an invited email", () => {
const invitedEmail = "[email protected]";
const mockThreePids = [
{ medium: "email", address: invitedEmail },
{ medium: "not-email", address: "address 2" },
];
const testJoinButton = (props: Omit<ComponentProps<typeof RoomPreviewBar>, 'roomId'>) => async () => {
const onJoinClick = jest.fn();
const onRejectClick = jest.fn();
const component = getComponent({ ...props, onJoinClick, onRejectClick });
await new Promise(setImmediate);
expect(getPrimaryActionButton(component)).toBeTruthy();
expect(getSecondaryActionButton(component)).toBeFalsy();
fireEvent.click(getPrimaryActionButton(component));
expect(onJoinClick).toHaveBeenCalled();
};
describe("when client fails to get 3PIDs", () => {
beforeEach(() => {
MatrixClientPeg.get().getThreePids = jest.fn().mockRejectedValue({ errCode: "TEST_ERROR" });
});
it("renders error message", async () => {
const component = getComponent({ inviterName, invitedEmail });
await new Promise(setImmediate);
expect(getMessage(component)).toMatchSnapshot();
});
it("renders join button", testJoinButton({ inviterName, invitedEmail }));
});
describe("when invitedEmail is not associated with current account", () => {
beforeEach(() => {
MatrixClientPeg.get().getThreePids = jest
.fn()
.mockResolvedValue({ threepids: mockThreePids.slice(1) });
});
it("renders invite message with invited email", async () => {
const component = getComponent({ inviterName, invitedEmail });
await new Promise(setImmediate);
expect(getMessage(component)).toMatchSnapshot();
});
it("renders join button", testJoinButton({ inviterName, invitedEmail }));
});
describe("when client has no identity server connected", () => {
beforeEach(() => {
MatrixClientPeg.get().getThreePids = jest.fn().mockResolvedValue({ threepids: mockThreePids });
MatrixClientPeg.get().getIdentityServerUrl = jest.fn().mockReturnValue(false);
});
it("renders invite message with invited email", async () => {
const component = getComponent({ inviterName, invitedEmail });
await new Promise(setImmediate);
expect(getMessage(component)).toMatchSnapshot();
});
it("renders join button", testJoinButton({ inviterName, invitedEmail }));
});
describe("when client has an identity server connected", () => {
beforeEach(() => {
MatrixClientPeg.get().getThreePids = jest.fn().mockResolvedValue({ threepids: mockThreePids });
MatrixClientPeg.get().getIdentityServerUrl = jest.fn().mockReturnValue("identity.test");
MatrixClientPeg.get().lookupThreePid = jest.fn().mockResolvedValue("identity.test");
});
it("renders email mismatch message when invite email mxid doesnt match", async () => {
MatrixClientPeg.get().lookupThreePid = jest.fn().mockReturnValue("not userid");
const component = getComponent({ inviterName, invitedEmail });
await new Promise(setImmediate);
expect(getMessage(component)).toMatchSnapshot();
expect(MatrixClientPeg.get().lookupThreePid).toHaveBeenCalledWith(
"email",
invitedEmail,
"mock-token",
);
await testJoinButton({ inviterName, invitedEmail })();
});
it("renders invite message when invite email mxid match", async () => {
MatrixClientPeg.get().lookupThreePid = jest.fn().mockReturnValue(userId);
const component = getComponent({ inviterName, invitedEmail });
await new Promise(setImmediate);
expect(getMessage(component)).toMatchSnapshot();
await testJoinButton({ inviterName, invitedEmail })();
});
});
});
});
});