Open History to see the last commit
1/**
2 * A Suspense render asks for its data, so it counts as a preload. When the
3 * source Collection already holds the rows, that preload starts the live-query
4 * Collection's sync run and makes it ready before the first snapshot is read:
5 * the synchronous observation cut. The component renders its data without
6 * showing the fallback. Without a DbClient, this holds for eager and on-demand
7 * source Collections and for ordered windows. A live-query Collection whose
8 * sync run started before the preload would resume acquisition through the
9 * restart path, one microtask later, and suspend once.
10 */
11import { expect, it } from 'vitest'
12import { act, render } from '@testing-library/react'
13import { BTreeIndex, createCollection } from '@tanstack/db'
14import { Suspense } from 'react'
15import { useLiveSuspenseQuery } from '../src/useLiveSuspenseQuery'
16
17type Row = { id: string; rank: number }
18const ROWS: Array<Row> = [
19 { id: `a`, rank: 1 },
20 { id: `b`, rank: 2 },
21 { id: `c`, rank: 3 },
22]
23
24for (const syncMode of [`eager`, `on-demand`] as const) {
25 for (const shape of [`all`, `window`] as const) {
26 it(`${syncMode} ${shape}: a source holding the rows renders without suspending`, () => {
27 const source = createCollection<Row>({
28 id: `warm-${syncMode}-${shape}`,
29 getKey: (row) => row.id,
30 syncMode,
31 startSync: true,
32 autoIndex: `eager`,
33 defaultIndexType: BTreeIndex,
34 sync: {
35 sync: ({ begin, write, commit, markReady }) => {
36 begin()
37 for (const row of ROWS) write({ type: `insert`, value: row })
38 commit()
39 markReady()
40 return syncMode === `on-demand`
41 ? { loadSubset: () => true }
42 : undefined
43 },
44 },
45 })
46 const renders: Array<string> = []
47 function View() {
48 const { data } = useLiveSuspenseQuery((q) => {
49 const base = q.from({ row: source }).orderBy(({ row }) => row.rank)
50 return shape === `window` ? base.limit(2) : base
51 })
52 renders.push(data.map((row) => row.id).join(``))
53 return null
54 }
55 function Fallback() {
56 renders.push(`fallback`)
57 return null
58 }
59 render(
60 <Suspense fallback={<Fallback />}>
61 <View />
62 </Suspense>,
63 )
64 expect(renders).toEqual([shape === `window` ? `ab` : `abc`])
65 })
66 }
67}
68
69it(`renders warm rows again after the held Suspense collection is cleaned up`, async () => {
70 // The rerender keeps the cleaned-up collection. It asks for its data again,
71 // so the in-render preload restarts it and the warm source serves it.
72 const source = createCollection<Row>({
73 id: `warm-after-cleanup`,
74 getKey: (row) => row.id,
75 syncMode: `on-demand`,
76 startSync: true,
77 autoIndex: `eager`,
78 defaultIndexType: BTreeIndex,
79 sync: {
80 sync: ({ begin, write, commit, markReady }) => {
81 begin()
82 for (const row of ROWS) write({ type: `insert`, value: row })
83 commit()
84 markReady()
85 return { loadSubset: () => true }
86 },
87 },
88 })
89 const renders: Array<string> = []
90 let held: { cleanup: () => Promise<void> } | undefined
91 function View({ pass }: { pass: number }) {
92 const result = useLiveSuspenseQuery((q) =>
93 q.from({ row: source }).orderBy(({ row }) => row.rank),
94 )
95 held = result.collection
96 renders.push(`${pass}:${result.data.map((row) => row.id).join(``)}`)
97 return null
98 }
99 function Fallback() {
100 renders.push(`fallback`)
101 return null
102 }
103 const view = render(
104 <Suspense fallback={<Fallback />}>
105 <View pass={1} />
106 </Suspense>,
107 )
108 await act(async () => {})
109 await act(async () => {
110 await held!.cleanup()
111 })
112 renders.length = 0
113 await act(async () => {
114 view.rerender(
115 <Suspense fallback={<Fallback />}>
116 <View pass={2} />
117 </Suspense>,
118 )
119 })
120 expect(renders[0]).toBe(`2:abc`)
121 expect(renders).not.toContain(`fallback`)
122})