1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-07-31 15:24:23 +03:00

Simplify thread-editing test (#3409)

* Fix an existing test for editing messages in threads

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.

* Move editing test into thread.spec.ts

* Isolate Thread global modification in beforeAll()

* Delete unneeded setUnsigned call

* Use standard message-creation methods

* Rename event variables

* Rename sender->user

* Remove unneeded variables

* Extract distractions into functions
This commit is contained in:
Andy Balaam
2023-05-25 15:03:58 +01:00
committed by GitHub
parent 7ed14dc749
commit 48b60bb885
2 changed files with 112 additions and 90 deletions

View File

@ -370,6 +370,37 @@ export function mkReaction(
); );
} }
export function mkEdit(
target: MatrixEvent,
client: MatrixClient,
userId: string,
roomId: string,
msg?: string,
ts?: number,
) {
msg = msg ?? `Edit of ${target.getId()}`;
return mkEvent(
{
event: true,
type: EventType.RoomMessage,
user: userId,
room: roomId,
content: {
"body": `* ${msg}`,
"m.new_content": {
body: msg,
},
"m.relates_to": {
rel_type: RelationType.Replace,
event_id: target.getId()!,
},
},
ts,
},
client,
);
}
/** /**
* A mock implementation of webstorage * A mock implementation of webstorage
*/ */

View File

@ -21,8 +21,8 @@ import { Room, RoomEvent } from "../../../src/models/room";
import { Thread, THREAD_RELATION_TYPE, ThreadEvent, FeatureSupport } from "../../../src/models/thread"; import { Thread, THREAD_RELATION_TYPE, ThreadEvent, FeatureSupport } from "../../../src/models/thread";
import { makeThreadEvent, mkThread } from "../../test-utils/thread"; import { makeThreadEvent, mkThread } from "../../test-utils/thread";
import { TestClient } from "../../TestClient"; import { TestClient } from "../../TestClient";
import { emitPromise, mkEvent, mkMessage, mkReaction, mock } from "../../test-utils/test-utils"; import { emitPromise, mkEdit, mkMessage, mkReaction, mock } from "../../test-utils/test-utils";
import { Direction, EventStatus, EventType, MatrixEvent, RelationType } from "../../../src"; import { Direction, EventStatus, MatrixEvent } from "../../../src";
import { ReceiptType } from "../../../src/@types/read_receipts"; import { ReceiptType } from "../../../src/@types/read_receipts";
import { getMockClientWithEventEmitter, mockClientMethodsUser } from "../../test-utils/client"; import { getMockClientWithEventEmitter, mockClientMethodsUser } from "../../test-utils/client";
import { ReEmitter } from "../../../src/ReEmitter"; import { ReEmitter } from "../../../src/ReEmitter";
@ -603,100 +603,91 @@ describe("Thread", () => {
}); });
describe("Editing events", () => { describe("Editing events", () => {
it("should allow edits to be added to thread timeline", async () => { describe("Given server support for threads", () => {
const roomId = "!foo:bar"; let previousThreadHasServerSideSupport: FeatureSupport;
const userA = "@alice:bar";
const client = mock(MatrixClient, "MatrixClient");
client.reEmitter = mock(ReEmitter, "ReEmitter");
client.canSupport = new Map();
const room = new Room(roomId, client, userA);
jest.spyOn(client, "supportsThreads").mockReturnValue(true);
jest.spyOn(client, "getEventMapper").mockReturnValue(eventMapperFor(client, {}));
Thread.hasServerSideSupport = FeatureSupport.Stable;
const sender = "@alice:matrix.org"; beforeAll(() => {
previousThreadHasServerSideSupport = Thread.hasServerSideSupport;
const root = mkEvent({ Thread.hasServerSideSupport = FeatureSupport.Stable;
event: true,
content: {
body: "Thread root",
},
type: EventType.RoomMessage,
sender,
});
room.addLiveEvents([root]);
const threadReply = mkEvent({
event: true,
content: {
"body": "Thread reply",
"m.relates_to": {
event_id: root.getId()!,
rel_type: RelationType.Thread,
},
},
type: EventType.RoomMessage,
sender,
}); });
root.setUnsigned({ afterAll(() => {
"m.relations": { Thread.hasServerSideSupport = previousThreadHasServerSideSupport;
[RelationType.Thread]: {
count: 1,
latest_event: {
content: threadReply.getContent(),
origin_server_ts: 5,
room_id: room.roomId,
sender,
type: EventType.RoomMessage,
event_id: threadReply.getId()!,
user_id: sender,
age: 1,
},
current_user_participated: true,
},
},
}); });
const editToThreadReply = mkEvent({ it("should allow edits to be added to thread timeline", async () => {
event: true, // Given a thread
content: { const client = createClient();
"body": " * edit", const user = "@alice:matrix.org";
"m.new_content": { const room = "!room:z";
"body": "edit", const thread = await createThread(client, user, room);
"msgtype": "m.text",
"org.matrix.msc1767.text": "edit", // When a message and an edit are added to the thread
}, const messageToEdit = createThreadMessage(thread.id, user, room, "Thread reply");
"m.relates_to": { const editEvent = mkEdit(messageToEdit, client, user, room, "edit");
event_id: threadReply.getId()!, await thread.addEvent(messageToEdit, false);
rel_type: RelationType.Replace, await thread.addEvent(editEvent, false);
},
}, // Then both events end up in the timeline
type: EventType.RoomMessage, const lastEvent = thread.timeline.at(-1)!;
sender, const secondLastEvent = thread.timeline.at(-2)!;
expect(lastEvent).toBe(editEvent);
expect(secondLastEvent).toBe(messageToEdit);
// And the first message has been edited
expect(secondLastEvent.getContent().body).toEqual("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");
}); });
}); });
}); });
/**
* Create a message event that lives in a thread
*/
function createThreadMessage(threadId: string, user: string, room: string, msg: string): MatrixEvent {
return makeThreadEvent({
event: true,
user,
room,
msg,
rootEventId: threadId,
replyToEventId: threadId,
});
}
/**
* Create a thread and wait for it to be properly initialised (so you can safely
* add events to it and expect them to appear in the timeline.
*/
async function createThread(client: MatrixClient, user: string, roomId: string): Promise<Thread> {
const root = mkMessage({ event: true, user, room: roomId, msg: "Thread root" });
const room = new Room(roomId, client, "@roomcreator:x");
room.addLiveEvents([root]);
// Create the 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()));
return thread;
}
/**
* Create a MatrixClient that supports threads and has all the methods used when
* creating a thread that call out to HTTP endpoints mocked out.
*/
function createClient(): MatrixClient {
const client = mock(MatrixClient, "MatrixClient");
client.reEmitter = mock(ReEmitter, "ReEmitter");
client.canSupport = new Map();
jest.spyOn(client, "supportsThreads").mockReturnValue(true);
jest.spyOn(client, "getEventMapper").mockReturnValue(eventMapperFor(client, {}));
// 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({});
return client;
}