11import type { TTL } from '@rocicorp/zero'
2- import { createSchema , number , string , table , Zero } from '@rocicorp/zero'
2+ import { createBuilder , createSchema , number , string , syncedQuery , table , Zero } from '@rocicorp/zero'
33import { describe , expect , it , vi } from 'vitest'
4- import { ref , watchEffect } from 'vue'
4+ import { createApp , nextTick , onMounted , ref , shallowRef , watchEffect } from 'vue'
5+ import { createUseZero } from './create-use-zero'
6+ import { createZero } from './create-zero'
57import { useQuery } from './query'
68import { VueView , vueViewFactory } from './view'
79
10+ export function withSetup < T > ( composable : ( ) => T ) {
11+ const result = shallowRef < T > ( )
12+ const app = createApp ( {
13+ setup ( ) {
14+ onMounted ( ( ) => {
15+ result . value = composable ( )
16+ } )
17+
18+ return ( ) => { }
19+ } ,
20+ } )
21+ return [ result , app ] as const
22+ }
23+
824async function setupTestEnvironment ( ) {
925 const schema = createSchema ( {
1026 tables : [
@@ -16,22 +32,41 @@ async function setupTestEnvironment() {
1632 . primaryKey ( 'a' ) ,
1733 ] ,
1834 } )
35+ const builder = createBuilder ( schema )
1936
20- const z = new Zero ( {
37+ const useZero = createUseZero < typeof schema > ( )
38+ const [ zero , app ] = withSetup ( useZero )
39+ app . use ( createZero ( {
2140 userID : 'asdf' ,
2241 server : null ,
2342 schema,
2443 // This is often easier to develop with if you're frequently changing
2544 // the schema. Switch to 'idb' for local-persistence.
2645 kvStore : 'mem' ,
27- } )
46+ } ) )
47+ app . mount ( document . createElement ( 'div' ) )
48+
49+ const z = zero . value ! . value
2850
2951 await z . mutate . table . insert ( { a : 1 , b : 'a' } )
3052 await z . mutate . table . insert ( { a : 2 , b : 'b' } )
3153
54+ const byIdQuery = syncedQuery (
55+ 'byId' ,
56+ ( [ id ] ) => {
57+ if ( typeof id !== 'number' ) {
58+ throw new TypeError ( 'id must be a number' )
59+ }
60+ return [ id ] as const
61+ } ,
62+ ( id : number ) => {
63+ return builder . table . where ( 'a' , id )
64+ } ,
65+ )
66+
3267 const tableQuery = z . query . table
3368
34- return { z, tableQuery }
69+ return { z, tableQuery, byIdQuery }
3570}
3671
3772describe ( 'useQuery' , ( ) => {
@@ -112,7 +147,7 @@ describe('useQuery', () => {
112147 z . close ( )
113148 } )
114149
115- it ( 'useQuery with ttl (zero@0.19)' , async ( ) => {
150+ it . only ( 'useQuery with ttl (zero@0.19)' , async ( ) => {
116151 const { z, tableQuery } = await setupTestEnvironment ( )
117152 if ( 'updateTTL' in tableQuery ) {
118153 // 0.19 removed updateTTL from the query
@@ -258,4 +293,23 @@ describe('useQuery', () => {
258293
259294 z . close ( )
260295 } )
296+
297+ it . skip ( 'useQuery with syncedQuery' , async ( ) => {
298+ const { z, byIdQuery } = await setupTestEnvironment ( )
299+
300+ const a = ref ( 1 )
301+ const { data : rows , status } = useQuery ( ( ) => byIdQuery ( a . value ) )
302+
303+ expect ( rows . value ) . toMatchInlineSnapshot ( `
304+ [
305+ {
306+ "a": 1,
307+ "b": "a",
308+ Symbol(rc): 1,
309+ },
310+ ]` )
311+ expect ( status . value ) . toEqual ( 'unknown' )
312+
313+ z . close ( )
314+ } )
261315} )
0 commit comments