From 056aae823dcf9156d445879848c65c9f16cc4442 Mon Sep 17 00:00:00 2001 From: Andy Balaam Date: Thu, 25 May 2023 13:38:53 +0100 Subject: [PATCH] Fix an existing test for editing messages in threads (#3407) While attempting to test a new change, I discovered that the test "should allow edits to be added to thread timeline" did not actually fail if its assertions failed. Further, those assertions were incorrect. So this change fixes the test to create the thread, wait for it to be initialised, and then add events to it. This simplifies the flow and ensures the test fails if something unexpected happens. --- spec/unit/event-timeline-set.spec.ts | 32 +++++++++++++++++----------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/spec/unit/event-timeline-set.spec.ts b/spec/unit/event-timeline-set.spec.ts index 6f03e7745..83844fe86 100644 --- a/spec/unit/event-timeline-set.spec.ts +++ b/spec/unit/event-timeline-set.spec.ts @@ -284,19 +284,27 @@ describe("EventTimelineSet", () => { sender, }); - jest.spyOn(client, "paginateEventTimeline").mockImplementation(async () => { - thread.timelineSet.getLiveTimeline().addEvent(threadReply, { toStartOfTimeline: true }); - return true; - }); - jest.spyOn(client, "relations").mockResolvedValue({ - events: [], - }); + // Mock methods that call out to HTTP endpoints + jest.spyOn(client, "paginateEventTimeline").mockResolvedValue(true); + jest.spyOn(client, "relations").mockResolvedValue({ events: [] }); + jest.spyOn(client, "fetchRoomEvent").mockResolvedValue({}); - const thread = room.createThread(root.getId()!, root, [threadReply, editToThreadReply], false); - thread.once(RoomEvent.TimelineReset, () => { - const lastEvent = thread.timeline.at(-1)!; - expect(lastEvent.getContent().body).toBe(" * edit"); - }); + // Create a thread and wait for it to be initialised + const thread = room.createThread(root.getId()!, root, [], false); + await new Promise((res) => thread.once(RoomEvent.TimelineReset, () => res())); + + // When a message and an edit are added to the thread + await thread.addEvent(threadReply, false); + await thread.addEvent(editToThreadReply, false); + + // Then both events end up in the timeline + const lastEvent = thread.timeline.at(-1)!; + const secondLastEvent = thread.timeline.at(-2)!; + expect(lastEvent).toBe(editToThreadReply); + expect(secondLastEvent).toBe(threadReply); + + // And the first message has been edited + expect(secondLastEvent.getContent().body).toEqual("edit"); }); });