Files

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

219 lines
9.3 KiB
TypeScript
Raw Permalink Normal View History

2026-05-06 09:34:36 +01:00
/*
* Copyright 2026 Element Creations Ltd.
*
* SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
* Please see LICENSE files in the repository root for full details.
*/
import { MatrixError, type MatrixClient } from "matrix-js-sdk/src/matrix";
import { waitFor } from "jest-matrix-react";
2026-06-19 16:34:18 +01:00
import type { MockedObject } from "jest-mock-vitest-adapter";
2026-05-06 09:34:36 +01:00
import { UserMenuViewModel } from "../../../src/viewmodels/menus/UserMenuViewModel";
import { getMockClientWithEventEmitter, mockClientMethodsServer, mockClientMethodsUser } from "../../test-utils";
import { MatrixDispatcher } from "../../../src/dispatcher/dispatcher";
2026-07-01 13:49:18 +01:00
import { SDKContextClass } from "../../../src/contexts/SDKContextClass";
2026-05-06 09:34:36 +01:00
import SdkConfig from "../../../src/SdkConfig";
import { Action } from "../../../src/dispatcher/actions";
import { UserTab } from "../../../src/components/views/dialogs/UserTab";
import Modal from "../../../src/Modal";
import FeedbackDialog from "../../../src/components/views/dialogs/FeedbackDialog";
2026-07-06 20:28:34 +01:00
import { type OwnProfileStore } from "../../../src/stores/OwnProfileStore";
import { TestSDKContext } from "../../unit-tests/TestSDKContext.ts";
2026-05-06 09:34:36 +01:00
describe("UserMenuViewModel", () => {
let dispatcher: MatrixDispatcher;
let client: MockedObject<MatrixClient>;
2026-07-06 20:28:34 +01:00
let mockOwnProfileStore: OwnProfileStore;
let sdkContext: TestSDKContext;
2026-05-06 09:34:36 +01:00
beforeEach(() => {
dispatcher = new MatrixDispatcher();
client = getMockClientWithEventEmitter({
...mockClientMethodsUser(),
...mockClientMethodsServer(),
getAuthMetadata: jest.fn().mockRejectedValue(new MatrixError({ errcode: "M_UNRECOGNIZED" }, 404)),
getExtendedProfileProperty: jest.fn().mockResolvedValue(undefined),
2026-06-16 10:01:34 +01:00
setExtendedProfileProperty: jest.fn().mockResolvedValue(undefined),
2026-05-06 09:34:36 +01:00
});
sdkContext = new TestSDKContext();
// @ts-ignore UserMenuViewModel uses SDKContext in the constructor
SDKContextClass.instance = sdkContext;
sdkContext._client = client;
2026-07-06 20:28:34 +01:00
mockOwnProfileStore = {
displayName: "Sally Sanderson",
userStatus: undefined,
getHttpAvatarUrl: jest.fn().mockReturnValue("https://foo.dummy/avatar.png"),
on: jest.fn(),
} as unknown as OwnProfileStore;
2026-05-06 09:34:36 +01:00
});
afterEach(() => {
jest.resetAllMocks();
SdkConfig.reset();
2026-07-01 13:49:18 +01:00
SDKContextClass.instance.onLoggedOut();
2026-05-06 09:34:36 +01:00
});
it("should generate a menu options for a logged in client", () => {
2026-07-06 20:28:34 +01:00
const vm = new UserMenuViewModel({ ownProfileStore: mockOwnProfileStore }, dispatcher, client, true);
2026-05-06 09:34:36 +01:00
vm.setOpen(true);
2026-07-06 20:28:34 +01:00
expect(vm.getSnapshot().userId).toEqual("@alice:domain");
expect(vm.getSnapshot().displayName).toEqual("Sally Sanderson");
expect(vm.getSnapshot().avatarUrl).toEqual("https://foo.dummy/avatar.png");
expect(vm.getSnapshot().showAvatar).toEqual(true);
expect(vm.getSnapshot().expanded).toEqual(false);
2026-05-06 09:34:36 +01:00
});
it("should show a link for account management", async () => {
client.getAuthMetadata.mockResolvedValue({ account_management_uri: "https://example.org/" } as any);
const vm = new UserMenuViewModel({ ownProfileStore: mockOwnProfileStore }, dispatcher, client, true);
2026-05-06 09:34:36 +01:00
vm.setOpen(true);
await waitFor(() => {
expect(vm.getSnapshot().manageAccountHref).toEqual("https://example.org/");
});
2026-05-06 09:34:36 +01:00
});
it("should generate a menu options for a guest", () => {
client.isGuest.mockReturnValue(true);
2026-07-06 20:28:34 +01:00
const vm = new UserMenuViewModel({ ownProfileStore: mockOwnProfileStore }, dispatcher, client, true);
2026-05-06 09:34:36 +01:00
vm.setOpen(true);
2026-07-06 20:28:34 +01:00
expect(vm.getSnapshot().displayName).toEqual("Sally Sanderson");
expect(vm.getSnapshot().showAvatar).toEqual(false);
expect(vm.getSnapshot().showUserStatus).toEqual(false);
2026-05-06 09:34:36 +01:00
});
it("should generate a menu options that include feedback", () => {
SdkConfig.put({ bug_report_endpoint_url: "https://example.org" });
2026-07-06 20:28:34 +01:00
const vm = new UserMenuViewModel({ ownProfileStore: mockOwnProfileStore }, dispatcher, client, true);
2026-05-06 09:34:36 +01:00
vm.setOpen(true);
expect(vm.getSnapshot().actions.openFeedback).toEqual(true);
});
it("should generate a menu options that includes a home page", () => {
SdkConfig.put({ embedded_pages: { home_url: "https://example.org" } });
2026-07-06 20:28:34 +01:00
const vm = new UserMenuViewModel({ ownProfileStore: mockOwnProfileStore }, dispatcher, client, true);
2026-05-06 09:34:36 +01:00
vm.setOpen(true);
expect(vm.getSnapshot().actions.openHomePage).toEqual(true);
});
it("can toggle menu", () => {
2026-07-06 20:28:34 +01:00
const vm = new UserMenuViewModel({ ownProfileStore: mockOwnProfileStore }, dispatcher, client, true);
2026-05-06 09:34:36 +01:00
vm.setOpen(true);
expect(vm.getSnapshot().open).toEqual(true);
vm.setOpen(false);
expect(vm.getSnapshot().open).toEqual(false);
});
it("can toggle expanded state", () => {
2026-07-06 20:28:34 +01:00
const vm = new UserMenuViewModel({ ownProfileStore: mockOwnProfileStore }, dispatcher, client, true);
2026-05-06 09:34:36 +01:00
vm.setExpanded(true);
expect(vm.getSnapshot().expanded).toEqual(true);
vm.setExpanded(false);
expect(vm.getSnapshot().expanded).toEqual(false);
});
it("can open the home menu", async () => {
SdkConfig.put({ embedded_pages: { home_url: "https://example.org" } });
2026-07-06 20:28:34 +01:00
const vm = new UserMenuViewModel({ ownProfileStore: mockOwnProfileStore }, dispatcher, client, true);
2026-05-06 09:34:36 +01:00
const dispatcherSpy = jest.fn();
dispatcher.register(dispatcherSpy);
vm.setOpen(true);
vm.openHomePage();
await waitFor(() =>
expect(dispatcherSpy).toHaveBeenCalledWith({
action: Action.ViewHomePage,
}),
);
});
it("can open the 'link new device' settings menu", async () => {
2026-07-06 20:28:34 +01:00
const vm = new UserMenuViewModel({ ownProfileStore: mockOwnProfileStore }, dispatcher, client, true);
2026-05-06 09:34:36 +01:00
const dispatcherSpy = jest.fn();
dispatcher.register(dispatcherSpy);
vm.setOpen(true);
vm.linkNewDevice();
await waitFor(() =>
expect(dispatcherSpy).toHaveBeenCalledWith({
action: Action.ViewUserSettings,
initialTabId: UserTab.SessionManager,
props: { showMsc4108QrCode: true },
}),
);
});
it("can open the 'security' settings menu", async () => {
2026-07-06 20:28:34 +01:00
const vm = new UserMenuViewModel({ ownProfileStore: mockOwnProfileStore }, dispatcher, client, true);
2026-05-06 09:34:36 +01:00
const dispatcherSpy = jest.fn();
dispatcher.register(dispatcherSpy);
vm.setOpen(true);
vm.openSecurity();
await waitFor(() =>
expect(dispatcherSpy).toHaveBeenCalledWith({
action: Action.ViewUserSettings,
initialTabId: UserTab.Security,
}),
);
});
it("can open the 'feedback' settings menu", async () => {
jest.spyOn(Modal, "createDialog");
SdkConfig.put({ bug_report_endpoint_url: "https://example.org" });
2026-07-06 20:28:34 +01:00
const vm = new UserMenuViewModel({ ownProfileStore: mockOwnProfileStore }, dispatcher, client, true);
2026-05-06 09:34:36 +01:00
const dispatcherSpy = jest.fn();
dispatcher.register(dispatcherSpy);
vm.setOpen(true);
vm.openFeedback();
expect(Modal.createDialog).toHaveBeenCalledWith(FeedbackDialog);
});
it("can open the settings menu", async () => {
2026-07-06 20:28:34 +01:00
const vm = new UserMenuViewModel({ ownProfileStore: mockOwnProfileStore }, dispatcher, client, true);
2026-05-06 09:34:36 +01:00
const dispatcherSpy = jest.fn();
dispatcher.register(dispatcherSpy);
vm.setOpen(true);
vm.openSettings();
await waitFor(() =>
expect(dispatcherSpy).toHaveBeenCalledWith({
action: Action.ViewUserSettings,
}),
);
});
2026-06-16 10:01:34 +01:00
it("can clear a user status", async () => {
client.getExtendedProfileProperty.mockResolvedValue({ emoji: "🧪", text: "Testing" });
2026-07-06 20:28:34 +01:00
const vm = new UserMenuViewModel({ ownProfileStore: mockOwnProfileStore }, dispatcher, client, true);
2026-06-16 10:01:34 +01:00
vm.setOpen(true);
vm.clearStatus();
await waitFor(() =>
expect(client.setExtendedProfileProperty).toHaveBeenCalledWith("org.matrix.msc4426.status", null),
);
});
2026-05-06 09:34:36 +01:00
it("should be able to open the createAccount screen as a guest", async () => {
client.isGuest.mockReturnValue(true);
const dispatcherSpy = jest.fn();
dispatcher.register(dispatcherSpy);
2026-07-06 20:28:34 +01:00
const vm = new UserMenuViewModel({ ownProfileStore: mockOwnProfileStore }, dispatcher, client, true);
2026-05-06 09:34:36 +01:00
vm.setOpen(true);
vm.createAccount();
await waitFor(() =>
expect(dispatcherSpy).toHaveBeenCalledWith({
action: "start_registration",
}),
);
});
it("should be able to open the onSignIn screen as a guest", async () => {
client.isGuest.mockReturnValue(true);
const dispatcherSpy = jest.fn();
dispatcher.register(dispatcherSpy);
2026-07-06 20:28:34 +01:00
const vm = new UserMenuViewModel({ ownProfileStore: mockOwnProfileStore }, dispatcher, client, true);
2026-05-06 09:34:36 +01:00
vm.setOpen(true);
vm.signIn();
await waitFor(() =>
expect(dispatcherSpy).toHaveBeenCalledWith({
action: "start_login",
}),
);
});
});