@@ -644,7 +644,7 @@ describe('useQuery', () => {
644644
645645 await queryCache . prefetchQuery ( key , ( ) => 'prefetch' )
646646
647- await sleep ( 40 )
647+ await sleep ( 20 )
648648
649649 function FirstComponent ( ) {
650650 const state = useQuery ( key , ( ) => 'one' , {
@@ -656,7 +656,7 @@ describe('useQuery', () => {
656656
657657 function SecondComponent ( ) {
658658 const state = useQuery ( key , ( ) => 'two' , {
659- staleTime : 20 ,
659+ staleTime : 10 ,
660660 } )
661661 states2 . push ( state )
662662 return null
@@ -673,6 +673,8 @@ describe('useQuery', () => {
673673
674674 render ( < Page /> )
675675
676+ console . log ( states2 )
677+
676678 await waitFor ( ( ) => expect ( states1 . length ) . toBe ( 4 ) )
677679 await waitFor ( ( ) => expect ( states2 . length ) . toBe ( 4 ) )
678680
@@ -1220,6 +1222,62 @@ describe('useQuery', () => {
12201222 consoleMock . mockRestore ( )
12211223 } )
12221224
1225+ it ( 'should refetch after focus regain' , async ( ) => {
1226+ const key = queryKey ( )
1227+ const states : QueryResult < string > [ ] = [ ]
1228+ const consoleMock = mockConsoleError ( )
1229+
1230+ // make page unfocused
1231+ const originalVisibilityState = document . visibilityState
1232+ mockVisibilityState ( 'hidden' )
1233+
1234+ // set data in cache to check if the hook query fn is actually called
1235+ queryCache . setQueryData ( key , 'prefetched' )
1236+
1237+ function Page ( ) {
1238+ const state = useQuery ( key , ( ) => 'data' )
1239+ states . push ( state )
1240+ return null
1241+ }
1242+
1243+ render ( < Page /> )
1244+
1245+ await waitFor ( ( ) => expect ( states . length ) . toBe ( 2 ) )
1246+
1247+ act ( ( ) => {
1248+ // reset visibilityState to original value
1249+ mockVisibilityState ( originalVisibilityState )
1250+ window . dispatchEvent ( new FocusEvent ( 'focus' ) )
1251+ } )
1252+
1253+ await waitFor ( ( ) => expect ( states . length ) . toBe ( 4 ) )
1254+
1255+ expect ( states ) . toMatchObject ( [
1256+ {
1257+ data : 'prefetched' ,
1258+ isFetching : false ,
1259+ isStale : false ,
1260+ } ,
1261+ {
1262+ data : 'prefetched' ,
1263+ isFetching : false ,
1264+ isStale : true ,
1265+ } ,
1266+ {
1267+ data : 'prefetched' ,
1268+ isFetching : true ,
1269+ isStale : true ,
1270+ } ,
1271+ {
1272+ data : 'data' ,
1273+ isFetching : false ,
1274+ isStale : true ,
1275+ } ,
1276+ ] )
1277+
1278+ consoleMock . mockRestore ( )
1279+ } )
1280+
12231281 // See https://github.com/tannerlinsley/react-query/issues/195
12241282 it ( 'should refetch if stale after a prefetch' , async ( ) => {
12251283 const key = queryKey ( )
0 commit comments