Skip to content

Commit ef0ea81

Browse files
committed
Write tests
1 parent c2d68f8 commit ef0ea81

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 { AccountDataApi } from "../../../src/modules/AccountDataApi";
9+
import { mkEvent, stubClient } from "../../test-utils/test-utils";
10+
11+
describe("AccountDataApi", () => {
12+
it("should return content of account data event on get()", () => {
13+
const cli = stubClient();
14+
const api = new AccountDataApi();
15+
// Mock cli to return a event
16+
const content = { foo: "bar" };
17+
const event = mkEvent({ content, type: "m.test", user: "@foobar:matrix.org", event: true });
18+
cli.getAccountData = () => event;
19+
expect(api.get("m.test").value).toStrictEqual(content);
20+
});
21+
22+
it("should set account data via js-sdk on set()", async () => {
23+
const cli = stubClient();
24+
const api = new AccountDataApi();
25+
await api.set("m.test", { foo: "bar" });
26+
expect(cli.setAccountData).toHaveBeenCalledTimes(1);
27+
});
28+
29+
it("should delete account data via js-sdk on set()", async () => {
30+
const cli = stubClient();
31+
const api = new AccountDataApi();
32+
await api.delete("m.test");
33+
expect(cli.deleteAccountData).toHaveBeenCalledTimes(1);
34+
});
35+
});
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)