Skip to content

Commit

Permalink
Merge pull request #2212 from reduxjs/pr/refetch-promise
Browse files Browse the repository at this point in the history
  • Loading branch information
markerikson authored Aug 21, 2022
2 parents ed3d162 + 737e039 commit eb34fb3
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 15 deletions.
7 changes: 3 additions & 4 deletions packages/toolkit/src/query/core/buildInitiate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export type QueryActionCreatorResult<
abort(): void
unwrap(): Promise<ResultTypeFrom<D>>
unsubscribe(): void
refetch(): void
refetch(): QueryActionCreatorResult<D>
updateSubscriptionOptions(options: SubscriptionOptions): void
queryCacheKey: string
}
Expand Down Expand Up @@ -311,11 +311,10 @@ Features like automatic cache collection, automatic refetching etc. will not be

return result.data
},
refetch() {
refetch: () =>
dispatch(
queryAction(arg, { subscribe: false, forceRefetch: true })
)
},
),
unsubscribe() {
if (subscribe)
dispatch(
Expand Down
8 changes: 7 additions & 1 deletion packages/toolkit/src/query/react/buildHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,13 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
/**
* A method to manually refetch data for the query
*/
refetch: () => void promiseRef.current?.refetch(),
refetch: () => {
if (!promiseRef.current)
throw new Error(
'Cannot refetch a query that has not been started yet.'
)
return promiseRef.current?.refetch()
},
}),
[]
)
Expand Down
28 changes: 25 additions & 3 deletions packages/toolkit/src/query/tests/buildHooks.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const api = createApi({
},
}),
}),
getIncrementedAmount: build.query<any, void>({
getIncrementedAmount: build.query<{ amount: number }, void>({
query: () => ({
url: '',
body: {
Expand Down Expand Up @@ -199,7 +199,7 @@ describe('hooks tests', () => {
expect(screen.getByTestId('isLoading').textContent).toBe('false')
)
// We call a refetch, should still be `false`
act(() => refetch())
act(() => void refetch())
await waitFor(() =>
expect(screen.getByTestId('isFetching').textContent).toBe('true')
)
Expand Down Expand Up @@ -255,7 +255,7 @@ describe('hooks tests', () => {
expect(getRenderCount()).toBe(5)

// We call a refetch, should set `isFetching` to true, then false when complete/errored
act(() => refetchMe())
act(() => void refetchMe())
await waitFor(() => {
expect(screen.getByTestId('isLoading').textContent).toBe('false')
expect(screen.getByTestId('isFetching').textContent).toBe('true')
Expand Down Expand Up @@ -607,6 +607,28 @@ describe('hooks tests', () => {
})
})
})

test('useQuery refetch method returns a promise that resolves with the result', async () => {
const { result } = renderHook(
() => api.endpoints.getIncrementedAmount.useQuery(),
{
wrapper: storeRef.wrapper,
}
)

await waitFor(() => expect(result.current.isSuccess).toBe(true))
const originalAmount = result.current.data!.amount

const { refetch } = result.current

let resPromise: ReturnType<typeof refetch> = null as any
await act(async () => {
resPromise = refetch()
})
expect(resPromise).toBeInstanceOf(Promise)
const res = await resPromise
expect(res.data!.amount).toBeGreaterThan(originalAmount)
})
})

describe('useLazyQuery', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/toolkit/src/query/tests/errorHandling.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ describe('query error handling', () => {
)
)

act(result.current.refetch)
act(() => void result.current.refetch())

await hookWaitFor(() => expect(result.current.isFetching).toBeFalsy())
expect(result.current).toEqual(
Expand Down Expand Up @@ -193,7 +193,7 @@ describe('query error handling', () => {
})
)

act(result.current.refetch)
act(() => void result.current.refetch())

await hookWaitFor(() => expect(result.current.isFetching).toBeFalsy())
expect(result.current).toEqual(
Expand Down
16 changes: 11 additions & 5 deletions packages/toolkit/src/query/tests/unionTypes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ describe.skip('TS only tests', () => {
}
})

test('queryHookResult (without selector) union', () => {
test('queryHookResult (without selector) union', async () => {
const useQueryStateResult = api.endpoints.test.useQueryState()
const useQueryResult = api.endpoints.test.useQuery()
const useQueryStateWithSelectFromResult = api.endpoints.test.useQueryState(
Expand All @@ -356,12 +356,15 @@ describe.skip('TS only tests', () => {
}
)

const { refetch: _omit, ...useQueryResultWithoutMethods } = useQueryResult
const { refetch, ...useQueryResultWithoutMethods } = useQueryResult
expectExactType(useQueryStateResult)(useQueryResultWithoutMethods)
expectExactType(useQueryStateWithSelectFromResult)(
// @ts-expect-error
useQueryResultWithoutMethods
)
expectType<ReturnType<ReturnType<typeof api.endpoints.test.select>>>(
await refetch()
)
})

test('useQueryState (with selectFromResult)', () => {
Expand Down Expand Up @@ -394,8 +397,8 @@ describe.skip('TS only tests', () => {
})(result)
})

test('useQuery (with selectFromResult)', () => {
const result = api.endpoints.test.useQuery(undefined, {
test('useQuery (with selectFromResult)', async () => {
const { refetch, ...result } = api.endpoints.test.useQuery(undefined, {
selectFromResult({
data,
isLoading,
Expand All @@ -421,8 +424,11 @@ describe.skip('TS only tests', () => {
isFetching: true,
isSuccess: false,
isError: false,
refetch: () => {},
})(result)

expectType<ReturnType<ReturnType<typeof api.endpoints.test.select>>>(
await refetch()
)
})

test('useMutation union', () => {
Expand Down

0 comments on commit eb34fb3

Please sign in to comment.