@@ -1032,6 +1032,82 @@ describe('useQuery', () => {
10321032 expect ( states [ 3 ] ) . toMatchObject ( { data : 2 } )
10331033 } )
10341034
1035+ it ( 'should be able to reset a query' , async ( ) => {
1036+ const key = queryKey ( )
1037+ const states : UseQueryResult < number > [ ] = [ ]
1038+ let count = 0
1039+
1040+ function Page ( ) {
1041+ const state = useQuery ( key , ( ) => ++ count )
1042+
1043+ states . push ( state )
1044+
1045+ const { reset } = state
1046+
1047+ React . useEffect ( ( ) => {
1048+ setActTimeout ( ( ) => {
1049+ reset ( )
1050+ } , 5 )
1051+ } , [ reset ] )
1052+
1053+ return null
1054+ }
1055+
1056+ renderWithClient ( queryClient , < Page /> )
1057+
1058+ await sleep ( 20 )
1059+
1060+ expect ( states . length ) . toBe ( 5 )
1061+ // Initial
1062+ expect ( states [ 0 ] ) . toMatchObject ( { data : undefined } )
1063+ // Fetched
1064+ expect ( states [ 1 ] ) . toMatchObject ( { data : 1 } )
1065+ // Reset
1066+ expect ( states [ 2 ] ) . toMatchObject ( { data : undefined } )
1067+ // Initial (refetch)
1068+ expect ( states [ 3 ] ) . toMatchObject ( { data : undefined } )
1069+ // Fetched
1070+ expect ( states [ 4 ] ) . toMatchObject ( { data : 2 } )
1071+ } )
1072+
1073+ it ( 'should reset the query to its initial state' , async ( ) => {
1074+ const key = queryKey ( )
1075+ const states : UseQueryResult < number > [ ] = [ ]
1076+ let count = 0
1077+
1078+ function Page ( ) {
1079+ const state = useQuery ( key , ( ) => ++ count , { initialData : 99 } )
1080+
1081+ states . push ( state )
1082+
1083+ const { reset } = state
1084+
1085+ React . useEffect ( ( ) => {
1086+ setActTimeout ( ( ) => {
1087+ reset ( )
1088+ } , 5 )
1089+ } , [ reset ] )
1090+
1091+ return null
1092+ }
1093+
1094+ renderWithClient ( queryClient , < Page /> )
1095+
1096+ await sleep ( 20 )
1097+
1098+ expect ( states . length ) . toBe ( 5 )
1099+ // Initial
1100+ expect ( states [ 0 ] ) . toMatchObject ( { data : 99 } )
1101+ // Fetched
1102+ expect ( states [ 1 ] ) . toMatchObject ( { data : 1 } )
1103+ // Reset
1104+ expect ( states [ 2 ] ) . toMatchObject ( { data : 99 } )
1105+ // Initial (refetch)
1106+ expect ( states [ 3 ] ) . toMatchObject ( { data : 99 } )
1107+ // Fetched
1108+ expect ( states [ 4 ] ) . toMatchObject ( { data : 2 } )
1109+ } )
1110+
10351111 it ( 'should share equal data structures between query results' , async ( ) => {
10361112 const key = queryKey ( )
10371113
0 commit comments