Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import { advanceWorkspaceVisit, workspaceRevisionOf } from "./useWorkspaceRevision";
4
5function visitThread(threadKey: string, unloaded: boolean, mutationId: string | null) {
6 let visit = advanceWorkspaceVisit(null, { threadKey, unloaded, mutationId }, "view");
7 return (next: { unloaded: boolean; mutationId: string | null }) => {
8 visit = advanceWorkspaceVisit(visit, { threadKey, ...next }, "view");
9 return workspaceRevisionOf(visit, next.mutationId);
10 };
11}
12
13describe("workspace revision", () => {
14 it("keeps the visit's revision for the mutation id the history loads with, not for a later one", () => {
15 const render = visitThread("thread-b", true, null);
16 const pending = render({ unloaded: true, mutationId: null });
17 expect(pending.pending).toBe(true);
18 expect(pending.revision).not.toBeNull();
19
20 expect(render({ unloaded: false, mutationId: "b1" })).toEqual({
21 revision: pending.revision,
22 pending: false,
23 });
24 expect(render({ unloaded: false, mutationId: "b2" }).revision).toBe("b2");
25 expect(render({ unloaded: false, mutationId: "b1" }).revision).toBe(pending.revision);
26 });
27
28 it("treats the first mutation of a thread whose history loaded without one as a change", () => {
29 const render = visitThread("thread-b", true, null);
30 const loaded = render({ unloaded: false, mutationId: null });
31 expect(loaded.revision).not.toBeNull();
32 expect(render({ unloaded: false, mutationId: "b1" }).revision).toBe("b1");
33 });
34
35 it("is the mutation id when the visit began with the history known", () => {
36 const render = visitThread("thread-b", false, "b1");
37 expect(render({ unloaded: false, mutationId: "b1" })).toEqual({
38 revision: "b1",
39 pending: false,
40 });
41 });
42
43 it("starts over, under another revision, for another thread and for a later visit", () => {
44 const input = { unloaded: true, mutationId: null };
45 const first = advanceWorkspaceVisit(null, { threadKey: "thread-a", ...input }, "view");
46 const second = advanceWorkspaceVisit(first, { threadKey: "thread-b", ...input }, "view");
47 const again = advanceWorkspaceVisit(second, { threadKey: "thread-a", ...input }, "view");
48 expect(new Set([first.revision, second.revision, again.revision]).size).toBe(3);
49 expect(workspaceRevisionOf(again, null).pending).toBe(true);
50 });
51});