@@ -5609,10 +5609,69 @@ describe('useQuery', () => {
56095609 onlineMock . mockRestore ( )
56105610 } )
56115611
5612- it ( 'online queries should fetch if paused and we go online even if already unmounted (because not cancelled) ' , async ( ) => {
5612+ it ( 'online queries should not fetch if paused initial load and we go online after unmount ' , async ( ) => {
56135613 const key = queryKey ( )
56145614 let count = 0
56155615
5616+ function Component ( ) {
5617+ const state = useQuery ( {
5618+ queryKey : key ,
5619+ queryFn : async ( { signal : _signal } ) => {
5620+ count ++
5621+ await sleep ( 10 )
5622+ return `signal${ count } `
5623+ } ,
5624+ } )
5625+
5626+ return (
5627+ < div >
5628+ < div >
5629+ status: { state . status } , fetchStatus: { state . fetchStatus }
5630+ </ div >
5631+ < div > data: { state . data } </ div >
5632+ </ div >
5633+ )
5634+ }
5635+
5636+ function Page ( ) {
5637+ const [ show , setShow ] = useState ( true )
5638+
5639+ return (
5640+ < div >
5641+ { show && < Component /> }
5642+ < button onClick = { ( ) => setShow ( false ) } > hide</ button >
5643+ </ div >
5644+ )
5645+ }
5646+
5647+ const onlineMock = mockOnlineManagerIsOnline ( false )
5648+
5649+ const rendered = renderWithClient ( queryClient , < Page /> )
5650+
5651+ rendered . getByText ( 'status: pending, fetchStatus: paused' )
5652+
5653+ fireEvent . click ( rendered . getByRole ( 'button' , { name : / h i d e / i } ) )
5654+
5655+ onlineMock . mockReturnValue ( true )
5656+ queryClient . getQueryCache ( ) . onOnline ( )
5657+
5658+ await vi . advanceTimersByTimeAsync ( 11 )
5659+ expect ( queryClient . getQueryState ( key ) ) . toMatchObject ( {
5660+ fetchStatus : 'idle' ,
5661+ status : 'pending' ,
5662+ } )
5663+
5664+ expect ( count ) . toBe ( 0 )
5665+
5666+ onlineMock . mockRestore ( )
5667+ } )
5668+
5669+ it ( 'online queries should re-fetch if paused and we go online even if already unmounted (because not cancelled)' , async ( ) => {
5670+ const key = queryKey ( )
5671+ let count = 0
5672+
5673+ queryClient . setQueryData ( key , 'initial' )
5674+
56165675 function Component ( ) {
56175676 const state = useQuery ( {
56185677 queryKey : key ,
@@ -5648,7 +5707,7 @@ describe('useQuery', () => {
56485707
56495708 const rendered = renderWithClient ( queryClient , < Page /> )
56505709
5651- rendered . getByText ( 'status: pending , fetchStatus: paused' )
5710+ rendered . getByText ( 'status: success , fetchStatus: paused' )
56525711
56535712 fireEvent . click ( rendered . getByRole ( 'button' , { name : / h i d e / i } ) )
56545713
@@ -5661,7 +5720,6 @@ describe('useQuery', () => {
56615720 status : 'success' ,
56625721 } )
56635722
5664- // give it a bit more time to make sure queryFn is not called again
56655723 expect ( count ) . toBe ( 1 )
56665724
56675725 onlineMock . mockRestore ( )
@@ -5722,17 +5780,16 @@ describe('useQuery', () => {
57225780 onlineMock . mockRestore ( )
57235781 } )
57245782
5725- it ( 'online queries should not fetch if paused and we go online if already unmounted when signal consumed ' , async ( ) => {
5783+ it ( 'online queries should fetch if paused and we go online even if already unmounted when refetch was not cancelled ' , async ( ) => {
57265784 const key = queryKey ( )
57275785 let count = 0
57285786
57295787 function Component ( ) {
57305788 const state = useQuery ( {
57315789 queryKey : key ,
5732- queryFn : async ( { signal : _signal } ) => {
5790+ queryFn : async ( ) => {
57335791 count ++
5734- await sleep ( 10 )
5735- return `signal${ count } `
5792+ return `data${ count } `
57365793 } ,
57375794 } )
57385795
@@ -5786,7 +5843,7 @@ describe('useQuery', () => {
57865843 status : 'success' ,
57875844 } )
57885845
5789- expect ( count ) . toBe ( 1 )
5846+ expect ( count ) . toBe ( 2 )
57905847
57915848 onlineMock . mockRestore ( )
57925849 } )
0 commit comments