|
1 | 1 | import type { TTL } from '@rocicorp/zero' |
2 | | -import type { MockInstance } from 'vitest' |
3 | 2 | import { createBuilder, createSchema, number, string, syncedQuery, table, Zero } from '@rocicorp/zero' |
4 | 3 | import { describe, expect, it, vi } from 'vitest' |
5 | 4 | import { nextTick, ref, watchEffect } from 'vue' |
@@ -98,77 +97,23 @@ describe('useQuery', () => { |
98 | 97 | z.value.close() |
99 | 98 | }) |
100 | 99 |
|
101 | | - it('useQuery with ttl ([email protected])', async () => { |
| 100 | + it('useQuery with ttl', async () => { |
102 | 101 | const { z, tableQuery, useQuery } = await setupTestEnvironment() |
103 | | - if (!('updateTTL' in tableQuery)) { |
104 | | - // 0.19 removed updateTTL from the query |
105 | | - return |
106 | | - } |
107 | 102 |
|
108 | 103 | const ttl = ref<TTL>('1m') |
109 | 104 |
|
110 | | - const materializeSpy = vi.spyOn(tableQuery, 'materialize') |
111 | | - // @ts-expect-error missing from v0.19+ |
112 | | - const updateTTLSpy = vi.spyOn(tableQuery, 'updateTTL') |
| 105 | + const materializeSpy = vi.spyOn(z.value, 'materialize') |
113 | 106 | const queryGetter = vi.fn(() => tableQuery) |
114 | 107 |
|
115 | 108 | useQuery(queryGetter, () => ({ ttl: ttl.value })) |
116 | | - |
117 | 109 | expect(queryGetter).toHaveBeenCalledTimes(1) |
118 | | - expect(updateTTLSpy).toHaveBeenCalledTimes(0) |
| 110 | + |
| 111 | + expect(materializeSpy).toHaveLastReturnedWith(expect.any(VueView)) |
119 | 112 | expect(materializeSpy).toHaveBeenCalledExactlyOnceWith( |
| 113 | + tableQuery, |
120 | 114 | vueViewFactory, |
121 | | - '1m', |
| 115 | + { ttl: '1m' }, |
122 | 116 | ) |
123 | | - materializeSpy.mockClear() |
124 | | - |
125 | | - ttl.value = '10m' |
126 | | - await nextTick() |
127 | | - |
128 | | - expect(materializeSpy).toHaveBeenCalledTimes(0) |
129 | | - expect(updateTTLSpy).toHaveBeenCalledExactlyOnceWith('10m') |
130 | | - |
131 | | - z.value.close() |
132 | | - }) |
133 | | - |
134 | | - it('useQuery with ttl ([email protected])', async () => { |
135 | | - const { z, tableQuery, useQuery } = await setupTestEnvironment() |
136 | | - if ('updateTTL' in tableQuery) { |
137 | | - // 0.19 removed updateTTL from the query |
138 | | - return |
139 | | - } |
140 | | - |
141 | | - const ttl = ref<TTL>('1m') |
142 | | - |
143 | | - let materializeSpy: MockInstance |
144 | | - // @ts-expect-error only present in v0.23+ |
145 | | - if (z.value.materialize) { |
146 | | - materializeSpy = vi.spyOn(z.value, 'materialize') |
147 | | - } |
148 | | - else { |
149 | | - materializeSpy = vi.spyOn(tableQuery, 'materialize') |
150 | | - } |
151 | | - |
152 | | - const queryGetter = vi.fn(() => tableQuery) |
153 | | - |
154 | | - useQuery(queryGetter, () => ({ ttl: ttl.value })) |
155 | | - expect(queryGetter).toHaveBeenCalledTimes(1) |
156 | | - |
157 | | - expect(materializeSpy).toHaveLastReturnedWith(expect.any(VueView)) |
158 | | - // @ts-expect-error only present in v0.23+ |
159 | | - if (z.value.materialize) { |
160 | | - expect(materializeSpy).toHaveBeenCalledExactlyOnceWith( |
161 | | - tableQuery, |
162 | | - vueViewFactory, |
163 | | - { ttl: '1m' }, |
164 | | - ) |
165 | | - } |
166 | | - else { |
167 | | - expect(materializeSpy).toHaveBeenCalledExactlyOnceWith( |
168 | | - vueViewFactory, |
169 | | - '1m', |
170 | | - ) |
171 | | - } |
172 | 117 |
|
173 | 118 | const view: VueView<unknown> = materializeSpy.mock.results[0]!.value |
174 | 119 | const updateTTLSpy = vi.spyOn(view, 'updateTTL') |
@@ -329,7 +274,7 @@ describe('useQuery', () => { |
329 | 274 | await z.mutate.table.insert({ a: 1, b: 'a' }) |
330 | 275 | await z.mutate.table.insert({ a: 2, b: 'b' }) |
331 | 276 |
|
332 | | - const { data: rows, status } = useQuery(() => z.query.table) |
| 277 | + const { data: rows, status } = useQuery(z, () => z.query.table) |
333 | 278 | expect(rows.value).toMatchInlineSnapshot(`[ |
334 | 279 | { |
335 | 280 | "a": 1, |
|
0 commit comments