@@ -16,6 +16,7 @@ function createWrapper() {
1616 queries : { retry : false } ,
1717 } ,
1818 } ) ;
19+ ( queryClient as any ) . _uid = "test-" + Math . random ( ) . toString ( 36 ) . substring ( 2 , 5 ) ;
1920 const wrapper = ( { children } : { children : React . ReactNode } ) => (
2021 < QueryClientProvider client = { queryClient } > { children } </ QueryClientProvider >
2122 ) ;
@@ -59,7 +60,7 @@ describe("useMutateCompleteAdoption", () => {
5960 } ) ;
6061
6162 // While in-flight, isPending should be true
62- expect ( result . current . isPending ) . toBe ( true ) ;
63+ await waitFor ( ( ) => expect ( result . current . isPending ) . toBe ( true ) ) ;
6364 expect ( result . current . isError ) . toBe ( false ) ;
6465
6566 resolveRequest ( ) ;
@@ -89,7 +90,8 @@ describe("useMutateCompleteAdoption", () => {
8990 const { queryClient, wrapper } = createWrapper ( ) ;
9091 queryClient . setQueryData < AdoptionDetails > ( [ "adoption" , "adoption-1" ] , MOCK_ADOPTION ) ;
9192
92- const invalidateSpy = vi . spyOn ( queryClient , "invalidateQueries" ) ;
93+ const invalidateSpy = vi . fn ( ) ;
94+ queryClient . invalidateQueries = invalidateSpy ;
9395
9496 const { result } = renderHook (
9597 ( ) => useMutateCompleteAdoption ( "adoption-1" ) ,
@@ -103,9 +105,9 @@ describe("useMutateCompleteAdoption", () => {
103105 await waitFor ( ( ) => expect ( result . current . isPending ) . toBe ( false ) ) ;
104106
105107 expect ( result . current . isError ) . toBe ( false ) ;
106- expect ( invalidateSpy ) . toHaveBeenCalledWith (
108+ await waitFor ( ( ) => expect ( invalidateSpy ) . toHaveBeenCalledWith (
107109 expect . objectContaining ( { queryKey : [ "adoption" , "adoption-1" ] } ) ,
108- ) ;
110+ ) ) ;
109111 } ) ;
110112 } ) ;
111113
@@ -155,11 +157,13 @@ describe("useMutateCompleteAdoption", () => {
155157 } ) ;
156158
157159 // While in-flight: cache should reflect optimistic SETTLEMENT_TRIGGERED status
158- const optimisticData = queryClient . getQueryData < AdoptionDetails > ( [
159- "adoption" ,
160- "adoption-1" ,
161- ] ) ;
162- expect ( optimisticData ?. status ) . toBe ( "SETTLEMENT_TRIGGERED" ) ;
160+ await waitFor ( ( ) => {
161+ const optimisticData = queryClient . getQueryData < AdoptionDetails > ( [
162+ "adoption" ,
163+ "adoption-1" ,
164+ ] ) ;
165+ expect ( optimisticData ?. status ) . toBe ( "SETTLEMENT_TRIGGERED" ) ;
166+ } ) ;
163167
164168 resolveRequest ( ) ;
165169 await waitFor ( ( ) => expect ( result . current . isPending ) . toBe ( false ) ) ;
@@ -169,8 +173,9 @@ describe("useMutateCompleteAdoption", () => {
169173 describe ( "rollback on error" , ( ) => {
170174 it ( "restores previous cache state when mutation fails" , async ( ) => {
171175 const { queryClient, wrapper } = createWrapper ( ) ;
172- // Seed cache with the original adoption state
173- queryClient . setQueryData < AdoptionDetails > ( [ "adoption" , "fail" ] , MOCK_ADOPTION ) ;
176+ // Seed cache with an adoption whose id matches the hook argument
177+ const failAdoption : AdoptionDetails = { ...MOCK_ADOPTION , id : "fail" } ;
178+ queryClient . setQueryData < AdoptionDetails > ( [ "adoption" , "fail" ] , failAdoption ) ;
174179
175180 const { result } = renderHook (
176181 ( ) => useMutateCompleteAdoption ( "fail" ) ,
@@ -183,7 +188,7 @@ describe("useMutateCompleteAdoption", () => {
183188
184189 await waitFor ( ( ) => expect ( result . current . isError ) . toBe ( true ) ) ;
185190
186- // After failure, cache should be restored to original state
191+ // After failure, cache should be restored to the original state
187192 const restoredData = queryClient . getQueryData < AdoptionDetails > ( [
188193 "adoption" ,
189194 "fail" ,
0 commit comments