2023-07-26 09:54:45 +00:00
|
|
|
/*
|
2024-09-09 13:57:16 +00:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2023-07-26 09:54:45 +00:00
|
|
|
Copyright 2023 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 13:57:16 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2023-07-26 09:54:45 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
import { MatrixClient, MatrixError } from "matrix-js-sdk/src/matrix";
|
|
|
|
import { MockedObject, mocked } from "jest-mock";
|
|
|
|
|
2024-10-15 13:57:26 +00:00
|
|
|
import { stubClient } from "../../test-utils";
|
|
|
|
import { OwnProfileStore } from "../../../src/stores/OwnProfileStore";
|
|
|
|
import { UPDATE_EVENT } from "../../../src/stores/AsyncStore";
|
2023-07-26 09:54:45 +00:00
|
|
|
|
|
|
|
describe("OwnProfileStore", () => {
|
|
|
|
let client: MockedObject<MatrixClient>;
|
|
|
|
let ownProfileStore: OwnProfileStore;
|
|
|
|
let onUpdate: ReturnType<typeof jest.fn>;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
client = mocked(stubClient());
|
|
|
|
onUpdate = jest.fn();
|
|
|
|
ownProfileStore = new OwnProfileStore();
|
|
|
|
ownProfileStore.addListener(UPDATE_EVENT, onUpdate);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
ownProfileStore.removeListener(UPDATE_EVENT, onUpdate);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("if the client has not yet been started, the displayname and avatar should be null", () => {
|
|
|
|
expect(onUpdate).not.toHaveBeenCalled();
|
|
|
|
expect(ownProfileStore.displayName).toBeNull();
|
|
|
|
expect(ownProfileStore.avatarMxc).toBeNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("if the client has been started and there is a profile, it should return the profile display name and avatar", async () => {
|
|
|
|
client.getProfileInfo.mockResolvedValue({
|
|
|
|
displayname: "Display Name",
|
|
|
|
avatar_url: "mxc://example.com/abc123",
|
|
|
|
});
|
2024-10-10 14:08:43 +00:00
|
|
|
await ownProfileStore.start();
|
2023-07-26 09:54:45 +00:00
|
|
|
|
|
|
|
expect(onUpdate).toHaveBeenCalled();
|
|
|
|
expect(ownProfileStore.displayName).toBe("Display Name");
|
|
|
|
expect(ownProfileStore.avatarMxc).toBe("mxc://example.com/abc123");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("if there is a M_NOT_FOUND error, it should report ready, displayname = MXID and avatar = null", async () => {
|
|
|
|
client.getProfileInfo.mockRejectedValue(
|
|
|
|
new MatrixError({
|
|
|
|
error: "Not found",
|
|
|
|
errcode: "M_NOT_FOUND",
|
|
|
|
}),
|
|
|
|
);
|
2024-10-10 14:08:43 +00:00
|
|
|
await ownProfileStore.start();
|
2023-07-26 09:54:45 +00:00
|
|
|
|
|
|
|
expect(onUpdate).toHaveBeenCalled();
|
|
|
|
expect(ownProfileStore.displayName).toBe(client.getSafeUserId());
|
|
|
|
expect(ownProfileStore.avatarMxc).toBeNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("if there is any other error, it should not report ready, displayname = MXID and avatar = null", async () => {
|
|
|
|
client.getProfileInfo.mockRejectedValue(
|
|
|
|
new MatrixError({
|
|
|
|
error: "Forbidden",
|
|
|
|
errcode: "M_FORBIDDEN",
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
try {
|
2024-10-10 14:08:43 +00:00
|
|
|
await ownProfileStore.start();
|
2024-10-16 15:43:07 +00:00
|
|
|
} catch {}
|
2023-07-26 09:54:45 +00:00
|
|
|
|
|
|
|
expect(onUpdate).not.toHaveBeenCalled();
|
|
|
|
expect(ownProfileStore.displayName).toBe(client.getSafeUserId());
|
|
|
|
expect(ownProfileStore.avatarMxc).toBeNull();
|
|
|
|
});
|
|
|
|
});
|