Skip to content

Commit

Permalink
Fix an existing test for editing messages in threads (#3407)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
andybalaam committed May 25, 2023
1 parent d1bfdca commit 056aae8
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions spec/unit/event-timeline-set.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [],
});

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");
});
// 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({});

// Create a thread and wait for it to be initialised
const thread = room.createThread(root.getId()!, root, [], false);
await new Promise<void>((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");
});
});

Expand Down

0 comments on commit 056aae8

Please sign in to comment.