Skip to content
Source
Sign inSign up
fuko/trytanstackdb.com
Open History to see the last commit

content.test.ts

96 lines3.4 KB

1import { createMemoryHistory, createRouter } from "@tanstack/react-router";
2import { describe, expect, it } from "vite-plus/test";
3​
4import { createSeedData } from "@/fixtures";
5import { APP_SCREENS } from "@/lib/app-state";
6import { resolveFolderView } from "@/lib/notes/folder-views";
7import { routeTree } from "@/routeTree.gen";
8​
9import { GUIDE_SECTIONS } from "./content";
10​
11const router = createRouter({
12 routeTree,
13 history: createMemoryHistory(),
14 context: { queryClient: undefined as never },
15});
16​
17describe("guide sections", () => {
18 it("have unique ids", () => {
19 const ids = GUIDE_SECTIONS.map((section) => section.id);
20 expect(new Set(ids).size).toBe(ids.length);
21 });
22​
23 it("have short chip labels", () => {
24 for (const section of GUIDE_SECTIONS) expect(section.shortTitle.length).toBeLessThanOrEqual(20);
25 });
26​
27 it("deep-link the hands-on ones into the app", () => {
28 const targets = Object.fromEntries(
29 GUIDE_SECTIONS.filter((section) => section.tryIt).map((section) => [
30 section.id,
31 router.buildLocation(section.tryIt!).pathname,
32 ]),
33 );
34 expect(targets).toEqual({
35 introduction: "/",
36 collections: "/",
37 "live-queries": "/folders/all",
38 "optimistic-updates": "/folders/work",
39 rollbacks: "/",
40 aggregations: "/analytics",
41 });
42 });
43​
44 it("only point at screens that exist", () => {
45 const { folders } = createSeedData();
46 for (const section of GUIDE_SECTIONS) {
47 if (!section.tryIt) continue;
48 const { pathname } = router.buildLocation(section.tryIt);
49 const matches = router.matchRoutes(pathname);
50 const leaf = matches.at(-1);
51 if (!leaf) throw new Error(`${section.id} → ${pathname} matches no route`);
52 expect(leaf.routeId, `${section.id} → ${pathname}`).not.toBe("__root__");
53 const { folderId } = leaf.params as { folderId?: string };
54 if (folderId) {
55 expect(resolveFolderView(folderId, folders), `${section.id} → ${pathname}`).toBeDefined();
56 }
57 }
58 });
59​
60 it("map to app states on the Notes app's screens", () => {
61 const { folders, notes } = createSeedData();
62 const note = notes.find((note) => note.deletedAt === null)!;
63 for (const section of GUIDE_SECTIONS) {
64 for (const state of section.appStates) {
65 const label = `${section.id} → ${JSON.stringify(state)}`;
66 if (!state.screen) continue;
67 expect(APP_SCREENS, label).toContain(state.screen);
68 const params = { folderId: note.folderId, noteId: note.id, ...state.params };
69 const { pathname } = router.buildLocation({ to: state.screen, params } as never);
70 const leaf = router.matchRoutes(pathname).at(-1);
71 expect(leaf?.routeId, label).toMatch(/^\/_workspace\//);
72 if (state.params?.folderId) {
73 expect(resolveFolderView(state.params.folderId, folders), label).toBeDefined();
74 }
75 }
76 }
77 });
78​
79 it("give every hands-on section at least one app state", () => {
80 for (const section of GUIDE_SECTIONS) {
81 if (section.tryIt) expect(section.appStates.length, section.id).toBeGreaterThan(0);
82 }
83 });
84});
85​
86describe("app screens", () => {
87 it("are all Notes app routes", () => {
88 for (const screen of APP_SCREENS) {
89 const { pathname } = router.buildLocation({
90 to: screen,
91 params: { folderId: "work", noteId: "note-01" },
92 } as never);
93 expect(router.matchRoutes(pathname).at(-1)?.routeId, screen).toMatch(/^\/_workspace\//);
94 }
95 });
96});