Skip to content

Commit 7cfd2bd

Browse files
refactor: cleanup useQuery tests (#158)
1 parent 6520aef commit 7cfd2bd

File tree

1 file changed

+19
-27
lines changed

1 file changed

+19
-27
lines changed

src/query.test.ts

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { TTL } from '@rocicorp/zero'
22
import { createBuilder, createSchema, number, string, syncedQuery, table, Zero } from '@rocicorp/zero'
3-
import { describe, expect, it, vi } from 'vitest'
3+
import { describe, expect, it, onTestFinished, vi } from 'vitest'
44
import { nextTick, ref, watchEffect } from 'vue'
55
import { createZeroComposables } from './create-zero-composables'
66
import { useQuery } from './query'
@@ -32,23 +32,25 @@ async function setupTestEnvironment() {
3232
await z.value.mutate.table.insert({ a: 2, b: 'b' })
3333

3434
const builder = createBuilder(schema)
35-
const byIdQuery = syncedQuery
36-
? syncedQuery(
37-
'byId',
38-
([id]) => {
39-
if (typeof id !== 'number') {
40-
throw new TypeError('id must be a number')
41-
}
42-
return [id] as const
43-
},
44-
(id: number) => {
45-
return builder.table.where('a', id)
46-
},
47-
)
48-
: undefined
35+
const byIdQuery = syncedQuery(
36+
'byId',
37+
([id]) => {
38+
if (typeof id !== 'number') {
39+
throw new TypeError('id must be a number')
40+
}
41+
return [id] as const
42+
},
43+
(id: number) => {
44+
return builder.table.where('a', id)
45+
},
46+
)
4947

5048
const tableQuery = z!.value.query.table
5149

50+
onTestFinished(() => {
51+
z.value.close()
52+
})
53+
5254
return { z, tableQuery, useQuery, byIdQuery, userID }
5355
}
5456

@@ -93,8 +95,6 @@ describe('useQuery', () => {
9395

9496
// TODO: this is not working at the moment, possibly because we don't have a server connection in test
9597
// expect(resultType.value).toEqual("complete");
96-
97-
z.value.close()
9898
})
9999

100100
it('useQuery with ttl', async () => {
@@ -125,12 +125,10 @@ describe('useQuery', () => {
125125

126126
expect(materializeSpy).toHaveBeenCalledTimes(0)
127127
expect(updateTTLSpy).toHaveBeenCalledExactlyOnceWith('10m')
128-
129-
z.value.close()
130128
})
131129

132130
it('useQuery deps change', async () => {
133-
const { z, tableQuery, useQuery } = await setupTestEnvironment()
131+
const { tableQuery, useQuery } = await setupTestEnvironment()
134132

135133
const a = ref(1)
136134

@@ -186,8 +184,6 @@ describe('useQuery', () => {
186184

187185
expect(rowLog).toEqual([])
188186
// expect(resultDetailsLog).toEqual(["complete"]);
189-
190-
z.value.close()
191187
})
192188

193189
it('useQuery deps change watchEffect', async () => {
@@ -238,12 +234,10 @@ describe('useQuery', () => {
238234
run++
239235
})
240236
})
241-
242-
z.value.close()
243237
})
244238

245239
it('useQuery with syncedQuery', async () => {
246-
const { z, byIdQuery, useQuery } = await setupTestEnvironment()
240+
const { byIdQuery, useQuery } = await setupTestEnvironment()
247241
if (!byIdQuery) {
248242
return
249243
}
@@ -260,8 +254,6 @@ describe('useQuery', () => {
260254
},
261255
]`)
262256
expect(status.value).toEqual('unknown')
263-
264-
z.value.close()
265257
})
266258

267259
it('can still be used without createZero', async () => {

0 commit comments

Comments
 (0)