|
| 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 | +}); |
0 commit comments