Move more tests over to vitest (#34249)

* Move more tests over to vitest

* Add exception for test_setting
This commit is contained in:
Michael Telatynski
2026-07-15 08:48:51 +00:00
committed by GitHub
parent cb07147d72
commit 0c61944ffd
24 changed files with 292 additions and 233 deletions
@@ -1,27 +0,0 @@
/*
Copyright 2024 New Vector Ltd.
Copyright 2024 The Matrix.org Foundation C.I.C.
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 PosthogTrackers from "../../../../src/PosthogTrackers";
import AnalyticsController from "../../../../src/settings/controllers/AnalyticsController";
import { SettingLevel } from "../../../../src/settings/SettingLevel";
describe("AnalyticsController", () => {
afterEach(() => {
jest.restoreAllMocks();
});
it("Tracks a Posthog interaction on change", () => {
const trackInteractionSpy = jest.spyOn(PosthogTrackers, "trackInteraction");
const controller = new AnalyticsController("WebSettingsNotificationsTACOnlyNotificationsToggle");
controller.onChange(SettingLevel.DEVICE, null, false);
expect(trackInteractionSpy).toHaveBeenCalledWith("WebSettingsNotificationsTACOnlyNotificationsToggle");
});
});
@@ -1,98 +0,0 @@
/*
Copyright 2024 New Vector Ltd.
Copyright 2023 The Matrix.org Foundation C.I.C.
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 { type MatrixClient, MatrixEvent } from "matrix-js-sdk/src/matrix";
import { mocked } from "jest-mock";
import { SETTINGS } from "../../../../src/settings/Settings";
import { stubClient } from "../../../test-utils";
import MatrixClientBackedController from "../../../../src/settings/controllers/MatrixClientBackedController";
import { SettingLevel } from "../../../../src/settings/SettingLevel.ts";
describe("BlockInvitesConfigController", () => {
describe("When server does not support MSC4380", () => {
let cli: MatrixClient;
beforeEach(() => {
cli = stubClient();
cli.doesServerSupportUnstableFeature = jest.fn(async () => false);
MatrixClientBackedController.matrixClient = cli;
});
test("settingDisabled() should give a message", () => {
const controller = SETTINGS.blockInvites.controller!;
expect(controller.settingDisabled).toEqual("Your server does not implement this feature.");
});
});
describe("When server supports MSC4380", () => {
let cli: MatrixClient;
beforeEach(async () => {
cli = stubClient();
cli.doesServerSupportUnstableFeature = jest.fn(async (feature) => {
return feature == "org.matrix.msc4380.stable";
});
MatrixClientBackedController.matrixClient = cli;
});
test("settingDisabled() should be false", () => {
const controller = SETTINGS.blockInvites.controller!;
expect(controller.settingDisabled).toEqual(false);
});
describe("getValueOverride()", () => {
it("should return true when invites are blocked", async () => {
const controller = SETTINGS.blockInvites.controller!;
mockAccountData(cli, { default_action: "block" });
expect(controller.getValueOverride(SettingLevel.DEVICE, null, null, null)).toEqual(true);
});
it("should return false when invites are not blocked", async () => {
const controller = SETTINGS.blockInvites.controller!;
mockAccountData(cli, { default_action: {} });
expect(controller.getValueOverride(SettingLevel.DEVICE, null, null, null)).toEqual(false);
});
});
describe("beforeChange()", () => {
it("should set the account data when the value is enabled", async () => {
const controller = SETTINGS.blockInvites.controller!;
await controller.beforeChange(SettingLevel.DEVICE, null, true);
expect(cli.setAccountData).toHaveBeenCalledTimes(1);
expect(cli.setAccountData).toHaveBeenCalledWith("m.invite_permission_config", {
default_action: "block",
});
});
it("should set the account data when the value is disabled", async () => {
const controller = SETTINGS.blockInvites.controller!;
await controller.beforeChange(SettingLevel.DEVICE, null, false);
expect(cli.setAccountData).toHaveBeenCalledTimes(1);
expect(cli.setAccountData).toHaveBeenCalledWith("m.invite_permission_config", {});
});
});
});
});
/**
* Add a mock implementation for {@link MatrixClient.getAccountData} which will return the given data
* in response to any request for `m.invite_permission_config`.
*/
function mockAccountData(cli: MatrixClient, mockAccountData: object) {
mocked(cli.getAccountData).mockImplementation((eventType) => {
if (eventType == "m.invite_permission_config") {
return new MatrixEvent({
type: "m.invite_permission_config",
content: mockAccountData,
});
} else {
return undefined;
}
});
}
@@ -1,34 +0,0 @@
/*
Copyright 2024 New Vector 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 { AllDevicesIsolationMode, OnlySignedDevicesIsolationMode } from "matrix-js-sdk/src/crypto-api";
import { stubClient } from "../../../test-utils";
import DeviceIsolationModeController from "../../../../src/settings/controllers/DeviceIsolationModeController.ts";
import { SettingLevel } from "../../../../src/settings/SettingLevel";
describe("DeviceIsolationModeController", () => {
afterEach(() => {
jest.resetAllMocks();
});
describe("tracks enabling and disabling", () => {
it("on sets signed device isolation mode", () => {
const cli = stubClient();
const controller = new DeviceIsolationModeController();
controller.onChange(SettingLevel.DEVICE, "", true);
expect(cli.getCrypto()?.setDeviceIsolationMode).toHaveBeenCalledWith(new OnlySignedDevicesIsolationMode());
});
it("off sets all device isolation mode", () => {
const cli = stubClient();
const controller = new DeviceIsolationModeController();
controller.onChange(SettingLevel.DEVICE, "", false);
expect(cli.getCrypto()?.setDeviceIsolationMode).toHaveBeenCalledWith(new AllDevicesIsolationMode(false));
});
});
});
@@ -1,57 +0,0 @@
/*
Copyright 2024 New Vector 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 fetchMock from "@fetch-mock/jest";
import { ClientEvent, MatrixClient } from "matrix-js-sdk/src/matrix";
import { SettingLevel } from "../../../../src/settings/SettingLevel";
import FallbackIceServerController from "../../../../src/settings/controllers/FallbackIceServerController.ts";
import MatrixClientBackedController from "../../../../src/settings/controllers/MatrixClientBackedController.ts";
import SettingsStore from "../../../../src/settings/SettingsStore.ts";
describe("FallbackIceServerController", () => {
beforeEach(() => {
fetchMock.get("https://matrix.org/_matrix/client/versions", { versions: ["v1.4"] });
});
afterEach(() => {
jest.restoreAllMocks();
});
it("should update MatrixClient's state when the setting is updated", async () => {
const client = new MatrixClient({
baseUrl: "https://matrix.org",
userId: "@alice:matrix.org",
accessToken: "token",
});
MatrixClientBackedController.matrixClient = client;
expect(client.isFallbackICEServerAllowed()).toBeFalsy();
await SettingsStore.setValue("fallbackICEServerAllowed", null, SettingLevel.DEVICE, true);
expect(client.isFallbackICEServerAllowed()).toBeTruthy();
});
it("should force the setting to be disabled if disable_fallback_ice=true", async () => {
const controller = new FallbackIceServerController();
const client = new MatrixClient({
baseUrl: "https://matrix.org",
userId: "@alice:matrix.org",
accessToken: "token",
});
MatrixClientBackedController.matrixClient = client;
expect(controller.settingDisabled).toBeFalsy();
client["clientWellKnown"] = {
"io.element.voip": {
disable_fallback_ice: true,
},
};
client.emit(ClientEvent.ClientWellKnown, client["clientWellKnown"]);
expect(controller.settingDisabled).toBeTruthy();
});
});
@@ -1,24 +0,0 @@
/*
Copyright 2024 New Vector Ltd.
Copyright 2022 The Matrix.org Foundation C.I.C.
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 { Action } from "../../../../src/dispatcher/actions";
import dis from "../../../../src/dispatcher/dispatcher";
import FontSizeController from "../../../../src/settings/controllers/FontSizeController";
import { SettingLevel } from "../../../../src/settings/SettingLevel";
const dispatchSpy = jest.spyOn(dis, "fire");
describe("FontSizeController", () => {
it("dispatches a font size action on change", () => {
const controller = new FontSizeController();
controller.onChange(SettingLevel.ACCOUNT, "$room:server", 12);
expect(dispatchSpy).toHaveBeenCalledWith(Action.MigrateBaseFontSize);
});
});
@@ -1,87 +0,0 @@
/*
Copyright 2024 New Vector Ltd.
Copyright 2022 The Matrix.org Foundation C.I.C.
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 IncompatibleController from "../../../../src/settings/controllers/IncompatibleController";
import { SettingLevel } from "../../../../src/settings/SettingLevel";
import SettingsStore from "../../../../src/settings/SettingsStore";
declare module "../../../../src/settings/Settings.tsx" {
interface Settings {
test_setting: IBaseSetting<string>;
}
}
describe("IncompatibleController", () => {
const settingsGetValueSpy = jest.spyOn(SettingsStore, "getValue");
beforeEach(() => {
settingsGetValueSpy.mockClear();
});
describe("incompatibleSetting", () => {
describe("when incompatibleValue is not set", () => {
it("returns true when setting value is true", () => {
// no incompatible value set, defaulted to true
const controller = new IncompatibleController("test_setting", { key: null });
settingsGetValueSpy.mockReturnValue(true);
// true === true
expect(controller.incompatibleSetting).toBe(true);
expect(controller.settingDisabled).toEqual(true);
expect(settingsGetValueSpy).toHaveBeenCalledWith("test_setting");
});
it("returns false when setting value is not true", () => {
// no incompatible value set, defaulted to true
const controller = new IncompatibleController("test_setting", { key: null });
settingsGetValueSpy.mockReturnValue("test");
expect(controller.incompatibleSetting).toBe(false);
});
});
describe("when incompatibleValue is set to a value", () => {
it("returns true when setting value matches incompatible value", () => {
const controller = new IncompatibleController("test_setting", { key: null }, "test");
settingsGetValueSpy.mockReturnValue("test");
expect(controller.incompatibleSetting).toBe(true);
});
it("returns false when setting value is not true", () => {
const controller = new IncompatibleController("test_setting", { key: null }, "test");
settingsGetValueSpy.mockReturnValue("not test");
expect(controller.incompatibleSetting).toBe(false);
});
});
describe("when incompatibleValue is set to a function", () => {
it("returns result from incompatibleValue function", () => {
const incompatibleValueFn = jest.fn().mockReturnValue(false);
const controller = new IncompatibleController("test_setting", { key: null }, incompatibleValueFn);
settingsGetValueSpy.mockReturnValue("test");
expect(controller.incompatibleSetting).toBe(false);
expect(incompatibleValueFn).toHaveBeenCalledWith("test");
});
});
});
describe("getValueOverride()", () => {
it("returns forced value when setting is incompatible", () => {
settingsGetValueSpy.mockReturnValue(true);
const controller = new IncompatibleController("test_setting", { key: null });
expect(
controller.getValueOverride(SettingLevel.ACCOUNT, "$room:server", true, SettingLevel.ACCOUNT),
).toEqual({ key: null });
});
it("returns null when setting is not incompatible", () => {
settingsGetValueSpy.mockReturnValue(false);
const controller = new IncompatibleController("test_setting", { key: null });
expect(
controller.getValueOverride(SettingLevel.ACCOUNT, "$room:server", true, SettingLevel.ACCOUNT),
).toEqual(null);
});
});
});
@@ -1,156 +0,0 @@
/*
Copyright 2025 New Vector 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 { MatrixEvent } from "matrix-js-sdk/src/matrix";
import MatrixClientBackedController from "../../../../src/settings/controllers/MatrixClientBackedController";
import InviteRulesConfigController from "../../../../src/settings/controllers/InviteRulesConfigController";
import { SettingLevel } from "../../../../src/settings/SettingLevel";
import { getMockClientWithEventEmitter, mockClientMethodsServer } from "../../../test-utils";
import { INVITE_RULES_ACCOUNT_DATA_TYPE, type InviteConfigAccountData } from "../../../../src/@types/invite-rules";
describe("InviteRulesConfigController", () => {
afterEach(() => {
jest.restoreAllMocks();
});
it("gets the default settings when none are specified.", () => {
const controller = new InviteRulesConfigController();
MatrixClientBackedController.matrixClient = getMockClientWithEventEmitter({
...mockClientMethodsServer(),
getAccountData: jest.fn().mockReturnValue(null),
});
const value = controller.getValueOverride(SettingLevel.ACCOUNT);
expect(value).toEqual(InviteRulesConfigController.default);
});
it("gets the default settings when the setting is empty.", () => {
const controller = new InviteRulesConfigController();
MatrixClientBackedController.matrixClient = getMockClientWithEventEmitter({
...mockClientMethodsServer(),
getAccountData: jest
.fn()
.mockReturnValue(new MatrixEvent({ type: INVITE_RULES_ACCOUNT_DATA_TYPE, content: {} })),
});
const value = controller.getValueOverride(SettingLevel.ACCOUNT);
expect(value).toEqual(InviteRulesConfigController.default);
});
it.each<InviteConfigAccountData>([{ blocked_users: ["foo_bar"] }, { blocked_users: [] }, {}])(
"calculates blockAll to be false",
(content: InviteConfigAccountData) => {
const controller = new InviteRulesConfigController();
MatrixClientBackedController.matrixClient = getMockClientWithEventEmitter({
...mockClientMethodsServer(),
getAccountData: jest.fn().mockReturnValue(
new MatrixEvent({
type: INVITE_RULES_ACCOUNT_DATA_TYPE,
content,
}),
),
});
const globalValue = controller.getValueOverride(SettingLevel.ACCOUNT);
expect(globalValue.allBlocked).toEqual(false);
},
);
it.each<InviteConfigAccountData>([
{ blocked_users: ["*"] },
{ blocked_users: ["*", "bob"] },
{ allowed_users: ["*"], blocked_users: ["*"] },
])("calculates blockAll to be true", (content: InviteConfigAccountData) => {
const controller = new InviteRulesConfigController();
MatrixClientBackedController.matrixClient = getMockClientWithEventEmitter({
...mockClientMethodsServer(),
getAccountData: jest.fn().mockReturnValue(
new MatrixEvent({
type: INVITE_RULES_ACCOUNT_DATA_TYPE,
content,
}),
),
});
const globalValue = controller.getValueOverride(SettingLevel.ACCOUNT);
expect(globalValue.allBlocked).toEqual(true);
});
it("sets the account data correctly for blockAll = true", async () => {
const controller = new InviteRulesConfigController();
const client = (MatrixClientBackedController.matrixClient = getMockClientWithEventEmitter({
...mockClientMethodsServer(),
getAccountData: jest.fn().mockReturnValue(
new MatrixEvent({
type: INVITE_RULES_ACCOUNT_DATA_TYPE,
content: {
existing_content: {},
allowed_servers: ["*"],
},
}),
),
setAccountData: jest.fn(),
}));
expect(await controller.beforeChange(SettingLevel.ACCOUNT, null, { allBlocked: true })).toBe(true);
expect(client.setAccountData).toHaveBeenCalledWith(INVITE_RULES_ACCOUNT_DATA_TYPE, {
existing_content: {},
allowed_servers: ["*"],
blocked_users: ["*"],
});
});
it("sets the account data correctly for blockAll = false", async () => {
const controller = new InviteRulesConfigController();
const client = (MatrixClientBackedController.matrixClient = getMockClientWithEventEmitter({
...mockClientMethodsServer(),
getAccountData: jest.fn().mockReturnValue(
new MatrixEvent({
type: INVITE_RULES_ACCOUNT_DATA_TYPE,
content: {
existing_content: {},
allowed_servers: ["*"],
blocked_users: ["extra_user", "*"],
},
}),
),
setAccountData: jest.fn(),
}));
expect(await controller.beforeChange(SettingLevel.ACCOUNT, null, { allBlocked: false })).toBe(true);
expect(client.setAccountData).toHaveBeenCalledWith(INVITE_RULES_ACCOUNT_DATA_TYPE, {
existing_content: {},
allowed_servers: ["*"],
blocked_users: ["extra_user"],
});
});
it.each([true, false])("ignores a no-op when allBlocked = %s", async (allBlocked) => {
const controller = new InviteRulesConfigController();
const client = (MatrixClientBackedController.matrixClient = getMockClientWithEventEmitter({
...mockClientMethodsServer(),
getAccountData: jest.fn().mockReturnValue(
new MatrixEvent({
type: INVITE_RULES_ACCOUNT_DATA_TYPE,
content: {
existing_content: {},
allowed_servers: ["*"],
blocked_users: allBlocked ? ["*"] : [],
},
}),
),
setAccountData: jest.fn(),
}));
expect(await controller.beforeChange(SettingLevel.ACCOUNT, null, { allBlocked })).toBe(false);
expect(client.setAccountData).not.toHaveBeenCalled();
});
});
@@ -1,164 +0,0 @@
/*
Copyright 2025 New Vector 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 { MatrixEvent } from "matrix-js-sdk/src/matrix";
import MatrixClientBackedController from "../../../../src/settings/controllers/MatrixClientBackedController";
import MediaPreviewConfigController from "../../../../src/settings/controllers/MediaPreviewConfigController";
import { SettingLevel } from "../../../../src/settings/SettingLevel";
import { getMockClientWithEventEmitter, mockClientMethodsServer } from "../../../test-utils";
import { MEDIA_PREVIEW_ACCOUNT_DATA_TYPE, MediaPreviewValue } from "../../../../src/@types/media_preview";
describe("MediaPreviewConfigController", () => {
afterEach(() => {
jest.restoreAllMocks();
});
const ROOM_ID = "!room:example.org";
it("gets the default settings when none are specified.", () => {
const controller = new MediaPreviewConfigController();
MatrixClientBackedController.matrixClient = getMockClientWithEventEmitter({
...mockClientMethodsServer(),
getAccountData: jest.fn().mockReturnValue(null),
});
const value = controller.getValueOverride(SettingLevel.ACCOUNT, null);
expect(value).toEqual(MediaPreviewConfigController.default);
});
it("gets the default settings when the setting is empty.", () => {
const controller = new MediaPreviewConfigController();
MatrixClientBackedController.matrixClient = getMockClientWithEventEmitter({
...mockClientMethodsServer(),
getAccountData: jest
.fn()
.mockReturnValue(new MatrixEvent({ type: MEDIA_PREVIEW_ACCOUNT_DATA_TYPE, content: {} })),
});
const value = controller.getValueOverride(SettingLevel.ACCOUNT, null);
expect(value).toEqual(MediaPreviewConfigController.default);
});
it.each([["media_previews"], ["invite_avatars"]])("gets the correct value for %s at the global level", (key) => {
const controller = new MediaPreviewConfigController();
MatrixClientBackedController.matrixClient = getMockClientWithEventEmitter({
...mockClientMethodsServer(),
getAccountData: jest.fn().mockReturnValue(
new MatrixEvent({
type: MEDIA_PREVIEW_ACCOUNT_DATA_TYPE,
content: {
[key]: MediaPreviewValue.Off,
},
}),
),
getRoom: jest.fn().mockReturnValue({
getAccountData: jest.fn().mockReturnValue(null),
}),
});
const globalValue = controller.getValueOverride(SettingLevel.ACCOUNT, null);
expect(globalValue[key]).toEqual(MediaPreviewValue.Off);
// Should follow the global value.
const roomValue = controller.getValueOverride(SettingLevel.ROOM_ACCOUNT, ROOM_ID);
expect(roomValue[key]).toEqual(MediaPreviewValue.Off);
});
it.each([["media_previews"], ["invite_avatars"]])("gets the correct value for %s at the room level", (key) => {
const controller = new MediaPreviewConfigController();
MatrixClientBackedController.matrixClient = getMockClientWithEventEmitter({
...mockClientMethodsServer(),
getAccountData: jest.fn().mockReturnValue(null),
getRoom: jest.fn().mockReturnValue({
getAccountData: jest.fn().mockReturnValue(
new MatrixEvent({
type: MEDIA_PREVIEW_ACCOUNT_DATA_TYPE,
content: {
[key]: MediaPreviewValue.Off,
},
}),
),
}),
});
const globalValue = controller.getValueOverride(SettingLevel.ACCOUNT, null);
expect(globalValue[key]).toEqual(MediaPreviewValue.On);
// Should follow the global value.
const roomValue = controller.getValueOverride(SettingLevel.ROOM_ACCOUNT, ROOM_ID);
expect(roomValue[key]).toEqual(MediaPreviewValue.Off);
});
it.each([["media_previews"], ["invite_avatars"]])(
"uses defaults when an invalid value is set on the global level",
(key) => {
const controller = new MediaPreviewConfigController();
MatrixClientBackedController.matrixClient = getMockClientWithEventEmitter({
...mockClientMethodsServer(),
getAccountData: jest.fn().mockReturnValue(
new MatrixEvent({
type: MEDIA_PREVIEW_ACCOUNT_DATA_TYPE,
content: {
[key]: "bibble",
},
}),
),
getRoom: jest.fn().mockReturnValue({
getAccountData: jest.fn().mockReturnValue(null),
}),
});
const globalValue = controller.getValueOverride(SettingLevel.ACCOUNT, null);
expect(globalValue[key]).toEqual(MediaPreviewValue.On);
// Should follow the global value.
const roomValue = controller.getValueOverride(SettingLevel.ROOM_ACCOUNT, ROOM_ID);
expect(roomValue[key]).toEqual(MediaPreviewValue.On);
},
);
it.each([["media_previews"], ["invite_avatars"]])(
"uses global value when an invalid value is set on the room level",
(key) => {
const controller = new MediaPreviewConfigController();
MatrixClientBackedController.matrixClient = getMockClientWithEventEmitter({
...mockClientMethodsServer(),
getAccountData: jest.fn().mockReturnValue(
new MatrixEvent({
type: MEDIA_PREVIEW_ACCOUNT_DATA_TYPE,
content: {
[key]: MediaPreviewValue.Off,
},
}),
),
getRoom: jest.fn().mockReturnValue({
getAccountData: jest.fn().mockReturnValue(
new MatrixEvent({
type: MEDIA_PREVIEW_ACCOUNT_DATA_TYPE,
content: {
[key]: "bibble",
},
}),
),
}),
});
const globalValue = controller.getValueOverride(SettingLevel.ACCOUNT, null);
expect(globalValue[key]).toEqual(MediaPreviewValue.Off);
// Should follow the global value.
const roomValue = controller.getValueOverride(SettingLevel.ROOM_ACCOUNT, ROOM_ID);
expect(roomValue[key]).toEqual(MediaPreviewValue.Off);
},
);
});
@@ -1,139 +0,0 @@
/*
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 type { Capabilities } from "matrix-js-sdk/src/matrix";
import RequiresSettingsController from "../../../../src/settings/controllers/RequiresSettingsController";
import { SettingLevel } from "../../../../src/settings/SettingLevel";
import SettingsStore from "../../../../src/settings/SettingsStore";
import MatrixClientBackedController from "../../../../src/settings/controllers/MatrixClientBackedController";
import { getMockClientWithEventEmitter, mockClientMethodsServer } from "../../../test-utils";
describe("RequiresSettingsController", () => {
afterEach(() => {
SettingsStore.reset();
});
it("forces a value if a setting is false", async () => {
const forcedValue = true;
await SettingsStore.setValue("useCompactLayout", null, SettingLevel.DEVICE, true);
await SettingsStore.setValue("useCustomFontSize", null, SettingLevel.DEVICE, false);
const controller = new RequiresSettingsController(["useCompactLayout", "useCustomFontSize"], forcedValue);
expect(controller.settingDisabled).toEqual(true);
expect(controller.getValueOverride()).toEqual(forcedValue);
});
it("does not force a value if all settings are true", async () => {
const controller = new RequiresSettingsController(["useCompactLayout", "useCustomFontSize"]);
await SettingsStore.setValue("useCompactLayout", null, SettingLevel.DEVICE, true);
await SettingsStore.setValue("useCustomFontSize", null, SettingLevel.DEVICE, true);
expect(controller.settingDisabled).toEqual(false);
expect(controller.getValueOverride()).toEqual(null);
});
describe("with capabilites", () => {
let client: ReturnType<typeof getMockClientWithEventEmitter>;
beforeEach(() => {
client = getMockClientWithEventEmitter({
...mockClientMethodsServer(),
getCachedCapabilities: jest.fn().mockImplementation(() => {}),
getCapabilities: jest.fn().mockRejectedValue({}),
});
MatrixClientBackedController["_matrixClient"] = client;
});
afterEach(() => {
jest.restoreAllMocks();
});
it("will disable setting if capability check is true", async () => {
const caps = {
"m.change_password": {
enabled: false,
},
};
client.getCachedCapabilities.mockImplementation(() => caps);
const controller = new RequiresSettingsController([], false, (c: Capabilities) => {
expect(c).toEqual(caps);
return !c["m.change_password"]?.enabled;
});
// Test that we fetch caps
controller["initMatrixClient"]();
expect(client.getCapabilities).toHaveBeenCalled();
// Test that we check caps.
expect(controller.settingDisabled).toEqual(true);
expect(controller.getValueOverride()).toEqual(false);
expect(client.getCachedCapabilities).toHaveBeenCalled();
});
it("will not disable setting if capability check is false", async () => {
const caps = {
"m.change_password": {
enabled: true,
},
};
client.getCachedCapabilities.mockImplementation(() => caps);
const controller = new RequiresSettingsController([], false, (c: Capabilities) => {
expect(c).toEqual(caps);
return !c["m.change_password"]?.enabled;
});
// Test that we fetch caps
controller["initMatrixClient"]();
expect(client.getCapabilities).toHaveBeenCalled();
// Test that we check caps.
expect(controller.settingDisabled).toEqual(false);
expect(controller.getValueOverride()).toEqual(null);
expect(client.getCachedCapabilities).toHaveBeenCalled();
});
it("will check dependency settings before checking capabilites", async () => {
const caps = {
"m.change_password": {
enabled: false,
},
};
client.getCachedCapabilities.mockImplementation(() => caps);
await SettingsStore.setValue("useCompactLayout", null, SettingLevel.DEVICE, false);
const controller = new RequiresSettingsController(["useCompactLayout"], false, (c: Capabilities) => false);
// Test that we fetch caps
controller["initMatrixClient"]();
expect(client.getCapabilities).toHaveBeenCalled();
// Test that we check caps.
expect(controller.settingDisabled).toEqual(true);
expect(controller.getValueOverride()).toEqual(false);
expect(client.getCachedCapabilities).not.toHaveBeenCalled();
});
it("will disable setting if capability check is true and dependency settings are true", async () => {
const caps = {
"m.change_password": {
enabled: false,
},
};
client.getCachedCapabilities.mockImplementation(() => caps);
await SettingsStore.setValue("useCompactLayout", null, SettingLevel.DEVICE, true);
const controller = new RequiresSettingsController(["useCompactLayout"], false, (c: Capabilities) => {
expect(c).toEqual(caps);
return !c["m.change_password"]?.enabled;
});
// Test that we fetch caps
controller["initMatrixClient"]();
expect(client.getCapabilities).toHaveBeenCalled();
// Test that we check caps.
expect(controller.settingDisabled).toEqual(true);
expect(controller.getValueOverride()).toEqual(false);
expect(client.getCachedCapabilities).toHaveBeenCalled();
});
});
});
@@ -1,139 +0,0 @@
/*
Copyright 2024 New Vector Ltd.
Copyright 2023 The Matrix.org Foundation C.I.C.
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 { type MatrixClient } from "matrix-js-sdk/src/matrix";
import ServerSupportUnstableFeatureController from "../../../../src/settings/controllers/ServerSupportUnstableFeatureController";
import { SettingLevel } from "../../../../src/settings/SettingLevel";
import { type FeatureSettingKey, LabGroup, SETTINGS } from "../../../../src/settings/Settings";
import { stubClient } from "../../../test-utils";
import { WatchManager } from "../../../../src/settings/WatchManager";
import MatrixClientBackedController from "../../../../src/settings/controllers/MatrixClientBackedController";
describe("ServerSupportUnstableFeatureController", () => {
const watchers = new WatchManager();
const setting = "setting_name" as FeatureSettingKey;
async function prepareSetting(
cli: MatrixClient,
controller: ServerSupportUnstableFeatureController,
): Promise<void> {
SETTINGS[setting] = {
isFeature: true,
labsGroup: LabGroup.Messaging,
displayName: "name of some kind" as TranslationKey,
supportedLevels: [SettingLevel.DEVICE, SettingLevel.CONFIG],
default: false,
controller,
};
const deferred = Promise.withResolvers<any>();
watchers.watchSetting(setting, null, deferred.resolve);
MatrixClientBackedController.matrixClient = cli;
await deferred.promise;
}
describe("getValueOverride()", () => {
it("should return forced value is setting is disabled", async () => {
const cli = stubClient();
cli.doesServerSupportUnstableFeature = jest.fn(async () => false);
const controller = new ServerSupportUnstableFeatureController(
setting,
watchers,
[["feature"]],
undefined,
undefined,
"other_value",
);
await prepareSetting(cli, controller);
expect(controller.getValueOverride(SettingLevel.DEVICE, null, true, SettingLevel.ACCOUNT)).toEqual(
"other_value",
);
});
it("should pass through to the handler if setting is not disabled", async () => {
const cli = stubClient();
cli.doesServerSupportUnstableFeature = jest.fn(async () => true);
const controller = new ServerSupportUnstableFeatureController(
setting,
watchers,
[["feature"]],
"other_value",
);
await prepareSetting(cli, controller);
expect(controller.getValueOverride(SettingLevel.DEVICE, null, true, SettingLevel.ACCOUNT)).toEqual(null);
});
});
describe("settingDisabled()", () => {
it("considered disabled if there is no matrix client", () => {
const controller = new ServerSupportUnstableFeatureController(setting, watchers, [["org.matrix.msc3030"]]);
expect(controller.settingDisabled).toEqual(true);
});
it("considered disabled if not all required features in the only feature group are supported", async () => {
const cli = stubClient();
cli.doesServerSupportUnstableFeature = jest.fn(async (featureName) => {
return featureName === "org.matrix.msc3827.stable";
});
const controller = new ServerSupportUnstableFeatureController(setting, watchers, [
["org.matrix.msc3827.stable", "org.matrix.msc3030"],
]);
await prepareSetting(cli, controller);
expect(controller.settingDisabled).toEqual(true);
});
it("considered enabled if all required features in the only feature group are supported", async () => {
const cli = stubClient();
cli.doesServerSupportUnstableFeature = jest.fn(async (featureName) => {
return featureName === "org.matrix.msc3827.stable" || featureName === "org.matrix.msc3030";
});
const controller = new ServerSupportUnstableFeatureController(setting, watchers, [
["org.matrix.msc3827.stable", "org.matrix.msc3030"],
]);
await prepareSetting(cli, controller);
expect(controller.settingDisabled).toEqual(false);
});
it("considered enabled if all required features in one of the feature groups are supported", async () => {
const cli = stubClient();
cli.doesServerSupportUnstableFeature = jest.fn(async (featureName) => {
return featureName === "org.matrix.msc3827.stable" || featureName === "org.matrix.msc3030";
});
const controller = new ServerSupportUnstableFeatureController(setting, watchers, [
["foo-unsupported", "bar-unsupported"],
["org.matrix.msc3827.stable", "org.matrix.msc3030"],
]);
await prepareSetting(cli, controller);
expect(controller.settingDisabled).toEqual(false);
});
it("considered disabled if not all required features in one of the feature groups are supported", async () => {
const cli = stubClient();
cli.doesServerSupportUnstableFeature = jest.fn(async (featureName) => {
return featureName === "org.matrix.msc3827.stable";
});
const controller = new ServerSupportUnstableFeatureController(setting, watchers, [
["foo-unsupported", "bar-unsupported"],
["org.matrix.msc3827.stable", "org.matrix.msc3030"],
]);
await prepareSetting(cli, controller);
expect(controller.settingDisabled).toEqual(true);
});
});
});
@@ -1,36 +0,0 @@
/*
Copyright 2024 New Vector Ltd.
Copyright 2022 The Matrix.org Foundation C.I.C.
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 { Action } from "../../../../src/dispatcher/actions";
import dis from "../../../../src/dispatcher/dispatcher";
import SystemFontController from "../../../../src/settings/controllers/SystemFontController";
import SettingsStore from "../../../../src/settings/SettingsStore";
const dispatchSpy = jest.spyOn(dis, "dispatch");
describe("SystemFontController", () => {
it("dispatches a system font update action on change", () => {
const controller = new SystemFontController();
const getValueSpy = jest.spyOn(SettingsStore, "getValue").mockImplementation((settingName): any => {
if (settingName === "useBundledEmojiFont") return false;
if (settingName === "useSystemFont") return true;
if (settingName === "systemFont") return "Comic Sans MS";
});
controller.onChange();
expect(dispatchSpy).toHaveBeenCalledWith({
action: Action.UpdateSystemFont,
useBundledEmojiFont: false,
useSystemFont: true,
font: "Comic Sans MS",
});
expect(getValueSpy).toHaveBeenCalledWith("useSystemFont");
});
});