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

queries.test.ts

119 lines4.9 KB

1import { createLiveQueryCollection } from "@tanstack/react-db";
2import type { Context, InitialQueryBuilder, QueryBuilder } from "@tanstack/react-db";
3import { beforeAll, describe, expect, it } from "vite-plus/test";
4​
5import { setNetworkConfig } from "@/api/network-config";
6​
7import { accountCollection, foldersCollection, notesCollection } from "./collections";
8import { moveNote, renameFolder, setNotePinned } from "./mutations";
9import { noteCountQuery, noteCountsQuery, noteListQuery } from "./queries";
10​
11beforeAll(async () => {
12 setNetworkConfig({ latencyMs: 0 });
13 await Promise.all([
14 foldersCollection.preload(),
15 notesCollection.preload(),
16 accountCollection.preload(),
17 ]);
18});
19​
20async function liveQuery<TContext extends Context>(
21 query: (q: InitialQueryBuilder) => QueryBuilder<TContext>,
22) {
23 const collection = createLiveQueryCollection(query);
24 await collection.preload();
25 return collection;
27​
28describe("note list", () => {
29 it("shows a folder's notes, pinned first, then newest edit first", async () => {
30 const list = await liveQuery(noteListQuery("work", "dateEdited"));
31 const notes = list.toArray;
32 expect(notes.length).toBeGreaterThan(1);
33 expect(notes.every((note) => note.folderId === "work")).toBe(true);
34 const firstUnpinned = notes.findIndex((note) => !note.pinned);
35 expect(notes.slice(firstUnpinned).some((note) => note.pinned)).toBe(false);
36 const dates = notes.slice(firstUnpinned).map((note) => note.updatedAt);
37 expect(dates).toEqual([...dates].sort().reverse());
38 });
39​
40 it("sorts by title, ignoring case", async () => {
41 const list = await liveQuery(noteListQuery("all", "title"));
42 const titles = list.toArray.filter((note) => !note.pinned).map((note) => note.title);
43 expect(titles).toEqual(
44 [...titles].sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase())),
45 );
46 });
47​
48 it("joins each note with its folder's name", async () => {
49 const list = await liveQuery(noteListQuery("all", "dateEdited"));
50 const folderNames = new Map(
51 foldersCollection.toArray.map((folder) => [folder.id, folder.name]),
52 );
53 for (const note of list.toArray) expect(note.folderName).toBe(folderNames.get(note.folderId));
54 });
55​
56 it("smart folders are filters: Shared, and Recently Deleted", async () => {
57 const shared = await liveQuery(noteListQuery("shared", "dateEdited"));
58 expect(shared.toArray.length).toBeGreaterThan(0);
59 expect(shared.toArray.every((note) => note.shared && note.deletedAt === null)).toBe(true);
60​
61 const deleted = await liveQuery(noteListQuery("recently-deleted", "dateEdited"));
62 expect(deleted.toArray.length).toBeGreaterThan(0);
63 expect(deleted.toArray.every((note) => note.deletedAt !== null)).toBe(true);
64 });
65});
66​
67describe("counts", () => {
68 it("count notes per folder and per smart folder", async () => {
69 const perFolder = await liveQuery(noteCountsQuery);
70 const all = await liveQuery(noteCountQuery("all"));
71 const total = perFolder.toArray.reduce((sum, row) => sum + row.count, 0);
72 expect(all.toArray[0]!.count).toBe(total);
73 const active = notesCollection.toArray.filter((note) => note.deletedAt === null);
74 expect(total).toBe(active.length);
75 });
76​
77 it("an empty folder has no count row", async () => {
78 const empty = await liveQuery(noteCountQuery("no-such-folder"));
79 expect(empty.toArray).toEqual([]);
80 });
81});
82​
83describe("optimistic changes", () => {
84 it("move a note between folders everywhere at once, then stick", async () => {
85 const counts = await liveQuery(noteCountsQuery);
86 const travel = await liveQuery(noteListQuery("travel", "dateEdited"));
87 const countOf = (folderId: string) =>
88 counts.toArray.find((row) => row.folderId === folderId)?.count ?? 0;
89 const workBefore = countOf("work");
90 const travelBefore = countOf("travel");
91​
92 const tx = moveNote("note-01", "travel");
93 // Synchronously, before the server has answered:
94 expect(countOf("work")).toBe(workBefore - 1);
95 expect(countOf("travel")).toBe(travelBefore + 1);
96 expect(travel.toArray.map((note) => note.id)).toContain("note-01");
97​
98 await tx.when("settled");
99 expect(notesCollection.get("note-01")?.folderId).toBe("travel");
100 expect(countOf("travel")).toBe(travelBefore + 1);
101 });
102​
103 it("pin a note to the top of its list", async () => {
104 const list = await liveQuery(noteListQuery("personal", "dateEdited"));
105 const last = list.toArray.at(-1)!.id;
106 const tx = setNotePinned(last, true);
107 expect(list.toArray[0]!.id).toBe(last);
108 await tx.when("settled");
109 expect(list.toArray[0]!.id).toBe(last);
110 });
111​
112 it("roll back a rename the server rejects", async () => {
113 const tx = renameFolder("recipes", "Personal");
114 expect(foldersCollection.get("recipes")?.name).toBe("Personal");
115 await expect(tx.when("settled")).rejects.toMatchObject({ status: 409 });
116 expect(foldersCollection.get("recipes")?.name).toBe("Recipes");
117 expect(tx.state).toBe("failed");
118 });
119});