Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

descriptor-start-release-oracle.test.tsx

111 lines4.0 KB

1/**
2 * React receiving driver for descriptor-start-release-oracle. Binding fails
3 * after zero through three source acquisitions. Native React retries may call
4 * the failing factory again; successful sources must still be constructed once.
5 * The exact preparation error crosses React's failed mount. A direct reader then
6 * starts lazy sources, and client cleanup releases every healthy resource once.
7 * No graph from the rejected input may start a lazy source on its own.
8 */
9import { cleanup, renderHook } from '@testing-library/react'
10import { expect, it } from 'vitest'
11import { useLiveQuery } from '../src/useLiveQuery'
12import { useLiveInfiniteQuery } from '../src/useLiveInfiniteQuery'
13import { useLiveSuspenseQuery } from '../src/useLiveSuspenseQuery'
14import {
15 checkDescriptorStart,
16 descriptorStartHistories,
17 expectedDescriptorStart,
18} from '../../db/tests/descriptor-start-release-oracle'
19import { createOracleReach } from '../../db/tests/oracle-reach'
20import { declareOracle } from '../../db/tests/oracle-declaration'
21import { descriptorStartFixture } from '../../db/tests/descriptor-start-release-support'
22import { withOracleCleanup } from '../../db/tests/utils'
23import type { DescriptorStartHistory } from '../../db/tests/descriptor-start-release-oracle'
24​
25const hooks = [`live`, `infinite`, `suspense`] as const
26async function checkHistory(
27 history: DescriptorStartHistory,
28 hook: (typeof hooks)[number],
29) {
30 const reach = createOracleReach(`React descriptor starts ${hook}`, [
31 `rejected`,
32 `read`,
33 `cleaned`,
34 ])
35 reach.beginHistory()
36 const fixture = descriptorStartFixture(history, `React`)
37 const { client, query, id, failure, startErrors, factories, observe } =
38 fixture
39 // Native root cleanup is registered before renderHook can acquire a root
40 // and fail without returning its unmount handle.
41 await withOracleCleanup(async () => {
42 let rejection: unknown
43 try {
44 renderHook(() => {
45 const config = { client, query, queryKey: [id] }
46 if (hook === `suspense`) return useLiveSuspenseQuery(config)
47 if (hook === `infinite`)
48 return useLiveInfiniteQuery(() => query, {
49 client,
50 queryKey: [id],
51 pageSize: 1,
52 })
53 return useLiveQuery(config)
54 })
55 } catch (error) {
56 rejection = error
57 }
58 expect(rejection).toBe(failure)
59 expect(factories).toEqual(
60 expectedDescriptorStart(history, `rejected`).factories,
61 )
62 fixture.retain()
63 expect(fixture.failures()).toBeGreaterThan(0)
64 checkDescriptorStart(observe(), history, `rejected`)
65 reach.reach(`rejected`)
66 for (const [index, row] of fixture.collections().entries()) {
67 try {
68 row.startSyncImmediate()
69 } catch (error) {
70 expect(error).toBe(startErrors[index])
71 }
72 }
73 checkDescriptorStart(observe(), history, `read`)
74 reach.reach(`read`)
75 cleanup()
76 await client.cleanup()
77 checkDescriptorStart(observe(), history, `cleaned`)
78 reach.reach(`cleaned`)
79 reach.assertReached()
80 }, [() => cleanup(), () => client.cleanup()])
82​
83it.each(
84 descriptorStartHistories.flatMap((history) =>
85 hooks.map((hook) => ({ history, hook })),
86 ),
87)(`releases independent descriptor starts in React: %j`, ({ history, hook }) =>
88 checkHistory(history, hook),
90​
91it.each([
92 `blocked-peer`,
93 `duplicate-start`,
94 `leaked-resource`,
95 `wrong-row`,
96] as const)(`rejects wrong multi-source release observation: %s`, (fault) => {
97 const history = { acquired: 3, failedStarts: 3, eagerStarts: 7 }
98 const actual = expectedDescriptorStart(history, `cleaned`)
99 checkDescriptorStart(actual, history, `cleaned`)
100 if (fault === `blocked-peer`) actual.starts[2] = 0
101 if (fault === `duplicate-start`) actual.starts[2] = actual.starts[2]! + 1
102 if (fault === `leaked-resource`) actual.cleanups[2] = 0
103 if (fault === `wrong-row`) actual.rows[2] = [{ id: `bad`, value: `bad` }]
104 expect(() => checkDescriptorStart(actual, history, `cleaned`)).toThrow()
105})
106​
107declareOracle({
108 check: [checkDescriptorStart],
109 model: [expectedDescriptorStart],
110 grammar: [descriptorStartHistories],
111})