@tanstack/query-core 5.0.0-alpha.1 → 5.0.0-alpha.18
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 +4 -4
- 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/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 +7 -7
- 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 +5 -4
- package/build/lib/mutation.esm.js +138 -99
- package/build/lib/mutation.esm.js.map +1 -1
- package/build/lib/mutation.js +138 -99
- package/build/lib/mutation.js.map +1 -1
- package/build/lib/mutation.mjs +16 -2
- package/build/lib/mutation.mjs.map +1 -1
- package/build/lib/mutationCache.d.ts +5 -4
- 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.d.ts +2 -2
- 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 +94 -69
- package/build/lib/queriesObserver.esm.js.map +1 -1
- package/build/lib/queriesObserver.js +94 -69
- package/build/lib/queriesObserver.js.map +1 -1
- package/build/lib/queriesObserver.mjs +1 -5
- package/build/lib/queriesObserver.mjs.map +1 -1
- package/build/lib/query.d.ts +8 -7
- package/build/lib/query.esm.js +210 -159
- package/build/lib/query.esm.js.map +1 -1
- package/build/lib/query.js +210 -159
- package/build/lib/query.js.map +1 -1
- package/build/lib/query.mjs +2 -0
- package/build/lib/query.mjs.map +1 -1
- package/build/lib/queryCache.d.ts +5 -4
- 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.d.ts +7 -7
- 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 -2
- package/build/lib/queryObserver.esm.js +308 -207
- package/build/lib/queryObserver.esm.js.map +1 -1
- package/build/lib/queryObserver.js +307 -206
- package/build/lib/queryObserver.js.map +1 -1
- 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.d.ts +5 -5
- 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/retryer.mjs.map +1 -1
- package/build/lib/tests/utils.d.ts +4 -5
- package/build/lib/types.d.ts +42 -40
- 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 +92 -88
- 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 +9 -5
- package/src/infiniteQueryBehavior.ts +52 -60
- package/src/infiniteQueryObserver.ts +17 -12
- package/src/mutation.ts +34 -7
- package/src/mutationCache.ts +15 -8
- package/src/mutationObserver.ts +3 -2
- package/src/queriesObserver.ts +3 -7
- package/src/query.ts +24 -9
- package/src/queryCache.ts +16 -10
- package/src/queryClient.ts +9 -11
- package/src/queryObserver.ts +2 -2
- package/src/retryer.ts +5 -5
- 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 +61 -16
- 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 +28 -15
- package/src/tests/queryClient.test.tsx +49 -48
- package/src/tests/queryObserver.test.tsx +10 -9
- package/src/tests/utils.test.tsx +2 -1
- package/src/tests/utils.ts +5 -4
- package/src/types.ts +53 -37
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { createQueryClient, queryKey, sleep } from './utils'
|
|
2
2
|
import type { QueryClient } from '..'
|
|
3
3
|
import { InfiniteQueryObserver } from '..'
|
|
4
|
+
import { vi } from 'vitest'
|
|
4
5
|
|
|
5
6
|
describe('InfiniteQueryObserver', () => {
|
|
6
7
|
let queryClient: QueryClient
|
|
@@ -43,7 +44,7 @@ describe('InfiniteQueryObserver', () => {
|
|
|
43
44
|
}
|
|
44
45
|
|
|
45
46
|
const key = queryKey()
|
|
46
|
-
const queryFn =
|
|
47
|
+
const queryFn = vi.fn(() => 1)
|
|
47
48
|
const observer = new InfiniteQueryObserver(queryClient, {
|
|
48
49
|
meta,
|
|
49
50
|
queryKey: key,
|
|
@@ -66,4 +67,64 @@ describe('InfiniteQueryObserver', () => {
|
|
|
66
67
|
})
|
|
67
68
|
expect(queryFn).toBeCalledWith(expect.objectContaining({ meta }))
|
|
68
69
|
})
|
|
70
|
+
|
|
71
|
+
test('getNextPagParam and getPreviousPageParam should receive current pageParams', async () => {
|
|
72
|
+
const key = queryKey()
|
|
73
|
+
let single: string[] = []
|
|
74
|
+
let all: string[] = []
|
|
75
|
+
const observer = new InfiniteQueryObserver(queryClient, {
|
|
76
|
+
queryKey: key,
|
|
77
|
+
queryFn: ({ pageParam }) => String(pageParam),
|
|
78
|
+
defaultPageParam: 1,
|
|
79
|
+
getNextPageParam: (_, __, lastPageParam, allPageParams) => {
|
|
80
|
+
single.push('next' + lastPageParam)
|
|
81
|
+
all.push('next' + allPageParams.join(','))
|
|
82
|
+
return lastPageParam + 1
|
|
83
|
+
},
|
|
84
|
+
getPreviousPageParam: (_, __, firstPageParam, allPageParams) => {
|
|
85
|
+
single.push('prev' + firstPageParam)
|
|
86
|
+
all.push('prev' + allPageParams.join(','))
|
|
87
|
+
return firstPageParam - 1
|
|
88
|
+
},
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
await observer.fetchNextPage()
|
|
92
|
+
await observer.fetchPreviousPage()
|
|
93
|
+
|
|
94
|
+
expect(single).toEqual(['next1', 'prev1', 'prev1', 'next1', 'prev0'])
|
|
95
|
+
expect(all).toEqual(['next1', 'prev1', 'prev1', 'next0,1', 'prev0,1'])
|
|
96
|
+
|
|
97
|
+
single = []
|
|
98
|
+
all = []
|
|
99
|
+
|
|
100
|
+
await observer.refetch()
|
|
101
|
+
|
|
102
|
+
expect(single).toEqual(['next0', 'next1', 'prev0'])
|
|
103
|
+
expect(all).toEqual(['next0', 'next0,1', 'prev0,1'])
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
test('should stop refetching if undefined is returned from getNextPageParam', async () => {
|
|
107
|
+
const key = queryKey()
|
|
108
|
+
let next: number | undefined = 2
|
|
109
|
+
const queryFn = vi.fn<any, any>(({ pageParam }) => String(pageParam))
|
|
110
|
+
const observer = new InfiniteQueryObserver(queryClient, {
|
|
111
|
+
queryKey: key,
|
|
112
|
+
queryFn,
|
|
113
|
+
defaultPageParam: 1,
|
|
114
|
+
getNextPageParam: () => next,
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
await observer.fetchNextPage()
|
|
118
|
+
await observer.fetchNextPage()
|
|
119
|
+
|
|
120
|
+
expect(observer.getCurrentResult().data?.pages).toEqual(['1', '2'])
|
|
121
|
+
expect(queryFn).toBeCalledTimes(2)
|
|
122
|
+
|
|
123
|
+
next = undefined
|
|
124
|
+
|
|
125
|
+
await observer.refetch()
|
|
126
|
+
|
|
127
|
+
expect(observer.getCurrentResult().data?.pages).toEqual(['1'])
|
|
128
|
+
expect(queryFn).toBeCalledTimes(3)
|
|
129
|
+
})
|
|
69
130
|
})
|
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
import { waitFor } from '@testing-library/react'
|
|
2
2
|
import { queryKey, sleep, executeMutation, createQueryClient } from './utils'
|
|
3
3
|
import { MutationCache, MutationObserver } from '..'
|
|
4
|
+
import { vi } from 'vitest'
|
|
4
5
|
|
|
5
6
|
describe('mutationCache', () => {
|
|
6
|
-
describe('MutationCacheConfig
|
|
7
|
-
test('should
|
|
7
|
+
describe('MutationCacheConfig error callbacks', () => {
|
|
8
|
+
test('should call onError and onSettled when a mutation errors', async () => {
|
|
8
9
|
const key = queryKey()
|
|
9
|
-
const onError =
|
|
10
|
-
const
|
|
10
|
+
const onError = vi.fn()
|
|
11
|
+
const onSuccess = vi.fn()
|
|
12
|
+
const onSettled = vi.fn()
|
|
13
|
+
const testCache = new MutationCache({ onError, onSuccess, onSettled })
|
|
11
14
|
const testClient = createQueryClient({ mutationCache: testCache })
|
|
12
15
|
|
|
13
16
|
try {
|
|
@@ -23,12 +26,22 @@ describe('mutationCache', () => {
|
|
|
23
26
|
} catch {}
|
|
24
27
|
|
|
25
28
|
const mutation = testCache.getAll()[0]
|
|
29
|
+
expect(onError).toHaveBeenCalledTimes(1)
|
|
26
30
|
expect(onError).toHaveBeenCalledWith(
|
|
27
31
|
new Error('error'),
|
|
28
32
|
'vars',
|
|
29
33
|
'context',
|
|
30
34
|
mutation,
|
|
31
35
|
)
|
|
36
|
+
expect(onSuccess).not.toHaveBeenCalled()
|
|
37
|
+
expect(onSettled).toHaveBeenCalledTimes(1)
|
|
38
|
+
expect(onSettled).toHaveBeenCalledWith(
|
|
39
|
+
undefined,
|
|
40
|
+
new Error('error'),
|
|
41
|
+
'vars',
|
|
42
|
+
'context',
|
|
43
|
+
mutation,
|
|
44
|
+
)
|
|
32
45
|
})
|
|
33
46
|
|
|
34
47
|
test('should be awaited', async () => {
|
|
@@ -39,7 +52,12 @@ describe('mutationCache', () => {
|
|
|
39
52
|
await sleep(1)
|
|
40
53
|
states.push(2)
|
|
41
54
|
}
|
|
42
|
-
const
|
|
55
|
+
const onSettled = async () => {
|
|
56
|
+
states.push(5)
|
|
57
|
+
await sleep(1)
|
|
58
|
+
states.push(6)
|
|
59
|
+
}
|
|
60
|
+
const testCache = new MutationCache({ onError, onSettled })
|
|
43
61
|
const testClient = createQueryClient({ mutationCache: testCache })
|
|
44
62
|
|
|
45
63
|
try {
|
|
@@ -53,19 +71,26 @@ describe('mutationCache', () => {
|
|
|
53
71
|
await sleep(1)
|
|
54
72
|
states.push(4)
|
|
55
73
|
},
|
|
74
|
+
onSettled: async () => {
|
|
75
|
+
states.push(7)
|
|
76
|
+
await sleep(1)
|
|
77
|
+
states.push(8)
|
|
78
|
+
},
|
|
56
79
|
},
|
|
57
80
|
'vars',
|
|
58
81
|
)
|
|
59
82
|
} catch {}
|
|
60
83
|
|
|
61
|
-
expect(states).toEqual([1, 2, 3, 4])
|
|
84
|
+
expect(states).toEqual([1, 2, 3, 4, 5, 6, 7, 8])
|
|
62
85
|
})
|
|
63
86
|
})
|
|
64
|
-
describe('MutationCacheConfig
|
|
65
|
-
test('should
|
|
87
|
+
describe('MutationCacheConfig success callbacks', () => {
|
|
88
|
+
test('should call onSuccess and onSettled when a mutation is successful', async () => {
|
|
66
89
|
const key = queryKey()
|
|
67
|
-
const
|
|
68
|
-
const
|
|
90
|
+
const onError = vi.fn()
|
|
91
|
+
const onSuccess = vi.fn()
|
|
92
|
+
const onSettled = vi.fn()
|
|
93
|
+
const testCache = new MutationCache({ onError, onSuccess, onSettled })
|
|
69
94
|
const testClient = createQueryClient({ mutationCache: testCache })
|
|
70
95
|
|
|
71
96
|
try {
|
|
@@ -81,12 +106,22 @@ describe('mutationCache', () => {
|
|
|
81
106
|
} catch {}
|
|
82
107
|
|
|
83
108
|
const mutation = testCache.getAll()[0]
|
|
109
|
+
expect(onSuccess).toHaveBeenCalledTimes(1)
|
|
84
110
|
expect(onSuccess).toHaveBeenCalledWith(
|
|
85
111
|
{ data: 5 },
|
|
86
112
|
'vars',
|
|
87
113
|
'context',
|
|
88
114
|
mutation,
|
|
89
115
|
)
|
|
116
|
+
expect(onError).not.toHaveBeenCalled()
|
|
117
|
+
expect(onSettled).toHaveBeenCalledTimes(1)
|
|
118
|
+
expect(onSettled).toHaveBeenCalledWith(
|
|
119
|
+
{ data: 5 },
|
|
120
|
+
null,
|
|
121
|
+
'vars',
|
|
122
|
+
'context',
|
|
123
|
+
mutation,
|
|
124
|
+
)
|
|
90
125
|
})
|
|
91
126
|
test('should be awaited', async () => {
|
|
92
127
|
const key = queryKey()
|
|
@@ -96,7 +131,12 @@ describe('mutationCache', () => {
|
|
|
96
131
|
await sleep(1)
|
|
97
132
|
states.push(2)
|
|
98
133
|
}
|
|
99
|
-
const
|
|
134
|
+
const onSettled = async () => {
|
|
135
|
+
states.push(5)
|
|
136
|
+
await sleep(1)
|
|
137
|
+
states.push(6)
|
|
138
|
+
}
|
|
139
|
+
const testCache = new MutationCache({ onSuccess, onSettled })
|
|
100
140
|
const testClient = createQueryClient({ mutationCache: testCache })
|
|
101
141
|
|
|
102
142
|
await executeMutation(
|
|
@@ -109,17 +149,22 @@ describe('mutationCache', () => {
|
|
|
109
149
|
await sleep(1)
|
|
110
150
|
states.push(4)
|
|
111
151
|
},
|
|
152
|
+
onSettled: async () => {
|
|
153
|
+
states.push(7)
|
|
154
|
+
await sleep(1)
|
|
155
|
+
states.push(8)
|
|
156
|
+
},
|
|
112
157
|
},
|
|
113
158
|
'vars',
|
|
114
159
|
)
|
|
115
160
|
|
|
116
|
-
expect(states).toEqual([1, 2, 3, 4])
|
|
161
|
+
expect(states).toEqual([1, 2, 3, 4, 5, 6, 7, 8])
|
|
117
162
|
})
|
|
118
163
|
})
|
|
119
164
|
describe('MutationCacheConfig.onMutate', () => {
|
|
120
165
|
test('should be called before a mutation executes', async () => {
|
|
121
166
|
const key = queryKey()
|
|
122
|
-
const onMutate =
|
|
167
|
+
const onMutate = vi.fn()
|
|
123
168
|
const testCache = new MutationCache({ onMutate })
|
|
124
169
|
const testClient = createQueryClient({ mutationCache: testCache })
|
|
125
170
|
|
|
@@ -242,7 +287,7 @@ describe('mutationCache', () => {
|
|
|
242
287
|
test('should remove unused mutations after gcTime has elapsed', async () => {
|
|
243
288
|
const testCache = new MutationCache()
|
|
244
289
|
const testClient = createQueryClient({ mutationCache: testCache })
|
|
245
|
-
const onSuccess =
|
|
290
|
+
const onSuccess = vi.fn()
|
|
246
291
|
await executeMutation(
|
|
247
292
|
testClient,
|
|
248
293
|
{
|
|
@@ -285,7 +330,7 @@ describe('mutationCache', () => {
|
|
|
285
330
|
|
|
286
331
|
test('should be garbage collected later when unsubscribed and mutation is pending', async () => {
|
|
287
332
|
const queryClient = createQueryClient()
|
|
288
|
-
const onSuccess =
|
|
333
|
+
const onSuccess = vi.fn()
|
|
289
334
|
const observer = new MutationObserver(queryClient, {
|
|
290
335
|
gcTime: 10,
|
|
291
336
|
mutationFn: async () => {
|
|
@@ -311,7 +356,7 @@ describe('mutationCache', () => {
|
|
|
311
356
|
|
|
312
357
|
test('should call callbacks even with gcTime 0 and mutation still pending', async () => {
|
|
313
358
|
const queryClient = createQueryClient()
|
|
314
|
-
const onSuccess =
|
|
359
|
+
const onSuccess = vi.fn()
|
|
315
360
|
const observer = new MutationObserver(queryClient, {
|
|
316
361
|
gcTime: 0,
|
|
317
362
|
mutationFn: async () => {
|
|
@@ -2,6 +2,7 @@ import { waitFor } from '@testing-library/react'
|
|
|
2
2
|
import { createQueryClient, sleep } from './utils'
|
|
3
3
|
import type { QueryClient } from '..'
|
|
4
4
|
import { MutationObserver } from '..'
|
|
5
|
+
import { vi } from 'vitest'
|
|
5
6
|
|
|
6
7
|
describe('mutationObserver', () => {
|
|
7
8
|
let queryClient: QueryClient
|
|
@@ -23,8 +24,8 @@ describe('mutationObserver', () => {
|
|
|
23
24
|
},
|
|
24
25
|
})
|
|
25
26
|
|
|
26
|
-
const subscription1Handler =
|
|
27
|
-
const subscription2Handler =
|
|
27
|
+
const subscription1Handler = vi.fn()
|
|
28
|
+
const subscription2Handler = vi.fn()
|
|
28
29
|
|
|
29
30
|
const unsubscribe1 = mutation.subscribe(subscription1Handler)
|
|
30
31
|
const unsubscribe2 = mutation.subscribe(subscription2Handler)
|
|
@@ -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)
|