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

Commit 6d354e3

Browse files
authored
Add test coverage (#9928)
1 parent baa120f commit 6d354e3

File tree

4 files changed

+302
-30
lines changed

4 files changed

+302
-30
lines changed

src/stores/widgets/WidgetLayoutStore.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ export class WidgetLayoutStore extends ReadyWatchingStore {
414414
widgets.forEach((w, i) => {
415415
localLayout[w.id] = {
416416
container: container,
417-
width: widths[i],
417+
width: widths?.[i],
418418
index: i,
419419
height: height,
420420
};
@@ -437,7 +437,7 @@ export class WidgetLayoutStore extends ReadyWatchingStore {
437437
widgets.forEach((w, i) => {
438438
localLayout[w.id] = {
439439
container: container,
440-
width: widths[i],
440+
width: widths?.[i],
441441
index: i,
442442
height: height,
443443
};

test/settings/handlers/RoomDeviceSettingsHandler-test.ts

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,49 @@ limitations under the License.
1515
*/
1616

1717
import RoomDeviceSettingsHandler from "../../../src/settings/handlers/RoomDeviceSettingsHandler";
18-
import { WatchManager } from "../../../src/settings/WatchManager";
18+
import { SettingLevel } from "../../../src/settings/SettingLevel";
19+
import { CallbackFn, WatchManager } from "../../../src/settings/WatchManager";
1920

2021
describe("RoomDeviceSettingsHandler", () => {
21-
it("should correctly read cached values", () => {
22-
const watchers = new WatchManager();
23-
const handler = new RoomDeviceSettingsHandler(watchers);
24-
25-
const settingName = "RightPanel.phases";
26-
const roomId = "!room:server";
27-
const value = {
28-
isOpen: true,
29-
history: [{}],
30-
};
31-
32-
handler.setValue(settingName, roomId, value);
33-
expect(handler.getValue(settingName, roomId)).toEqual(value);
22+
const roomId = "!room:example.com";
23+
const value = "test value";
24+
const testSettings = [
25+
"RightPanel.phases",
26+
// special case in RoomDeviceSettingsHandler
27+
"blacklistUnverifiedDevices",
28+
];
29+
let watchers: WatchManager;
30+
let handler: RoomDeviceSettingsHandler;
31+
let settingListener: CallbackFn;
32+
33+
beforeEach(() => {
34+
watchers = new WatchManager();
35+
handler = new RoomDeviceSettingsHandler(watchers);
36+
settingListener = jest.fn();
37+
});
38+
39+
afterEach(() => {
40+
watchers.unwatchSetting(settingListener);
41+
});
42+
43+
it.each(testSettings)("should write/read/clear the value for »%s«", (setting: string): void => {
44+
// initial value should be null
45+
watchers.watchSetting(setting, roomId, settingListener);
46+
47+
expect(handler.getValue(setting, roomId)).toBeNull();
48+
49+
// set and read value
50+
handler.setValue(setting, roomId, value);
51+
expect(settingListener).toHaveBeenCalledWith(roomId, SettingLevel.ROOM_DEVICE, value);
52+
expect(handler.getValue(setting, roomId)).toEqual(value);
53+
54+
// clear value
55+
handler.setValue(setting, roomId, null);
56+
expect(settingListener).toHaveBeenCalledWith(roomId, SettingLevel.ROOM_DEVICE, null);
57+
expect(handler.getValue(setting, roomId)).toBeNull();
58+
});
59+
60+
it("canSetValue should return true", () => {
61+
expect(handler.canSetValue("test setting", roomId)).toBe(true);
3462
});
3563
});
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
Copyright 2023 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import { mocked } from "jest-mock";
18+
import { ClientEvent, EventType, MatrixClient, MatrixEvent, MatrixEventEvent } from "matrix-js-sdk/src/matrix";
19+
import { SyncState } from "matrix-js-sdk/src/sync";
20+
21+
import SettingsStore from "../../src/settings/SettingsStore";
22+
import AutoRageshakeStore from "../../src/stores/AutoRageshakeStore";
23+
import { mkEvent, stubClient } from "../test-utils";
24+
25+
jest.mock("../../src/rageshake/submit-rageshake");
26+
27+
describe("AutoRageshakeStore", () => {
28+
const roomId = "!room:example.com";
29+
let client: MatrixClient;
30+
let utdEvent: MatrixEvent;
31+
let autoRageshakeStore: AutoRageshakeStore;
32+
33+
beforeAll(() => {
34+
jest.useFakeTimers();
35+
});
36+
37+
afterAll(() => {
38+
jest.useRealTimers();
39+
});
40+
41+
beforeEach(() => {
42+
jest.spyOn(SettingsStore, "getValue").mockReturnValue(true);
43+
44+
client = stubClient();
45+
46+
// @ts-ignore bypass private ctor for tests
47+
autoRageshakeStore = new AutoRageshakeStore();
48+
autoRageshakeStore.start();
49+
50+
utdEvent = mkEvent({
51+
event: true,
52+
content: {},
53+
room: roomId,
54+
user: client.getSafeUserId(),
55+
type: EventType.RoomMessage,
56+
});
57+
jest.spyOn(utdEvent, "isDecryptionFailure").mockReturnValue(true);
58+
});
59+
60+
afterEach(() => {
61+
jest.restoreAllMocks();
62+
});
63+
64+
describe("when the initial sync completed", () => {
65+
beforeEach(() => {
66+
client.emit(ClientEvent.Sync, SyncState.Syncing, SyncState.Stopped, { nextSyncToken: "abc123" });
67+
});
68+
69+
describe("and an undecryptable event occurs", () => {
70+
beforeEach(() => {
71+
client.emit(MatrixEventEvent.Decrypted, utdEvent);
72+
// simulate event grace period
73+
jest.advanceTimersByTime(5500);
74+
});
75+
76+
it("should send a rageshake", () => {
77+
expect(mocked(client).sendToDevice.mock.calls).toMatchInlineSnapshot(`
78+
[
79+
[
80+
"im.vector.auto_rs_request",
81+
{
82+
"@userId:matrix.org": {
83+
"undefined": {
84+
"device_id": undefined,
85+
"event_id": "${utdEvent.getId()}",
86+
"recipient_rageshake": undefined,
87+
"room_id": "!room:example.com",
88+
"sender_key": undefined,
89+
"session_id": undefined,
90+
"user_id": "@userId:matrix.org",
91+
},
92+
},
93+
},
94+
],
95+
]
96+
`);
97+
});
98+
});
99+
});
100+
});

0 commit comments

Comments
 (0)