Skip to content

Commit b94d40f

Browse files
committed
Write tests
1 parent c2d68f8 commit b94d40f

File tree

4 files changed

+143
-0
lines changed

4 files changed

+143
-0
lines changed

test/test-utils/test-utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,7 @@ export function mkStubRoom(
650650
getJoinedMembers: jest.fn().mockReturnValue([]),
651651
getLiveTimeline: jest.fn().mockReturnValue(stubTimeline),
652652
getLastLiveEvent: jest.fn().mockReturnValue(undefined),
653+
getLastActiveTimestamp: jest.fn().mockReturnValue(1183140000),
653654
getMember: jest.fn().mockReturnValue({
654655
userId: "@member:domain.bla",
655656
name: "Member",
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
Copyright 2025 Element Creations Ltd.
3+
4+
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
5+
Please see LICENSE files in the repository root for full details.
6+
*/
7+
8+
import { ClientEvent } from "matrix-js-sdk/src/matrix";
9+
10+
import { AccountDataApi } from "../../../src/modules/AccountDataApi";
11+
import { mkEvent, stubClient } from "../../test-utils/test-utils";
12+
13+
describe("AccountDataApi", () => {
14+
describe("AccountDataWatchable", () => {
15+
it("should return content of account data event on get()", () => {
16+
const cli = stubClient();
17+
const api = new AccountDataApi();
18+
// Mock cli to return a event
19+
const content = { foo: "bar" };
20+
const event = mkEvent({ content, type: "m.test", user: "@foobar:matrix.org", event: true });
21+
cli.getAccountData = () => event;
22+
expect(api.get("m.test").value).toStrictEqual(content);
23+
});
24+
25+
it("should update value on event", () => {
26+
const cli = stubClient();
27+
const api = new AccountDataApi();
28+
// Mock cli to return a event
29+
const content = { foo: "bar" };
30+
const event = mkEvent({ content, type: "m.test", user: "@foobar:matrix.org", event: true });
31+
cli.getAccountData = () => event;
32+
33+
const watchable = api.get("m.test");
34+
expect(watchable.value).toStrictEqual(content);
35+
36+
const fn = jest.fn();
37+
watchable.watch(fn);
38+
39+
// Let's say that the account data event changed
40+
const event2 = mkEvent({
41+
content: { foo: "abc" },
42+
type: "m.test",
43+
user: "@foobar:matrix.org",
44+
event: true,
45+
});
46+
cli.emit(ClientEvent.AccountData, event2);
47+
// Watchable value should have been updated
48+
expect(watchable.value).toStrictEqual({ foo: "abc" });
49+
// Watched callbacks should be called
50+
expect(fn).toHaveBeenCalledTimes(1);
51+
52+
// Make sure unwatch removed the event listener
53+
cli.off = jest.fn();
54+
watchable.unwatch(fn);
55+
expect(cli.off).toHaveBeenCalledTimes(1);
56+
});
57+
});
58+
59+
it("should set account data via js-sdk on set()", async () => {
60+
const cli = stubClient();
61+
const api = new AccountDataApi();
62+
await api.set("m.test", { foo: "bar" });
63+
expect(cli.setAccountData).toHaveBeenCalledTimes(1);
64+
});
65+
66+
it("should delete account data via js-sdk on set()", async () => {
67+
const cli = stubClient();
68+
const api = new AccountDataApi();
69+
await api.delete("m.test");
70+
expect(cli.deleteAccountData).toHaveBeenCalledTimes(1);
71+
});
72+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
Copyright 2025 Element Creations Ltd.
3+
4+
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
5+
Please see LICENSE files in the repository root for full details.
6+
*/
7+
8+
import { ClientApi } from "../../../src/modules/ClientApi";
9+
import { Room } from "../../../src/modules/models/Room";
10+
import { stubClient } from "../../test-utils/test-utils";
11+
12+
describe("ClientApi", () => {
13+
it("should return module room from getRoom()", () => {
14+
stubClient();
15+
const client = new ClientApi();
16+
const moduleRoom = client.getRoom("!foo:matrix.org");
17+
expect(moduleRoom).toBeInstanceOf(Room);
18+
expect(moduleRoom?.id).toStrictEqual("!foo:matrix.org");
19+
});
20+
});
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
Copyright 2025 Element Creations Ltd.
3+
4+
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
5+
Please see LICENSE files in the repository root for full details.
6+
*/
7+
8+
import { Room } from "../../../../src/modules/models/Room";
9+
import { mkRoom, stubClient } from "../../../test-utils";
10+
11+
describe("Room", () => {
12+
it("should return id from sdk room", () => {
13+
const cli = stubClient();
14+
const sdkRoom = mkRoom(cli, "!foo:m.org");
15+
const room = new Room(sdkRoom);
16+
expect(room.id).toStrictEqual("!foo:m.org");
17+
});
18+
19+
it("should return last timestamp from sdk room", () => {
20+
const cli = stubClient();
21+
const sdkRoom = mkRoom(cli, "!foo:m.org");
22+
const room = new Room(sdkRoom);
23+
expect(room.getLastActiveTimestamp()).toStrictEqual(sdkRoom.getLastActiveTimestamp());
24+
});
25+
26+
describe("watchableName", () => {
27+
it("should return name from sdkRoom", () => {
28+
const cli = stubClient();
29+
const sdkRoom = mkRoom(cli, "!foo:m.org");
30+
sdkRoom.name = "Foo Name";
31+
const room = new Room(sdkRoom);
32+
expect(room.name.value).toStrictEqual("Foo Name");
33+
});
34+
35+
it("should add/remove event listener on sdk room", () => {
36+
const cli = stubClient();
37+
const sdkRoom = mkRoom(cli, "!foo:m.org");
38+
sdkRoom.name = "Foo Name";
39+
40+
const room = new Room(sdkRoom);
41+
const fn = jest.fn();
42+
43+
room.name.watch(fn);
44+
expect(sdkRoom.on).toHaveBeenCalledTimes(1);
45+
46+
room.name.unwatch(fn);
47+
expect(sdkRoom.off).toHaveBeenCalledTimes(1);
48+
});
49+
});
50+
});

0 commit comments

Comments
 (0)