@@ -6,6 +6,7 @@ import { openPowerSync } from './utils';
66import { PowerSyncContext } from '../src/hooks/PowerSyncContext' ;
77import { useSyncStream , UseSyncStreamOptions } from '../src/hooks/streams' ;
88import { useQuery } from '../src/hooks/watched/useQuery' ;
9+ import { QuerySyncStreamOptions } from '../src/hooks/watched/watch-types' ;
910
1011describe ( 'stream hooks' , ( ) => {
1112 let db : AbstractPowerSyncDatabase ;
@@ -51,10 +52,13 @@ describe('stream hooks', () => {
5152 expect ( currentStreams ( ) ) . toStrictEqual ( [ ] ) ;
5253 } ) ;
5354
54- it ( 'useQuery' , async ( ) => {
55- const { result } = renderHook ( ( ) => useQuery ( 'SELECT 1' , [ ] , { streams : [ { name : 'a' } ] } ) , {
56- wrapper : testWrapper
57- } ) ;
55+ it ( 'useQuery waiting on stream' , async ( ) => {
56+ const { result } = renderHook (
57+ ( ) => useQuery ( 'SELECT 1' , [ ] , { streams : [ { name : 'a' , waitForStream : true } ] } ) ,
58+ {
59+ wrapper : testWrapper
60+ }
61+ ) ;
5862 expect ( result . current ) . toMatchObject ( { isLoading : true } ) ;
5963 // Including the stream should subscribe.
6064 await waitFor ( ( ) => expect ( currentStreams ( ) ) . toHaveLength ( 1 ) , { timeout : 1000 , interval : 100 } ) ;
@@ -68,6 +72,16 @@ describe('stream hooks', () => {
6872 await waitFor ( ( ) => expect ( result . current . data ) . toHaveLength ( 1 ) , { timeout : 1000 , interval : 100 } ) ;
6973 } ) ;
7074
75+ it ( 'useQuery not waiting on stream' , async ( ) => {
76+ // By default, it should still run the query immediately instead of waiting for the stream to resolve.
77+ const { result } = renderHook ( ( ) => useQuery ( 'SELECT 1' , [ ] , { streams : [ { name : 'a' } ] } ) , {
78+ wrapper : testWrapper
79+ } ) ;
80+
81+ // Not resolving the subscription.
82+ await waitFor ( ( ) => expect ( result . current . data ) . toHaveLength ( 1 ) , { timeout : 1000 , interval : 100 } ) ;
83+ } ) ;
84+
7185 it ( 'unsubscribes on unmount' , async ( ) => {
7286 const { unmount } = renderHook ( ( ) => useQuery ( 'SELECT 1' , [ ] , { streams : [ { name : 'a' } , { name : 'b' } ] } ) , {
7387 wrapper : testWrapper
@@ -80,7 +94,7 @@ describe('stream hooks', () => {
8094
8195 it ( 'handles stream parameter changes' , async ( ) => {
8296 // Start without streams
83- let streams : UseSyncStreamOptions [ ] = [ ] ;
97+ let streams : QuerySyncStreamOptions [ ] = [ ] ;
8498 let streamUpdateListeners : ( ( ) => void ) [ ] = [ ] ;
8599
86100 const { result } = renderHook (
@@ -108,7 +122,7 @@ describe('stream hooks', () => {
108122 await waitFor ( ( ) => expect ( result . current . data ) . toHaveLength ( 1 ) , { timeout : 1000 , interval : 100 } ) ;
109123
110124 // Adopt streams - this should reset back to loading
111- streams = [ { name : 'a' } ] ;
125+ streams = [ { name : 'a' , waitForStream : true } ] ;
112126 act ( ( ) => streamUpdateListeners . forEach ( ( cb ) => cb ( ) ) ) ;
113127 expect ( result . current ) . toMatchObject ( { isLoading : true } ) ;
114128
0 commit comments