@tanstack/react-query 5.0.0-alpha.4 → 5.0.0-alpha.43
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/codemods/__testfixtures__/default-import.input.tsx +94 -0
- package/build/codemods/__testfixtures__/default-import.output.tsx +96 -0
- package/build/codemods/__testfixtures__/named-import.input.tsx +96 -0
- package/build/codemods/__testfixtures__/named-import.output.tsx +98 -0
- package/build/codemods/__testfixtures__/namespaced-import.input.tsx +86 -0
- package/build/codemods/__testfixtures__/namespaced-import.output.tsx +88 -0
- package/build/codemods/__testfixtures__/parameter-is-identifier.input.tsx +49 -0
- package/build/codemods/__testfixtures__/parameter-is-identifier.output.tsx +49 -0
- package/build/codemods/__testfixtures__/parameter-is-object-expression.input.tsx +128 -0
- package/build/codemods/__testfixtures__/parameter-is-object-expression.output.tsx +175 -0
- package/build/codemods/__testfixtures__/replace-import-specifier.input.tsx +10 -0
- package/build/codemods/__testfixtures__/replace-import-specifier.output.tsx +10 -0
- package/build/codemods/__testfixtures__/type-arguments.input.tsx +25 -0
- package/build/codemods/__testfixtures__/type-arguments.output.tsx +31 -0
- package/build/codemods/__tests__/key-transformation.test.js +32 -0
- package/build/codemods/__tests__/replace-import-specifier.test.js +12 -0
- package/build/codemods/remove-overloads/__testfixtures__/default-import.input.tsx +199 -0
- package/build/codemods/remove-overloads/__testfixtures__/default-import.output.tsx +484 -0
- package/build/codemods/remove-overloads/__tests__/remove-overloads.test.js +6 -0
- package/build/codemods/remove-overloads/remove-overloads.js +59 -0
- package/build/codemods/remove-overloads/transformers/filter-aware-usage-transformer.js +153 -0
- package/build/codemods/remove-overloads/transformers/query-fn-aware-usage-transformer.js +188 -0
- package/build/codemods/remove-overloads/utils/index.js +124 -0
- package/build/codemods/remove-overloads/utils/unknown-usage-error.js +26 -0
- package/build/codemods/src/utils/index.js +205 -0
- package/build/codemods/src/v4/key-transformation.js +138 -0
- package/build/codemods/src/v4/replace-import-specifier.js +25 -0
- package/build/codemods/transformers/query-cache-transformer.js +116 -0
- package/build/codemods/transformers/query-client-transformer.js +48 -0
- package/build/codemods/transformers/use-query-like-transformer.js +32 -0
- package/build/codemods/utils/replacers/key-replacer.js +164 -0
- package/build/lib/HydrationBoundary.d.ts +1 -0
- package/build/lib/HydrationBoundary.d.ts.map +1 -0
- package/build/lib/HydrationBoundary.esm.js +1 -0
- package/build/lib/HydrationBoundary.esm.js.map +1 -1
- package/build/lib/HydrationBoundary.js +1 -0
- package/build/lib/HydrationBoundary.js.map +1 -1
- package/build/lib/HydrationBoundary.mjs +1 -0
- package/build/lib/HydrationBoundary.mjs.map +1 -1
- package/build/lib/QueryClientProvider.d.ts +2 -1
- package/build/lib/QueryClientProvider.d.ts.map +1 -0
- package/build/lib/QueryClientProvider.esm.js +1 -0
- package/build/lib/QueryClientProvider.esm.js.map +1 -1
- package/build/lib/QueryClientProvider.js +1 -0
- package/build/lib/QueryClientProvider.js.map +1 -1
- package/build/lib/QueryClientProvider.mjs +1 -0
- package/build/lib/QueryClientProvider.mjs.map +1 -1
- package/build/lib/QueryErrorResetBoundary.d.ts +2 -1
- package/build/lib/QueryErrorResetBoundary.d.ts.map +1 -0
- package/build/lib/QueryErrorResetBoundary.esm.js +1 -0
- package/build/lib/QueryErrorResetBoundary.esm.js.map +1 -1
- package/build/lib/QueryErrorResetBoundary.js +1 -0
- package/build/lib/QueryErrorResetBoundary.js.map +1 -1
- package/build/lib/QueryErrorResetBoundary.mjs +1 -0
- package/build/lib/QueryErrorResetBoundary.mjs.map +1 -1
- package/build/lib/__tests__/HydrationBoundary.test.d.ts +1 -0
- package/build/lib/__tests__/HydrationBoundary.test.d.ts.map +1 -0
- package/build/lib/__tests__/QueryClientProvider.test.d.ts +1 -0
- package/build/lib/__tests__/QueryClientProvider.test.d.ts.map +1 -0
- package/build/lib/__tests__/QueryResetErrorBoundary.test.d.ts +1 -0
- package/build/lib/__tests__/QueryResetErrorBoundary.test.d.ts.map +1 -0
- package/build/lib/__tests__/ssr-hydration.test.d.ts +1 -0
- package/build/lib/__tests__/ssr-hydration.test.d.ts.map +1 -0
- package/build/lib/__tests__/ssr.test.d.ts +1 -3
- package/build/lib/__tests__/ssr.test.d.ts.map +1 -0
- package/build/lib/__tests__/suspense.test.d.ts +1 -0
- package/build/lib/__tests__/suspense.test.d.ts.map +1 -0
- package/build/lib/__tests__/useInfiniteQuery.test.d.ts +1 -0
- package/build/lib/__tests__/useInfiniteQuery.test.d.ts.map +1 -0
- package/build/lib/__tests__/useInfiniteQuery.type.test.d.ts +1 -0
- package/build/lib/__tests__/useInfiniteQuery.type.test.d.ts.map +1 -0
- package/build/lib/__tests__/useIsFetching.test.d.ts +1 -0
- package/build/lib/__tests__/useIsFetching.test.d.ts.map +1 -0
- package/build/lib/__tests__/useMutation.test.d.ts +1 -0
- package/build/lib/__tests__/useMutation.test.d.ts.map +1 -0
- package/build/lib/__tests__/useMutationState.test.d.ts +1 -0
- package/build/lib/__tests__/useMutationState.test.d.ts.map +1 -0
- package/build/lib/__tests__/useQueries.test.d.ts +1 -0
- package/build/lib/__tests__/useQueries.test.d.ts.map +1 -0
- package/build/lib/__tests__/useQuery.test.d.ts +1 -0
- package/build/lib/__tests__/useQuery.test.d.ts.map +1 -0
- package/build/lib/__tests__/useQuery.types.test.d.ts +1 -0
- package/build/lib/__tests__/useQuery.types.test.d.ts.map +1 -0
- package/build/lib/__tests__/utils.d.ts +6 -6
- package/build/lib/__tests__/utils.d.ts.map +1 -0
- package/build/lib/errorBoundaryUtils.d.ts +4 -3
- package/build/lib/errorBoundaryUtils.d.ts.map +1 -0
- package/build/lib/errorBoundaryUtils.esm.js +4 -3
- package/build/lib/errorBoundaryUtils.esm.js.map +1 -1
- package/build/lib/errorBoundaryUtils.js +4 -3
- package/build/lib/errorBoundaryUtils.js.map +1 -1
- package/build/lib/errorBoundaryUtils.mjs +4 -3
- package/build/lib/errorBoundaryUtils.mjs.map +1 -1
- package/build/lib/index.d.ts +2 -1
- package/build/lib/index.d.ts.map +1 -0
- package/build/lib/index.esm.js +1 -1
- package/build/lib/index.js +1 -0
- package/build/lib/index.js.map +1 -1
- package/build/lib/index.mjs +1 -1
- package/build/lib/isRestoring.d.ts +1 -0
- package/build/lib/isRestoring.d.ts.map +1 -0
- package/build/lib/isRestoring.esm.js +1 -0
- package/build/lib/isRestoring.esm.js.map +1 -1
- package/build/lib/isRestoring.js +1 -0
- package/build/lib/isRestoring.js.map +1 -1
- package/build/lib/isRestoring.mjs +1 -0
- package/build/lib/isRestoring.mjs.map +1 -1
- package/build/lib/suspense.d.ts +3 -5
- package/build/lib/suspense.d.ts.map +1 -0
- package/build/lib/suspense.esm.js +2 -9
- package/build/lib/suspense.esm.js.map +1 -1
- package/build/lib/suspense.js +2 -9
- package/build/lib/suspense.js.map +1 -1
- package/build/lib/suspense.mjs +1 -8
- package/build/lib/suspense.mjs.map +1 -1
- package/build/lib/types.d.ts +11 -10
- package/build/lib/types.d.ts.map +1 -0
- package/build/lib/useBaseQuery.d.ts +1 -0
- package/build/lib/useBaseQuery.d.ts.map +1 -0
- package/build/lib/useBaseQuery.esm.js +2 -12
- package/build/lib/useBaseQuery.esm.js.map +1 -1
- package/build/lib/useBaseQuery.js +2 -12
- package/build/lib/useBaseQuery.js.map +1 -1
- package/build/lib/useBaseQuery.mjs +2 -12
- package/build/lib/useBaseQuery.mjs.map +1 -1
- package/build/lib/useInfiniteQuery.d.ts +1 -0
- package/build/lib/useInfiniteQuery.d.ts.map +1 -0
- package/build/lib/useInfiniteQuery.esm.js +1 -0
- package/build/lib/useInfiniteQuery.esm.js.map +1 -1
- package/build/lib/useInfiniteQuery.js +1 -0
- package/build/lib/useInfiniteQuery.js.map +1 -1
- package/build/lib/useInfiniteQuery.mjs +1 -0
- package/build/lib/useInfiniteQuery.mjs.map +1 -1
- package/build/lib/useIsFetching.d.ts +1 -0
- package/build/lib/useIsFetching.d.ts.map +1 -0
- package/build/lib/useIsFetching.esm.js +1 -0
- package/build/lib/useIsFetching.esm.js.map +1 -1
- package/build/lib/useIsFetching.js +1 -0
- package/build/lib/useIsFetching.js.map +1 -1
- package/build/lib/useIsFetching.mjs +1 -0
- package/build/lib/useIsFetching.mjs.map +1 -1
- package/build/lib/useMutation.d.ts +1 -0
- package/build/lib/useMutation.d.ts.map +1 -0
- package/build/lib/useMutation.esm.js +2 -1
- package/build/lib/useMutation.esm.js.map +1 -1
- package/build/lib/useMutation.js +2 -1
- package/build/lib/useMutation.js.map +1 -1
- package/build/lib/useMutation.mjs +2 -1
- package/build/lib/useMutation.mjs.map +1 -1
- package/build/lib/useMutationState.d.ts +4 -3
- package/build/lib/useMutationState.d.ts.map +1 -0
- package/build/lib/useMutationState.esm.js +1 -0
- package/build/lib/useMutationState.esm.js.map +1 -1
- package/build/lib/useMutationState.js +1 -0
- package/build/lib/useMutationState.js.map +1 -1
- package/build/lib/useMutationState.mjs +1 -0
- package/build/lib/useMutationState.mjs.map +1 -1
- package/build/lib/useQueries.d.ts +10 -8
- package/build/lib/useQueries.d.ts.map +1 -0
- package/build/lib/useQueries.esm.js +39 -27
- package/build/lib/useQueries.esm.js.map +1 -1
- package/build/lib/useQueries.js +39 -27
- package/build/lib/useQueries.js.map +1 -1
- package/build/lib/useQueries.mjs +31 -22
- package/build/lib/useQueries.mjs.map +1 -1
- package/build/lib/useQuery.d.ts +5 -2
- package/build/lib/useQuery.d.ts.map +1 -0
- package/build/lib/useQuery.esm.js +6 -1
- package/build/lib/useQuery.esm.js.map +1 -1
- package/build/lib/useQuery.js +6 -0
- package/build/lib/useQuery.js.map +1 -1
- package/build/lib/useQuery.mjs +6 -1
- package/build/lib/useQuery.mjs.map +1 -1
- package/build/lib/utils.d.ts +1 -0
- package/build/lib/utils.d.ts.map +1 -0
- package/build/lib/utils.esm.js.map +1 -1
- package/build/lib/utils.js.map +1 -1
- package/build/lib/utils.mjs.map +1 -1
- package/build/umd/index.development.js +191 -187
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +1 -1
- package/build/umd/index.production.js.map +1 -1
- package/package.json +13 -6
- package/src/__tests__/HydrationBoundary.test.tsx +4 -3
- package/src/__tests__/QueryClientProvider.test.tsx +2 -1
- package/src/__tests__/QueryResetErrorBoundary.test.tsx +753 -620
- package/src/__tests__/ssr-hydration.test.tsx +11 -10
- package/src/__tests__/ssr.test.tsx +4 -7
- package/src/__tests__/suspense.test.tsx +17 -98
- package/src/__tests__/useInfiniteQuery.test.tsx +18 -16
- package/src/__tests__/useInfiniteQuery.type.test.tsx +94 -13
- package/src/__tests__/useMutation.test.tsx +25 -24
- package/src/__tests__/useMutationState.test.tsx +24 -58
- package/src/__tests__/useQueries.test.tsx +217 -154
- package/src/__tests__/useQuery.test.tsx +234 -365
- package/src/__tests__/useQuery.types.test.tsx +21 -1
- package/src/__tests__/utils.tsx +3 -2
- package/src/errorBoundaryUtils.ts +6 -5
- package/src/index.ts +1 -1
- package/src/suspense.ts +9 -15
- package/src/useBaseQuery.ts +2 -20
- package/src/useInfiniteQuery.ts +1 -0
- package/src/useIsFetching.ts +1 -0
- package/src/useMutation.ts +2 -1
- package/src/useMutationState.ts +4 -3
- package/src/useQueries.ts +44 -26
- package/src/useQuery.ts +23 -0
|
@@ -4,6 +4,7 @@ import ReactDOMTestUtils from 'react-dom/test-utils'
|
|
|
4
4
|
import ReactDOMServer from 'react-dom/server'
|
|
5
5
|
// eslint-disable-next-line import/no-unresolved -- types only for module augmentation
|
|
6
6
|
import type {} from 'react-dom/next'
|
|
7
|
+
import { vi } from 'vitest'
|
|
7
8
|
|
|
8
9
|
import {
|
|
9
10
|
useQuery,
|
|
@@ -46,12 +47,12 @@ describe('Server side rendering with de/rehydration', () => {
|
|
|
46
47
|
globalThis.IS_REACT_ACT_ENVIRONMENT = previousIsReactActEnvironment
|
|
47
48
|
})
|
|
48
49
|
it('should not mismatch on success', async () => {
|
|
49
|
-
const consoleMock =
|
|
50
|
+
const consoleMock = vi.spyOn(console, 'error')
|
|
50
51
|
consoleMock.mockImplementation(() => undefined)
|
|
51
52
|
|
|
52
|
-
const fetchDataSuccess =
|
|
53
|
-
|
|
54
|
-
|
|
53
|
+
const fetchDataSuccess = vi.fn<
|
|
54
|
+
Parameters<typeof fetchData>,
|
|
55
|
+
ReturnType<typeof fetchData>
|
|
55
56
|
>(fetchData)
|
|
56
57
|
|
|
57
58
|
// -- Shared part --
|
|
@@ -127,10 +128,10 @@ describe('Server side rendering with de/rehydration', () => {
|
|
|
127
128
|
})
|
|
128
129
|
|
|
129
130
|
it('should not mismatch on error', async () => {
|
|
130
|
-
const consoleMock =
|
|
131
|
+
const consoleMock = vi.spyOn(console, 'error')
|
|
131
132
|
consoleMock.mockImplementation(() => undefined)
|
|
132
133
|
|
|
133
|
-
const fetchDataError =
|
|
134
|
+
const fetchDataError = vi.fn(() => {
|
|
134
135
|
throw new Error('fetchDataError')
|
|
135
136
|
})
|
|
136
137
|
|
|
@@ -207,12 +208,12 @@ describe('Server side rendering with de/rehydration', () => {
|
|
|
207
208
|
})
|
|
208
209
|
|
|
209
210
|
it('should not mismatch on queries that were not prefetched', async () => {
|
|
210
|
-
const consoleMock =
|
|
211
|
+
const consoleMock = vi.spyOn(console, 'error')
|
|
211
212
|
consoleMock.mockImplementation(() => undefined)
|
|
212
213
|
|
|
213
|
-
const fetchDataSuccess =
|
|
214
|
-
|
|
215
|
-
|
|
214
|
+
const fetchDataSuccess = vi.fn<
|
|
215
|
+
Parameters<typeof fetchData>,
|
|
216
|
+
ReturnType<typeof fetchData>
|
|
216
217
|
>(fetchData)
|
|
217
218
|
|
|
218
219
|
// -- Shared part --
|
|
@@ -1,20 +1,17 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @jest-environment node
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
1
|
import * as React from 'react'
|
|
6
2
|
// @ts-ignore
|
|
7
3
|
import { renderToString } from 'react-dom/server'
|
|
8
4
|
|
|
9
5
|
import { sleep, queryKey, createQueryClient } from './utils'
|
|
10
6
|
import { useQuery, QueryClientProvider, QueryCache, useInfiniteQuery } from '..'
|
|
7
|
+
import { vi } from 'vitest'
|
|
11
8
|
|
|
12
9
|
describe('Server Side Rendering', () => {
|
|
13
10
|
it('should not trigger fetch', () => {
|
|
14
11
|
const queryCache = new QueryCache()
|
|
15
12
|
const queryClient = createQueryClient({ queryCache })
|
|
16
13
|
const key = queryKey()
|
|
17
|
-
const queryFn =
|
|
14
|
+
const queryFn = vi.fn().mockReturnValue('data')
|
|
18
15
|
|
|
19
16
|
function Page() {
|
|
20
17
|
const query = useQuery({ queryKey: key, queryFn })
|
|
@@ -57,7 +54,7 @@ describe('Server Side Rendering', () => {
|
|
|
57
54
|
const queryCache = new QueryCache()
|
|
58
55
|
const queryClient = createQueryClient({ queryCache })
|
|
59
56
|
const key = queryKey()
|
|
60
|
-
const queryFn =
|
|
57
|
+
const queryFn = vi.fn(() => {
|
|
61
58
|
sleep(10)
|
|
62
59
|
return 'data'
|
|
63
60
|
})
|
|
@@ -125,7 +122,7 @@ describe('Server Side Rendering', () => {
|
|
|
125
122
|
const queryCache = new QueryCache()
|
|
126
123
|
const queryClient = createQueryClient({ queryCache })
|
|
127
124
|
const key = queryKey()
|
|
128
|
-
const queryFn =
|
|
125
|
+
const queryFn = vi.fn(async () => {
|
|
129
126
|
await sleep(5)
|
|
130
127
|
return 'page 1'
|
|
131
128
|
})
|
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
useQueryErrorResetBoundary,
|
|
12
12
|
} from '..'
|
|
13
13
|
import { createQueryClient, queryKey, renderWithClient, sleep } from './utils'
|
|
14
|
+
import { vi } from 'vitest'
|
|
14
15
|
|
|
15
16
|
describe("useQuery's in Suspense mode", () => {
|
|
16
17
|
const queryCache = new QueryCache()
|
|
@@ -119,7 +120,7 @@ describe("useQuery's in Suspense mode", () => {
|
|
|
119
120
|
it('should not call the queryFn twice when used in Suspense mode', async () => {
|
|
120
121
|
const key = queryKey()
|
|
121
122
|
|
|
122
|
-
const queryFn =
|
|
123
|
+
const queryFn = vi.fn<unknown[], string>()
|
|
123
124
|
queryFn.mockImplementation(() => {
|
|
124
125
|
sleep(10)
|
|
125
126
|
return 'data'
|
|
@@ -189,91 +190,9 @@ describe("useQuery's in Suspense mode", () => {
|
|
|
189
190
|
expect(queryCache.find({ queryKey: key })?.getObserversCount()).toBe(0)
|
|
190
191
|
})
|
|
191
192
|
|
|
192
|
-
it('should call onSuccess on the first successful call', async () => {
|
|
193
|
-
const key = queryKey()
|
|
194
|
-
|
|
195
|
-
const successFn = jest.fn()
|
|
196
|
-
|
|
197
|
-
function Page() {
|
|
198
|
-
useQuery({
|
|
199
|
-
queryKey: [key],
|
|
200
|
-
queryFn: async () => {
|
|
201
|
-
await sleep(10)
|
|
202
|
-
return key
|
|
203
|
-
},
|
|
204
|
-
suspense: true,
|
|
205
|
-
select: () => 'selected',
|
|
206
|
-
onSuccess: successFn,
|
|
207
|
-
})
|
|
208
|
-
|
|
209
|
-
return <>rendered</>
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
const rendered = renderWithClient(
|
|
213
|
-
queryClient,
|
|
214
|
-
<React.Suspense fallback="loading">
|
|
215
|
-
<Page />
|
|
216
|
-
</React.Suspense>,
|
|
217
|
-
)
|
|
218
|
-
|
|
219
|
-
await waitFor(() => rendered.getByText('rendered'))
|
|
220
|
-
|
|
221
|
-
await waitFor(() => expect(successFn).toHaveBeenCalledTimes(1))
|
|
222
|
-
await waitFor(() => expect(successFn).toHaveBeenCalledWith('selected'))
|
|
223
|
-
})
|
|
224
|
-
|
|
225
|
-
it('should call every onSuccess handler within a suspense boundary', async () => {
|
|
226
|
-
const key = queryKey()
|
|
227
|
-
|
|
228
|
-
const successFn1 = jest.fn()
|
|
229
|
-
const successFn2 = jest.fn()
|
|
230
|
-
|
|
231
|
-
function FirstComponent() {
|
|
232
|
-
useQuery({
|
|
233
|
-
queryKey: key,
|
|
234
|
-
queryFn: () => {
|
|
235
|
-
sleep(10)
|
|
236
|
-
return 'data'
|
|
237
|
-
},
|
|
238
|
-
suspense: true,
|
|
239
|
-
onSuccess: successFn1,
|
|
240
|
-
})
|
|
241
|
-
|
|
242
|
-
return <span>first</span>
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
function SecondComponent() {
|
|
246
|
-
useQuery({
|
|
247
|
-
queryKey: key,
|
|
248
|
-
queryFn: () => {
|
|
249
|
-
sleep(10)
|
|
250
|
-
return 'data'
|
|
251
|
-
},
|
|
252
|
-
|
|
253
|
-
suspense: true,
|
|
254
|
-
onSuccess: successFn2,
|
|
255
|
-
})
|
|
256
|
-
|
|
257
|
-
return <span>second</span>
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
const rendered = renderWithClient(
|
|
261
|
-
queryClient,
|
|
262
|
-
<React.Suspense fallback="loading">
|
|
263
|
-
<FirstComponent />
|
|
264
|
-
<SecondComponent />
|
|
265
|
-
</React.Suspense>,
|
|
266
|
-
)
|
|
267
|
-
|
|
268
|
-
await waitFor(() => rendered.getByText('second'))
|
|
269
|
-
|
|
270
|
-
await waitFor(() => expect(successFn1).toHaveBeenCalledTimes(1))
|
|
271
|
-
await waitFor(() => expect(successFn2).toHaveBeenCalledTimes(1))
|
|
272
|
-
})
|
|
273
|
-
|
|
274
193
|
// https://github.com/tannerlinsley/react-query/issues/468
|
|
275
194
|
it('should reset error state if new component instances are mounted', async () => {
|
|
276
|
-
const consoleMock =
|
|
195
|
+
const consoleMock = vi
|
|
277
196
|
.spyOn(console, 'error')
|
|
278
197
|
.mockImplementation(() => undefined)
|
|
279
198
|
const key = queryKey()
|
|
@@ -345,7 +264,7 @@ describe("useQuery's in Suspense mode", () => {
|
|
|
345
264
|
})
|
|
346
265
|
|
|
347
266
|
it('should retry fetch if the reset error boundary has been reset', async () => {
|
|
348
|
-
const consoleMock =
|
|
267
|
+
const consoleMock = vi
|
|
349
268
|
.spyOn(console, 'error')
|
|
350
269
|
.mockImplementation(() => undefined)
|
|
351
270
|
const key = queryKey()
|
|
@@ -510,7 +429,7 @@ describe("useQuery's in Suspense mode", () => {
|
|
|
510
429
|
})
|
|
511
430
|
|
|
512
431
|
it('should retry fetch if the reset error boundary has been reset with global hook', async () => {
|
|
513
|
-
const consoleMock =
|
|
432
|
+
const consoleMock = vi
|
|
514
433
|
.spyOn(console, 'error')
|
|
515
434
|
.mockImplementation(() => undefined)
|
|
516
435
|
const key = queryKey()
|
|
@@ -574,7 +493,7 @@ describe("useQuery's in Suspense mode", () => {
|
|
|
574
493
|
})
|
|
575
494
|
|
|
576
495
|
it('should throw errors to the error boundary by default', async () => {
|
|
577
|
-
const consoleMock =
|
|
496
|
+
const consoleMock = vi
|
|
578
497
|
.spyOn(console, 'error')
|
|
579
498
|
.mockImplementation(() => undefined)
|
|
580
499
|
const key = queryKey()
|
|
@@ -615,7 +534,7 @@ describe("useQuery's in Suspense mode", () => {
|
|
|
615
534
|
consoleMock.mockRestore()
|
|
616
535
|
})
|
|
617
536
|
|
|
618
|
-
it('should not throw errors to the error boundary when
|
|
537
|
+
it('should not throw errors to the error boundary when throwOnError: false', async () => {
|
|
619
538
|
const key = queryKey()
|
|
620
539
|
|
|
621
540
|
function Page() {
|
|
@@ -627,7 +546,7 @@ describe("useQuery's in Suspense mode", () => {
|
|
|
627
546
|
},
|
|
628
547
|
retry: false,
|
|
629
548
|
suspense: true,
|
|
630
|
-
|
|
549
|
+
throwOnError: false,
|
|
631
550
|
})
|
|
632
551
|
return <div>rendered</div>
|
|
633
552
|
}
|
|
@@ -654,8 +573,8 @@ describe("useQuery's in Suspense mode", () => {
|
|
|
654
573
|
await waitFor(() => rendered.getByText('rendered'))
|
|
655
574
|
})
|
|
656
575
|
|
|
657
|
-
it('should throw errors to the error boundary when a
|
|
658
|
-
const consoleMock =
|
|
576
|
+
it('should throw errors to the error boundary when a throwOnError function returns true', async () => {
|
|
577
|
+
const consoleMock = vi
|
|
659
578
|
.spyOn(console, 'error')
|
|
660
579
|
.mockImplementation(() => undefined)
|
|
661
580
|
const key = queryKey()
|
|
@@ -669,7 +588,7 @@ describe("useQuery's in Suspense mode", () => {
|
|
|
669
588
|
},
|
|
670
589
|
retry: false,
|
|
671
590
|
suspense: true,
|
|
672
|
-
|
|
591
|
+
throwOnError: (err) => err.message !== 'Local Error',
|
|
673
592
|
})
|
|
674
593
|
return <div>rendered</div>
|
|
675
594
|
}
|
|
@@ -697,7 +616,7 @@ describe("useQuery's in Suspense mode", () => {
|
|
|
697
616
|
consoleMock.mockRestore()
|
|
698
617
|
})
|
|
699
618
|
|
|
700
|
-
it('should not throw errors to the error boundary when a
|
|
619
|
+
it('should not throw errors to the error boundary when a throwOnError function returns false', async () => {
|
|
701
620
|
const key = queryKey()
|
|
702
621
|
|
|
703
622
|
function Page() {
|
|
@@ -709,7 +628,7 @@ describe("useQuery's in Suspense mode", () => {
|
|
|
709
628
|
},
|
|
710
629
|
retry: false,
|
|
711
630
|
suspense: true,
|
|
712
|
-
|
|
631
|
+
throwOnError: (err) => err.message !== 'Local Error',
|
|
713
632
|
})
|
|
714
633
|
return <div>rendered</div>
|
|
715
634
|
}
|
|
@@ -739,7 +658,7 @@ describe("useQuery's in Suspense mode", () => {
|
|
|
739
658
|
it('should not call the queryFn when not enabled', async () => {
|
|
740
659
|
const key = queryKey()
|
|
741
660
|
|
|
742
|
-
const queryFn =
|
|
661
|
+
const queryFn = vi.fn<unknown[], Promise<string>>()
|
|
743
662
|
queryFn.mockImplementation(async () => {
|
|
744
663
|
await sleep(10)
|
|
745
664
|
return '23'
|
|
@@ -781,7 +700,7 @@ describe("useQuery's in Suspense mode", () => {
|
|
|
781
700
|
})
|
|
782
701
|
|
|
783
702
|
it('should error catched in error boundary without infinite loop', async () => {
|
|
784
|
-
const consoleMock =
|
|
703
|
+
const consoleMock = vi
|
|
785
704
|
.spyOn(console, 'error')
|
|
786
705
|
.mockImplementation(() => undefined)
|
|
787
706
|
const key = queryKey()
|
|
@@ -854,7 +773,7 @@ describe("useQuery's in Suspense mode", () => {
|
|
|
854
773
|
})
|
|
855
774
|
|
|
856
775
|
it('should error catched in error boundary without infinite loop when query keys changed', async () => {
|
|
857
|
-
const consoleMock =
|
|
776
|
+
const consoleMock = vi
|
|
858
777
|
.spyOn(console, 'error')
|
|
859
778
|
.mockImplementation(() => undefined)
|
|
860
779
|
let succeed = true
|
|
@@ -921,7 +840,7 @@ describe("useQuery's in Suspense mode", () => {
|
|
|
921
840
|
})
|
|
922
841
|
|
|
923
842
|
it('should error catched in error boundary without infinite loop when enabled changed', async () => {
|
|
924
|
-
const consoleMock =
|
|
843
|
+
const consoleMock = vi
|
|
925
844
|
.spyOn(console, 'error')
|
|
926
845
|
.mockImplementation(() => undefined)
|
|
927
846
|
function Page() {
|
|
@@ -14,6 +14,8 @@ import type {
|
|
|
14
14
|
UseInfiniteQueryResult,
|
|
15
15
|
} from '..'
|
|
16
16
|
import { QueryCache, useInfiniteQuery, keepPreviousData } from '..'
|
|
17
|
+
import { vi } from 'vitest'
|
|
18
|
+
import type { Mock } from 'vitest'
|
|
17
19
|
|
|
18
20
|
interface Result {
|
|
19
21
|
items: number[]
|
|
@@ -680,14 +682,14 @@ describe('useInfiniteQuery', () => {
|
|
|
680
682
|
it('should silently cancel an ongoing fetchNextPage request when another fetchNextPage is invoked', async () => {
|
|
681
683
|
const key = queryKey()
|
|
682
684
|
const start = 10
|
|
683
|
-
const onAborts:
|
|
684
|
-
const abortListeners:
|
|
685
|
-
const fetchPage =
|
|
686
|
-
|
|
687
|
-
|
|
685
|
+
const onAborts: Mock<any, any>[] = []
|
|
686
|
+
const abortListeners: Mock<any, any>[] = []
|
|
687
|
+
const fetchPage = vi.fn<
|
|
688
|
+
[QueryFunctionContext<typeof key, number>],
|
|
689
|
+
Promise<number>
|
|
688
690
|
>(async ({ pageParam, signal }) => {
|
|
689
|
-
const onAbort =
|
|
690
|
-
const abortListener =
|
|
691
|
+
const onAbort = vi.fn()
|
|
692
|
+
const abortListener = vi.fn()
|
|
691
693
|
onAborts.push(onAbort)
|
|
692
694
|
abortListeners.push(abortListener)
|
|
693
695
|
signal.onabort = onAbort
|
|
@@ -756,14 +758,14 @@ describe('useInfiniteQuery', () => {
|
|
|
756
758
|
it('should not cancel an ongoing fetchNextPage request when another fetchNextPage is invoked if `cancelRefetch: false` is used ', async () => {
|
|
757
759
|
const key = queryKey()
|
|
758
760
|
const start = 10
|
|
759
|
-
const onAborts:
|
|
760
|
-
const abortListeners:
|
|
761
|
-
const fetchPage =
|
|
762
|
-
|
|
763
|
-
|
|
761
|
+
const onAborts: Mock<any, any>[] = []
|
|
762
|
+
const abortListeners: Mock<any, any>[] = []
|
|
763
|
+
const fetchPage = vi.fn<
|
|
764
|
+
[QueryFunctionContext<typeof key, number>],
|
|
765
|
+
Promise<number>
|
|
764
766
|
>(async ({ pageParam, signal }) => {
|
|
765
|
-
const onAbort =
|
|
766
|
-
const abortListener =
|
|
767
|
+
const onAbort = vi.fn()
|
|
768
|
+
const abortListener = vi.fn()
|
|
767
769
|
onAborts.push(onAbort)
|
|
768
770
|
abortListeners.push(abortListener)
|
|
769
771
|
signal.onabort = onAbort
|
|
@@ -1491,11 +1493,11 @@ describe('useInfiniteQuery', () => {
|
|
|
1491
1493
|
|
|
1492
1494
|
it('should cancel the query function when there are no more subscriptions', async () => {
|
|
1493
1495
|
const key = queryKey()
|
|
1494
|
-
let cancelFn:
|
|
1496
|
+
let cancelFn: Mock = vi.fn()
|
|
1495
1497
|
|
|
1496
1498
|
const queryFn = ({ signal }: { signal?: AbortSignal }) => {
|
|
1497
1499
|
const promise = new Promise<string>((resolve, reject) => {
|
|
1498
|
-
cancelFn =
|
|
1500
|
+
cancelFn = vi.fn(() => reject('Cancelled'))
|
|
1499
1501
|
signal?.addEventListener('abort', cancelFn)
|
|
1500
1502
|
sleep(1000).then(() => resolve('OK'))
|
|
1501
1503
|
})
|
|
@@ -20,7 +20,23 @@ describe('pageParam', () => {
|
|
|
20
20
|
})
|
|
21
21
|
})
|
|
22
22
|
|
|
23
|
-
it('
|
|
23
|
+
it('direction should be passed to queryFn of useInfiniteQuery', () => {
|
|
24
|
+
doNotExecute(() => {
|
|
25
|
+
useInfiniteQuery({
|
|
26
|
+
queryKey: ['key'],
|
|
27
|
+
queryFn: ({ direction }) => {
|
|
28
|
+
const result: Expect<
|
|
29
|
+
Equal<'forward' | 'backward', typeof direction>
|
|
30
|
+
> = true
|
|
31
|
+
return result
|
|
32
|
+
},
|
|
33
|
+
defaultPageParam: 1,
|
|
34
|
+
getNextPageParam: () => undefined,
|
|
35
|
+
})
|
|
36
|
+
})
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
it('there should be no pageParam passed to the queryFn of useQuery', () => {
|
|
24
40
|
doNotExecute(() => {
|
|
25
41
|
useQuery({
|
|
26
42
|
queryKey: ['key'],
|
|
@@ -32,6 +48,18 @@ describe('pageParam', () => {
|
|
|
32
48
|
})
|
|
33
49
|
})
|
|
34
50
|
|
|
51
|
+
it('there should be no direction passed to the queryFn of useQuery', () => {
|
|
52
|
+
doNotExecute(() => {
|
|
53
|
+
useQuery({
|
|
54
|
+
queryKey: ['key'],
|
|
55
|
+
// @ts-expect-error there should be no pageParam passed to queryFn of useQuery
|
|
56
|
+
queryFn: ({ direction }) => {
|
|
57
|
+
return String(direction)
|
|
58
|
+
},
|
|
59
|
+
})
|
|
60
|
+
})
|
|
61
|
+
})
|
|
62
|
+
|
|
35
63
|
it('defaultPageParam should define type of param passed to queryFunctionContext for fetchInfiniteQuery', () => {
|
|
36
64
|
doNotExecute(() => {
|
|
37
65
|
const queryClient = new QueryClient()
|
|
@@ -60,7 +88,6 @@ describe('pageParam', () => {
|
|
|
60
88
|
})
|
|
61
89
|
})
|
|
62
90
|
})
|
|
63
|
-
|
|
64
91
|
describe('select', () => {
|
|
65
92
|
it('should still return paginated data if no select result', () => {
|
|
66
93
|
doNotExecute(() => {
|
|
@@ -101,24 +128,78 @@ describe('select', () => {
|
|
|
101
128
|
return result
|
|
102
129
|
})
|
|
103
130
|
})
|
|
104
|
-
|
|
131
|
+
})
|
|
132
|
+
describe('getNextPageParam / getPreviousPageParam', () => {
|
|
133
|
+
it('should get typed params', () => {
|
|
105
134
|
doNotExecute(() => {
|
|
106
135
|
const infiniteQuery = useInfiniteQuery({
|
|
107
136
|
queryKey: ['key'],
|
|
108
137
|
queryFn: ({ pageParam }) => {
|
|
109
|
-
return pageParam
|
|
138
|
+
return String(pageParam)
|
|
110
139
|
},
|
|
111
140
|
defaultPageParam: 1,
|
|
112
|
-
getNextPageParam: (
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
141
|
+
getNextPageParam: (
|
|
142
|
+
lastPage,
|
|
143
|
+
allPages,
|
|
144
|
+
lastPageParam,
|
|
145
|
+
allPageParams,
|
|
146
|
+
) => {
|
|
147
|
+
doNotExecute(() => {
|
|
148
|
+
const lastPageResult: Expect<Equal<string, typeof lastPage>> = true
|
|
149
|
+
return lastPageResult
|
|
150
|
+
})
|
|
151
|
+
doNotExecute(() => {
|
|
152
|
+
const allPagesResult: Expect<
|
|
153
|
+
Equal<Array<string>, typeof allPages>
|
|
154
|
+
> = true
|
|
155
|
+
return allPagesResult
|
|
156
|
+
})
|
|
157
|
+
doNotExecute(() => {
|
|
158
|
+
const lastPageParamResult: Expect<
|
|
159
|
+
Equal<number, typeof lastPageParam>
|
|
160
|
+
> = true
|
|
161
|
+
return lastPageParamResult
|
|
162
|
+
})
|
|
163
|
+
doNotExecute(() => {
|
|
164
|
+
const allPageParamsResult: Expect<
|
|
165
|
+
Equal<Array<number>, typeof allPageParams>
|
|
166
|
+
> = true
|
|
167
|
+
return allPageParamsResult
|
|
168
|
+
})
|
|
169
|
+
|
|
170
|
+
return undefined
|
|
118
171
|
},
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
172
|
+
getPreviousPageParam: (
|
|
173
|
+
firstPage,
|
|
174
|
+
allPages,
|
|
175
|
+
firstPageParam,
|
|
176
|
+
allPageParams,
|
|
177
|
+
) => {
|
|
178
|
+
doNotExecute(() => {
|
|
179
|
+
const firstPageResult: Expect<Equal<string, typeof firstPage>> =
|
|
180
|
+
true
|
|
181
|
+
return firstPageResult
|
|
182
|
+
})
|
|
183
|
+
doNotExecute(() => {
|
|
184
|
+
const allPagesResult: Expect<
|
|
185
|
+
Equal<Array<string>, typeof allPages>
|
|
186
|
+
> = true
|
|
187
|
+
return allPagesResult
|
|
188
|
+
})
|
|
189
|
+
doNotExecute(() => {
|
|
190
|
+
const firstPageParamResult: Expect<
|
|
191
|
+
Equal<number, typeof firstPageParam>
|
|
192
|
+
> = true
|
|
193
|
+
return firstPageParamResult
|
|
194
|
+
})
|
|
195
|
+
doNotExecute(() => {
|
|
196
|
+
const allPageParamsResult: Expect<
|
|
197
|
+
Equal<Array<number>, typeof allPageParams>
|
|
198
|
+
> = true
|
|
199
|
+
return allPageParamsResult
|
|
200
|
+
})
|
|
201
|
+
|
|
202
|
+
return undefined
|
|
122
203
|
},
|
|
123
204
|
})
|
|
124
205
|
|
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
setActTimeout,
|
|
14
14
|
sleep,
|
|
15
15
|
} from './utils'
|
|
16
|
+
import { vi } from 'vitest'
|
|
16
17
|
|
|
17
18
|
describe('useMutation', () => {
|
|
18
19
|
const queryCache = new QueryCache()
|
|
@@ -95,8 +96,8 @@ describe('useMutation', () => {
|
|
|
95
96
|
|
|
96
97
|
it('should be able to call `onSuccess` and `onSettled` after each successful mutate', async () => {
|
|
97
98
|
let count = 0
|
|
98
|
-
const onSuccessMock =
|
|
99
|
-
const onSettledMock =
|
|
99
|
+
const onSuccessMock = vi.fn()
|
|
100
|
+
const onSettledMock = vi.fn()
|
|
100
101
|
|
|
101
102
|
function Page() {
|
|
102
103
|
const { mutate } = useMutation({
|
|
@@ -151,7 +152,7 @@ describe('useMutation', () => {
|
|
|
151
152
|
let count = 0
|
|
152
153
|
type Value = { count: number }
|
|
153
154
|
|
|
154
|
-
const mutateFn =
|
|
155
|
+
const mutateFn = vi.fn<[value: Value], Promise<Value>>()
|
|
155
156
|
|
|
156
157
|
mutateFn.mockImplementationOnce(() => {
|
|
157
158
|
return Promise.reject(new Error('Error test Jonas'))
|
|
@@ -197,8 +198,8 @@ describe('useMutation', () => {
|
|
|
197
198
|
})
|
|
198
199
|
|
|
199
200
|
it('should be able to call `onError` and `onSettled` after each failed mutate', async () => {
|
|
200
|
-
const onErrorMock =
|
|
201
|
-
const onSettledMock =
|
|
201
|
+
const onErrorMock = vi.fn()
|
|
202
|
+
const onSettledMock = vi.fn()
|
|
202
203
|
let count = 0
|
|
203
204
|
|
|
204
205
|
function Page() {
|
|
@@ -488,7 +489,7 @@ describe('useMutation', () => {
|
|
|
488
489
|
|
|
489
490
|
it('should call onMutate even if paused', async () => {
|
|
490
491
|
const onlineMock = mockNavigatorOnLine(false)
|
|
491
|
-
const onMutate =
|
|
492
|
+
const onMutate = vi.fn()
|
|
492
493
|
let count = 0
|
|
493
494
|
|
|
494
495
|
function Page() {
|
|
@@ -687,8 +688,8 @@ describe('useMutation', () => {
|
|
|
687
688
|
fireEvent.click(getByText('unmount'))
|
|
688
689
|
})
|
|
689
690
|
|
|
690
|
-
it('should be able to throw an error when
|
|
691
|
-
const consoleMock =
|
|
691
|
+
it('should be able to throw an error when throwOnError is set to true', async () => {
|
|
692
|
+
const consoleMock = vi
|
|
692
693
|
.spyOn(console, 'error')
|
|
693
694
|
.mockImplementation(() => undefined)
|
|
694
695
|
function Page() {
|
|
@@ -698,7 +699,7 @@ describe('useMutation', () => {
|
|
|
698
699
|
err.stack = ''
|
|
699
700
|
return Promise.reject(err)
|
|
700
701
|
},
|
|
701
|
-
|
|
702
|
+
throwOnError: true,
|
|
702
703
|
})
|
|
703
704
|
|
|
704
705
|
return (
|
|
@@ -734,8 +735,8 @@ describe('useMutation', () => {
|
|
|
734
735
|
consoleMock.mockRestore()
|
|
735
736
|
})
|
|
736
737
|
|
|
737
|
-
it('should be able to throw an error when
|
|
738
|
-
const consoleMock =
|
|
738
|
+
it('should be able to throw an error when throwOnError is a function that returns true', async () => {
|
|
739
|
+
const consoleMock = vi
|
|
739
740
|
.spyOn(console, 'error')
|
|
740
741
|
.mockImplementation(() => undefined)
|
|
741
742
|
let boundary = false
|
|
@@ -746,7 +747,7 @@ describe('useMutation', () => {
|
|
|
746
747
|
err.stack = ''
|
|
747
748
|
return Promise.reject(err)
|
|
748
749
|
},
|
|
749
|
-
|
|
750
|
+
throwOnError: () => {
|
|
750
751
|
boundary = !boundary
|
|
751
752
|
return !boundary
|
|
752
753
|
},
|
|
@@ -788,8 +789,8 @@ describe('useMutation', () => {
|
|
|
788
789
|
})
|
|
789
790
|
|
|
790
791
|
it('should pass meta to mutation', async () => {
|
|
791
|
-
const errorMock =
|
|
792
|
-
const successMock =
|
|
792
|
+
const errorMock = vi.fn()
|
|
793
|
+
const successMock = vi.fn()
|
|
793
794
|
|
|
794
795
|
const queryClientMutationMeta = createQueryClient({
|
|
795
796
|
mutationCache: new MutationCache({
|
|
@@ -847,10 +848,10 @@ describe('useMutation', () => {
|
|
|
847
848
|
})
|
|
848
849
|
|
|
849
850
|
it('should call cache callbacks when unmounted', async () => {
|
|
850
|
-
const onSuccess =
|
|
851
|
-
const onSuccessMutate =
|
|
852
|
-
const onSettled =
|
|
853
|
-
const onSettledMutate =
|
|
851
|
+
const onSuccess = vi.fn()
|
|
852
|
+
const onSuccessMutate = vi.fn()
|
|
853
|
+
const onSettled = vi.fn()
|
|
854
|
+
const onSettledMutate = vi.fn()
|
|
854
855
|
const mutationKey = queryKey()
|
|
855
856
|
let count = 0
|
|
856
857
|
|
|
@@ -919,10 +920,10 @@ describe('useMutation', () => {
|
|
|
919
920
|
})
|
|
920
921
|
|
|
921
922
|
it('should call mutate callbacks only for the last observer', async () => {
|
|
922
|
-
const onSuccess =
|
|
923
|
-
const onSuccessMutate =
|
|
924
|
-
const onSettled =
|
|
925
|
-
const onSettledMutate =
|
|
923
|
+
const onSuccess = vi.fn()
|
|
924
|
+
const onSuccessMutate = vi.fn()
|
|
925
|
+
const onSettled = vi.fn()
|
|
926
|
+
const onSettledMutate = vi.fn()
|
|
926
927
|
let count = 0
|
|
927
928
|
|
|
928
929
|
function Page() {
|
|
@@ -1008,7 +1009,7 @@ describe('useMutation', () => {
|
|
|
1008
1009
|
|
|
1009
1010
|
it('should go to error state if onSuccess callback errors', async () => {
|
|
1010
1011
|
const error = new Error('error from onSuccess')
|
|
1011
|
-
const onError =
|
|
1012
|
+
const onError = vi.fn()
|
|
1012
1013
|
|
|
1013
1014
|
function Page() {
|
|
1014
1015
|
const mutation = useMutation({
|
|
@@ -1076,7 +1077,7 @@ describe('useMutation', () => {
|
|
|
1076
1077
|
it('should go to error state if onSettled callback errors', async () => {
|
|
1077
1078
|
const error = new Error('error from onSettled')
|
|
1078
1079
|
const mutateFnError = new Error('mutateFnError')
|
|
1079
|
-
const onError =
|
|
1080
|
+
const onError = vi.fn()
|
|
1080
1081
|
|
|
1081
1082
|
function Page() {
|
|
1082
1083
|
const mutation = useMutation({
|