From 652f2c1b6e8ddaa71030d5ad1ce41c1be63fb13c Mon Sep 17 00:00:00 2001 From: Janne Mareike Koschinski Date: Tue, 22 Nov 2022 13:36:19 +0100 Subject: [PATCH] test thread updates correctly updating timeline --- .../structures/TimelinePanel-test.tsx | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/test/components/structures/TimelinePanel-test.tsx b/test/components/structures/TimelinePanel-test.tsx index 542f0c88878c..3e6b256871e4 100644 --- a/test/components/structures/TimelinePanel-test.tsx +++ b/test/components/structures/TimelinePanel-test.tsx @@ -28,7 +28,9 @@ import { } from 'matrix-js-sdk/src/matrix'; import { ReceiptType } from "matrix-js-sdk/src/@types/read_receipts"; import { render, RenderResult } from "@testing-library/react"; +import { FeatureSupport, THREAD_RELATION_TYPE, ThreadFilterType, Thread } from "matrix-js-sdk/src/models/thread"; +import MatrixClientContext from "../../../src/contexts/MatrixClientContext"; import { mkRoom, stubClient } from "../../test-utils"; import TimelinePanel from '../../../src/components/structures/TimelinePanel'; import { MatrixClientPeg } from '../../../src/MatrixClientPeg'; @@ -172,4 +174,95 @@ describe('TimelinePanel', () => { expect(client.setRoomReadMarkers).toHaveBeenCalledWith(room.roomId, "", undefined, events[0]); }); }); + + it('updates thread previews', async () => { + const client = MatrixClientPeg.get(); + + Thread.hasServerSideSupport = FeatureSupport.Stable; + client.supportsExperimentalThreads = () => true; + const getValueCopy = SettingsStore.getValue; + SettingsStore.getValue = jest.fn().mockImplementation((name: string) => { + if (name === "feature_thread") return true; + return getValueCopy(name); + }); + + const room = new Room("roomId", client, "userId"); + const allThreads = new EventTimelineSet(room, { + pendingEvents: false, + }, undefined, undefined, ThreadFilterType.All); + const timeline = new EventTimeline(allThreads); + allThreads.getLiveTimeline = () => timeline; + allThreads.getTimelineForEvent = () => timeline; + + const reply1 = new MatrixEvent({ + room_id: room.roomId, + event_id: `event_reply_1`, + type: EventType.RoomMessage, + user_id: "userId", + content: MessageEvent.from(`ReplyEvent1`).serialize().content, + }); + + const rootEvent = new MatrixEvent({ + room_id: room.roomId, + event_id: `event_root`, + type: EventType.RoomMessage, + user_id: "userId", + content: MessageEvent.from(`RootEvent`).serialize().content, + unsigned: { + "m.relations": { + [THREAD_RELATION_TYPE.name]: { + "latest_event": reply1.event, + "count": 1, + "current_user_participated": true, + }, + }, + }, + }); + + console.log("mocking thread"); + const thread = room.createThread(rootEvent.getId(), rootEvent, [], true); + // So that we do not have to mock the thread loading + thread.initialEventsFetched = true; + // @ts-ignore + thread.fetchEditsWhereNeeded = () => Promise.resolve(); + // @ts-ignore + thread.fetchRootEvent = () => { + thread.rootEvent = rootEvent; + }; + await thread.addEvent(reply1, true); + await timeline.addEvent(thread.rootEvent, { toStartOfTimeline: true }); + + const dom = render( + + + , + ); + await dom.findByText("RootEvent"); + await dom.findByText("ReplyEvent1"); + + const reply2 = new MatrixEvent({ + room_id: room.roomId, + event_id: `event_reply_2`, + type: EventType.RoomMessage, + user_id: "userId", + content: MessageEvent.from(`ReplyEvent2`).serialize().content, + }); + + rootEvent.setUnsigned({ + "m.relations": { + [THREAD_RELATION_TYPE.name]: { + "latest_event": reply2.event, + "count": 2, + "current_user_participated": true, + }, + }, + }); + + await thread.addEvent(reply2, false, true); + await dom.findByText("ReplyEvent2"); + }); });