|
| 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 | +}); |
0 commit comments