Migrate more jest tests to vitest (#33898)
* Migrate more jest tests to vitest * Fix jest config * Fix jest config * Make remaining jest tests type-happy
This commit is contained in:
@@ -1,167 +0,0 @@
|
||||
/*
|
||||
Copyright 2024, 2025 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 { ClientEvent, type MatrixClient, type Room, SyncState } from "matrix-js-sdk/src/matrix";
|
||||
import { waitFor } from "jest-matrix-react";
|
||||
|
||||
import type BasePlatform from "../../../src/BasePlatform";
|
||||
import SdkConfig from "../../../src/SdkConfig";
|
||||
import { SettingLevel } from "../../../src/settings/SettingLevel";
|
||||
import SettingsStore from "../../../src/settings/SettingsStore";
|
||||
import { mkStubRoom, mockPlatformPeg, stubClient } from "../../test-utils";
|
||||
import { SETTINGS, type SettingKey } from "../../../src/settings/Settings.tsx";
|
||||
import MatrixClientBackedController from "../../../src/settings/controllers/MatrixClientBackedController.ts";
|
||||
|
||||
const TEST_DATA = [
|
||||
{
|
||||
name: "Electron.showTrayIcon" as SettingKey,
|
||||
level: SettingLevel.PLATFORM,
|
||||
value: true,
|
||||
},
|
||||
];
|
||||
|
||||
/**
|
||||
* An existing setting that has {@link IBaseSetting#supportedLevelsAreOrdered} set to true.
|
||||
*/
|
||||
const SETTING_NAME_WITH_CONFIG_OVERRIDE = "feature_msc3531_hide_messages_pending_moderation";
|
||||
|
||||
describe("SettingsStore", () => {
|
||||
let platformSettings: Record<string, any>;
|
||||
|
||||
beforeAll(() => {
|
||||
jest.clearAllMocks();
|
||||
platformSettings = {};
|
||||
mockPlatformPeg({
|
||||
isLevelSupported: jest.fn().mockReturnValue(true),
|
||||
supportsSetting: jest.fn().mockReturnValue(true),
|
||||
setSettingValue: jest.fn().mockImplementation((settingName: string, value: any) => {
|
||||
platformSettings[settingName] = value;
|
||||
}),
|
||||
getSettingValue: jest.fn().mockImplementation((settingName: string) => {
|
||||
return platformSettings[settingName];
|
||||
}),
|
||||
reload: jest.fn(),
|
||||
} as unknown as BasePlatform);
|
||||
|
||||
TEST_DATA.forEach((d) => {
|
||||
SettingsStore.setValue(d.name, null, d.level, d.value);
|
||||
});
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
SdkConfig.reset();
|
||||
SettingsStore.reset();
|
||||
});
|
||||
|
||||
describe("getValueAt", () => {
|
||||
TEST_DATA.forEach((d) => {
|
||||
it(`should return the value "${d.level}"."${d.name}"`, () => {
|
||||
expect(SettingsStore.getValueAt(d.level, d.name)).toBe(d.value);
|
||||
// regression test #22545
|
||||
expect(SettingsStore.getValueAt(d.level, d.name)).toBe(d.value);
|
||||
});
|
||||
});
|
||||
|
||||
it(`supportedLevelsAreOrdered correctly overrides setting`, async () => {
|
||||
SdkConfig.put({
|
||||
features: {
|
||||
[SETTING_NAME_WITH_CONFIG_OVERRIDE]: false,
|
||||
},
|
||||
});
|
||||
await SettingsStore.setValue(SETTING_NAME_WITH_CONFIG_OVERRIDE, null, SettingLevel.DEVICE, true);
|
||||
expect(SettingsStore.getValue(SETTING_NAME_WITH_CONFIG_OVERRIDE)).toBe(false);
|
||||
});
|
||||
|
||||
it(`supportedLevelsAreOrdered doesn't incorrectly override setting`, async () => {
|
||||
await SettingsStore.setValue(SETTING_NAME_WITH_CONFIG_OVERRIDE, null, SettingLevel.DEVICE, true);
|
||||
expect(SettingsStore.getValueAt(SettingLevel.DEVICE, SETTING_NAME_WITH_CONFIG_OVERRIDE)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("exportForRageshake", () => {
|
||||
it("should not export settings marked as non-exportable", async () => {
|
||||
await SettingsStore.setValue("userTimezone", null, SettingLevel.DEVICE, "Europe/London");
|
||||
const values = JSON.parse(SettingsStore.exportForRageshake()) as Record<SettingKey, unknown>;
|
||||
for (const exportedKey of Object.keys(values) as SettingKey[]) {
|
||||
expect(SETTINGS[exportedKey].shouldExportToRageshake).not.toEqual(false);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("runMigrations", () => {
|
||||
let client: MatrixClient;
|
||||
let room: Room;
|
||||
|
||||
beforeEach(() => {
|
||||
client = stubClient();
|
||||
room = mkStubRoom("!room:example.org", "Room", client);
|
||||
client.getRooms = jest.fn().mockReturnValue([room]);
|
||||
client.getRoom = jest.fn().mockReturnValue(room);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.restoreAllMocks();
|
||||
});
|
||||
|
||||
describe("Migrate media preview configuration", () => {
|
||||
beforeEach(() => {
|
||||
MatrixClientBackedController.matrixClient = client;
|
||||
client.getAccountData = jest.fn().mockImplementation((type) => {
|
||||
if (type === "im.vector.web.settings") {
|
||||
return {
|
||||
getContent: jest.fn().mockReturnValue({
|
||||
showImages: false,
|
||||
showAvatarsOnInvites: false,
|
||||
}),
|
||||
};
|
||||
} else {
|
||||
return undefined;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it("migrates media preview configuration immediately", async () => {
|
||||
client.setAccountData = jest.fn();
|
||||
SettingsStore.runMigrations(false);
|
||||
expect(client.setAccountData).toHaveBeenCalledWith("io.element.msc4278.media_preview_config", {
|
||||
invite_avatars: "off",
|
||||
media_previews: "off",
|
||||
});
|
||||
});
|
||||
it("migrates media preview configuration once client is ready", async () => {
|
||||
client.setAccountData = jest.fn();
|
||||
const mockInitialSync = (client.isInitialSyncComplete = jest.fn().mockReturnValue(false));
|
||||
SettingsStore.runMigrations(false);
|
||||
mockInitialSync.mockReturnValue(true);
|
||||
client.emit(ClientEvent.Sync, SyncState.Prepared, null);
|
||||
// Update is asynchronous
|
||||
waitFor(() => {
|
||||
expect(client.setAccountData).toHaveBeenCalledWith("io.element.msc4278.media_preview_config", {
|
||||
invite_avatars: "off",
|
||||
media_previews: "off",
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it("does not migrate media preview configuration if the session is fresh", async () => {
|
||||
client.setAccountData = jest.fn();
|
||||
SettingsStore.runMigrations(true);
|
||||
client.emit(ClientEvent.Sync, SyncState.Prepared, null);
|
||||
expect(client.setAccountData).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("does not migrate media preview configuration if the account data is already set", async () => {
|
||||
client.setAccountData = jest.fn();
|
||||
client.getAccountData = jest.fn().mockReturnValue({});
|
||||
SettingsStore.runMigrations(false);
|
||||
client.emit(ClientEvent.Sync, SyncState.Prepared, null);
|
||||
expect(client.setAccountData).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,38 +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 { ImageSize, suggestedSize } from "../../../../src/settings/enums/ImageSize";
|
||||
|
||||
describe("ImageSize", () => {
|
||||
describe("suggestedSize", () => {
|
||||
it("constrains width", () => {
|
||||
const size = suggestedSize(ImageSize.Normal, { w: 648, h: 162 });
|
||||
expect(size).toStrictEqual({ w: 324, h: 81 });
|
||||
});
|
||||
it("constrains height", () => {
|
||||
const size = suggestedSize(ImageSize.Normal, { w: 162, h: 648 });
|
||||
expect(size).toStrictEqual({ w: 81, h: 324 });
|
||||
});
|
||||
it("constrains width in large mode", () => {
|
||||
const size = suggestedSize(ImageSize.Large, { w: 2400, h: 1200 });
|
||||
expect(size).toStrictEqual({ w: 800, h: 400 });
|
||||
});
|
||||
it("returns max values if content size is not specified", () => {
|
||||
const size = suggestedSize(ImageSize.Normal, {});
|
||||
expect(size).toStrictEqual({ w: 324, h: 324 });
|
||||
});
|
||||
it("returns integer values", () => {
|
||||
const size = suggestedSize(ImageSize.Normal, { w: 642, h: 350 }); // does not divide evenly
|
||||
expect(size).toStrictEqual({ w: 324, h: 176 });
|
||||
});
|
||||
it("returns integer values for portrait images", () => {
|
||||
const size = suggestedSize(ImageSize.Normal, { w: 720, h: 1280 });
|
||||
expect(size).toStrictEqual({ w: 182, h: 324 });
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,208 +0,0 @@
|
||||
/*
|
||||
Copyright 2024 New Vector Ltd.
|
||||
Copyright 2021 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 SettingsStore from "../../../../src/settings/SettingsStore";
|
||||
import ThemeWatcher from "../../../../src/settings/watchers/ThemeWatcher";
|
||||
import { type SettingLevel } from "../../../../src/settings/SettingLevel";
|
||||
import { type SettingKey, type Settings } from "../../../../src/settings/Settings.tsx";
|
||||
|
||||
function makeMatchMedia(values: any) {
|
||||
class FakeMediaQueryList {
|
||||
matches: false;
|
||||
media?: null;
|
||||
onchange?: null;
|
||||
addListener() {}
|
||||
removeListener() {}
|
||||
addEventListener() {}
|
||||
removeEventListener() {}
|
||||
dispatchEvent() {
|
||||
return true;
|
||||
}
|
||||
|
||||
constructor(query: string) {
|
||||
this.matches = values[query];
|
||||
}
|
||||
}
|
||||
|
||||
return function matchMedia(query: string) {
|
||||
return new FakeMediaQueryList(query) as unknown as MediaQueryList;
|
||||
};
|
||||
}
|
||||
|
||||
function makeGetValue(values: any): any {
|
||||
return function getValue<S extends SettingKey>(
|
||||
settingName: S,
|
||||
_roomId: string | null = null,
|
||||
_excludeDefault = false,
|
||||
): Settings[S] {
|
||||
return values[settingName];
|
||||
};
|
||||
}
|
||||
|
||||
function makeGetValueAt(values: any) {
|
||||
return function getValueAt(
|
||||
_level: SettingLevel,
|
||||
settingName: string,
|
||||
_roomId: string | null = null,
|
||||
_explicit = false,
|
||||
_excludeDefault = false,
|
||||
): any {
|
||||
return values[settingName];
|
||||
};
|
||||
}
|
||||
|
||||
describe("ThemeWatcher", function () {
|
||||
it("should choose a light theme by default", () => {
|
||||
// Given no system settings
|
||||
global.matchMedia = makeMatchMedia({});
|
||||
|
||||
// Then getEffectiveTheme returns light
|
||||
const themeWatcher = new ThemeWatcher();
|
||||
expect(themeWatcher.getEffectiveTheme()).toBe("light");
|
||||
});
|
||||
|
||||
it("should choose default theme if system settings are inconclusive", () => {
|
||||
// Given no system settings but we asked to use them
|
||||
global.matchMedia = makeMatchMedia({});
|
||||
SettingsStore.getValue = makeGetValue({
|
||||
use_system_theme: true,
|
||||
theme: "light",
|
||||
});
|
||||
|
||||
// Then getEffectiveTheme returns light
|
||||
const themeWatcher = new ThemeWatcher();
|
||||
expect(themeWatcher.getEffectiveTheme()).toBe("light");
|
||||
});
|
||||
|
||||
it("should choose a dark theme if that is selected", () => {
|
||||
// Given system says light high contrast but theme is set to dark
|
||||
global.matchMedia = makeMatchMedia({
|
||||
"(prefers-contrast: more)": true,
|
||||
"(prefers-color-scheme: light)": true,
|
||||
});
|
||||
SettingsStore.getValueAt = makeGetValueAt({ theme: "dark" });
|
||||
|
||||
// Then getEffectiveTheme returns dark
|
||||
const themeWatcher = new ThemeWatcher();
|
||||
expect(themeWatcher.getEffectiveTheme()).toBe("dark");
|
||||
});
|
||||
|
||||
it("should choose a light theme if that is selected", () => {
|
||||
// Given system settings say dark high contrast but theme set to light
|
||||
global.matchMedia = makeMatchMedia({
|
||||
"(prefers-contrast: more)": true,
|
||||
"(prefers-color-scheme: dark)": true,
|
||||
});
|
||||
SettingsStore.getValueAt = makeGetValueAt({ theme: "light" });
|
||||
|
||||
// Then getEffectiveTheme returns light
|
||||
const themeWatcher = new ThemeWatcher();
|
||||
expect(themeWatcher.getEffectiveTheme()).toBe("light");
|
||||
});
|
||||
|
||||
it("should choose a light-high-contrast theme if that is selected", () => {
|
||||
// Given system settings say dark and theme set to light-high-contrast
|
||||
global.matchMedia = makeMatchMedia({ "(prefers-color-scheme: dark)": true });
|
||||
SettingsStore.getValueAt = makeGetValueAt({ theme: "light-high-contrast" });
|
||||
|
||||
// Then getEffectiveTheme returns light-high-contrast
|
||||
const themeWatcher = new ThemeWatcher();
|
||||
expect(themeWatcher.getEffectiveTheme()).toBe("light-high-contrast");
|
||||
});
|
||||
|
||||
it("should choose a light theme if system prefers it (via default)", () => {
|
||||
// Given system prefers lightness, even though we did not
|
||||
// click "Use system theme" or choose a theme explicitly
|
||||
global.matchMedia = makeMatchMedia({ "(prefers-color-scheme: light)": true });
|
||||
SettingsStore.getValueAt = makeGetValueAt({});
|
||||
SettingsStore.getValue = makeGetValue({ use_system_theme: true });
|
||||
|
||||
// Then getEffectiveTheme returns light
|
||||
const themeWatcher = new ThemeWatcher();
|
||||
expect(themeWatcher.getEffectiveTheme()).toBe("light");
|
||||
});
|
||||
|
||||
it("should choose a dark theme if system prefers it (via default)", () => {
|
||||
// Given system prefers darkness, even though we did not
|
||||
// click "Use system theme" or choose a theme explicitly
|
||||
global.matchMedia = makeMatchMedia({ "(prefers-color-scheme: dark)": true });
|
||||
SettingsStore.getValueAt = makeGetValueAt({});
|
||||
SettingsStore.getValue = makeGetValue({ use_system_theme: true });
|
||||
|
||||
// Then getEffectiveTheme returns dark
|
||||
const themeWatcher = new ThemeWatcher();
|
||||
expect(themeWatcher.getEffectiveTheme()).toBe("dark");
|
||||
});
|
||||
|
||||
it("should choose a light theme if system prefers it (explicit)", () => {
|
||||
// Given system prefers lightness
|
||||
global.matchMedia = makeMatchMedia({ "(prefers-color-scheme: light)": true });
|
||||
SettingsStore.getValueAt = makeGetValueAt({ use_system_theme: true });
|
||||
SettingsStore.getValue = makeGetValue({ use_system_theme: true });
|
||||
|
||||
// Then getEffectiveTheme returns light
|
||||
const themeWatcher = new ThemeWatcher();
|
||||
expect(themeWatcher.getEffectiveTheme()).toBe("light");
|
||||
});
|
||||
|
||||
it("should choose a dark theme if system prefers it (explicit)", () => {
|
||||
// Given system prefers darkness
|
||||
global.matchMedia = makeMatchMedia({ "(prefers-color-scheme: dark)": true });
|
||||
SettingsStore.getValueAt = makeGetValueAt({ use_system_theme: true });
|
||||
SettingsStore.getValue = makeGetValue({ use_system_theme: true });
|
||||
|
||||
// Then getEffectiveTheme returns dark
|
||||
const themeWatcher = new ThemeWatcher();
|
||||
expect(themeWatcher.getEffectiveTheme()).toBe("dark");
|
||||
});
|
||||
|
||||
it("should choose a high-contrast theme if system prefers it", () => {
|
||||
// Given system prefers high contrast and light
|
||||
global.matchMedia = makeMatchMedia({
|
||||
"(prefers-contrast: more)": true,
|
||||
"(prefers-color-scheme: light)": true,
|
||||
});
|
||||
SettingsStore.getValueAt = makeGetValueAt({ use_system_theme: true });
|
||||
SettingsStore.getValue = makeGetValue({ use_system_theme: true });
|
||||
|
||||
// Then getEffectiveTheme returns light-high-contrast
|
||||
const themeWatcher = new ThemeWatcher();
|
||||
expect(themeWatcher.getEffectiveTheme()).toBe("light-high-contrast");
|
||||
});
|
||||
|
||||
it("should not choose a high-contrast theme if not available", () => {
|
||||
// Given system prefers high contrast and dark, but we don't (yet)
|
||||
// have a high-contrast dark theme
|
||||
global.matchMedia = makeMatchMedia({
|
||||
"(prefers-contrast: more)": true,
|
||||
"(prefers-color-scheme: dark)": true,
|
||||
});
|
||||
SettingsStore.getValueAt = makeGetValueAt({ use_system_theme: true });
|
||||
SettingsStore.getValue = makeGetValue({ use_system_theme: true });
|
||||
|
||||
// Then getEffectiveTheme returns dark
|
||||
const themeWatcher = new ThemeWatcher();
|
||||
expect(themeWatcher.getEffectiveTheme()).toBe("dark");
|
||||
});
|
||||
|
||||
it("should identify custom dark themes as dark", () => {
|
||||
SettingsStore.getValueAt = makeGetValueAt({ use_system_theme: false, theme: "custom-darkula" });
|
||||
SettingsStore.getValue = makeGetValue({
|
||||
custom_themes: [
|
||||
{
|
||||
name: "darkula",
|
||||
is_dark: true,
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const themeWatcher = new ThemeWatcher();
|
||||
expect(themeWatcher.getEffectiveTheme()).toBe("custom-darkula");
|
||||
expect(themeWatcher.isUserOnDarkTheme()).toBe(true);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user