@@ -205,7 +205,7 @@ describe('useQuery', () => {
205205 expect ( secondFetchingEvent ?. hookResults . isFetching ) . true ;
206206 } ) ;
207207
208- it ( 'should react to updated queries (fast update )' , async ( ) => {
208+ it ( 'should react to updated queries (many updates )' , async ( ) => {
209209 const db = openPowerSync ( ) ;
210210
211211 await db . execute ( /* sql */ `
@@ -359,6 +359,108 @@ describe('useQuery', () => {
359359 ) ;
360360 } ) ;
361361
362+ it ( 'should react to updated queries (immediate updates)' , async ( ) => {
363+ const db = openPowerSync ( ) ;
364+
365+ await db . execute ( /* sql */ `
366+ INSERT INTO
367+ lists (id, name)
368+ VALUES
369+ (uuid (), 'first'),
370+ (uuid (), 'second'),
371+ (uuid (), 'third')
372+ ` ) ;
373+
374+ type TestEvent = {
375+ parameters : number [ ] ;
376+ hookResults : QueryResult < any > ;
377+ } ;
378+
379+ const hookEvents : TestEvent [ ] = [ ] ;
380+
381+ const baseQuery = 'SELECT * FROM lists LIMIT ?' ;
382+
383+ const query = ( ) => {
384+ const [ query , setQuery ] = React . useState ( {
385+ sql : baseQuery ,
386+ params : [ 1 ]
387+ } ) ;
388+
389+ // Change the params after the first render
390+ useEffect ( ( ) => {
391+ setQuery ( {
392+ sql : baseQuery ,
393+ params : [ 2 ]
394+ } ) ;
395+ } , [ ] ) ;
396+
397+ const result = useQuery ( query . sql , query . params ) ;
398+ hookEvents . push ( {
399+ parameters : query . params ,
400+ hookResults : result
401+ } ) ;
402+ return result ;
403+ } ;
404+
405+ const { result } = renderHook ( query , { wrapper : ( { children } ) => testWrapper ( { children, db } ) } ) ;
406+ // Wait for the first result to be emitted
407+ await vi . waitFor (
408+ ( ) => {
409+ expect ( result . current . data . length ) . toEqual ( 2 ) ;
410+ expect ( result . current . isFetching ) . false ;
411+ expect ( result . current . isLoading ) . false ;
412+ } ,
413+ { timeout : 500 , interval : 100 }
414+ ) ;
415+
416+ console . log ( JSON . stringify ( hookEvents ) ) ;
417+
418+ // We changed the params before the initial query could execute (we changed the params immediately)
419+ // We should not see isLoading=false for the first set of params
420+ expect (
421+ hookEvents . find (
422+ ( event ) =>
423+ event . parameters [ 0 ] == 1 &&
424+ ( event . hookResults . isLoading == false || event . hookResults . isFetching == false )
425+ )
426+ ) . toBeUndefined ( ) ;
427+ // We should have an event where this was both loading and fetching
428+ expect (
429+ hookEvents . find (
430+ ( event ) =>
431+ event . parameters [ 0 ] == 1 && event . hookResults . isLoading == true && event . hookResults . isFetching == true
432+ )
433+ ) . toBeDefined ( ) ;
434+
435+ // We should not have any event where isLoading or isFetching is false without data being presented
436+ expect (
437+ hookEvents . find (
438+ ( event ) =>
439+ event . parameters [ 0 ] == 1 &&
440+ ( event . hookResults . isLoading || event . hookResults . isFetching ) == false &&
441+ event . hookResults . data . length != 1
442+ )
443+ ) . toBeUndefined ( ) ;
444+
445+ expect (
446+ hookEvents . find (
447+ ( event ) =>
448+ event . parameters [ 0 ] == 2 &&
449+ ( event . hookResults . isLoading || event . hookResults . isFetching ) == false &&
450+ event . hookResults . data . length != 2
451+ )
452+ ) . toBeUndefined ( ) ;
453+
454+ expect (
455+ hookEvents . find (
456+ ( event ) =>
457+ event . parameters [ 0 ] == 2 &&
458+ ( event . hookResults . isLoading && event . hookResults . isFetching ) == false &&
459+ event . hookResults . data . length == 2
460+ )
461+ ) . toBeDefined ( ) ;
462+ } ) ;
463+
362464 it ( 'should execute compatible queries' , async ( ) => {
363465 const db = openPowerSync ( ) ;
364466
0 commit comments