|
| 1 | +/* |
| 2 | +Copyright 2022 The Matrix.org Foundation C.I.C. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import React from "react"; |
| 18 | +import { fireEvent, render, RenderResult, waitFor } from "@testing-library/react"; |
| 19 | +import "@testing-library/jest-dom"; |
| 20 | + |
| 21 | +import PreferencesUserSettingsTab from |
| 22 | + "../../../../../../src/components/views/settings/tabs/user/PreferencesUserSettingsTab"; |
| 23 | +import { MatrixClientPeg } from "../../../../../../src/MatrixClientPeg"; |
| 24 | +import { mockPlatformPeg, stubClient } from "../../../../../test-utils"; |
| 25 | +import SettingsStore from "../../../../../../src/settings/SettingsStore"; |
| 26 | +import { SettingLevel } from "../../../../../../src/settings/SettingLevel"; |
| 27 | + |
| 28 | +describe("PreferencesUserSettingsTab", () => { |
| 29 | + beforeEach(() => { |
| 30 | + mockPlatformPeg(); |
| 31 | + }); |
| 32 | + |
| 33 | + const renderTab = (): RenderResult => { |
| 34 | + return render(<PreferencesUserSettingsTab closeSettingsFn={() => {}} />); |
| 35 | + }; |
| 36 | + |
| 37 | + describe("send read receipts", () => { |
| 38 | + beforeEach(() => { |
| 39 | + stubClient(); |
| 40 | + jest.spyOn(SettingsStore, "setValue"); |
| 41 | + jest.spyOn(window, "matchMedia").mockReturnValue({ matches: false } as MediaQueryList); |
| 42 | + }); |
| 43 | + |
| 44 | + afterEach(() => { |
| 45 | + jest.resetAllMocks(); |
| 46 | + }); |
| 47 | + |
| 48 | + const getToggle = () => renderTab().getByRole("switch", { name: "Send read receipts" }); |
| 49 | + |
| 50 | + const mockIsVersionSupported = (val: boolean) => { |
| 51 | + const client = MatrixClientPeg.get(); |
| 52 | + jest.spyOn(client, "isVersionSupported").mockImplementation(async (version: string) => { |
| 53 | + if (version === "v1.4") return val; |
| 54 | + }); |
| 55 | + }; |
| 56 | + |
| 57 | + const mockGetValue = (val: boolean) => { |
| 58 | + const copyOfGetValueAt = SettingsStore.getValueAt; |
| 59 | + |
| 60 | + SettingsStore.getValueAt = (level: SettingLevel, name: string, roomId?: string, isExplicit?: boolean) => { |
| 61 | + if (name === "sendReadReceipts") return val; |
| 62 | + return copyOfGetValueAt(level, name, roomId, isExplicit); |
| 63 | + }; |
| 64 | + }; |
| 65 | + |
| 66 | + const expectSetValueToHaveBeenCalled = ( |
| 67 | + name: string, |
| 68 | + roomId: string, |
| 69 | + level: SettingLevel, |
| 70 | + value: boolean, |
| 71 | + ) => expect(SettingsStore.setValue).toHaveBeenCalledWith(name, roomId, level, value); |
| 72 | + |
| 73 | + describe("with server support", () => { |
| 74 | + beforeEach(() => { |
| 75 | + mockIsVersionSupported(true); |
| 76 | + }); |
| 77 | + |
| 78 | + it("can be enabled", async () => { |
| 79 | + mockGetValue(false); |
| 80 | + const toggle = getToggle(); |
| 81 | + |
| 82 | + await waitFor(() => expect(toggle).toHaveAttribute("aria-disabled", "false")); |
| 83 | + fireEvent.click(toggle); |
| 84 | + expectSetValueToHaveBeenCalled("sendReadReceipts", undefined, SettingLevel.ACCOUNT, true); |
| 85 | + }); |
| 86 | + |
| 87 | + it("can be disabled", async () => { |
| 88 | + mockGetValue(true); |
| 89 | + const toggle = getToggle(); |
| 90 | + |
| 91 | + await waitFor(() => expect(toggle).toHaveAttribute("aria-disabled", "false")); |
| 92 | + fireEvent.click(toggle); |
| 93 | + expectSetValueToHaveBeenCalled("sendReadReceipts", undefined, SettingLevel.ACCOUNT, false); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + describe("without server support", () => { |
| 98 | + beforeEach(() => { |
| 99 | + mockIsVersionSupported(false); |
| 100 | + }); |
| 101 | + |
| 102 | + it("can be enabled", async () => { |
| 103 | + mockGetValue(false); |
| 104 | + const toggle = getToggle(); |
| 105 | + |
| 106 | + await waitFor(() => expect(toggle).toHaveAttribute("aria-disabled", "false")); |
| 107 | + fireEvent.click(toggle); |
| 108 | + expectSetValueToHaveBeenCalled("sendReadReceipts", undefined, SettingLevel.ACCOUNT, true); |
| 109 | + }); |
| 110 | + |
| 111 | + it("cannot be disabled", async () => { |
| 112 | + mockGetValue(true); |
| 113 | + const toggle = getToggle(); |
| 114 | + |
| 115 | + await waitFor(() => expect(toggle).toHaveAttribute("aria-disabled", "true")); |
| 116 | + fireEvent.click(toggle); |
| 117 | + expect(SettingsStore.setValue).not.toHaveBeenCalled(); |
| 118 | + }); |
| 119 | + }); |
| 120 | + }); |
| 121 | +}); |
0 commit comments