@@ -2,7 +2,7 @@ import * as commonSdk from '@powersync/common';
22import { PowerSyncDatabase } from '@powersync/web' ;
33import { act , cleanup , renderHook , waitFor } from '@testing-library/react' ;
44import pDefer from 'p-defer' ;
5- import React from 'react' ;
5+ import React , { useEffect } from 'react' ;
66import { beforeEach , describe , expect , it , onTestFinished , vi } from 'vitest' ;
77import { PowerSyncContext } from '../src/hooks/PowerSyncContext' ;
88import { useQuery } from '../src/hooks/watched/useQuery' ;
@@ -168,6 +168,64 @@ describe('useQuery', () => {
168168 ) ;
169169 } ) ;
170170
171+ it ( 'should react to updated queries' , async ( ) => {
172+ const db = openPowerSync ( ) ;
173+
174+ const wrapper = ( { children } ) => < PowerSyncContext . Provider value = { db } > { children } </ PowerSyncContext . Provider > ;
175+
176+ let updateParameters = ( params : string [ ] ) : void => { } ;
177+ const newParametersPromise = new Promise < string [ ] > ( ( resolve ) => {
178+ updateParameters = resolve ;
179+ } ) ;
180+
181+ await db . execute ( /* sql */ `
182+ INSERT INTO
183+ lists (id, name)
184+ VALUES
185+ (uuid (), 'first'),
186+ (uuid (), 'second')
187+ ` ) ;
188+
189+ const query = ( ) => {
190+ const [ parameters , setParameters ] = React . useState < string [ ] > ( [ 'first' ] ) ;
191+
192+ useEffect ( ( ) => {
193+ // allow updating the parameters externally
194+ newParametersPromise . then ( ( params ) => setParameters ( params ) ) ;
195+ } , [ ] ) ;
196+
197+ const query = React . useMemo ( ( ) => {
198+ return {
199+ execute : ( ) => db . getAll < { name : string } > ( 'SELECT * FROM lists WHERE name = ?' , parameters ) ,
200+ compile : ( ) => ( { sql : 'SELECT * FROM lists WHERE name = ?' , parameters } )
201+ } ;
202+ } , [ parameters ] ) ;
203+
204+ return useQuery ( query ) ;
205+ } ;
206+
207+ const { result } = renderHook ( query , { wrapper } ) ;
208+
209+ // We should only receive the first list due to the WHERE clause
210+ await vi . waitFor (
211+ ( ) => {
212+ expect ( result . current . data [ 0 ] ?. name ) . toEqual ( 'first' ) ;
213+ } ,
214+ { timeout : 500 , interval : 100 }
215+ ) ;
216+
217+ // Now update the parameter
218+ updateParameters ( [ 'second' ] ) ;
219+
220+ // We should now only receive the second list due to the WHERE clause and updated parameter
221+ await vi . waitFor (
222+ ( ) => {
223+ expect ( result . current . data [ 0 ] ?. name ) . toEqual ( 'second' ) ;
224+ } ,
225+ { timeout : 500 , interval : 100 }
226+ ) ;
227+ } ) ;
228+
171229 it ( 'should show an error if parsing the query results in an error' , async ( ) => {
172230 const db = openPowerSync ( ) ;
173231
0 commit comments