Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 6b1ee13

Browse files
Always allow enabling sending read receipts (#9367)
Co-authored-by: Michael Telatynski <7t3chguy@gmail.com>
1 parent 1345825 commit 6b1ee13

File tree

3 files changed

+126
-1
lines changed

3 files changed

+126
-1
lines changed

src/components/views/settings/tabs/user/PreferencesUserSettingsTab.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,10 @@ export default class PreferencesUserSettingsTab extends React.Component<IProps,
214214
{ _t("Share your activity and status with others.") }
215215
</span>
216216
<SettingsFlag
217-
disabled={!this.state.disablingReadReceiptsSupported}
217+
disabled={
218+
!this.state.disablingReadReceiptsSupported
219+
&& SettingsStore.getValue("sendReadReceipts") // Make sure the feature can always be enabled
220+
}
218221
disabledDescription={_t("Your server doesn't support disabling sending read receipts.")}
219222
name="sendReadReceipts"
220223
level={SettingLevel.ACCOUNT}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
});

test/test-utils/test-utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ export function createTestClient(): MatrixClient {
160160
getIdentityAccount: jest.fn().mockResolvedValue({}),
161161
getTerms: jest.fn().mockResolvedValueOnce(undefined),
162162
doesServerSupportUnstableFeature: jest.fn().mockResolvedValue(undefined),
163+
isVersionSupported: jest.fn().mockResolvedValue(undefined),
163164
getPushRules: jest.fn().mockResolvedValue(undefined),
164165
getPushers: jest.fn().mockResolvedValue({ pushers: [] }),
165166
getThreePids: jest.fn().mockResolvedValue({ threepids: [] }),

0 commit comments

Comments
 (0)