11import type { TTL } from '@rocicorp/zero'
22import type { MockInstance } from 'vitest'
3- import { createBuilder , createSchema , number , string , syncedQuery , table } from '@rocicorp/zero'
3+ import type { ShallowRef } from 'vue'
4+ import { createBuilder , createSchema , number , string , syncedQuery , table , Zero } from '@rocicorp/zero'
45import { describe , expect , it , vi } from 'vitest'
5- import { computed , createApp , nextTick , onMounted , ref , shallowRef , watchEffect } from 'vue'
6+ import { computed , createApp , inject , onMounted , ref , shallowRef , watchEffect } from 'vue'
67import { createUseZero } from './create-use-zero'
7- import { createZero } from './create-zero'
8+ import { createZero , zeroSymbol } from './create-zero'
89import { useQuery } from './query'
910import { VueView , vueViewFactory } from './view'
1011
@@ -22,7 +23,7 @@ export function withSetup<T>(composable: () => T) {
2223 return [ result , app ] as const
2324}
2425
25- async function setupTestEnvironment ( ) {
26+ async function setupTestEnvironment ( registerPlugin = true ) {
2627 const schema = createSchema ( {
2728 tables : [
2829 table ( 'table' )
@@ -34,21 +35,37 @@ async function setupTestEnvironment() {
3435 ] ,
3536 } )
3637
37- const useZero = createUseZero < typeof schema > ( )
38- const [ zero , app ] = withSetup ( useZero )
38+ let zero : ShallowRef < ShallowRef < Zero < typeof schema > | undefined > | undefined | void >
39+
3940 const userID = ref ( 'asdf' )
40- app . use ( createZero ( ( ) => ( {
41- userID : userID . value ,
42- server : null ,
43- schema,
44- kvStore : 'mem' ,
45- } ) ) )
41+ const useZero = createUseZero < typeof schema > ( )
42+
43+ const setupResult = withSetup ( registerPlugin ? useZero : ( ) => { } )
44+ if ( setupResult [ 0 ] ) {
45+ zero = setupResult [ 0 ]
46+ }
47+ const app = setupResult [ 1 ]
48+
49+ if ( registerPlugin ) {
50+ app . use ( createZero ( ( ) => ( {
51+ userID : userID . value ,
52+ server : null ,
53+ schema,
54+ kvStore : 'mem' ,
55+ } ) ) )
56+ }
4657 app . mount ( document . createElement ( 'div' ) )
4758
48- const z = computed ( ( ) => zero . value ! . value )
59+ const z = computed ( ( ) => {
60+ if ( zero ?. value ) {
61+ return zero . value ! . value !
62+ }
63+
64+ return new Zero ( { userID : 'asdf' , server : null , schema, kvStore : 'mem' } )
65+ } )
4966
50- await z ! . value . mutate . table . insert ( { a : 1 , b : 'a' } )
51- await z ! . value . mutate . table . insert ( { a : 2 , b : 'b' } )
67+ await z . value . mutate . table . insert ( { a : 1 , b : 'a' } )
68+ await z . value . mutate . table . insert ( { a : 2 , b : 'b' } )
5269
5370 const builder = createBuilder ( schema )
5471 const byIdQuery = syncedQuery
@@ -74,7 +91,7 @@ async function setupTestEnvironment() {
7491describe ( 'useQuery' , ( ) => {
7592 it ( 'useQuery' , async ( ) => {
7693 const { z, tableQuery, app } = await setupTestEnvironment ( )
77- await app . runWithContext ( async ( ) => {
94+ await app ! . runWithContext ( async ( ) => {
7895 const { data : rows , status } = useQuery ( ( ) => tableQuery )
7996 expect ( rows . value ) . toMatchInlineSnapshot ( `[
8097 {
@@ -125,7 +142,7 @@ describe('useQuery', () => {
125142 return
126143 }
127144
128- await app . runWithContext ( async ( ) => {
145+ await app ! . runWithContext ( async ( ) => {
129146 const ttl = ref < TTL > ( '1m' )
130147
131148 const materializeSpy = vi . spyOn ( tableQuery , 'materialize' )
@@ -160,7 +177,7 @@ describe('useQuery', () => {
160177 return
161178 }
162179
163- await app . runWithContext ( async ( ) => {
180+ await app ! . runWithContext ( async ( ) => {
164181 const ttl = ref < TTL > ( '1m' )
165182
166183 let materializeSpy : MockInstance
@@ -211,7 +228,7 @@ describe('useQuery', () => {
211228 it ( 'useQuery deps change' , async ( ) => {
212229 const { z, tableQuery, app } = await setupTestEnvironment ( )
213230
214- await app . runWithContext ( async ( ) => {
231+ await app ! . runWithContext ( async ( ) => {
215232 const a = ref ( 1 )
216233
217234 const { data : rows , status } = useQuery ( ( ) =>
@@ -273,7 +290,7 @@ describe('useQuery', () => {
273290
274291 it ( 'useQuery deps change watchEffect' , async ( ) => {
275292 const { z, tableQuery, app } = await setupTestEnvironment ( )
276- await app . runWithContext ( async ( ) => {
293+ await app ! . runWithContext ( async ( ) => {
277294 const a = ref ( 1 )
278295 const { data : rows } = useQuery ( ( ) => tableQuery . where ( 'a' , a . value ) )
279296
@@ -331,7 +348,7 @@ describe('useQuery', () => {
331348 return
332349 }
333350
334- app . runWithContext ( ( ) => {
351+ app ! . runWithContext ( ( ) => {
335352 const a = ref ( 1 )
336353 const { data : rows , status } = useQuery ( ( ) => byIdQuery ( a . value ) )
337354
@@ -349,8 +366,8 @@ describe('useQuery', () => {
349366 } )
350367 } )
351368
352- it ( 'useQuery can be used without plugin (dropped in future versions)' , async ( ) => {
353- const { z, tableQuery } = await setupTestEnvironment ( )
369+ it ( 'useQuery can be used without plugin (will be dropped in future versions)' , async ( ) => {
370+ const { z, tableQuery, app } = await setupTestEnvironment ( false )
354371
355372 const { data : rows , status } = useQuery ( ( ) => tableQuery )
356373 expect ( rows . value ) . toMatchInlineSnapshot ( `[
@@ -367,6 +384,12 @@ describe('useQuery', () => {
367384]` )
368385 expect ( status . value ) . toEqual ( 'unknown' )
369386
387+ app . runWithContext ( ( ) => {
388+ if ( zeroSymbol ) {
389+ expect ( inject ( zeroSymbol , null ) ) . toBeNull ( )
390+ }
391+ } )
392+
370393 z . value . close ( )
371394 } )
372395} )
0 commit comments