Update tests to use SettingsStore where possible.

This commit is contained in:
Half-Shot
2025-04-14 12:24:11 +01:00
parent 4a11e0e454
commit 2c95cf118a
6 changed files with 55 additions and 48 deletions

View File

@@ -7,7 +7,6 @@ Please see LICENSE files in the repository root for full details.
*/
import React from "react";
import { mocked } from "jest-mock";
import { render, screen } from "jest-matrix-react";
import parse from "html-react-parser";
@@ -15,16 +14,13 @@ import { bodyToHtml, bodyToNode, formatEmojis, topicToHtml } from "../../src/Htm
import SettingsStore from "../../src/settings/SettingsStore";
import { getMockClientWithEventEmitter } from "../test-utils";
import { SettingLevel } from "../../src/settings/SettingLevel";
jest.mock("../../src/settings/SettingsStore");
const enableHtmlTopicFeature = () => {
mocked(SettingsStore).getValue.mockImplementation((arg): any => {
return arg === "feature_html_topic";
});
};
import SdkConfig from "../../src/SdkConfig";
describe("topicToHtml", () => {
afterEach(() => {
SettingsStore.reset();
});
function getContent() {
return screen.getByRole("contentinfo").children[0].innerHTML;
}
@@ -40,19 +36,19 @@ describe("topicToHtml", () => {
});
it("converts literal HTML topic to HTML", async () => {
enableHtmlTopicFeature();
SettingsStore.setValue("feature_html_topic", null, SettingLevel.DEVICE, true);
render(<div role="contentinfo">{topicToHtml("<b>pizza</b>", undefined, null, false)}</div>);
expect(getContent()).toEqual("&lt;b&gt;pizza&lt;/b&gt;");
});
it("converts true HTML topic to HTML", async () => {
enableHtmlTopicFeature();
SettingsStore.setValue("feature_html_topic", null, SettingLevel.DEVICE, true);
render(<div role="contentinfo">{topicToHtml("**pizza**", "<b>pizza</b>", null, false)}</div>);
expect(getContent()).toEqual("<b>pizza</b>");
});
it("converts true HTML topic with emoji to HTML", async () => {
enableHtmlTopicFeature();
SettingsStore.setValue("feature_html_topic", null, SettingLevel.DEVICE, true);
render(<div role="contentinfo">{topicToHtml("**pizza** 🍕", "<b>pizza</b> 🍕", null, false)}</div>);
expect(getContent()).toEqual('<b>pizza</b> <span class="mx_Emoji" title=":pizza:">🍕</span>');
});
@@ -109,11 +105,12 @@ describe("bodyToHtml", () => {
describe("feature_latex_maths", () => {
beforeEach(() => {
SettingsStore.setValue("feature_latex_maths", null, SettingLevel.ACCOUNT, true);
SettingsStore.setValue("feature_latex_maths", null, SettingLevel.DEVICE, true);
});
afterEach(() => {
SettingsStore.reset();
SdkConfig.reset();
});
it("should render inline katex", () => {

View File

@@ -486,7 +486,7 @@ describe("<MatrixChat />", () => {
afterEach(() => {
SettingsStore.reset();
})
});
it("should persist login credentials", async () => {
getComponent({ realQueryParams });

View File

@@ -313,8 +313,7 @@ describe("TimelinePanel", () => {
describe("and sending receipts is disabled", () => {
beforeEach(async () => {
// Ensure this setting is supported, otherwise it will use the default value.
client.isVersionSupported.mockResolvedValue(true);
client.doesServerSupportUnstableFeature.mockResolvedValue(true);
client.isVersionSupported.mockImplementation(async (v) => v === "v1.4");
MatrixClientBackedController.matrixClient = client;
SettingsStore.setValue("sendReadReceipts", null, SettingLevel.DEVICE, false);
});

View File

@@ -360,7 +360,7 @@ describe("RoomHeader", () => {
SdkConfig.put({
features: {
feature_group_calls: true,
}
},
});
});
@@ -422,7 +422,7 @@ describe("RoomHeader", () => {
},
features: {
feature_group_calls: false,
}
},
});
// allow calls
jest.spyOn(room.currentState, "mayClientSendStateEvent").mockReturnValue(true);
@@ -475,9 +475,9 @@ describe("RoomHeader", () => {
jest.spyOn(room, "canInvite").mockReturnValue(false);
SdkConfig.add({
element_call: {
guest_spa_url: "https://guest_spa_url.com"
}
})
guest_spa_url: "https://guest_spa_url.com",
},
});
const { container: containerNoInviteNotPublicCanUpgradeAccess } = render(
<RoomHeader room={room} />,
getWrapper(),
@@ -696,7 +696,7 @@ describe("RoomHeader", () => {
afterEach(() => {
SdkConfig.reset();
})
});
it.each([
[ShieldUtils.E2EStatus.Verified, "Verified"],
@@ -712,8 +712,8 @@ describe("RoomHeader", () => {
it("does not show the face pile for DMs", () => {
SdkConfig.put({
features: {
feature_notifications: false
}
feature_notifications: false,
},
});
const { asFragment } = render(<RoomHeader room={room} />, getWrapper());

View File

@@ -1187,7 +1187,9 @@ exports[`PreferencesUserSettingsTab should render 1`] = `
</div>
</div>
<div
aria-label="Show media in timeline"
class="_field_19upo_26"
id="mx_media_previews"
role="radiogroup"
>
<label
@@ -1213,6 +1215,7 @@ exports[`PreferencesUserSettingsTab should render 1`] = `
>
<input
class="_input_1e0uz_18"
id="mx_media_previews_off"
type="radio"
/>
<div
@@ -1225,7 +1228,7 @@ exports[`PreferencesUserSettingsTab should render 1`] = `
>
<label
class="_label_19upo_59"
for="radix-:rb:"
for="mx_media_previews_off"
>
Always hide
</label>
@@ -1242,6 +1245,7 @@ exports[`PreferencesUserSettingsTab should render 1`] = `
>
<input
class="_input_1e0uz_18"
id="mx_media_previews_private"
type="radio"
/>
<div
@@ -1254,7 +1258,7 @@ exports[`PreferencesUserSettingsTab should render 1`] = `
>
<label
class="_label_19upo_59"
for="radix-:rc:"
for="mx_media_previews_private"
>
In private rooms
</label>
@@ -1272,6 +1276,7 @@ exports[`PreferencesUserSettingsTab should render 1`] = `
<input
checked=""
class="_input_1e0uz_18"
id="mx_media_previews_on"
type="radio"
/>
<div
@@ -1284,7 +1289,7 @@ exports[`PreferencesUserSettingsTab should render 1`] = `
>
<label
class="_label_19upo_59"
for="radix-:rd:"
for="mx_media_previews_on"
>
Always show
</label>

View File

@@ -28,14 +28,13 @@ import {
getMarkedUnreadState,
setMarkedUnreadState,
} from "../../../src/utils/notifications";
import SettingsStore from "../../../src/settings/SettingsStore";
import { getMockClientWithEventEmitter } from "../../test-utils/client";
import { getMockClientWithEventEmitter, mockClientMethodsServer } from "../../test-utils/client";
import { mkMessage, stubClient } from "../../test-utils/test-utils";
import { MatrixClientPeg } from "../../../src/MatrixClientPeg";
import { NotificationLevel } from "../../../src/stores/notifications/NotificationLevel";
import { SettingLevel } from "../../../src/settings/SettingLevel";
jest.mock("../../../src/settings/SettingsStore");
import MatrixClientBackedController from "../../../src/settings/controllers/MatrixClientBackedController";
import SettingsStore from "../../../src/settings/SettingsStore";
describe("notifications", () => {
let accountDataStore: Record<string, MatrixEvent> = {};
@@ -45,6 +44,7 @@ describe("notifications", () => {
beforeEach(() => {
jest.clearAllMocks();
mockClient = getMockClientWithEventEmitter({
...mockClientMethodsServer(),
isGuest: jest.fn().mockReturnValue(false),
getAccountData: jest.fn().mockImplementation((eventType) => accountDataStore[eventType]),
setAccountData: jest.fn().mockImplementation((eventType, content) => {
@@ -53,10 +53,20 @@ describe("notifications", () => {
content,
});
}),
isVersionSupported: jest.fn().mockImplementation(async (v) => v === "v1.4"),
});
// Ensure unstable settings are supported, otherwise it will use the default value.
MatrixClientBackedController.matrixClient = mockClient;
accountDataStore = {};
accountDataEventKey = getLocalNotificationAccountDataEventType(mockClient.deviceId!);
mocked(SettingsStore).getValue.mockReturnValue(false);
// Disable all notifications
deviceNotificationSettingsKeys.forEach((k) => SettingsStore.setValue(k, null, SettingLevel.DEVICE, false));
});
afterEach(() => {
jest.restoreAllMocks();
SettingsStore.reset();
});
describe("createLocalNotification", () => {
@@ -76,10 +86,15 @@ describe("notifications", () => {
it.each(deviceNotificationSettingsKeys)(
"unsilenced for existing sessions when %s setting is truthy",
async (settingKey) => {
mocked(SettingsStore).getValue.mockImplementation((key): any => {
return key === settingKey;
// We need to spy `getValue` because setting these keys requires mocking
// the platform to support notifications, which is out of scope for this test.
const origFn = SettingsStore.getValue;
jest.spyOn(SettingsStore, "getValue").mockImplementation((name, ...args) => {
if (name === settingKey) {
return true;
}
return origFn(name, ...args);
});
await createLocalNotificationSettingsIfNeeded(mockClient);
const event = mockClient.getAccountData(accountDataEventKey);
expect(event?.getContent().is_silenced).toBe(false);
@@ -117,11 +132,6 @@ describe("notifications", () => {
const ROOM_ID = "123";
const USER_ID = "@bob:example.org";
let message: MatrixEvent;
const sendReceiptsSetting = true;
afterEach(() => {
SettingsStore.reset();
});
beforeEach(() => {
stubClient();
@@ -136,7 +146,7 @@ describe("notifications", () => {
room.addLiveEvents([message], { addToState: true });
sendReadReceiptSpy = jest.spyOn(client, "sendReadReceipt").mockResolvedValue({});
jest.spyOn(client, "getRooms").mockReturnValue([room]);
SettingsStore.setValue("sendReadReceipts", null, SettingLevel.ACCOUNT, sendReceiptsSetting);
SettingsStore.setValue("sendReadReceipts", null, SettingLevel.DEVICE, true);
});
it("sends a request even if everything has been read", async () => {
@@ -156,7 +166,7 @@ describe("notifications", () => {
describe("when sendReadReceipts setting is disabled", () => {
it("should send a private read receipt", async () => {
SettingsStore.setValue("sendReadReceipts", null, SettingLevel.ACCOUNT, sendReceiptsSetting);
SettingsStore.setValue("sendReadReceipts", null, SettingLevel.DEVICE, false);
await clearRoomNotification(room, client);
expect(sendReadReceiptSpy).toHaveBeenCalledWith(message, ReceiptType.ReadPrivate, true);
});
@@ -177,11 +187,7 @@ describe("notifications", () => {
room = new Room(ROOM_ID, client, USER_ID);
sendReadReceiptSpy = jest.spyOn(client, "sendReadReceipt").mockResolvedValue({});
jest.spyOn(client, "getRooms").mockReturnValue([room]);
SettingsStore.setValue("sendReadReceipts", null, SettingLevel.ACCOUNT, true);
});
afterEach(() => {
SettingsStore.reset();
SettingsStore.setValue("sendReadReceipts", null, SettingLevel.DEVICE, true);
});
it("does not send any requests if everything has been read", () => {
@@ -214,7 +220,7 @@ describe("notifications", () => {
room.addLiveEvents([message], { addToState: true });
room.setUnreadNotificationCount(NotificationCountType.Total, 1);
SettingsStore.setValue("sendReadReceipts", null, SettingLevel.ACCOUNT, false);
SettingsStore.setValue("sendReadReceipts", null, SettingLevel.DEVICE, false);
await clearAllNotifications(client);