File tree Expand file tree Collapse file tree 1 file changed +80
-0
lines changed
Expand file tree Collapse file tree 1 file changed +80
-0
lines changed Original file line number Diff line number Diff line change @@ -672,6 +672,86 @@ describe('useQuery', () => {
672672 } )
673673 } )
674674
675+ it ( 'should keep the previous data on disabled query when keepPreviousData is set and switching query key multiple times' , async ( ) => {
676+ const key = queryKey ( )
677+ const states : QueryResult < number > [ ] = [ ]
678+
679+ queryCache . setQueryData ( [ key , 10 ] , 10 )
680+
681+ await sleep ( 10 )
682+
683+ function Page ( ) {
684+ const [ count , setCount ] = React . useState ( 10 )
685+
686+ const state = useQuery (
687+ [ key , count ] ,
688+ async ( ) => {
689+ await sleep ( 10 )
690+ return count
691+ } ,
692+ { enabled : false , keepPreviousData : true }
693+ )
694+
695+ states . push ( state )
696+
697+ const { refetch } = state
698+
699+ React . useEffect ( ( ) => {
700+ setTimeout ( ( ) => {
701+ setCount ( 11 )
702+ } , 20 )
703+ setTimeout ( ( ) => {
704+ setCount ( 12 )
705+ } , 30 )
706+ setTimeout ( ( ) => {
707+ refetch ( )
708+ } , 40 )
709+ } , [ refetch ] )
710+
711+ return null
712+ }
713+
714+ render ( < Page /> )
715+
716+ await waitFor ( ( ) => expect ( states . length ) . toBe ( 5 ) )
717+
718+ // Disabled query
719+ expect ( states [ 0 ] ) . toMatchObject ( {
720+ data : 10 ,
721+ isFetching : false ,
722+ isSuccess : true ,
723+ isPreviousData : false ,
724+ } )
725+ // Switched query key
726+ expect ( states [ 1 ] ) . toMatchObject ( {
727+ data : 10 ,
728+ isFetching : false ,
729+ isSuccess : true ,
730+ isPreviousData : true ,
731+ } )
732+ // Switched query key
733+ expect ( states [ 2 ] ) . toMatchObject ( {
734+ data : 10 ,
735+ isFetching : false ,
736+ isSuccess : true ,
737+ isPreviousData : true ,
738+ } )
739+ // Refetch
740+ expect ( states [ 3 ] ) . toMatchObject ( {
741+ data : 10 ,
742+ isFetching : true ,
743+ isSuccess : true ,
744+ isPreviousData : true ,
745+ } )
746+ // Refetch done
747+ expect ( states [ 4 ] ) . toMatchObject ( {
748+ data : 12 ,
749+ isFetching : false ,
750+ isSuccess : true ,
751+ isPreviousData : false ,
752+ } )
753+ } )
754+
675755 it ( 'should use the correct query function when components use different configurations' , async ( ) => {
676756 const key = queryKey ( )
677757 const states : QueryResult < number > [ ] = [ ]
You can’t perform that action at this time.
0 commit comments