|
| 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 { mount, ReactWrapper } from "enzyme"; |
| 19 | +import { MessageEvent } from 'matrix-events-sdk'; |
| 20 | +import { EventTimelineSet, MatrixEvent, PendingEventOrdering, Room } from 'matrix-js-sdk/src/matrix'; |
| 21 | + |
| 22 | +import { stubClient } from "../../test-utils"; |
| 23 | +import TimelinePanel from '../../../src/components/structures/TimelinePanel'; |
| 24 | +import { MatrixClientPeg } from '../../../src/MatrixClientPeg'; |
| 25 | + |
| 26 | +function newReceipt(eventId: string, userId: string, readTs: number, fullyReadTs: number): MatrixEvent { |
| 27 | + const receiptContent = { |
| 28 | + [eventId]: { |
| 29 | + "m.read": { [userId]: { ts: readTs } }, |
| 30 | + "org.matrix.msc2285.read.private": { [userId]: { ts: readTs } }, |
| 31 | + "m.fully_read": { [userId]: { ts: fullyReadTs } }, |
| 32 | + }, |
| 33 | + }; |
| 34 | + return new MatrixEvent({ content: receiptContent, type: "m.receipt" }); |
| 35 | +} |
| 36 | + |
| 37 | +describe('TimelinePanel', () => { |
| 38 | + describe('Read Receipts and Markers', () => { |
| 39 | + it('Forgets the read marker when asked to', () => { |
| 40 | + stubClient(); |
| 41 | + const cli = MatrixClientPeg.get(); |
| 42 | + const readMarkersSent = []; |
| 43 | + |
| 44 | + // Track calls to setRoomReadMarkers |
| 45 | + cli.setRoomReadMarkers = (_roomId, rmEventId, _a, _b) => { |
| 46 | + readMarkersSent.push(rmEventId); |
| 47 | + return Promise.resolve({}); |
| 48 | + }; |
| 49 | + |
| 50 | + const ev0 = new MatrixEvent({ |
| 51 | + event_id: "ev0", |
| 52 | + sender: "@u2:m.org", |
| 53 | + origin_server_ts: 111, |
| 54 | + ...MessageEvent.from("hello 1").serialize(), |
| 55 | + }); |
| 56 | + const ev1 = new MatrixEvent({ |
| 57 | + event_id: "ev1", |
| 58 | + sender: "@u2:m.org", |
| 59 | + origin_server_ts: 222, |
| 60 | + ...MessageEvent.from("hello 2").serialize(), |
| 61 | + }); |
| 62 | + |
| 63 | + const roomId = "#room:example.com"; |
| 64 | + const userId = cli.credentials.userId; |
| 65 | + const room = new Room( |
| 66 | + roomId, |
| 67 | + cli, |
| 68 | + userId, |
| 69 | + { pendingEventOrdering: PendingEventOrdering.Detached }, |
| 70 | + ); |
| 71 | + |
| 72 | + // Create a TimelinePanel with ev0 already present |
| 73 | + const timelineSet = new EventTimelineSet(room, {}); |
| 74 | + timelineSet.addLiveEvent(ev0); |
| 75 | + const component: ReactWrapper<TimelinePanel> = mount(<TimelinePanel |
| 76 | + timelineSet={timelineSet} |
| 77 | + manageReadMarkers={true} |
| 78 | + manageReadReceipts={true} |
| 79 | + eventId={ev0.getId()} |
| 80 | + />); |
| 81 | + const timelinePanel = component.instance() as TimelinePanel; |
| 82 | + |
| 83 | + // An event arrived, and we read it |
| 84 | + timelineSet.addLiveEvent(ev1); |
| 85 | + room.addEphemeralEvents([newReceipt("ev1", userId, 222, 220)]); |
| 86 | + |
| 87 | + // Sanity: We have not sent any read marker yet |
| 88 | + expect(readMarkersSent).toEqual([]); |
| 89 | + |
| 90 | + // This is what we are testing: forget the read marker - this should |
| 91 | + // update our read marker to match the latest receipt we sent |
| 92 | + timelinePanel.forgetReadMarker(); |
| 93 | + |
| 94 | + // We sent off a read marker for the new event |
| 95 | + expect(readMarkersSent).toEqual(["ev1"]); |
| 96 | + }); |
| 97 | + }); |
| 98 | +}); |
0 commit comments