@@ -1267,103 +1267,100 @@ describe('createQuery', () => {
12671267 } ) ,
12681268 )
12691269
1270- it (
1271- 'should be able to set different stale times for a query' ,
1272- async ( ) => {
1273- const key = [ 'test-key' ]
1274- const states1 : Array < CreateQueryResult < string > > = [ ]
1275- const states2 : Array < CreateQueryResult < string > > = [ ]
1270+ it ( 'should be able to set different stale times for a query' , async ( ) => {
1271+ const key = [ 'test-key' ]
1272+ const states1 : Array < CreateQueryResult < string > > = [ ]
1273+ const states2 : Array < CreateQueryResult < string > > = [ ]
12761274
1277- // Prefetch the query
1278- const prefetchPromise = queryClient . prefetchQuery ( {
1279- queryKey : key ,
1280- queryFn : async ( ) => {
1281- await sleep ( 10 )
1282- return 'prefetch'
1283- } ,
1275+ // Prefetch the query
1276+ const prefetchPromise = queryClient . prefetchQuery ( {
1277+ queryKey : key ,
1278+ queryFn : async ( ) => {
1279+ await sleep ( 10 )
1280+ return 'prefetch'
1281+ } ,
1282+ } )
1283+ await vi . advanceTimersByTimeAsync ( 10 )
1284+ await prefetchPromise
1285+
1286+ expect ( queryClient . getQueryState ( key ) ?. data ) . toBe ( 'prefetch' )
1287+ // Advance time so secondQuery (staleTime: 10) sees prefetched data as stale
1288+ await vi . advanceTimersByTimeAsync ( 11 )
1289+
1290+ await withEffectRoot ( async ( ) => {
1291+ const firstQuery = createQuery < string > (
1292+ ( ) => ( {
1293+ queryKey : key ,
1294+ queryFn : ( ) => Promise . resolve ( 'one' ) ,
1295+ staleTime : 100 ,
1296+ } ) ,
1297+ ( ) => queryClient ,
1298+ )
1299+
1300+ $effect ( ( ) => {
1301+ states1 . push ( { ...firstQuery } )
12841302 } )
1285- await vi . advanceTimersByTimeAsync ( 10 )
1286- await prefetchPromise
1287-
1288- expect ( queryClient . getQueryState ( key ) ?. data ) . toBe ( 'prefetch' )
1289- // Advance time so secondQuery (staleTime: 10) sees prefetched data as stale
1290- await vi . advanceTimersByTimeAsync ( 11 )
1291-
1292- await withEffectRoot ( async ( ) => {
1293- const firstQuery = createQuery < string > (
1294- ( ) => ( {
1295- queryKey : key ,
1296- queryFn : ( ) => Promise . resolve ( 'one' ) ,
1297- staleTime : 100 ,
1298- } ) ,
1299- ( ) => queryClient ,
1300- )
1301-
1302- $effect ( ( ) => {
1303- states1 . push ( { ...firstQuery } )
1304- } )
1305-
1306- const secondQuery = createQuery < string > (
1307- ( ) => ( {
1308- queryKey : key ,
1309- queryFn : ( ) => Promise . resolve ( 'two' ) ,
1310- staleTime : 10 ,
1311- } ) ,
1312- ( ) => queryClient ,
1313- )
1314-
1315- $effect ( ( ) => {
1316- states2 . push ( { ...secondQuery } )
1317- } )
1318-
1319- // Wait for both staleTime to expire (100ms for firstQuery)
1320- await vi . advanceTimersByTimeAsync ( 101 )
1321- expect ( firstQuery ) . toMatchObject ( { data : 'two' , isStale : true } )
1322- expect ( secondQuery ) . toMatchObject ( { data : 'two' , isStale : true } )
1323-
1324- expect ( states1 ) . toMatchObject ( [
1325- // First render
1326- {
1327- data : 'prefetch' ,
1328- isStale : false ,
1329- } ,
1330- // Second createQuery started fetching
1331- {
1332- data : 'prefetch' ,
1333- isStale : false ,
1334- } ,
1335- // Second createQuery data came in
1336- {
1337- data : 'two' ,
1338- isStale : false ,
1339- } ,
1340- // Data became stale after 100ms
1341- {
1342- data : 'two' ,
1343- isStale : true ,
1344- } ,
1345- ] )
13461303
1347- expect ( states2 ) . toMatchObject ( [
1348- // First render, data is stale and starts fetching
1349- {
1350- data : 'prefetch' ,
1351- isStale : true ,
1352- } ,
1353- // Second createQuery data came in
1354- {
1355- data : 'two' ,
1356- isStale : false ,
1357- } ,
1358- // Data became stale after 10ms
1359- {
1360- data : 'two' ,
1361- isStale : true ,
1362- } ,
1363- ] )
1364- } ) ( )
1365- } ,
1366- )
1304+ const secondQuery = createQuery < string > (
1305+ ( ) => ( {
1306+ queryKey : key ,
1307+ queryFn : ( ) => Promise . resolve ( 'two' ) ,
1308+ staleTime : 10 ,
1309+ } ) ,
1310+ ( ) => queryClient ,
1311+ )
1312+
1313+ $effect ( ( ) => {
1314+ states2 . push ( { ...secondQuery } )
1315+ } )
1316+
1317+ // Wait for both staleTime to expire (100ms for firstQuery)
1318+ await vi . advanceTimersByTimeAsync ( 101 )
1319+ expect ( firstQuery ) . toMatchObject ( { data : 'two' , isStale : true } )
1320+ expect ( secondQuery ) . toMatchObject ( { data : 'two' , isStale : true } )
1321+
1322+ expect ( states1 ) . toMatchObject ( [
1323+ // First render
1324+ {
1325+ data : 'prefetch' ,
1326+ isStale : false ,
1327+ } ,
1328+ // Second createQuery started fetching
1329+ {
1330+ data : 'prefetch' ,
1331+ isStale : false ,
1332+ } ,
1333+ // Second createQuery data came in
1334+ {
1335+ data : 'two' ,
1336+ isStale : false ,
1337+ } ,
1338+ // Data became stale after 100ms
1339+ {
1340+ data : 'two' ,
1341+ isStale : true ,
1342+ } ,
1343+ ] )
1344+
1345+ expect ( states2 ) . toMatchObject ( [
1346+ // First render, data is stale and starts fetching
1347+ {
1348+ data : 'prefetch' ,
1349+ isStale : true ,
1350+ } ,
1351+ // Second createQuery data came in
1352+ {
1353+ data : 'two' ,
1354+ isStale : false ,
1355+ } ,
1356+ // Data became stale after 10ms
1357+ {
1358+ data : 'two' ,
1359+ isStale : true ,
1360+ } ,
1361+ ] )
1362+ } ) ( )
1363+ } )
13671364
13681365 it (
13691366 'should re-render when a query becomes stale' ,
0 commit comments