Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 7d574ef

Browse files
committed
Clean up RoomView tests
Fixes a couple of issues with the RoomView tests: - In many cases, the tests were erroring, but, due to error boundaries, not being caught. - In many cases, room membership was not being set, so it was just rendering the room preview - The mocks were being manually reset, which was causing console spam. Signed-off-by: Clark Fischer <clark.fischer@gmail.com>
1 parent 79f7e02 commit 7d574ef

File tree

3 files changed

+38
-20
lines changed

3 files changed

+38
-20
lines changed

test/components/structures/RoomView-test.tsx

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,7 @@ import { EventType } from "matrix-js-sdk/src/matrix";
2525
import { MEGOLM_ALGORITHM } from "matrix-js-sdk/src/crypto/olmlib";
2626
import { fireEvent, render } from "@testing-library/react";
2727

28-
import {
29-
stubClient,
30-
mockPlatformPeg,
31-
unmockPlatformPeg,
32-
wrapInMatrixClientContext,
33-
flushPromises,
34-
} from "../../test-utils";
35-
import { MatrixClientPeg } from "../../../src/MatrixClientPeg";
28+
import { stubClient, mockPlatformPeg, wrapInMatrixClientContext, flushPromises } from "../../test-utils";
3629
import { Action } from "../../../src/dispatcher/actions";
3730
import { defaultDispatcher } from "../../../src/dispatcher/dispatcher";
3831
import { ViewRoomPayload } from "../../../src/dispatcher/payloads/ViewRoomPayload";
@@ -60,8 +53,7 @@ describe("RoomView", () => {
6053

6154
beforeEach(async () => {
6255
mockPlatformPeg({ reload: () => {} });
63-
stubClient();
64-
cli = mocked(MatrixClientPeg.get());
56+
cli = mocked(stubClient());
6557

6658
room = new Room(`!${roomCount++}:example.org`, cli, "@alice:example.org");
6759
room.getPendingEvents = () => [];
@@ -78,11 +70,6 @@ describe("RoomView", () => {
7870
jest.spyOn(VoipUserMapper.sharedInstance(), "getVirtualRoomForRoom").mockResolvedValue(null);
7971
});
8072

81-
afterEach(async () => {
82-
unmockPlatformPeg();
83-
jest.restoreAllMocks();
84-
});
85-
8673
const mountRoomView = async (): Promise<ReactWrapper> => {
8774
if (stores.roomViewStore.getRoomId() !== room.roomId) {
8875
const switchedRoom = new Promise<void>((resolve) => {
@@ -199,32 +186,35 @@ describe("RoomView", () => {
199186
});
200187

201188
it("checks for a virtual room on room event", async () => {
189+
const mock = VoipUserMapper.sharedInstance().getVirtualRoomForRoom as unknown as jest.SpyInstance;
202190
await renderRoomView();
203-
expect(VoipUserMapper.sharedInstance().getVirtualRoomForRoom).toHaveBeenCalledWith(room.roomId);
191+
expect(mock).toHaveBeenCalledWith(room.roomId);
204192

193+
mock.mockReset();
205194
cli.emit(ClientEvent.Room, room);
206195

207196
// called again after room event
208-
expect(VoipUserMapper.sharedInstance().getVirtualRoomForRoom).toHaveBeenCalledTimes(2);
197+
expect(VoipUserMapper.sharedInstance().getVirtualRoomForRoom).toHaveBeenCalled();
209198
});
210199
});
211200

212201
describe("video rooms", () => {
213202
beforeEach(async () => {
214203
// Make it a video room
215204
room.isElementVideoRoom = () => true;
205+
room.updateMyMembership("join");
216206
await SettingsStore.setValue("feature_video_rooms", null, SettingLevel.DEVICE, true);
217207
});
218208

219209
it("normally doesn't open the chat panel", async () => {
220210
jest.spyOn(NotificationState.prototype, "isUnread", "get").mockReturnValue(false);
221-
await mountRoomView();
211+
await renderRoomView();
222212
expect(stores.rightPanelStore.isOpen).toEqual(false);
223213
});
224214

225215
it("opens the chat panel if there are unread messages", async () => {
226216
jest.spyOn(NotificationState.prototype, "isUnread", "get").mockReturnValue(true);
227-
await mountRoomView();
217+
await renderRoomView();
228218
expect(stores.rightPanelStore.isOpen).toEqual(true);
229219
expect(stores.rightPanelStore.currentCard.phase).toEqual(RightPanelPhases.Timeline);
230220
});

test/components/structures/__snapshots__/RoomView-test.tsx.snap

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,20 @@ exports[`RoomView for a local room in state ERROR should match the snapshot 1`]
188188
class="mx_RoomView_MessageList"
189189
style="height: 400px;"
190190
>
191+
<div
192+
class="mx_EventTileBubble mx_cryptoEvent mx_cryptoEvent_icon mx_cryptoEvent_icon_warning"
193+
>
194+
<div
195+
class="mx_EventTileBubble_title"
196+
>
197+
Encryption not enabled
198+
</div>
199+
<div
200+
class="mx_EventTileBubble_subtitle"
201+
>
202+
The encryption used by this room isn't supported.
203+
</div>
204+
</div>
191205
<li
192206
class="mx_NewRoomIntro"
193207
>
@@ -383,6 +397,20 @@ exports[`RoomView for a local room in state NEW should match the snapshot 1`] =
383397
class="mx_RoomView_MessageList"
384398
style="height: 400px;"
385399
>
400+
<div
401+
class="mx_EventTileBubble mx_cryptoEvent mx_cryptoEvent_icon mx_cryptoEvent_icon_warning"
402+
>
403+
<div
404+
class="mx_EventTileBubble_title"
405+
>
406+
Encryption not enabled
407+
</div>
408+
<div
409+
class="mx_EventTileBubble_subtitle"
410+
>
411+
The encryption used by this room isn't supported.
412+
</div>
413+
</div>
386414
<li
387415
class="mx_NewRoomIntro"
388416
>

test/test-utils/test-utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ export function createTestClient(): MatrixClient {
185185
isCryptoEnabled: jest.fn().mockReturnValue(false),
186186
hasLazyLoadMembersEnabled: jest.fn().mockReturnValue(false),
187187
isInitialSyncComplete: jest.fn().mockReturnValue(true),
188-
downloadKeys: jest.fn(),
188+
downloadKeys: jest.fn().mockResolvedValue([]),
189189
fetchRoomEvent: jest.fn().mockRejectedValue({}),
190190
makeTxnId: jest.fn().mockImplementation(() => `t${txnId++}`),
191191
sendToDevice: jest.fn().mockResolvedValue(undefined),

0 commit comments

Comments
 (0)