@@ -581,42 +581,43 @@ describe('queryCache', () => {
581581 const callback = jest . fn ( )
582582 const testCache = new QueryCache ( )
583583 const testClient = new QueryClient ( { cache : testCache } )
584- const observer = testClient . watchQuery ( key )
584+ const observer = testClient . watchQuery ( key , { enabled : false } )
585585 const unsubscribe = observer . subscribe ( callback )
586586 await testClient . fetchQueryData ( key , queryFn )
587587 unsubscribe ( )
588588 testCache . clear ( )
589589 expect ( queryFn ) . toHaveBeenCalledTimes ( 1 )
590- expect ( callback ) . toHaveBeenCalledTimes ( 1 )
590+ expect ( callback ) . toHaveBeenCalledTimes ( 2 )
591591 } )
592592
593593 test ( 'watchQuery should accept unresolved query config in update function' , async ( ) => {
594594 const key = queryKey ( )
595595 const queryFn = jest . fn ( )
596596 const testCache = new QueryCache ( )
597597 const testClient = new QueryClient ( { cache : testCache } )
598- const observer = testClient . watchQuery ( key )
598+ const observer = testClient . watchQuery ( key , { enabled : false } )
599599 const results : QueryObserverResult < unknown > [ ] = [ ]
600600 const unsubscribe = observer . subscribe ( x => {
601601 results . push ( x )
602602 } )
603- observer . setOptions ( { staleTime : 10 } )
603+ observer . setOptions ( { enabled : false , staleTime : 10 } )
604604 await testClient . fetchQueryData ( key , queryFn )
605605 await sleep ( 100 )
606606 unsubscribe ( )
607607 testCache . clear ( )
608608 expect ( queryFn ) . toHaveBeenCalledTimes ( 1 )
609- expect ( results . length ) . toBe ( 2 )
610- expect ( results [ 0 ] ) . toMatchObject ( { isStale : false } )
611- expect ( results [ 1 ] ) . toMatchObject ( { isStale : true } )
609+ expect ( results . length ) . toBe ( 3 )
610+ expect ( results [ 0 ] ) . toMatchObject ( { isStale : true } )
611+ expect ( results [ 1 ] ) . toMatchObject ( { isStale : false } )
612+ expect ( results [ 2 ] ) . toMatchObject ( { isStale : true } )
612613 } )
613614
614615 test ( 'watchQuery should be able to handle multiple subscribers' , async ( ) => {
615616 const key = queryKey ( )
616617 const queryFn = jest . fn ( ) . mockReturnValue ( 'data' )
617618 const testCache = new QueryCache ( )
618619 const testClient = new QueryClient ( { cache : testCache } )
619- const observer = testClient . watchQuery < string > ( key )
620+ const observer = testClient . watchQuery < string > ( key , { enabled : false } )
620621 const results1 : QueryObserverResult < string > [ ] = [ ]
621622 const results2 : QueryObserverResult < string > [ ] = [ ]
622623 const unsubscribe1 = observer . subscribe ( x => {
@@ -631,18 +632,20 @@ describe('queryCache', () => {
631632 unsubscribe2 ( )
632633 testCache . clear ( )
633634 expect ( queryFn ) . toHaveBeenCalledTimes ( 1 )
634- expect ( results1 . length ) . toBe ( 1 )
635- expect ( results2 . length ) . toBe ( 1 )
636- expect ( results1 [ 0 ] ) . toMatchObject ( { data : 'data' } )
637- expect ( results1 [ 0 ] ) . toMatchObject ( { data : 'data' } )
635+ expect ( results1 . length ) . toBe ( 2 )
636+ expect ( results2 . length ) . toBe ( 2 )
637+ expect ( results1 [ 0 ] ) . toMatchObject ( { data : undefined } )
638+ expect ( results1 [ 1 ] ) . toMatchObject ( { data : 'data' } )
639+ expect ( results2 [ 0 ] ) . toMatchObject ( { data : undefined } )
640+ expect ( results2 [ 1 ] ) . toMatchObject ( { data : 'data' } )
638641 } )
639642
640643 test ( 'watchQuery should be able to resolve a promise' , async ( ) => {
641644 const key = queryKey ( )
642645 const queryFn = jest . fn ( ) . mockReturnValue ( 'data' )
643646 const testCache = new QueryCache ( )
644647 const testClient = new QueryClient ( { cache : testCache } )
645- const observer = testClient . watchQuery < string > ( key )
648+ const observer = testClient . watchQuery < string > ( key , { enabled : false } )
646649 let value
647650 observer . getNextResult ( ) . then ( x => {
648651 value = x
@@ -659,7 +662,7 @@ describe('queryCache', () => {
659662 const key = queryKey ( )
660663 const testCache = new QueryCache ( )
661664 const testClient = new QueryClient ( { cache : testCache } )
662- const observer = testClient . watchQuery < string > ( key )
665+ const observer = testClient . watchQuery < string > ( key , { enabled : false } )
663666 let error
664667 observer . getNextResult ( { throwOnError : true } ) . catch ( e => {
665668 error = e
@@ -731,8 +734,8 @@ describe('queryCache', () => {
731734 const testClient = new QueryClient ( { cache : testCache } )
732735 await testClient . fetchQueryData ( key1 , queryFn1 )
733736 await testClient . fetchQueryData ( key2 , queryFn2 )
734- const observer1 = testClient . watchQuery ( key1 )
735- const observer2 = testClient . watchQuery ( key2 )
737+ const observer1 = testClient . watchQuery ( key1 , { enabled : false } )
738+ const observer2 = testClient . watchQuery ( key2 , { enabled : false } )
736739 observer1 . subscribe ( )
737740 observer2 . subscribe ( )
738741 await testClient . refetchQueries ( )
@@ -752,7 +755,9 @@ describe('queryCache', () => {
752755 const testClient = new QueryClient ( { cache : testCache } )
753756 await testClient . fetchQueryData ( key1 , queryFn1 )
754757 await testClient . fetchQueryData ( key2 , queryFn2 )
755- const observer = testClient . watchQuery ( key1 , { staleTime : Infinity } )
758+ const observer = testClient . watchQuery ( key1 , queryFn1 , {
759+ staleTime : Infinity ,
760+ } )
756761 const unsubscribe = observer . subscribe ( )
757762 await testClient . refetchQueries ( { active : true , stale : false } )
758763 unsubscribe ( )
@@ -770,7 +775,7 @@ describe('queryCache', () => {
770775 const testClient = new QueryClient ( { cache : testCache } )
771776 await testClient . fetchQueryData ( key1 , queryFn1 )
772777 await testClient . fetchQueryData ( key2 , queryFn2 )
773- const observer = testClient . watchQuery ( key1 )
778+ const observer = testClient . watchQuery ( key1 , queryFn1 )
774779 const unsubscribe = observer . subscribe ( )
775780 testClient . invalidateQueries ( key1 )
776781 await testClient . refetchQueries ( { stale : true } )
@@ -790,7 +795,7 @@ describe('queryCache', () => {
790795 await testClient . fetchQueryData ( key1 , queryFn1 )
791796 await testClient . fetchQueryData ( key2 , queryFn2 )
792797 testClient . invalidateQueries ( key1 )
793- const observer = testClient . watchQuery ( key1 )
798+ const observer = testClient . watchQuery ( key1 , queryFn1 )
794799 const unsubscribe = observer . subscribe ( )
795800 await testClient . refetchQueries ( { active : true , stale : true } )
796801 unsubscribe ( )
@@ -808,7 +813,9 @@ describe('queryCache', () => {
808813 const testClient = new QueryClient ( { cache : testCache } )
809814 await testClient . fetchQueryData ( key1 , queryFn1 )
810815 await testClient . fetchQueryData ( key2 , queryFn2 )
811- const observer = testClient . watchQuery ( key1 , { staleTime : Infinity } )
816+ const observer = testClient . watchQuery ( key1 , queryFn1 , {
817+ staleTime : Infinity ,
818+ } )
812819 const unsubscribe = observer . subscribe ( )
813820 await testClient . refetchQueries ( )
814821 unsubscribe ( )
@@ -826,7 +833,9 @@ describe('queryCache', () => {
826833 const testClient = new QueryClient ( { cache : testCache } )
827834 await testClient . fetchQueryData ( key1 , queryFn1 )
828835 await testClient . fetchQueryData ( key2 , queryFn2 )
829- const observer = testClient . watchQuery ( key1 , { staleTime : Infinity } )
836+ const observer = testClient . watchQuery ( key1 , queryFn1 , {
837+ staleTime : Infinity ,
838+ } )
830839 const unsubscribe = observer . subscribe ( )
831840 await testClient . refetchQueries ( { active : true , inactive : true } )
832841 unsubscribe ( )
@@ -1186,7 +1195,7 @@ describe('queryCache', () => {
11861195 await sleep ( 10 )
11871196
11881197 // Subscribe and unsubscribe to simulate cancellation because the last observer unsubscribed
1189- const observer = client . watchQuery ( key )
1198+ const observer = client . watchQuery ( key , { enabled : false } )
11901199 const unsubscribe = observer . subscribe ( )
11911200 unsubscribe ( )
11921201
@@ -1220,7 +1229,7 @@ describe('queryCache', () => {
12201229 await sleep ( 10 )
12211230
12221231 // Subscribe and unsubscribe to simulate cancellation because the last observer unsubscribed
1223- const observer = client . watchQuery ( key )
1232+ const observer = client . watchQuery ( key , { enabled : false } )
12241233 const unsubscribe = observer . subscribe ( )
12251234 unsubscribe ( )
12261235
0 commit comments