@tanstack/react-query 5.0.0-alpha.4 → 5.0.0-alpha.6
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/lib/__tests__/ssr.test.d.ts +0 -3
- package/build/lib/__tests__/utils.d.ts +2 -3
- package/build/lib/suspense.esm.js +5 -5
- package/build/lib/suspense.esm.js.map +1 -1
- package/build/lib/suspense.js +5 -5
- package/build/lib/suspense.js.map +1 -1
- package/build/lib/useQueries.esm.js +10 -7
- package/build/lib/useQueries.esm.js.map +1 -1
- package/build/lib/useQueries.js +10 -7
- package/build/lib/useQueries.js.map +1 -1
- package/build/umd/index.development.js +52 -51
- 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 +3 -3
- package/src/__tests__/HydrationBoundary.test.tsx +4 -3
- package/src/__tests__/QueryClientProvider.test.tsx +2 -1
- package/src/__tests__/QueryResetErrorBoundary.test.tsx +12 -11
- package/src/__tests__/ssr-hydration.test.tsx +11 -10
- package/src/__tests__/ssr.test.tsx +4 -7
- package/src/__tests__/suspense.test.tsx +14 -13
- package/src/__tests__/useInfiniteQuery.test.tsx +18 -16
- package/src/__tests__/useInfiniteQuery.type.test.tsx +110 -2
- package/src/__tests__/useMutation.test.tsx +21 -20
- package/src/__tests__/useMutationState.test.tsx +1 -58
- package/src/__tests__/useQueries.test.tsx +13 -73
- package/src/__tests__/useQuery.test.tsx +30 -36
- package/src/__tests__/utils.tsx +3 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/react-query",
|
|
3
|
-
"version": "5.0.0-alpha.
|
|
3
|
+
"version": "5.0.0-alpha.6",
|
|
4
4
|
"description": "Hooks for managing, caching and syncing asynchronous and remote data in React",
|
|
5
5
|
"author": "tannerlinsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"react-error-boundary": "^3.1.4"
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@tanstack/query-core": "5.0.0-alpha.
|
|
38
|
+
"@tanstack/query-core": "5.0.0-alpha.6"
|
|
39
39
|
},
|
|
40
40
|
"peerDependencies": {
|
|
41
41
|
"react": "^18.0.0",
|
|
@@ -54,7 +54,7 @@
|
|
|
54
54
|
"clean": "rimraf ./build",
|
|
55
55
|
"test:eslint": "eslint --ext .ts,.tsx ./src",
|
|
56
56
|
"test:types": "tsc",
|
|
57
|
-
"test:lib": "
|
|
57
|
+
"test:lib": "vitest run --coverage",
|
|
58
58
|
"test:lib:dev": "pnpm run test:lib --watch",
|
|
59
59
|
"build:types": "tsc --build"
|
|
60
60
|
}
|
|
@@ -8,9 +8,10 @@ import {
|
|
|
8
8
|
useQuery,
|
|
9
9
|
dehydrate,
|
|
10
10
|
HydrationBoundary,
|
|
11
|
-
} from '
|
|
11
|
+
} from '..'
|
|
12
12
|
import { createQueryClient, sleep } from './utils'
|
|
13
13
|
import * as coreModule from '@tanstack/query-core'
|
|
14
|
+
import { vi } from 'vitest'
|
|
14
15
|
|
|
15
16
|
describe('React hydration', () => {
|
|
16
17
|
const fetchData: (value: string) => Promise<string> = (value) =>
|
|
@@ -213,7 +214,7 @@ describe('React hydration', () => {
|
|
|
213
214
|
const queryCache = new QueryCache()
|
|
214
215
|
const queryClient = createQueryClient({ queryCache })
|
|
215
216
|
|
|
216
|
-
const hydrateSpy =
|
|
217
|
+
const hydrateSpy = vi.spyOn(coreModule, 'hydrate')
|
|
217
218
|
|
|
218
219
|
function Page() {
|
|
219
220
|
return null
|
|
@@ -237,7 +238,7 @@ describe('React hydration', () => {
|
|
|
237
238
|
const queryCache = new QueryCache()
|
|
238
239
|
const queryClient = createQueryClient({ queryCache })
|
|
239
240
|
|
|
240
|
-
const hydrateSpy =
|
|
241
|
+
const hydrateSpy = vi.spyOn(coreModule, 'hydrate')
|
|
241
242
|
|
|
242
243
|
function Page() {
|
|
243
244
|
return null
|
|
@@ -3,6 +3,7 @@ import { render, waitFor } from '@testing-library/react'
|
|
|
3
3
|
|
|
4
4
|
import { sleep, queryKey, createQueryClient } from './utils'
|
|
5
5
|
import { QueryClientProvider, QueryCache, useQuery, useQueryClient } from '..'
|
|
6
|
+
import { vi } from 'vitest'
|
|
6
7
|
|
|
7
8
|
describe('QueryClientProvider', () => {
|
|
8
9
|
test('sets a specific cache for all queries to use', async () => {
|
|
@@ -142,7 +143,7 @@ describe('QueryClientProvider', () => {
|
|
|
142
143
|
|
|
143
144
|
describe('useQueryClient', () => {
|
|
144
145
|
test('should throw an error if no query client has been set', () => {
|
|
145
|
-
const consoleMock =
|
|
146
|
+
const consoleMock = vi
|
|
146
147
|
.spyOn(console, 'error')
|
|
147
148
|
.mockImplementation(() => undefined)
|
|
148
149
|
|
|
@@ -4,6 +4,7 @@ import * as React from 'react'
|
|
|
4
4
|
|
|
5
5
|
import { createQueryClient, queryKey, renderWithClient, sleep } from './utils'
|
|
6
6
|
import { QueryCache, QueryErrorResetBoundary, useQuery } from '..'
|
|
7
|
+
import { vi } from 'vitest'
|
|
7
8
|
|
|
8
9
|
// TODO: This should be removed with the types for react-error-boundary get updated.
|
|
9
10
|
declare module 'react-error-boundary' {
|
|
@@ -17,7 +18,7 @@ describe('QueryErrorResetBoundary', () => {
|
|
|
17
18
|
const queryClient = createQueryClient({ queryCache })
|
|
18
19
|
|
|
19
20
|
it('should retry fetch if the reset error boundary has been reset', async () => {
|
|
20
|
-
const consoleMock =
|
|
21
|
+
const consoleMock = vi
|
|
21
22
|
.spyOn(console, 'error')
|
|
22
23
|
.mockImplementation(() => undefined)
|
|
23
24
|
const key = queryKey()
|
|
@@ -75,7 +76,7 @@ describe('QueryErrorResetBoundary', () => {
|
|
|
75
76
|
})
|
|
76
77
|
|
|
77
78
|
it('should not throw error if query is disabled', async () => {
|
|
78
|
-
const consoleMock =
|
|
79
|
+
const consoleMock = vi
|
|
79
80
|
.spyOn(console, 'error')
|
|
80
81
|
.mockImplementation(() => undefined)
|
|
81
82
|
const key = queryKey()
|
|
@@ -139,7 +140,7 @@ describe('QueryErrorResetBoundary', () => {
|
|
|
139
140
|
})
|
|
140
141
|
|
|
141
142
|
it('should not throw error if query is disabled, and refetch if query becomes enabled again', async () => {
|
|
142
|
-
const consoleMock =
|
|
143
|
+
const consoleMock = vi
|
|
143
144
|
.spyOn(console, 'error')
|
|
144
145
|
.mockImplementation(() => undefined)
|
|
145
146
|
|
|
@@ -205,7 +206,7 @@ describe('QueryErrorResetBoundary', () => {
|
|
|
205
206
|
})
|
|
206
207
|
|
|
207
208
|
it('should throw error if query is disabled and manually refetched', async () => {
|
|
208
|
-
const consoleMock =
|
|
209
|
+
const consoleMock = vi
|
|
209
210
|
.spyOn(console, 'error')
|
|
210
211
|
.mockImplementation(() => undefined)
|
|
211
212
|
|
|
@@ -267,7 +268,7 @@ describe('QueryErrorResetBoundary', () => {
|
|
|
267
268
|
})
|
|
268
269
|
|
|
269
270
|
it('should not retry fetch if the reset error boundary has not been reset', async () => {
|
|
270
|
-
const consoleMock =
|
|
271
|
+
const consoleMock = vi
|
|
271
272
|
.spyOn(console, 'error')
|
|
272
273
|
.mockImplementation(() => undefined)
|
|
273
274
|
|
|
@@ -325,7 +326,7 @@ describe('QueryErrorResetBoundary', () => {
|
|
|
325
326
|
})
|
|
326
327
|
|
|
327
328
|
it('should retry fetch if the reset error boundary has been reset and the query contains data from a previous fetch', async () => {
|
|
328
|
-
const consoleMock =
|
|
329
|
+
const consoleMock = vi
|
|
329
330
|
.spyOn(console, 'error')
|
|
330
331
|
.mockImplementation(() => undefined)
|
|
331
332
|
|
|
@@ -385,7 +386,7 @@ describe('QueryErrorResetBoundary', () => {
|
|
|
385
386
|
})
|
|
386
387
|
|
|
387
388
|
it('should not retry fetch if the reset error boundary has not been reset after a previous reset', async () => {
|
|
388
|
-
const consoleMock =
|
|
389
|
+
const consoleMock = vi
|
|
389
390
|
.spyOn(console, 'error')
|
|
390
391
|
.mockImplementation(() => undefined)
|
|
391
392
|
|
|
@@ -453,7 +454,7 @@ describe('QueryErrorResetBoundary', () => {
|
|
|
453
454
|
})
|
|
454
455
|
|
|
455
456
|
it('should throw again on error after the reset error boundary has been reset', async () => {
|
|
456
|
-
const consoleMock =
|
|
457
|
+
const consoleMock = vi
|
|
457
458
|
.spyOn(console, 'error')
|
|
458
459
|
.mockImplementation(() => undefined)
|
|
459
460
|
|
|
@@ -511,7 +512,7 @@ describe('QueryErrorResetBoundary', () => {
|
|
|
511
512
|
})
|
|
512
513
|
|
|
513
514
|
it('should never render the component while the query is in error state', async () => {
|
|
514
|
-
const consoleMock =
|
|
515
|
+
const consoleMock = vi
|
|
515
516
|
.spyOn(console, 'error')
|
|
516
517
|
.mockImplementation(() => undefined)
|
|
517
518
|
|
|
@@ -578,7 +579,7 @@ describe('QueryErrorResetBoundary', () => {
|
|
|
578
579
|
})
|
|
579
580
|
|
|
580
581
|
it('should render children', async () => {
|
|
581
|
-
const consoleMock =
|
|
582
|
+
const consoleMock = vi
|
|
582
583
|
.spyOn(console, 'error')
|
|
583
584
|
.mockImplementation(() => undefined)
|
|
584
585
|
|
|
@@ -602,7 +603,7 @@ describe('QueryErrorResetBoundary', () => {
|
|
|
602
603
|
})
|
|
603
604
|
|
|
604
605
|
it('should show error boundary when using tracked queries even though we do not track the error field', async () => {
|
|
605
|
-
const consoleMock =
|
|
606
|
+
const consoleMock = vi
|
|
606
607
|
.spyOn(console, 'error')
|
|
607
608
|
.mockImplementation(() => undefined)
|
|
608
609
|
|
|
@@ -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'
|
|
@@ -192,7 +193,7 @@ describe("useQuery's in Suspense mode", () => {
|
|
|
192
193
|
it('should call onSuccess on the first successful call', async () => {
|
|
193
194
|
const key = queryKey()
|
|
194
195
|
|
|
195
|
-
const successFn =
|
|
196
|
+
const successFn = vi.fn()
|
|
196
197
|
|
|
197
198
|
function Page() {
|
|
198
199
|
useQuery({
|
|
@@ -225,8 +226,8 @@ describe("useQuery's in Suspense mode", () => {
|
|
|
225
226
|
it('should call every onSuccess handler within a suspense boundary', async () => {
|
|
226
227
|
const key = queryKey()
|
|
227
228
|
|
|
228
|
-
const successFn1 =
|
|
229
|
-
const successFn2 =
|
|
229
|
+
const successFn1 = vi.fn()
|
|
230
|
+
const successFn2 = vi.fn()
|
|
230
231
|
|
|
231
232
|
function FirstComponent() {
|
|
232
233
|
useQuery({
|
|
@@ -273,7 +274,7 @@ describe("useQuery's in Suspense mode", () => {
|
|
|
273
274
|
|
|
274
275
|
// https://github.com/tannerlinsley/react-query/issues/468
|
|
275
276
|
it('should reset error state if new component instances are mounted', async () => {
|
|
276
|
-
const consoleMock =
|
|
277
|
+
const consoleMock = vi
|
|
277
278
|
.spyOn(console, 'error')
|
|
278
279
|
.mockImplementation(() => undefined)
|
|
279
280
|
const key = queryKey()
|
|
@@ -345,7 +346,7 @@ describe("useQuery's in Suspense mode", () => {
|
|
|
345
346
|
})
|
|
346
347
|
|
|
347
348
|
it('should retry fetch if the reset error boundary has been reset', async () => {
|
|
348
|
-
const consoleMock =
|
|
349
|
+
const consoleMock = vi
|
|
349
350
|
.spyOn(console, 'error')
|
|
350
351
|
.mockImplementation(() => undefined)
|
|
351
352
|
const key = queryKey()
|
|
@@ -510,7 +511,7 @@ describe("useQuery's in Suspense mode", () => {
|
|
|
510
511
|
})
|
|
511
512
|
|
|
512
513
|
it('should retry fetch if the reset error boundary has been reset with global hook', async () => {
|
|
513
|
-
const consoleMock =
|
|
514
|
+
const consoleMock = vi
|
|
514
515
|
.spyOn(console, 'error')
|
|
515
516
|
.mockImplementation(() => undefined)
|
|
516
517
|
const key = queryKey()
|
|
@@ -574,7 +575,7 @@ describe("useQuery's in Suspense mode", () => {
|
|
|
574
575
|
})
|
|
575
576
|
|
|
576
577
|
it('should throw errors to the error boundary by default', async () => {
|
|
577
|
-
const consoleMock =
|
|
578
|
+
const consoleMock = vi
|
|
578
579
|
.spyOn(console, 'error')
|
|
579
580
|
.mockImplementation(() => undefined)
|
|
580
581
|
const key = queryKey()
|
|
@@ -655,7 +656,7 @@ describe("useQuery's in Suspense mode", () => {
|
|
|
655
656
|
})
|
|
656
657
|
|
|
657
658
|
it('should throw errors to the error boundary when a throwErrors function returns true', async () => {
|
|
658
|
-
const consoleMock =
|
|
659
|
+
const consoleMock = vi
|
|
659
660
|
.spyOn(console, 'error')
|
|
660
661
|
.mockImplementation(() => undefined)
|
|
661
662
|
const key = queryKey()
|
|
@@ -739,7 +740,7 @@ describe("useQuery's in Suspense mode", () => {
|
|
|
739
740
|
it('should not call the queryFn when not enabled', async () => {
|
|
740
741
|
const key = queryKey()
|
|
741
742
|
|
|
742
|
-
const queryFn =
|
|
743
|
+
const queryFn = vi.fn<unknown[], Promise<string>>()
|
|
743
744
|
queryFn.mockImplementation(async () => {
|
|
744
745
|
await sleep(10)
|
|
745
746
|
return '23'
|
|
@@ -781,7 +782,7 @@ describe("useQuery's in Suspense mode", () => {
|
|
|
781
782
|
})
|
|
782
783
|
|
|
783
784
|
it('should error catched in error boundary without infinite loop', async () => {
|
|
784
|
-
const consoleMock =
|
|
785
|
+
const consoleMock = vi
|
|
785
786
|
.spyOn(console, 'error')
|
|
786
787
|
.mockImplementation(() => undefined)
|
|
787
788
|
const key = queryKey()
|
|
@@ -854,7 +855,7 @@ describe("useQuery's in Suspense mode", () => {
|
|
|
854
855
|
})
|
|
855
856
|
|
|
856
857
|
it('should error catched in error boundary without infinite loop when query keys changed', async () => {
|
|
857
|
-
const consoleMock =
|
|
858
|
+
const consoleMock = vi
|
|
858
859
|
.spyOn(console, 'error')
|
|
859
860
|
.mockImplementation(() => undefined)
|
|
860
861
|
let succeed = true
|
|
@@ -921,7 +922,7 @@ describe("useQuery's in Suspense mode", () => {
|
|
|
921
922
|
})
|
|
922
923
|
|
|
923
924
|
it('should error catched in error boundary without infinite loop when enabled changed', async () => {
|
|
924
|
-
const consoleMock =
|
|
925
|
+
const consoleMock = vi
|
|
925
926
|
.spyOn(console, 'error')
|
|
926
927
|
.mockImplementation(() => undefined)
|
|
927
928
|
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(() => {
|
|
@@ -129,3 +156,84 @@ describe('select', () => {
|
|
|
129
156
|
})
|
|
130
157
|
})
|
|
131
158
|
})
|
|
159
|
+
describe('getNextPageParam / getPreviousPageParam', () => {
|
|
160
|
+
it('should get typed params', () => {
|
|
161
|
+
doNotExecute(() => {
|
|
162
|
+
const infiniteQuery = useInfiniteQuery({
|
|
163
|
+
queryKey: ['key'],
|
|
164
|
+
queryFn: ({ pageParam }) => {
|
|
165
|
+
return String(pageParam)
|
|
166
|
+
},
|
|
167
|
+
defaultPageParam: 1,
|
|
168
|
+
getNextPageParam: (
|
|
169
|
+
lastPage,
|
|
170
|
+
allPages,
|
|
171
|
+
lastPageParam,
|
|
172
|
+
allPageParams,
|
|
173
|
+
) => {
|
|
174
|
+
doNotExecute(() => {
|
|
175
|
+
const lastPageResult: Expect<Equal<string, typeof lastPage>> = true
|
|
176
|
+
return lastPageResult
|
|
177
|
+
})
|
|
178
|
+
doNotExecute(() => {
|
|
179
|
+
const allPagesResult: Expect<
|
|
180
|
+
Equal<Array<string>, typeof allPages>
|
|
181
|
+
> = true
|
|
182
|
+
return allPagesResult
|
|
183
|
+
})
|
|
184
|
+
doNotExecute(() => {
|
|
185
|
+
const lastPageParamResult: Expect<
|
|
186
|
+
Equal<number, typeof lastPageParam>
|
|
187
|
+
> = true
|
|
188
|
+
return lastPageParamResult
|
|
189
|
+
})
|
|
190
|
+
doNotExecute(() => {
|
|
191
|
+
const allPageParamsResult: Expect<
|
|
192
|
+
Equal<Array<number>, typeof allPageParams>
|
|
193
|
+
> = true
|
|
194
|
+
return allPageParamsResult
|
|
195
|
+
})
|
|
196
|
+
|
|
197
|
+
return undefined
|
|
198
|
+
},
|
|
199
|
+
getPreviousPageParam: (
|
|
200
|
+
firstPage,
|
|
201
|
+
allPages,
|
|
202
|
+
firstPageParam,
|
|
203
|
+
allPageParams,
|
|
204
|
+
) => {
|
|
205
|
+
doNotExecute(() => {
|
|
206
|
+
const firstPageResult: Expect<Equal<string, typeof firstPage>> =
|
|
207
|
+
true
|
|
208
|
+
return firstPageResult
|
|
209
|
+
})
|
|
210
|
+
doNotExecute(() => {
|
|
211
|
+
const allPagesResult: Expect<
|
|
212
|
+
Equal<Array<string>, typeof allPages>
|
|
213
|
+
> = true
|
|
214
|
+
return allPagesResult
|
|
215
|
+
})
|
|
216
|
+
doNotExecute(() => {
|
|
217
|
+
const firstPageParamResult: Expect<
|
|
218
|
+
Equal<number, typeof firstPageParam>
|
|
219
|
+
> = true
|
|
220
|
+
return firstPageParamResult
|
|
221
|
+
})
|
|
222
|
+
doNotExecute(() => {
|
|
223
|
+
const allPageParamsResult: Expect<
|
|
224
|
+
Equal<Array<number>, typeof allPageParams>
|
|
225
|
+
> = true
|
|
226
|
+
return allPageParamsResult
|
|
227
|
+
})
|
|
228
|
+
|
|
229
|
+
return undefined
|
|
230
|
+
},
|
|
231
|
+
})
|
|
232
|
+
|
|
233
|
+
const result: Expect<
|
|
234
|
+
Equal<InfiniteData<string> | undefined, (typeof infiniteQuery)['data']>
|
|
235
|
+
> = true
|
|
236
|
+
return result
|
|
237
|
+
})
|
|
238
|
+
})
|
|
239
|
+
})
|