@tanstack/query-core 5.0.0-alpha.2 → 5.0.0-alpha.21
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/_virtual/_rollupPluginBabelHelpers.esm.js +13 -0
- package/build/lib/_virtual/_rollupPluginBabelHelpers.esm.js.map +1 -0
- package/build/lib/_virtual/_rollupPluginBabelHelpers.js +16 -0
- package/build/lib/_virtual/_rollupPluginBabelHelpers.js.map +1 -0
- package/build/lib/focusManager.esm.js +31 -15
- package/build/lib/focusManager.esm.js.map +1 -1
- package/build/lib/focusManager.js +31 -15
- package/build/lib/focusManager.js.map +1 -1
- package/build/lib/hydration.d.ts +2 -6
- package/build/lib/hydration.esm.js +9 -20
- package/build/lib/hydration.esm.js.map +1 -1
- package/build/lib/hydration.js +9 -20
- package/build/lib/hydration.js.map +1 -1
- package/build/lib/hydration.mjs +4 -18
- package/build/lib/hydration.mjs.map +1 -1
- package/build/lib/index.d.ts +3 -3
- package/build/lib/infiniteQueryBehavior.d.ts +2 -2
- package/build/lib/infiniteQueryBehavior.esm.js +52 -48
- package/build/lib/infiniteQueryBehavior.esm.js.map +1 -1
- package/build/lib/infiniteQueryBehavior.js +52 -48
- package/build/lib/infiniteQueryBehavior.js.map +1 -1
- package/build/lib/infiniteQueryBehavior.mjs +48 -45
- package/build/lib/infiniteQueryBehavior.mjs.map +1 -1
- package/build/lib/infiniteQueryObserver.d.ts +6 -6
- package/build/lib/infiniteQueryObserver.esm.js +7 -8
- package/build/lib/infiniteQueryObserver.esm.js.map +1 -1
- package/build/lib/infiniteQueryObserver.js +7 -8
- package/build/lib/infiniteQueryObserver.js.map +1 -1
- package/build/lib/infiniteQueryObserver.mjs +4 -6
- package/build/lib/infiniteQueryObserver.mjs.map +1 -1
- package/build/lib/mutation.d.ts +2 -1
- package/build/lib/mutation.esm.js +134 -101
- package/build/lib/mutation.esm.js.map +1 -1
- package/build/lib/mutation.js +134 -101
- package/build/lib/mutation.js.map +1 -1
- package/build/lib/mutation.mjs +10 -2
- package/build/lib/mutation.mjs.map +1 -1
- package/build/lib/mutationCache.d.ts +2 -2
- package/build/lib/mutationCache.esm.js +34 -20
- package/build/lib/mutationCache.esm.js.map +1 -1
- package/build/lib/mutationCache.js +34 -20
- package/build/lib/mutationCache.js.map +1 -1
- package/build/lib/mutationCache.mjs +5 -4
- package/build/lib/mutationCache.mjs.map +1 -1
- package/build/lib/mutationObserver.esm.js +81 -50
- package/build/lib/mutationObserver.esm.js.map +1 -1
- package/build/lib/mutationObserver.js +81 -50
- package/build/lib/mutationObserver.js.map +1 -1
- package/build/lib/mutationObserver.mjs +1 -0
- package/build/lib/mutationObserver.mjs.map +1 -1
- package/build/lib/onlineManager.esm.js +29 -14
- package/build/lib/onlineManager.esm.js.map +1 -1
- package/build/lib/onlineManager.js +29 -14
- package/build/lib/onlineManager.js.map +1 -1
- package/build/lib/queriesObserver.esm.js +95 -65
- package/build/lib/queriesObserver.esm.js.map +1 -1
- package/build/lib/queriesObserver.js +95 -65
- package/build/lib/queriesObserver.js.map +1 -1
- package/build/lib/queriesObserver.mjs +4 -3
- package/build/lib/queriesObserver.mjs.map +1 -1
- package/build/lib/query.d.ts +4 -3
- package/build/lib/query.esm.js +210 -161
- package/build/lib/query.esm.js.map +1 -1
- package/build/lib/query.js +210 -161
- package/build/lib/query.js.map +1 -1
- package/build/lib/query.mjs +1 -1
- package/build/lib/query.mjs.map +1 -1
- package/build/lib/queryCache.d.ts +2 -2
- package/build/lib/queryCache.esm.js +21 -13
- package/build/lib/queryCache.esm.js.map +1 -1
- package/build/lib/queryCache.js +21 -13
- package/build/lib/queryCache.js.map +1 -1
- package/build/lib/queryCache.mjs +5 -4
- package/build/lib/queryCache.mjs.map +1 -1
- package/build/lib/queryClient.esm.js +107 -66
- package/build/lib/queryClient.esm.js.map +1 -1
- package/build/lib/queryClient.js +107 -66
- package/build/lib/queryClient.js.map +1 -1
- package/build/lib/queryClient.mjs +5 -4
- package/build/lib/queryClient.mjs.map +1 -1
- package/build/lib/queryObserver.d.ts +2 -4
- package/build/lib/queryObserver.esm.js +299 -211
- package/build/lib/queryObserver.esm.js.map +1 -1
- package/build/lib/queryObserver.js +297 -209
- package/build/lib/queryObserver.js.map +1 -1
- package/build/lib/queryObserver.mjs +11 -23
- package/build/lib/queryObserver.mjs.map +1 -1
- package/build/lib/removable.esm.js +13 -6
- package/build/lib/removable.esm.js.map +1 -1
- package/build/lib/removable.js +13 -6
- package/build/lib/removable.js.map +1 -1
- package/build/lib/retryer.esm.js +15 -14
- package/build/lib/retryer.esm.js.map +1 -1
- package/build/lib/retryer.js +15 -14
- package/build/lib/retryer.js.map +1 -1
- package/build/lib/tests/utils.d.ts +4 -5
- package/build/lib/types.d.ts +5 -15
- package/build/lib/utils.esm.js +1 -1
- package/build/lib/utils.esm.js.map +1 -1
- package/build/lib/utils.js +1 -1
- package/build/lib/utils.js.map +1 -1
- package/build/umd/index.development.js +97 -109
- 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 +2 -2
- package/src/hydration.ts +18 -37
- package/src/index.ts +2 -4
- package/src/infiniteQueryBehavior.ts +52 -60
- package/src/infiniteQueryObserver.ts +15 -10
- package/src/mutation.ts +14 -5
- package/src/mutationCache.ts +6 -6
- package/src/mutationObserver.ts +1 -0
- package/src/queriesObserver.ts +8 -8
- package/src/query.ts +13 -8
- package/src/queryCache.ts +9 -8
- package/src/queryClient.ts +2 -4
- package/src/queryObserver.ts +17 -31
- package/src/tests/focusManager.test.tsx +12 -14
- package/src/tests/hydration.test.tsx +22 -17
- package/src/tests/infiniteQueryBehavior.test.tsx +16 -9
- package/src/tests/infiniteQueryObserver.test.tsx +62 -1
- package/src/tests/mutationCache.test.tsx +11 -10
- package/src/tests/mutationObserver.test.tsx +3 -2
- package/src/tests/mutations.test.tsx +41 -9
- package/src/tests/notifyManager.test.tsx +7 -6
- package/src/tests/onlineManager.test.tsx +12 -17
- package/src/tests/queriesObserver.test.tsx +18 -17
- package/src/tests/query.test.tsx +18 -17
- package/src/tests/queryCache.test.tsx +14 -13
- package/src/tests/queryClient.test.tsx +49 -48
- package/src/tests/queryObserver.test.tsx +65 -9
- package/src/tests/utils.test.tsx +2 -1
- package/src/tests/utils.ts +5 -4
- package/src/types.ts +9 -13
|
@@ -2,6 +2,8 @@ import type { QueryClient } from '..'
|
|
|
2
2
|
import { createQueryClient, executeMutation, queryKey, sleep } from './utils'
|
|
3
3
|
import type { MutationState } from '../mutation'
|
|
4
4
|
import { MutationObserver } from '../mutationObserver'
|
|
5
|
+
import { vi } from 'vitest'
|
|
6
|
+
import { waitFor } from '@testing-library/react'
|
|
5
7
|
|
|
6
8
|
describe('mutations', () => {
|
|
7
9
|
let queryClient: QueryClient
|
|
@@ -32,7 +34,7 @@ describe('mutations', () => {
|
|
|
32
34
|
|
|
33
35
|
test('setMutationDefaults should be able to set defaults', async () => {
|
|
34
36
|
const key = queryKey()
|
|
35
|
-
const fn =
|
|
37
|
+
const fn = vi.fn()
|
|
36
38
|
|
|
37
39
|
queryClient.setMutationDefaults(key, {
|
|
38
40
|
mutationFn: fn,
|
|
@@ -249,9 +251,9 @@ describe('mutations', () => {
|
|
|
249
251
|
test('should be able to restore a mutation', async () => {
|
|
250
252
|
const key = queryKey()
|
|
251
253
|
|
|
252
|
-
const onMutate =
|
|
253
|
-
const onSuccess =
|
|
254
|
-
const onSettled =
|
|
254
|
+
const onMutate = vi.fn()
|
|
255
|
+
const onSuccess = vi.fn()
|
|
256
|
+
const onSettled = vi.fn()
|
|
255
257
|
|
|
256
258
|
queryClient.setMutationDefaults(key, {
|
|
257
259
|
mutationFn: async (text: string) => text,
|
|
@@ -316,7 +318,7 @@ describe('mutations', () => {
|
|
|
316
318
|
const observer = new MutationObserver(queryClient, {})
|
|
317
319
|
const currentMutation = mutationCache.build(queryClient, {})
|
|
318
320
|
|
|
319
|
-
const fn =
|
|
321
|
+
const fn = vi.fn()
|
|
320
322
|
|
|
321
323
|
const unsubscribe = mutationCache.subscribe((event) => {
|
|
322
324
|
fn(event.type)
|
|
@@ -347,8 +349,8 @@ describe('mutations', () => {
|
|
|
347
349
|
})
|
|
348
350
|
|
|
349
351
|
test('mutate update the mutation state even without an active subscription', async () => {
|
|
350
|
-
const onSuccess =
|
|
351
|
-
const onSettled =
|
|
352
|
+
const onSuccess = vi.fn()
|
|
353
|
+
const onSettled = vi.fn()
|
|
352
354
|
|
|
353
355
|
const mutation = new MutationObserver(queryClient, {
|
|
354
356
|
mutationFn: async () => {
|
|
@@ -363,8 +365,8 @@ describe('mutations', () => {
|
|
|
363
365
|
})
|
|
364
366
|
|
|
365
367
|
test('mutate update the mutation state even without an active subscription', async () => {
|
|
366
|
-
const onSuccess =
|
|
367
|
-
const onSettled =
|
|
368
|
+
const onSuccess = vi.fn()
|
|
369
|
+
const onSettled = vi.fn()
|
|
368
370
|
|
|
369
371
|
const mutation = new MutationObserver(queryClient, {
|
|
370
372
|
mutationFn: async () => {
|
|
@@ -377,4 +379,34 @@ describe('mutations', () => {
|
|
|
377
379
|
expect(onSuccess).not.toHaveBeenCalled()
|
|
378
380
|
expect(onSettled).not.toHaveBeenCalled()
|
|
379
381
|
})
|
|
382
|
+
|
|
383
|
+
test('mutation callbacks should see updated options', async () => {
|
|
384
|
+
const onSuccess = vi.fn()
|
|
385
|
+
|
|
386
|
+
const mutation = new MutationObserver(queryClient, {
|
|
387
|
+
mutationFn: async () => {
|
|
388
|
+
sleep(100)
|
|
389
|
+
return 'update'
|
|
390
|
+
},
|
|
391
|
+
onSuccess: () => {
|
|
392
|
+
onSuccess(1)
|
|
393
|
+
},
|
|
394
|
+
})
|
|
395
|
+
|
|
396
|
+
void mutation.mutate()
|
|
397
|
+
|
|
398
|
+
mutation.setOptions({
|
|
399
|
+
mutationFn: async () => {
|
|
400
|
+
sleep(100)
|
|
401
|
+
return 'update'
|
|
402
|
+
},
|
|
403
|
+
onSuccess: () => {
|
|
404
|
+
onSuccess(2)
|
|
405
|
+
},
|
|
406
|
+
})
|
|
407
|
+
|
|
408
|
+
await waitFor(() => expect(onSuccess).toHaveBeenCalledTimes(1))
|
|
409
|
+
|
|
410
|
+
expect(onSuccess).toHaveBeenCalledWith(2)
|
|
411
|
+
})
|
|
380
412
|
})
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { createNotifyManager } from '../notifyManager'
|
|
2
2
|
import { sleep } from './utils'
|
|
3
|
+
import { vi } from 'vitest'
|
|
3
4
|
|
|
4
5
|
describe('notifyManager', () => {
|
|
5
6
|
it('should use default notifyFn', async () => {
|
|
6
7
|
const notifyManagerTest = createNotifyManager()
|
|
7
|
-
const callbackSpy =
|
|
8
|
+
const callbackSpy = vi.fn()
|
|
8
9
|
notifyManagerTest.schedule(callbackSpy)
|
|
9
10
|
await sleep(1)
|
|
10
11
|
expect(callbackSpy).toHaveBeenCalled()
|
|
@@ -12,13 +13,13 @@ describe('notifyManager', () => {
|
|
|
12
13
|
|
|
13
14
|
it('should use default batchNotifyFn', async () => {
|
|
14
15
|
const notifyManagerTest = createNotifyManager()
|
|
15
|
-
const callbackScheduleSpy =
|
|
16
|
+
const callbackScheduleSpy = vi
|
|
16
17
|
.fn()
|
|
17
18
|
.mockImplementation(async () => await sleep(20))
|
|
18
|
-
const callbackBatchLevel2Spy =
|
|
19
|
+
const callbackBatchLevel2Spy = vi.fn().mockImplementation(async () => {
|
|
19
20
|
notifyManagerTest.schedule(callbackScheduleSpy)
|
|
20
21
|
})
|
|
21
|
-
const callbackBatchLevel1Spy =
|
|
22
|
+
const callbackBatchLevel1Spy = vi.fn().mockImplementation(async () => {
|
|
22
23
|
notifyManagerTest.batch(callbackBatchLevel2Spy)
|
|
23
24
|
})
|
|
24
25
|
|
|
@@ -32,13 +33,13 @@ describe('notifyManager', () => {
|
|
|
32
33
|
|
|
33
34
|
it('should notify if error is thrown', async () => {
|
|
34
35
|
const notifyManagerTest = createNotifyManager()
|
|
35
|
-
const notifySpy =
|
|
36
|
+
const notifySpy = vi.fn()
|
|
36
37
|
|
|
37
38
|
notifyManagerTest.setNotifyFunction(notifySpy)
|
|
38
39
|
|
|
39
40
|
try {
|
|
40
41
|
notifyManagerTest.batch(() => {
|
|
41
|
-
notifyManagerTest.schedule(
|
|
42
|
+
notifyManagerTest.schedule(vi.fn)
|
|
42
43
|
throw new Error('Foo')
|
|
43
44
|
})
|
|
44
45
|
} catch {}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { OnlineManager } from '../onlineManager'
|
|
2
2
|
import { setIsServer, sleep } from './utils'
|
|
3
|
+
import { vi } from 'vitest'
|
|
3
4
|
|
|
4
5
|
describe('onlineManager', () => {
|
|
5
6
|
let onlineManager: OnlineManager
|
|
@@ -8,7 +9,7 @@ describe('onlineManager', () => {
|
|
|
8
9
|
})
|
|
9
10
|
|
|
10
11
|
test('isOnline should return true if navigator is undefined', () => {
|
|
11
|
-
const navigatorSpy =
|
|
12
|
+
const navigatorSpy = vi.spyOn(globalThis, 'navigator', 'get')
|
|
12
13
|
|
|
13
14
|
// Force navigator to be undefined
|
|
14
15
|
//@ts-expect-error
|
|
@@ -19,7 +20,7 @@ describe('onlineManager', () => {
|
|
|
19
20
|
})
|
|
20
21
|
|
|
21
22
|
test('isOnline should return true if navigator.onLine is true', () => {
|
|
22
|
-
const navigatorSpy =
|
|
23
|
+
const navigatorSpy = vi.spyOn(navigator, 'onLine', 'get')
|
|
23
24
|
navigatorSpy.mockImplementation(() => true)
|
|
24
25
|
|
|
25
26
|
expect(onlineManager.isOnline()).toBeTruthy()
|
|
@@ -46,8 +47,8 @@ describe('onlineManager', () => {
|
|
|
46
47
|
})
|
|
47
48
|
|
|
48
49
|
test('setEventListener should call previous remove handler when replacing an event listener', () => {
|
|
49
|
-
const remove1Spy =
|
|
50
|
-
const remove2Spy =
|
|
50
|
+
const remove1Spy = vi.fn()
|
|
51
|
+
const remove2Spy = vi.fn()
|
|
51
52
|
|
|
52
53
|
onlineManager.setEventListener(() => remove1Spy)
|
|
53
54
|
onlineManager.setEventListener(() => remove2Spy)
|
|
@@ -59,7 +60,7 @@ describe('onlineManager', () => {
|
|
|
59
60
|
test('cleanup (removeEventListener) should not be called if window is not defined', async () => {
|
|
60
61
|
const restoreIsServer = setIsServer(true)
|
|
61
62
|
|
|
62
|
-
const removeEventListenerSpy =
|
|
63
|
+
const removeEventListenerSpy = vi.spyOn(globalThis, 'removeEventListener')
|
|
63
64
|
|
|
64
65
|
const unsubscribe = onlineManager.subscribe(() => undefined)
|
|
65
66
|
|
|
@@ -76,7 +77,7 @@ describe('onlineManager', () => {
|
|
|
76
77
|
// @ts-expect-error
|
|
77
78
|
globalThis.window.addEventListener = undefined
|
|
78
79
|
|
|
79
|
-
const removeEventListenerSpy =
|
|
80
|
+
const removeEventListenerSpy = vi.spyOn(globalThis, 'removeEventListener')
|
|
80
81
|
|
|
81
82
|
const unsubscribe = onlineManager.subscribe(() => undefined)
|
|
82
83
|
|
|
@@ -88,12 +89,9 @@ describe('onlineManager', () => {
|
|
|
88
89
|
})
|
|
89
90
|
|
|
90
91
|
test('it should replace default window listener when a new event listener is set', async () => {
|
|
91
|
-
const addEventListenerSpy =
|
|
92
|
-
globalThis.window,
|
|
93
|
-
'addEventListener',
|
|
94
|
-
)
|
|
92
|
+
const addEventListenerSpy = vi.spyOn(globalThis.window, 'addEventListener')
|
|
95
93
|
|
|
96
|
-
const removeEventListenerSpy =
|
|
94
|
+
const removeEventListenerSpy = vi.spyOn(
|
|
97
95
|
globalThis.window,
|
|
98
96
|
'removeEventListener',
|
|
99
97
|
)
|
|
@@ -116,12 +114,9 @@ describe('onlineManager', () => {
|
|
|
116
114
|
})
|
|
117
115
|
|
|
118
116
|
test('should call removeEventListener when last listener unsubscribes', () => {
|
|
119
|
-
const addEventListenerSpy =
|
|
120
|
-
globalThis.window,
|
|
121
|
-
'addEventListener',
|
|
122
|
-
)
|
|
117
|
+
const addEventListenerSpy = vi.spyOn(globalThis.window, 'addEventListener')
|
|
123
118
|
|
|
124
|
-
const removeEventListenerSpy =
|
|
119
|
+
const removeEventListenerSpy = vi.spyOn(
|
|
125
120
|
globalThis.window,
|
|
126
121
|
'removeEventListener',
|
|
127
122
|
)
|
|
@@ -137,7 +132,7 @@ describe('onlineManager', () => {
|
|
|
137
132
|
})
|
|
138
133
|
|
|
139
134
|
test('should keep setup function even if last listener unsubscribes', () => {
|
|
140
|
-
const setupSpy =
|
|
135
|
+
const setupSpy = vi.fn().mockImplementation(() => () => undefined)
|
|
141
136
|
|
|
142
137
|
onlineManager.setEventListener(setupSpy)
|
|
143
138
|
|
|
@@ -2,6 +2,7 @@ import { waitFor } from '@testing-library/react'
|
|
|
2
2
|
import { sleep, queryKey, createQueryClient } from './utils'
|
|
3
3
|
import type { QueryClient, QueryObserverResult } from '..'
|
|
4
4
|
import { QueriesObserver } from '..'
|
|
5
|
+
import { vi } from 'vitest'
|
|
5
6
|
|
|
6
7
|
describe('queriesObserver', () => {
|
|
7
8
|
let queryClient: QueryClient
|
|
@@ -18,8 +19,8 @@ describe('queriesObserver', () => {
|
|
|
18
19
|
test('should return an array with all query results', async () => {
|
|
19
20
|
const key1 = queryKey()
|
|
20
21
|
const key2 = queryKey()
|
|
21
|
-
const queryFn1 =
|
|
22
|
-
const queryFn2 =
|
|
22
|
+
const queryFn1 = vi.fn().mockReturnValue(1)
|
|
23
|
+
const queryFn2 = vi.fn().mockReturnValue(2)
|
|
23
24
|
const observer = new QueriesObserver(queryClient, [
|
|
24
25
|
{ queryKey: key1, queryFn: queryFn1 },
|
|
25
26
|
{ queryKey: key2, queryFn: queryFn2 },
|
|
@@ -34,11 +35,11 @@ describe('queriesObserver', () => {
|
|
|
34
35
|
})
|
|
35
36
|
|
|
36
37
|
test('should still return value for undefined query key', async () => {
|
|
37
|
-
const consoleMock =
|
|
38
|
+
const consoleMock = vi.spyOn(console, 'error')
|
|
38
39
|
consoleMock.mockImplementation(() => undefined)
|
|
39
40
|
const key1 = queryKey()
|
|
40
|
-
const queryFn1 =
|
|
41
|
-
const queryFn2 =
|
|
41
|
+
const queryFn1 = vi.fn().mockReturnValue(1)
|
|
42
|
+
const queryFn2 = vi.fn().mockReturnValue(2)
|
|
42
43
|
const observer = new QueriesObserver(queryClient, [
|
|
43
44
|
{ queryKey: key1, queryFn: queryFn1 },
|
|
44
45
|
{ queryKey: undefined, queryFn: queryFn2 },
|
|
@@ -61,8 +62,8 @@ describe('queriesObserver', () => {
|
|
|
61
62
|
test('should update when a query updates', async () => {
|
|
62
63
|
const key1 = queryKey()
|
|
63
64
|
const key2 = queryKey()
|
|
64
|
-
const queryFn1 =
|
|
65
|
-
const queryFn2 =
|
|
65
|
+
const queryFn1 = vi.fn().mockReturnValue(1)
|
|
66
|
+
const queryFn2 = vi.fn().mockReturnValue(2)
|
|
66
67
|
const observer = new QueriesObserver(queryClient, [
|
|
67
68
|
{ queryKey: key1, queryFn: queryFn1 },
|
|
68
69
|
{ queryKey: key2, queryFn: queryFn2 },
|
|
@@ -106,8 +107,8 @@ describe('queriesObserver', () => {
|
|
|
106
107
|
test('should update when a query is removed', async () => {
|
|
107
108
|
const key1 = queryKey()
|
|
108
109
|
const key2 = queryKey()
|
|
109
|
-
const queryFn1 =
|
|
110
|
-
const queryFn2 =
|
|
110
|
+
const queryFn1 = vi.fn().mockReturnValue(1)
|
|
111
|
+
const queryFn2 = vi.fn().mockReturnValue(2)
|
|
111
112
|
const observer = new QueriesObserver(queryClient, [
|
|
112
113
|
{ queryKey: key1, queryFn: queryFn1 },
|
|
113
114
|
{ queryKey: key2, queryFn: queryFn2 },
|
|
@@ -153,8 +154,8 @@ describe('queriesObserver', () => {
|
|
|
153
154
|
test('should update when a query changed position', async () => {
|
|
154
155
|
const key1 = queryKey()
|
|
155
156
|
const key2 = queryKey()
|
|
156
|
-
const queryFn1 =
|
|
157
|
-
const queryFn2 =
|
|
157
|
+
const queryFn1 = vi.fn().mockReturnValue(1)
|
|
158
|
+
const queryFn2 = vi.fn().mockReturnValue(2)
|
|
158
159
|
const observer = new QueriesObserver(queryClient, [
|
|
159
160
|
{ queryKey: key1, queryFn: queryFn1 },
|
|
160
161
|
{ queryKey: key2, queryFn: queryFn2 },
|
|
@@ -201,8 +202,8 @@ describe('queriesObserver', () => {
|
|
|
201
202
|
test('should not update when nothing has changed', async () => {
|
|
202
203
|
const key1 = queryKey()
|
|
203
204
|
const key2 = queryKey()
|
|
204
|
-
const queryFn1 =
|
|
205
|
-
const queryFn2 =
|
|
205
|
+
const queryFn1 = vi.fn().mockReturnValue(1)
|
|
206
|
+
const queryFn2 = vi.fn().mockReturnValue(2)
|
|
206
207
|
const observer = new QueriesObserver(queryClient, [
|
|
207
208
|
{ queryKey: key1, queryFn: queryFn1 },
|
|
208
209
|
{ queryKey: key2, queryFn: queryFn2 },
|
|
@@ -245,8 +246,8 @@ describe('queriesObserver', () => {
|
|
|
245
246
|
test('should trigger all fetches when subscribed', async () => {
|
|
246
247
|
const key1 = queryKey()
|
|
247
248
|
const key2 = queryKey()
|
|
248
|
-
const queryFn1 =
|
|
249
|
-
const queryFn2 =
|
|
249
|
+
const queryFn1 = vi.fn().mockReturnValue(1)
|
|
250
|
+
const queryFn2 = vi.fn().mockReturnValue(2)
|
|
250
251
|
const observer = new QueriesObserver(queryClient, [
|
|
251
252
|
{ queryKey: key1, queryFn: queryFn1 },
|
|
252
253
|
{ queryKey: key2, queryFn: queryFn2 },
|
|
@@ -270,8 +271,8 @@ describe('queriesObserver', () => {
|
|
|
270
271
|
},
|
|
271
272
|
])
|
|
272
273
|
|
|
273
|
-
const subscription1Handler =
|
|
274
|
-
const subscription2Handler =
|
|
274
|
+
const subscription1Handler = vi.fn()
|
|
275
|
+
const subscription2Handler = vi.fn()
|
|
275
276
|
|
|
276
277
|
const unsubscribe1 = observer.subscribe(subscription1Handler)
|
|
277
278
|
const unsubscribe2 = observer.subscribe(subscription2Handler)
|
package/src/tests/query.test.tsx
CHANGED
|
@@ -12,6 +12,7 @@ import type {
|
|
|
12
12
|
} from '..'
|
|
13
13
|
import { QueryObserver, isCancelledError, onlineManager } from '..'
|
|
14
14
|
import { waitFor } from '@testing-library/react'
|
|
15
|
+
import { vi } from 'vitest'
|
|
15
16
|
|
|
16
17
|
describe('query', () => {
|
|
17
18
|
let queryClient: QueryClient
|
|
@@ -186,10 +187,10 @@ describe('query', () => {
|
|
|
186
187
|
test('should provide context to queryFn', async () => {
|
|
187
188
|
const key = queryKey()
|
|
188
189
|
|
|
189
|
-
const queryFn =
|
|
190
|
+
const queryFn = vi
|
|
190
191
|
.fn<
|
|
191
|
-
|
|
192
|
-
|
|
192
|
+
[QueryFunctionContext<ReturnType<typeof queryKey>>],
|
|
193
|
+
Promise<'data'>
|
|
193
194
|
>()
|
|
194
195
|
.mockResolvedValue('data')
|
|
195
196
|
|
|
@@ -273,12 +274,12 @@ describe('query', () => {
|
|
|
273
274
|
test('should provide an AbortSignal to the queryFn that provides info about the cancellation state', async () => {
|
|
274
275
|
const key = queryKey()
|
|
275
276
|
|
|
276
|
-
const queryFn =
|
|
277
|
-
|
|
278
|
-
|
|
277
|
+
const queryFn = vi.fn<
|
|
278
|
+
[QueryFunctionContext<ReturnType<typeof queryKey>>],
|
|
279
|
+
Promise<unknown>
|
|
279
280
|
>()
|
|
280
|
-
const onAbort =
|
|
281
|
-
const abortListener =
|
|
281
|
+
const onAbort = vi.fn()
|
|
282
|
+
const abortListener = vi.fn()
|
|
282
283
|
let error
|
|
283
284
|
|
|
284
285
|
queryFn.mockImplementation(async ({ signal }) => {
|
|
@@ -323,7 +324,7 @@ describe('query', () => {
|
|
|
323
324
|
test('should not continue if explicitly cancelled', async () => {
|
|
324
325
|
const key = queryKey()
|
|
325
326
|
|
|
326
|
-
const queryFn =
|
|
327
|
+
const queryFn = vi.fn<unknown[], unknown>()
|
|
327
328
|
|
|
328
329
|
queryFn.mockImplementation(async () => {
|
|
329
330
|
await sleep(10)
|
|
@@ -355,7 +356,7 @@ describe('query', () => {
|
|
|
355
356
|
test('should not error if reset while pending', async () => {
|
|
356
357
|
const key = queryKey()
|
|
357
358
|
|
|
358
|
-
const queryFn =
|
|
359
|
+
const queryFn = vi.fn<unknown[], unknown>()
|
|
359
360
|
|
|
360
361
|
queryFn.mockImplementation(async () => {
|
|
361
362
|
await sleep(10)
|
|
@@ -382,7 +383,7 @@ describe('query', () => {
|
|
|
382
383
|
test('should be able to refetch a cancelled query', async () => {
|
|
383
384
|
const key = queryKey()
|
|
384
385
|
|
|
385
|
-
const queryFn =
|
|
386
|
+
const queryFn = vi.fn<unknown[], unknown>()
|
|
386
387
|
|
|
387
388
|
queryFn.mockImplementation(async () => {
|
|
388
389
|
await sleep(50)
|
|
@@ -631,7 +632,7 @@ describe('query', () => {
|
|
|
631
632
|
it: 'works',
|
|
632
633
|
}
|
|
633
634
|
|
|
634
|
-
const queryFn =
|
|
635
|
+
const queryFn = vi.fn(() => 'data')
|
|
635
636
|
|
|
636
637
|
const key = queryKey()
|
|
637
638
|
|
|
@@ -652,7 +653,7 @@ describe('query', () => {
|
|
|
652
653
|
queryFn: () => 'data',
|
|
653
654
|
})
|
|
654
655
|
|
|
655
|
-
const refetchSpy =
|
|
656
|
+
const refetchSpy = vi.spyOn(observer, 'refetch')
|
|
656
657
|
const unsubscribe = observer.subscribe(() => undefined)
|
|
657
658
|
queryCache.onOnline()
|
|
658
659
|
|
|
@@ -692,7 +693,7 @@ describe('query', () => {
|
|
|
692
693
|
})
|
|
693
694
|
expect(query.getObserversCount()).toEqual(0)
|
|
694
695
|
|
|
695
|
-
const notifySpy =
|
|
696
|
+
const notifySpy = vi.spyOn(queryCache, 'notify')
|
|
696
697
|
expect(() => query.removeObserver(observer)).not.toThrow()
|
|
697
698
|
expect(notifySpy).not.toHaveBeenCalled()
|
|
698
699
|
|
|
@@ -769,7 +770,7 @@ describe('query', () => {
|
|
|
769
770
|
})
|
|
770
771
|
|
|
771
772
|
test('fetch should dispatch an error if the queryFn returns undefined', async () => {
|
|
772
|
-
const consoleMock =
|
|
773
|
+
const consoleMock = vi.spyOn(console, 'error')
|
|
773
774
|
consoleMock.mockImplementation(() => undefined)
|
|
774
775
|
const key = queryKey()
|
|
775
776
|
|
|
@@ -804,7 +805,7 @@ describe('query', () => {
|
|
|
804
805
|
test('constructor should call initialDataUpdatedAt if defined as a function', async () => {
|
|
805
806
|
const key = queryKey()
|
|
806
807
|
|
|
807
|
-
const initialDataUpdatedAtSpy =
|
|
808
|
+
const initialDataUpdatedAtSpy = vi.fn()
|
|
808
809
|
|
|
809
810
|
await queryClient.prefetchQuery({
|
|
810
811
|
queryKey: key,
|
|
@@ -821,7 +822,7 @@ describe('query', () => {
|
|
|
821
822
|
|
|
822
823
|
queryClient.setQueryDefaults(key, { gcTime: 10 })
|
|
823
824
|
|
|
824
|
-
const fn =
|
|
825
|
+
const fn = vi.fn()
|
|
825
826
|
|
|
826
827
|
const unsubscribe = queryClient.getQueryCache().subscribe(fn)
|
|
827
828
|
|
|
@@ -2,6 +2,7 @@ import { sleep, queryKey, createQueryClient } from './utils'
|
|
|
2
2
|
import { QueryClient } from '..'
|
|
3
3
|
import { QueryCache, QueryObserver } from '..'
|
|
4
4
|
import { waitFor } from '@testing-library/react'
|
|
5
|
+
import { vi } from 'vitest'
|
|
5
6
|
|
|
6
7
|
describe('queryCache', () => {
|
|
7
8
|
let queryClient: QueryClient
|
|
@@ -19,7 +20,7 @@ describe('queryCache', () => {
|
|
|
19
20
|
describe('subscribe', () => {
|
|
20
21
|
test('should pass the correct query', async () => {
|
|
21
22
|
const key = queryKey()
|
|
22
|
-
const subscriber =
|
|
23
|
+
const subscriber = vi.fn()
|
|
23
24
|
const unsubscribe = queryCache.subscribe(subscriber)
|
|
24
25
|
queryClient.setQueryData(key, 'foo')
|
|
25
26
|
const query = queryCache.find({ queryKey: key })
|
|
@@ -30,7 +31,7 @@ describe('queryCache', () => {
|
|
|
30
31
|
|
|
31
32
|
test('should notify listeners when new query is added', async () => {
|
|
32
33
|
const key = queryKey()
|
|
33
|
-
const callback =
|
|
34
|
+
const callback = vi.fn()
|
|
34
35
|
queryCache.subscribe(callback)
|
|
35
36
|
queryClient.prefetchQuery({ queryKey: key, queryFn: () => 'data' })
|
|
36
37
|
await sleep(100)
|
|
@@ -50,7 +51,7 @@ describe('queryCache', () => {
|
|
|
50
51
|
staleTime: 10,
|
|
51
52
|
})
|
|
52
53
|
|
|
53
|
-
const unsubScribeObserver = observer.subscribe(
|
|
54
|
+
const unsubScribeObserver = observer.subscribe(vi.fn())
|
|
54
55
|
|
|
55
56
|
await waitFor(() => {
|
|
56
57
|
expect(events.length).toBe(8)
|
|
@@ -73,7 +74,7 @@ describe('queryCache', () => {
|
|
|
73
74
|
|
|
74
75
|
test('should include the queryCache and query when notifying listeners', async () => {
|
|
75
76
|
const key = queryKey()
|
|
76
|
-
const callback =
|
|
77
|
+
const callback = vi.fn()
|
|
77
78
|
queryCache.subscribe(callback)
|
|
78
79
|
queryClient.prefetchQuery({ queryKey: key, queryFn: () => 'data' })
|
|
79
80
|
const query = queryCache.find({ queryKey: key })
|
|
@@ -83,7 +84,7 @@ describe('queryCache', () => {
|
|
|
83
84
|
|
|
84
85
|
test('should notify subscribers when new query with initialData is added', async () => {
|
|
85
86
|
const key = queryKey()
|
|
86
|
-
const callback =
|
|
87
|
+
const callback = vi.fn()
|
|
87
88
|
queryCache.subscribe(callback)
|
|
88
89
|
queryClient.prefetchQuery({
|
|
89
90
|
queryKey: key,
|
|
@@ -289,9 +290,9 @@ describe('queryCache', () => {
|
|
|
289
290
|
describe('QueryCacheConfig error callbacks', () => {
|
|
290
291
|
test('should call onError and onSettled when a query errors', async () => {
|
|
291
292
|
const key = queryKey()
|
|
292
|
-
const onSuccess =
|
|
293
|
-
const onSettled =
|
|
294
|
-
const onError =
|
|
293
|
+
const onSuccess = vi.fn()
|
|
294
|
+
const onSettled = vi.fn()
|
|
295
|
+
const onError = vi.fn()
|
|
295
296
|
const testCache = new QueryCache({ onSuccess, onError, onSettled })
|
|
296
297
|
const testClient = createQueryClient({ queryCache: testCache })
|
|
297
298
|
await testClient.prefetchQuery({
|
|
@@ -310,9 +311,9 @@ describe('queryCache', () => {
|
|
|
310
311
|
describe('QueryCacheConfig success callbacks', () => {
|
|
311
312
|
test('should call onSuccess and onSettled when a query is successful', async () => {
|
|
312
313
|
const key = queryKey()
|
|
313
|
-
const onSuccess =
|
|
314
|
-
const onSettled =
|
|
315
|
-
const onError =
|
|
314
|
+
const onSuccess = vi.fn()
|
|
315
|
+
const onSettled = vi.fn()
|
|
316
|
+
const onError = vi.fn()
|
|
316
317
|
const testCache = new QueryCache({ onSuccess, onError, onSettled })
|
|
317
318
|
const testClient = createQueryClient({ queryCache: testCache })
|
|
318
319
|
await testClient.prefetchQuery({
|
|
@@ -330,14 +331,14 @@ describe('queryCache', () => {
|
|
|
330
331
|
|
|
331
332
|
describe('QueryCacheConfig.createStore', () => {
|
|
332
333
|
test('should call createStore', async () => {
|
|
333
|
-
const createStore =
|
|
334
|
+
const createStore = vi.fn().mockImplementation(() => new Map())
|
|
334
335
|
new QueryCache({ createStore })
|
|
335
336
|
expect(createStore).toHaveBeenCalledWith()
|
|
336
337
|
})
|
|
337
338
|
|
|
338
339
|
test('should use created store', async () => {
|
|
339
340
|
const store = new Map()
|
|
340
|
-
const spy =
|
|
341
|
+
const spy = vi.spyOn(store, 'get')
|
|
341
342
|
|
|
342
343
|
new QueryCache({ createStore: () => store }).get('key')
|
|
343
344
|
|