Skip to content

Commit 28330b0

Browse files
committed
Write unit test
1 parent 765ddfa commit 28330b0

File tree

1 file changed

+91
-11
lines changed

1 file changed

+91
-11
lines changed

spec/unit/models/thread.spec.ts

Lines changed: 91 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
import { MatrixClient, PendingEventOrdering } from "../../../src/client";
18-
import { Room } from "../../../src/models/room";
19-
import { Thread, THREAD_RELATION_TYPE, ThreadEvent } from "../../../src/models/thread";
20-
import { mkThread } from "../../test-utils/thread";
21-
import { TestClient } from "../../TestClient";
22-
import { emitPromise, mkMessage, mock } from "../../test-utils/test-utils";
23-
import { EventStatus, MatrixEvent } from "../../../src";
24-
import { ReceiptType } from "../../../src/@types/read_receipts";
25-
import { getMockClientWithEventEmitter, mockClientMethodsUser } from "../../test-utils/client";
26-
import { ReEmitter } from "../../../src/ReEmitter";
27-
import { Feature, ServerSupport } from "../../../src/feature";
17+
import {MatrixClient, PendingEventOrdering} from "../../../src/client";
18+
import {Room} from "../../../src/models/room";
19+
import {Thread, THREAD_RELATION_TYPE, ThreadEvent} from "../../../src/models/thread";
20+
import {mkThread} from "../../test-utils/thread";
21+
import {TestClient} from "../../TestClient";
22+
import {emitPromise, mkMessage, mock} from "../../test-utils/test-utils";
23+
import {Direction, EventStatus, MatrixEvent} from "../../../src";
24+
import {ReceiptType} from "../../../src/@types/read_receipts";
25+
import {getMockClientWithEventEmitter, mockClientMethodsUser} from "../../test-utils/client";
26+
import {ReEmitter} from "../../../src/ReEmitter";
27+
import {Feature, ServerSupport} from "../../../src/feature";
2828

2929
describe("Thread", () => {
3030
describe("constructor", () => {
@@ -283,4 +283,84 @@ describe("Thread", () => {
283283
expect(thread2.getEventReadUpTo(myUserId)).toBe(null);
284284
});
285285
});
286+
287+
describe("resetLiveTimeline", () => {
288+
it("correctly resets the live timeline", async () => {
289+
const myUserId = "@bob:example.org";
290+
const testClient = new TestClient(myUserId, "DEVICE", "ACCESS_TOKEN", undefined, { timelineSupport: false });
291+
const client = testClient.client;
292+
const room = new Room("123", client, myUserId, {
293+
pendingEventOrdering: PendingEventOrdering.Detached,
294+
});
295+
296+
jest.spyOn(client, "getRoom").mockReturnValue(room);
297+
298+
const { thread } = mkThread({
299+
room,
300+
client,
301+
authorId: myUserId,
302+
participantUserIds: ["@alice:example.org"],
303+
length: 3,
304+
});
305+
await emitPromise(thread, ThreadEvent.Update);
306+
expect(thread.length).toBe(2);
307+
308+
jest.spyOn(client, "createMessagesRequest").mockImplementation((_, token) => Promise.resolve({
309+
chunk: [],
310+
start: `${token}-new`,
311+
end: `${token}-new`,
312+
}));
313+
314+
function timelines(): [string | null, string | null][] {
315+
return thread.timelineSet.getTimelines()
316+
.map(it => [it.getPaginationToken(Direction.Backward), it.getPaginationToken(Direction.Forward)]);
317+
}
318+
319+
expect(timelines()).toEqual([[null, null]]);
320+
const promise = thread.resetLiveTimeline("b1", "f1");
321+
expect(timelines()).toEqual([[null, "f1"], ["b1", null]]);
322+
await promise;
323+
expect(timelines()).toEqual([[null, "f1-new"], ["b1-new", null]]);
324+
});
325+
326+
it("does not modify changed tokens", async () => {
327+
const myUserId = "@bob:example.org";
328+
const testClient = new TestClient(myUserId, "DEVICE", "ACCESS_TOKEN", undefined, { timelineSupport: false });
329+
const client = testClient.client;
330+
const room = new Room("123", client, myUserId, {
331+
pendingEventOrdering: PendingEventOrdering.Detached,
332+
});
333+
334+
jest.spyOn(client, "getRoom").mockReturnValue(room);
335+
336+
const { thread } = mkThread({
337+
room,
338+
client,
339+
authorId: myUserId,
340+
participantUserIds: ["@alice:example.org"],
341+
length: 3,
342+
});
343+
await emitPromise(thread, ThreadEvent.Update);
344+
expect(thread.length).toBe(2);
345+
346+
jest.spyOn(client, "createMessagesRequest").mockImplementation((_, token) => Promise.resolve({
347+
chunk: [],
348+
start: `${token}-new`,
349+
end: `${token}-new`,
350+
}));
351+
352+
function timelines(): [string | null, string | null][] {
353+
return thread.timelineSet.getTimelines()
354+
.map(it => [it.getPaginationToken(Direction.Backward), it.getPaginationToken(Direction.Forward)]);
355+
}
356+
357+
expect(timelines()).toEqual([[null, null]]);
358+
const promise = thread.resetLiveTimeline("b1", "f1");
359+
expect(timelines()).toEqual([[null, "f1"], ["b1", null]]);
360+
thread.timelineSet.getTimelines()[0].setPaginationToken("f2", Direction.Forward);
361+
thread.timelineSet.getTimelines()[1].setPaginationToken("b2", Direction.Backward);
362+
await promise;
363+
expect(timelines()).toEqual([[null, "f2"], ["b2", null]]);
364+
});
365+
});
286366
});

0 commit comments

Comments
 (0)