Skip to content

Commit eb34fb3

Browse files
authored
Merge pull request #2212 from reduxjs/pr/refetch-promise
2 parents ed3d162 + 737e039 commit eb34fb3

File tree

5 files changed

+48
-15
lines changed

5 files changed

+48
-15
lines changed

packages/toolkit/src/query/core/buildInitiate.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export type QueryActionCreatorResult<
5656
abort(): void
5757
unwrap(): Promise<ResultTypeFrom<D>>
5858
unsubscribe(): void
59-
refetch(): void
59+
refetch(): QueryActionCreatorResult<D>
6060
updateSubscriptionOptions(options: SubscriptionOptions): void
6161
queryCacheKey: string
6262
}
@@ -311,11 +311,10 @@ Features like automatic cache collection, automatic refetching etc. will not be
311311

312312
return result.data
313313
},
314-
refetch() {
314+
refetch: () =>
315315
dispatch(
316316
queryAction(arg, { subscribe: false, forceRefetch: true })
317-
)
318-
},
317+
),
319318
unsubscribe() {
320319
if (subscribe)
321320
dispatch(

packages/toolkit/src/query/react/buildHooks.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,13 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
771771
/**
772772
* A method to manually refetch data for the query
773773
*/
774-
refetch: () => void promiseRef.current?.refetch(),
774+
refetch: () => {
775+
if (!promiseRef.current)
776+
throw new Error(
777+
'Cannot refetch a query that has not been started yet.'
778+
)
779+
return promiseRef.current?.refetch()
780+
},
775781
}),
776782
[]
777783
)

packages/toolkit/src/query/tests/buildHooks.test.tsx

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ const api = createApi({
6565
},
6666
}),
6767
}),
68-
getIncrementedAmount: build.query<any, void>({
68+
getIncrementedAmount: build.query<{ amount: number }, void>({
6969
query: () => ({
7070
url: '',
7171
body: {
@@ -199,7 +199,7 @@ describe('hooks tests', () => {
199199
expect(screen.getByTestId('isLoading').textContent).toBe('false')
200200
)
201201
// We call a refetch, should still be `false`
202-
act(() => refetch())
202+
act(() => void refetch())
203203
await waitFor(() =>
204204
expect(screen.getByTestId('isFetching').textContent).toBe('true')
205205
)
@@ -255,7 +255,7 @@ describe('hooks tests', () => {
255255
expect(getRenderCount()).toBe(5)
256256

257257
// We call a refetch, should set `isFetching` to true, then false when complete/errored
258-
act(() => refetchMe())
258+
act(() => void refetchMe())
259259
await waitFor(() => {
260260
expect(screen.getByTestId('isLoading').textContent).toBe('false')
261261
expect(screen.getByTestId('isFetching').textContent).toBe('true')
@@ -607,6 +607,28 @@ describe('hooks tests', () => {
607607
})
608608
})
609609
})
610+
611+
test('useQuery refetch method returns a promise that resolves with the result', async () => {
612+
const { result } = renderHook(
613+
() => api.endpoints.getIncrementedAmount.useQuery(),
614+
{
615+
wrapper: storeRef.wrapper,
616+
}
617+
)
618+
619+
await waitFor(() => expect(result.current.isSuccess).toBe(true))
620+
const originalAmount = result.current.data!.amount
621+
622+
const { refetch } = result.current
623+
624+
let resPromise: ReturnType<typeof refetch> = null as any
625+
await act(async () => {
626+
resPromise = refetch()
627+
})
628+
expect(resPromise).toBeInstanceOf(Promise)
629+
const res = await resPromise
630+
expect(res.data!.amount).toBeGreaterThan(originalAmount)
631+
})
610632
})
611633

612634
describe('useLazyQuery', () => {

packages/toolkit/src/query/tests/errorHandling.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ describe('query error handling', () => {
147147
)
148148
)
149149

150-
act(result.current.refetch)
150+
act(() => void result.current.refetch())
151151

152152
await hookWaitFor(() => expect(result.current.isFetching).toBeFalsy())
153153
expect(result.current).toEqual(
@@ -193,7 +193,7 @@ describe('query error handling', () => {
193193
})
194194
)
195195

196-
act(result.current.refetch)
196+
act(() => void result.current.refetch())
197197

198198
await hookWaitFor(() => expect(result.current.isFetching).toBeFalsy())
199199
expect(result.current).toEqual(

packages/toolkit/src/query/tests/unionTypes.test.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ describe.skip('TS only tests', () => {
346346
}
347347
})
348348

349-
test('queryHookResult (without selector) union', () => {
349+
test('queryHookResult (without selector) union', async () => {
350350
const useQueryStateResult = api.endpoints.test.useQueryState()
351351
const useQueryResult = api.endpoints.test.useQuery()
352352
const useQueryStateWithSelectFromResult = api.endpoints.test.useQueryState(
@@ -356,12 +356,15 @@ describe.skip('TS only tests', () => {
356356
}
357357
)
358358

359-
const { refetch: _omit, ...useQueryResultWithoutMethods } = useQueryResult
359+
const { refetch, ...useQueryResultWithoutMethods } = useQueryResult
360360
expectExactType(useQueryStateResult)(useQueryResultWithoutMethods)
361361
expectExactType(useQueryStateWithSelectFromResult)(
362362
// @ts-expect-error
363363
useQueryResultWithoutMethods
364364
)
365+
expectType<ReturnType<ReturnType<typeof api.endpoints.test.select>>>(
366+
await refetch()
367+
)
365368
})
366369

367370
test('useQueryState (with selectFromResult)', () => {
@@ -394,8 +397,8 @@ describe.skip('TS only tests', () => {
394397
})(result)
395398
})
396399

397-
test('useQuery (with selectFromResult)', () => {
398-
const result = api.endpoints.test.useQuery(undefined, {
400+
test('useQuery (with selectFromResult)', async () => {
401+
const { refetch, ...result } = api.endpoints.test.useQuery(undefined, {
399402
selectFromResult({
400403
data,
401404
isLoading,
@@ -421,8 +424,11 @@ describe.skip('TS only tests', () => {
421424
isFetching: true,
422425
isSuccess: false,
423426
isError: false,
424-
refetch: () => {},
425427
})(result)
428+
429+
expectType<ReturnType<ReturnType<typeof api.endpoints.test.select>>>(
430+
await refetch()
431+
)
426432
})
427433

428434
test('useMutation union', () => {

0 commit comments

Comments
 (0)