Open History to see the last commit
1import { createLiveQueryCollection } from "@tanstack/react-db";
2import { beforeAll, describe, expect, it } from "vite-plus/test";
3
4import { api } from "@/api/client";
5import { setNetworkConfig } from "@/api/network-config";
6import { clearRequestLog, requestLogStore } from "@/api/request-log";
7import type { Note } from "@/db/schema";
8
9import { folderNotesQuery, onDemandNotesCollection } from "./on-demand-notes";
10
11beforeAll(() => {
12 setNetworkConfig({ latencyMs: 0 });
13});
14
15describe("on-demand notes", () => {
16 it("loads nothing until a live query asks", () => {
17 expect(onDemandNotesCollection.size).toBe(0);
18 });
19
20 it("loads one folder's notes, filtered on the server by folder", async () => {
21 clearRequestLog();
22 const work = createLiveQueryCollection(folderNotesQuery("work"));
23 await work.preload();
24
25 const urls = requestLogStore.get().map((entry) => entry.url);
26 expect(urls).toEqual(["/api/notes?folderId=work"]);
27
28 const serverNotes = await api.get<Note[]>("/api/notes?folderId=work");
29 expect(serverNotes.some((note) => note.deletedAt !== null)).toBe(true);
30 // The collection holds what the server sent for the folder, deleted notes included…
31 expect(onDemandNotesCollection.toArray.map((note) => note.id).sort()).toEqual(
32 serverNotes.map((note) => note.id).sort(),
33 );
34 // …and the live query applies the condition the API couldn't (not deleted).
35 expect(work.toArray.map((note) => note.id).sort()).toEqual(
36 serverNotes
37 .filter((note) => note.deletedAt === null)
38 .map((note) => note.id)
39 .sort(),
40 );
41 await work.cleanup();
42 });
43
44 it("loads another folder with its own request", async () => {
45 clearRequestLog();
46 const recipes = createLiveQueryCollection(folderNotesQuery("recipes"));
47 await recipes.preload();
48 expect(requestLogStore.get().map((entry) => entry.url)).toEqual([
49 "/api/notes?folderId=recipes",
50 ]);
51 expect(recipes.toArray.length).toBeGreaterThan(0);
52 expect(recipes.toArray.every((note) => note.folderId === "recipes")).toBe(true);
53 await recipes.cleanup();
54 });
55});