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

Commit 28d6fd6

Browse files
committed
Add a test for URL previews being disabled in e2e rooms
Regression test for #8227
1 parent f63923d commit 28d6fd6

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@
152152
"@types/react": "17.0.14",
153153
"@types/react-beautiful-dnd": "^13.0.0",
154154
"@types/react-dom": "17.0.9",
155+
"@types/react-test-renderer": "^17.0.1",
155156
"@types/react-transition-group": "^4.4.0",
156157
"@types/sanitize-html": "^2.3.1",
157158
"@types/zxcvbn": "^4.4.0",
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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 TestRenderer from "react-test-renderer";
19+
import { Room, RoomEvent } from "matrix-js-sdk/src/models/room";
20+
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
21+
22+
import { MatrixClientPeg } from "../../../src/MatrixClientPeg";
23+
import { stubClient } from "../../test-utils";
24+
import { Action } from "../../../src/dispatcher/actions";
25+
import dis from "../../../src/dispatcher/dispatcher";
26+
import { ViewRoomPayload } from "../../../src/dispatcher/payloads/ViewRoomPayload";
27+
import MatrixClientContext from "../../../src/contexts/MatrixClientContext";
28+
import { RoomView } from "../../../src/components/structures/RoomView";
29+
import ResizeNotifier from "../../../src/utils/ResizeNotifier";
30+
import { RoomViewStore } from "../../../src/stores/RoomViewStore";
31+
import DMRoomMap from "../../../src/utils/DMRoomMap";
32+
33+
describe("RoomView", () => {
34+
it("updates url preview visibility on encryption state change", async () => {
35+
stubClient();
36+
const cli = MatrixClientPeg.get();
37+
cli.hasLazyLoadMembersEnabled = () => false;
38+
cli.isInitialSyncComplete = () => true;
39+
cli.stopPeeking = () => undefined;
40+
41+
const r1 = new Room("r1", cli, "@name:example.com");
42+
cli.getRoom = () => r1;
43+
r1.getPendingEvents = () => [];
44+
45+
DMRoomMap.makeShared();
46+
47+
const switchRoomPromise = new Promise<void>(resolve => {
48+
const subscription = RoomViewStore.instance.addListener(() => {
49+
if (RoomViewStore.instance.getRoomId()) {
50+
subscription.remove();
51+
resolve();
52+
}
53+
});
54+
});
55+
56+
dis.dispatch<ViewRoomPayload>({
57+
action: Action.ViewRoom,
58+
room_id: r1.roomId,
59+
metricsTrigger: null,
60+
});
61+
62+
await switchRoomPromise;
63+
64+
const renderer = TestRenderer.create(<MatrixClientContext.Provider value={cli}>
65+
<RoomView mxClient={cli}
66+
threepidInvite={null}
67+
oobData={null}
68+
resizeNotifier={new ResizeNotifier()}
69+
justCreatedOpts={null}
70+
forceTimeline={false}
71+
onRegistered={null}
72+
/>
73+
</MatrixClientContext.Provider>);
74+
75+
const roomViewInstance = renderer.root.findByType(RoomView).instance;
76+
77+
// in a default (non-encrypted room, it should start out with url previews enabled)
78+
// This is a white-box test in that we're asserting things about the state, which
79+
// is not ideal, but asserting that a URL preview just isn't there could risk the
80+
// test being invalid because the previews just hasn't rendered yet. This feels
81+
// like the safest way I think?
82+
// This also relies on the default settings being URL previews on normally and
83+
// off for e2e rooms because 1) it's probably useful to assert this and
84+
// 2) SettingsStore is a static class and so very hard to mock out.
85+
expect(roomViewInstance.state.showUrlPreview).toBe(true);
86+
87+
// now enable encryption (by mocking out the tests for whether a room is encrypted)
88+
cli.isCryptoEnabled = () => true;
89+
cli.isRoomEncrypted = () => true;
90+
91+
// and fake an encryption event into the room to prompt it to re-check
92+
// wait until the event has been added
93+
const eventAddedPromise = new Promise<void>(resolve => {
94+
r1.once(RoomEvent.Timeline, (...args) => {
95+
// we're also using mock client that doesn't re-emit, so
96+
// we emit the event to client manually
97+
cli.emit(RoomEvent.Timeline, ...args);
98+
resolve();
99+
});
100+
});
101+
102+
r1.addLiveEvents([new MatrixEvent({
103+
type: "m.room.encryption",
104+
sender: cli.getUserId(),
105+
content: {},
106+
event_id: "someid",
107+
room_id: r1.roomId,
108+
})]);
109+
110+
await eventAddedPromise;
111+
112+
// URL previews should now be disabled
113+
expect(roomViewInstance.state.showUrlPreview).toBe(false);
114+
});
115+
});

yarn.lock

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1936,6 +1936,13 @@
19361936
hoist-non-react-statics "^3.3.0"
19371937
redux "^4.0.0"
19381938

1939+
"@types/react-test-renderer@^17.0.1":
1940+
version "17.0.1"
1941+
resolved "https://registry.yarnpkg.com/@types/react-test-renderer/-/react-test-renderer-17.0.1.tgz#3120f7d1c157fba9df0118dae20cb0297ee0e06b"
1942+
integrity sha512-3Fi2O6Zzq/f3QR9dRnlnHso9bMl7weKCviFmfF6B4LS1Uat6Hkm15k0ZAQuDz+UBq6B3+g+NM6IT2nr5QgPzCw==
1943+
dependencies:
1944+
"@types/react" "*"
1945+
19391946
"@types/react-transition-group@^4.4.0":
19401947
version "4.4.4"
19411948
resolved "https://registry.yarnpkg.com/@types/react-transition-group/-/react-transition-group-4.4.4.tgz#acd4cceaa2be6b757db61ed7b432e103242d163e"

0 commit comments

Comments
 (0)