Open History to see the last commit
1/**
2 * Reactive update and update-in-place cases whose expectations are written
3 * out row by row. The notification history and unsubscribe laws that the
4 * oracle suite declares are in live-updates-oracle.suite.ts, which runs this
5 * suite too.
6 */
7import { describe, expect, it } from 'vitest'
8import { createLiveQueryCollection, gt } from '@tanstack/db'
9import { waitForQueryData } from '../utils/helpers'
10import {
11 assertUserRows,
12 captureUserRows,
13 userFixture,
14 waitForOwnedUser,
15 waitForUserRows,
16 withOwnedUsers,
17} from './mutations.suite'
18import type { E2ETestConfig } from '../types'
19
20export function createLiveUpdatesUnitTestSuite(
21 getConfig: () => Promise<E2ETestConfig>,
22) {
23 describe('Live Updates Suite', () => {
24 describe('Reactive Updates', () => {
25 it('should receive updates when backend data changes', async () => {
26 const config = await getConfig()
27 await withOwnedUsers(config, async (owned) => {
28 const query = owned.track(
29 createLiveQueryCollection((q) =>
30 q
31 .from({ user: config.collections.onDemand.users })
32 .where(({ user }) => gt(user.age, 30)),
33 ),
34 )
35 await query.preload()
36 await waitForQueryData(query, { minSize: 1 })
37 const peers = captureUserRows(query.values())
38 const initialSize = query.size
39 expect(initialSize).toBeGreaterThan(0)
40 const row = userFixture('Live Update User', 45)
41 row.metadata = { owned: true }
42 const remove = await owned.insert(row)
43 const observed = await waitForUserRows(query, [...peers, row])
44 expect(query.size).toBe(initialSize + 1)
45 const wrongKey = structuredClone(observed)
46 wrongKey[0]!.id += '-wrong'
47 expect(() => assertUserRows(wrongKey, [...peers, row])).toThrow()
48 const wrongMetadata = structuredClone(observed)
49 const metadataPeer = wrongMetadata.find(
50 (value) => value.id === row.id,
51 )
52 if (!metadataPeer || metadataPeer.metadata === null)
53 throw new Error('Expected the owned row with non-null metadata')
54 Object.defineProperty(metadataPeer.metadata, 'unexpected', {
55 value: undefined,
56 enumerable: true,
57 })
58 expect(
59 Object.prototype.hasOwnProperty.call(
60 metadataPeer.metadata,
61 'unexpected',
62 ),
63 ).toBe(true)
64 expect(() => assertUserRows(wrongMetadata, [...peers, row])).toThrow()
65 await remove()
66 await waitForUserRows(query, peers)
67 assertUserRows(observed, [...peers, row])
68 })
69 })
70
71 it('should add new records that match query predicate', async () => {
72 const config = await getConfig()
73 await withOwnedUsers(config, async (owned) => {
74 const query = owned.track(
75 createLiveQueryCollection((q) =>
76 q
77 .from({ user: config.collections.onDemand.users })
78 .where(({ user }) => gt(user.age, 30)),
79 ),
80 )
81 await query.preload()
82 await waitForQueryData(query, { minSize: 1 })
83 const peers = captureUserRows(query.values())
84 const initialSize = query.size
85 expect(initialSize).toBeGreaterThan(0)
86 const row = userFixture('Matching User', 35)
87 const remove = await owned.insert(row)
88 await waitForUserRows(query, [...peers, row])
89 expect(query.size).toBe(initialSize + 1)
90 await remove()
91 await waitForUserRows(query, peers)
92 })
93 })
94
95 it('should remove records that no longer match predicate', async () => {
96 const config = await getConfig()
97 await withOwnedUsers(config, async (owned) => {
98 const query = owned.track(
99 createLiveQueryCollection((q) =>
100 q
101 .from({ user: config.collections.onDemand.users })
102 .where(({ user }) => gt(user.age, 30)),
103 ),
104 )
105 await query.preload()
106 await waitForQueryData(query, { minSize: 1 })
107 const peers = captureUserRows(query.values())
108 const row = userFixture('Live Exit User', 45)
109 await owned.insert(row)
110 await waitForUserRows(query, [...peers, row])
111 const initialSize = query.size
112 expect(initialSize).toBeGreaterThan(0)
113 await config.mutations!.updateUser(row.id, { age: 25 })
114 await waitForOwnedUser(config, { ...row, age: 25 })
115 await waitForUserRows(query, peers)
116 expect(query.size).toBe(initialSize - 1)
117 expect(query.has(row.id)).toBe(false)
118 })
119 })
120 })
121
122 describe('Update Existing Records', () => {
123 it('should update existing records in query results', async () => {
124 const config = await getConfig()
125 await withOwnedUsers(config, async (owned) => {
126 const query = owned.track(
127 createLiveQueryCollection((q) =>
128 q.from({ user: config.collections.onDemand.users }),
129 ),
130 )
131 await query.preload()
132 await waitForQueryData(query, { minSize: 1 })
133 const peers = captureUserRows(query.values())
134 const row = userFixture('Existing Owned User', 38)
135 await owned.insert(row)
136 await waitForUserRows(query, [...peers, row])
137 const originalAge = row.age
138 await config.mutations!.updateUser(row.id, { age: originalAge + 10 })
139 const updated = { ...row, age: originalAge + 10 }
140 await waitForOwnedUser(config, updated)
141 await waitForUserRows(query, [...peers, updated])
142 expect(query.get(row.id)?.age).toBe(originalAge + 10)
143 })
144 })
145 })
146 })
147}