Open History to see the last commit
1import { createMemoryHistory, createRouter } from "@tanstack/react-router";
2import { afterEach, beforeAll, describe, expect, it, vi } from "vite-plus/test";
3
4import { setNetworkConfig } from "@/api/network-config";
5import { isSectionDone } from "@/components/guide/guide-store";
6import { foldersCollection } from "@/db/collections";
7import { renameFolder } from "@/db/mutations";
8import { routeTree } from "@/routeTree.gen";
9
10import { rollbackNotice, startAppNotifications } from "./app-notifications";
11
12beforeAll(async () => {
13 setNetworkConfig({ latencyMs: 0 });
14 await foldersCollection.preload();
15});
16
17let stop: (() => void) | undefined;
18afterEach(() => stop?.());
19
20describe("app notifications", () => {
21 it("are mounted by the root route, which every page shares", () => {
22 // The workspace and the deep dives are both children of the root route.
23 expect(routeTree.options.component).toBeDefined();
24 const router = createRouter({
25 routeTree,
26 history: createMemoryHistory(),
27 context: { queryClient: undefined as never },
28 });
29 for (const id of ["/_workspace", "/deep-dives/query-driven-sync"] as const) {
30 expect(router.routesById[id].parentRoute, id).toBe(routeTree);
31 }
32 });
33
34 it("report a rejected change and tick off Rollbacks, wherever the reader is", async () => {
35 setNetworkConfig({ latencyMs: 30 });
36 const notify = vi.fn();
37 stop = startAppNotifications(notify);
38
39 // The reader leaves the Notes app while the rename is in flight; nothing
40 // here depends on the workspace being mounted.
41 const tx = renameFolder("work", "Personal");
42 expect(foldersCollection.get("work")?.name).toBe("Personal");
43 await tx.when("settled").catch(() => {});
44
45 expect(foldersCollection.get("work")?.name).toBe("Work");
46 expect(notify).toHaveBeenCalledWith(
47 expect.objectContaining({
48 title: "Couldn't rename the folder",
49 description: "There's already a folder named “Personal”. The change was undone.",
50 }),
51 );
52 expect(isSectionDone("rollbacks")).toBe(true);
53 });
54
55 it("only speak up for rollbacks", () => {
56 expect(
57 rollbackNotice({ type: "folder.renamed", folderId: "work", name: "X", status: "confirmed" }),
58 ).toBeNull();
59 });
60});