From 60d6e5c9360a7e61c12d6ba770d4b1973980a749 Mon Sep 17 00:00:00 2001 From: Half-Shot Date: Mon, 14 Apr 2025 14:29:01 +0100 Subject: [PATCH] Add a test for MediaPreviewAccountSettingsTab --- .../tabs/room/GeneralRoomSettingsTab.tsx | 2 +- ...ng.tsx => MediaPreviewAccountSettings.tsx} | 0 .../tabs/user/PreferencesUserSettingsTab.tsx | 2 +- .../MediaPreviewAccountSettingsTab-test.tsx | 76 +++++++++++++++++++ 4 files changed, 78 insertions(+), 2 deletions(-) rename src/components/views/settings/tabs/user/{MediaPreviewSetting.tsx => MediaPreviewAccountSettings.tsx} (100%) create mode 100644 test/unit-tests/components/views/settings/tabs/user/MediaPreviewAccountSettingsTab-test.tsx diff --git a/src/components/views/settings/tabs/room/GeneralRoomSettingsTab.tsx b/src/components/views/settings/tabs/room/GeneralRoomSettingsTab.tsx index 29978c8c70..a44f23a895 100644 --- a/src/components/views/settings/tabs/room/GeneralRoomSettingsTab.tsx +++ b/src/components/views/settings/tabs/room/GeneralRoomSettingsTab.tsx @@ -22,7 +22,7 @@ import { SettingsSubsection } from "../../shared/SettingsSubsection"; import SettingsTab from "../SettingsTab"; import { SettingsSection } from "../../shared/SettingsSection"; import { UrlPreviewSettings } from "../../../room_settings/UrlPreviewSettings"; -import { MediaPreviewAccountSettings } from "../user/MediaPreviewSetting"; +import { MediaPreviewAccountSettings } from "../user/MediaPreviewAccountSettings"; interface IProps { room: Room; diff --git a/src/components/views/settings/tabs/user/MediaPreviewSetting.tsx b/src/components/views/settings/tabs/user/MediaPreviewAccountSettings.tsx similarity index 100% rename from src/components/views/settings/tabs/user/MediaPreviewSetting.tsx rename to src/components/views/settings/tabs/user/MediaPreviewAccountSettings.tsx diff --git a/src/components/views/settings/tabs/user/PreferencesUserSettingsTab.tsx b/src/components/views/settings/tabs/user/PreferencesUserSettingsTab.tsx index ea6ef20605..d966f7810a 100644 --- a/src/components/views/settings/tabs/user/PreferencesUserSettingsTab.tsx +++ b/src/components/views/settings/tabs/user/PreferencesUserSettingsTab.tsx @@ -32,7 +32,7 @@ import SpellCheckSettings from "../../SpellCheckSettings"; import LabelledToggleSwitch from "../../../elements/LabelledToggleSwitch"; import * as TimezoneHandler from "../../../../../TimezoneHandler"; import { type BooleanSettingKey } from "../../../../../settings/Settings.tsx"; -import { MediaPreviewAccountSettings } from "./MediaPreviewSetting.tsx"; +import { MediaPreviewAccountSettings } from "./MediaPreviewAccountSettings.tsx"; interface IProps { closeSettingsFn(success: boolean): void; diff --git a/test/unit-tests/components/views/settings/tabs/user/MediaPreviewAccountSettingsTab-test.tsx b/test/unit-tests/components/views/settings/tabs/user/MediaPreviewAccountSettingsTab-test.tsx new file mode 100644 index 0000000000..2a9bc371e6 --- /dev/null +++ b/test/unit-tests/components/views/settings/tabs/user/MediaPreviewAccountSettingsTab-test.tsx @@ -0,0 +1,76 @@ +/* +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 { render } from "jest-matrix-react"; +import React from "react"; +import userEvent from "@testing-library/user-event"; + +import { MediaPreviewAccountSettings } from "../../../../../../../src/components/views/settings/tabs/user/MediaPreviewAccountSettings"; +import { + getMockClientWithEventEmitter, + mockClientMethodsServer, + mockClientMethodsUser, +} from "../../../../../../test-utils"; +import MatrixClientBackedController from "../../../../../../../src/settings/controllers/MatrixClientBackedController"; +import MatrixClientBackedSettingsHandler from "../../../../../../../src/settings/handlers/MatrixClientBackedSettingsHandler"; +import type { MockedObject } from "jest-mock"; +import type { MatrixClient } from "matrix-js-sdk/src/client"; +import { MEDIA_PREVIEW_ACCOUNT_DATA_TYPE, MediaPreviewValue } from "../../../../../../../src/@types/media_preview"; + +describe("MediaPreviewAccountSettings", () => { + let client: MockedObject; + beforeEach(() => { + client = getMockClientWithEventEmitter({ + ...mockClientMethodsServer(), + ...mockClientMethodsUser(), + getRoom: jest.fn(), + setAccountData: jest.fn(), + isVersionSupported: jest.fn().mockResolvedValue(true), + }); + MatrixClientBackedController.matrixClient = client; + MatrixClientBackedSettingsHandler.matrixClient = client; + }); + + afterEach(() => { + jest.restoreAllMocks(); + }); + + it("should render", () => { + const { getByLabelText } = render(); + // Defaults + expect(getByLabelText("Hide avatars of room and inviter")).not.toBeChecked(); + expect(getByLabelText("Always hide")).not.toBeChecked(); + expect(getByLabelText("In private rooms")).not.toBeChecked(); + expect(getByLabelText("Always show")).toBeChecked(); + }); + + it("should be able to toggle hide avatar", async () => { + const { getByLabelText } = render(); + // Defaults + const element = getByLabelText("Hide avatars of room and inviter"); + await userEvent.click(element); + expect(client.setAccountData).toHaveBeenCalledWith(MEDIA_PREVIEW_ACCOUNT_DATA_TYPE, { + invite_avatars: MediaPreviewValue.Off, + media_previews: MediaPreviewValue.On, + }); + }); + + // Skip the default. + it.each([ + ["Always hide", MediaPreviewValue.Off], + ["In private rooms", MediaPreviewValue.Private], + ])("should be able to toggle media preview %s", async (key, value) => { + const { getByLabelText } = render(); + // Defaults + const element = getByLabelText(key); + await userEvent.click(element); + expect(client.setAccountData).toHaveBeenCalledWith(MEDIA_PREVIEW_ACCOUNT_DATA_TYPE, { + invite_avatars: MediaPreviewValue.On, + media_previews: value, + }); + }); +});