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

useLiveQuery-row-shape.test.tsx

91 lines3.5 KB

1import { createCollection } from '@tanstack/db'
2import { createRenderEffect, createRoot } from 'solid-js'
3import { expect, it } from 'vitest'
4import { waitFor } from '@solidjs/testing-library'
5import { useLiveQuery } from '../src/useLiveQuery'
6import { mockSyncCollectionOptions } from '../../db/tests/utils'
7import { ScenarioLifetime } from '../../db/tests/conformance/scenario-lifetime'
8​
9type Row = {
10 id: string
11 config: { optional?: undefined }
12 optional?: undefined
14​
15// The shared oracle checks complete values. This receiving check observes
16// Solid's enumeration and membership subscriptions when only row shape changes.
17it.each([`root`, `nested`] as const)(
18 `notifies %s row shape subscriptions when an undefined field enters and leaves`,
19 async (location) => {
20 const resources = new ScenarioLifetime()
21 await resources.run(async () => {
22 const collection = createCollection(
23 mockSyncCollectionOptions<Row>({
24 id: `solid-row-shape-${location}`,
25 getKey: (row) => row.id,
26 initialData: [{ id: `a`, config: {} }],
27 rowUpdateMode: `full`,
28 }),
29 )
30 try {
31 const query = createRoot((dispose) => {
32 resources.defer(dispose)
33 return useLiveQuery(() => collection)
34 })
35 await waitFor(() => expect(query()).toHaveLength(1))
36 const row = query()[0]!
37 // The current path is reactive. A replaced source container does not
38 // promise that a separately captured old proxy becomes the new path.
39 const container = () => (location === `root` ? row : row.config)
40 const enumerated: Array<boolean> = []
41 const memberships: Array<boolean> = []
42 createRoot((dispose) => {
43 resources.defer(dispose)
44 createRenderEffect(() => {
45 enumerated.push(Object.keys(container()).includes(`optional`))
46 })
47 createRenderEffect(() => {
48 memberships.push(`optional` in container())
49 })
50 })
51 let publications = 0
52 const subscription = collection.subscribeChanges(
53 (changes) => {
54 publications += changes.length
55 },
56 { includeInitialState: false },
57 )
58 resources.defer(() => subscription.unsubscribe())
59 const write = (next: Row) => {
60 collection.utils.begin()
61 collection.utils.write({ type: `update`, value: next })
62 collection.utils.commit()
63 }
64 const next: Row = { id: `a`, config: {} }
65 if (location === `root`) next.optional = undefined
66 else next.config.optional = undefined
67 write(next)
68 // Core emits even when only own-key presence changes. The hook gets
69 // that source fact; neither subscriber reads a changed scalar value.
70 expect(publications).toBe(1)
71 await waitFor(() => {
72 expect(enumerated).toEqual([false, true])
73 expect(memberships).toEqual([false, true])
74 })
75 expect(Object.hasOwn(container(), `optional`)).toBe(true)
76 expect(container().optional).toBeUndefined()
77​
78 write({ id: `a`, config: {} })
79 expect(publications).toBe(2)
80 await waitFor(() => {
81 expect(enumerated).toEqual([false, true, false])
82 expect(memberships).toEqual([false, true, false])
83 })
84 expect(Object.hasOwn(container(), `optional`)).toBe(false)
85 expect(query()[0]).toBe(row)
86 } finally {
87 resources.defer(() => collection.cleanup())
88 }
89 })
90 },