@tanstack/query-core 4.24.10 → 5.0.0-alpha.1
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/focusManager.d.ts +1 -3
- package/build/lib/focusManager.esm.js +19 -36
- package/build/lib/focusManager.esm.js.map +1 -1
- package/build/lib/focusManager.js +19 -38
- package/build/lib/focusManager.js.map +1 -1
- package/build/lib/focusManager.mjs +19 -36
- package/build/lib/focusManager.mjs.map +1 -1
- package/build/lib/hydration.esm.js +21 -23
- package/build/lib/hydration.esm.js.map +1 -1
- package/build/lib/hydration.js +21 -25
- package/build/lib/hydration.js.map +1 -1
- package/build/lib/hydration.mjs +21 -23
- package/build/lib/hydration.mjs.map +1 -1
- package/build/lib/index.d.ts +1 -2
- package/build/lib/index.esm.js +1 -1
- package/build/lib/index.js +2 -8
- package/build/lib/index.js.map +1 -1
- package/build/lib/index.mjs +1 -1
- package/build/lib/infiniteQueryBehavior.d.ts +3 -7
- package/build/lib/infiniteQueryBehavior.esm.js +52 -75
- package/build/lib/infiniteQueryBehavior.esm.js.map +1 -1
- package/build/lib/infiniteQueryBehavior.js +50 -77
- package/build/lib/infiniteQueryBehavior.js.map +1 -1
- package/build/lib/infiniteQueryBehavior.mjs +52 -75
- package/build/lib/infiniteQueryBehavior.mjs.map +1 -1
- package/build/lib/infiniteQueryObserver.d.ts +4 -4
- package/build/lib/infiniteQueryObserver.esm.js +18 -26
- package/build/lib/infiniteQueryObserver.esm.js.map +1 -1
- package/build/lib/infiniteQueryObserver.js +18 -28
- package/build/lib/infiniteQueryObserver.js.map +1 -1
- package/build/lib/infiniteQueryObserver.mjs +18 -26
- package/build/lib/infiniteQueryObserver.mjs.map +1 -1
- package/build/lib/mutation.d.ts +11 -22
- package/build/lib/mutation.esm.js +73 -105
- package/build/lib/mutation.esm.js.map +1 -1
- package/build/lib/mutation.js +73 -107
- package/build/lib/mutation.js.map +1 -1
- package/build/lib/mutation.mjs +73 -105
- package/build/lib/mutation.mjs.map +1 -1
- package/build/lib/mutationCache.d.ts +4 -6
- package/build/lib/mutationCache.esm.js +23 -32
- package/build/lib/mutationCache.esm.js.map +1 -1
- package/build/lib/mutationCache.js +23 -34
- package/build/lib/mutationCache.js.map +1 -1
- package/build/lib/mutationCache.mjs +23 -32
- package/build/lib/mutationCache.mjs.map +1 -1
- package/build/lib/mutationObserver.d.ts +4 -9
- package/build/lib/mutationObserver.esm.js +42 -73
- package/build/lib/mutationObserver.esm.js.map +1 -1
- package/build/lib/mutationObserver.js +42 -75
- package/build/lib/mutationObserver.js.map +1 -1
- package/build/lib/mutationObserver.mjs +42 -73
- package/build/lib/mutationObserver.mjs.map +1 -1
- package/build/lib/notifyManager.esm.js +7 -17
- package/build/lib/notifyManager.esm.js.map +1 -1
- package/build/lib/notifyManager.js +7 -19
- package/build/lib/notifyManager.js.map +1 -1
- package/build/lib/notifyManager.mjs +7 -17
- package/build/lib/notifyManager.mjs.map +1 -1
- package/build/lib/onlineManager.d.ts +1 -3
- package/build/lib/onlineManager.esm.js +16 -30
- package/build/lib/onlineManager.esm.js.map +1 -1
- package/build/lib/onlineManager.js +16 -32
- package/build/lib/onlineManager.js.map +1 -1
- package/build/lib/onlineManager.mjs +16 -30
- package/build/lib/onlineManager.mjs.map +1 -1
- package/build/lib/queriesObserver.d.ts +3 -10
- package/build/lib/queriesObserver.esm.js +47 -71
- package/build/lib/queriesObserver.esm.js.map +1 -1
- package/build/lib/queriesObserver.js +49 -75
- package/build/lib/queriesObserver.js.map +1 -1
- package/build/lib/queriesObserver.mjs +47 -71
- package/build/lib/queriesObserver.mjs.map +1 -1
- package/build/lib/query.d.ts +14 -21
- package/build/lib/query.esm.js +140 -194
- package/build/lib/query.esm.js.map +1 -1
- package/build/lib/query.js +139 -195
- package/build/lib/query.js.map +1 -1
- package/build/lib/query.mjs +140 -194
- package/build/lib/query.mjs.map +1 -1
- package/build/lib/queryCache.d.ts +12 -7
- package/build/lib/queryCache.esm.js +21 -45
- package/build/lib/queryCache.esm.js.map +1 -1
- package/build/lib/queryCache.js +20 -46
- package/build/lib/queryCache.js.map +1 -1
- package/build/lib/queryCache.mjs +21 -45
- package/build/lib/queryCache.mjs.map +1 -1
- package/build/lib/queryClient.d.ts +18 -46
- package/build/lib/queryClient.esm.js +137 -216
- package/build/lib/queryClient.esm.js.map +1 -1
- package/build/lib/queryClient.js +136 -217
- package/build/lib/queryClient.js.map +1 -1
- package/build/lib/queryClient.mjs +137 -216
- package/build/lib/queryClient.mjs.map +1 -1
- package/build/lib/queryObserver.d.ts +4 -29
- package/build/lib/queryObserver.esm.js +176 -258
- package/build/lib/queryObserver.esm.js.map +1 -1
- package/build/lib/queryObserver.js +176 -260
- package/build/lib/queryObserver.js.map +1 -1
- package/build/lib/queryObserver.mjs +176 -258
- package/build/lib/queryObserver.mjs.map +1 -1
- package/build/lib/removable.d.ts +3 -3
- package/build/lib/removable.esm.js +10 -14
- package/build/lib/removable.esm.js.map +1 -1
- package/build/lib/removable.js +10 -16
- package/build/lib/removable.js.map +1 -1
- package/build/lib/removable.mjs +10 -14
- package/build/lib/removable.mjs.map +1 -1
- package/build/lib/retryer.d.ts +5 -5
- package/build/lib/retryer.esm.js +27 -44
- package/build/lib/retryer.esm.js.map +1 -1
- package/build/lib/retryer.js +27 -46
- package/build/lib/retryer.js.map +1 -1
- package/build/lib/retryer.mjs +27 -44
- package/build/lib/retryer.mjs.map +1 -1
- package/build/lib/subscribable.esm.js +4 -7
- package/build/lib/subscribable.esm.js.map +1 -1
- package/build/lib/subscribable.js +4 -9
- package/build/lib/subscribable.js.map +1 -1
- package/build/lib/subscribable.mjs +4 -7
- package/build/lib/subscribable.mjs.map +1 -1
- package/build/lib/tests/utils.d.ts +3 -12
- package/build/lib/types.d.ts +111 -99
- package/build/lib/utils.d.ts +8 -18
- package/build/lib/utils.esm.js +39 -132
- package/build/lib/utils.esm.js.map +1 -1
- package/build/lib/utils.js +42 -144
- package/build/lib/utils.js.map +1 -1
- package/build/lib/utils.mjs +39 -132
- package/build/lib/utils.mjs.map +1 -1
- package/build/umd/index.development.js +867 -1399
- 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 +1 -1
- package/src/focusManager.ts +17 -24
- package/src/index.ts +1 -11
- package/src/infiniteQueryBehavior.ts +54 -94
- package/src/infiniteQueryObserver.ts +10 -12
- package/src/mutation.ts +68 -92
- package/src/mutationCache.ts +27 -27
- package/src/mutationObserver.ts +58 -97
- package/src/onlineManager.ts +14 -14
- package/src/queriesObserver.ts +50 -54
- package/src/query.ts +107 -111
- package/src/queryCache.ts +42 -41
- package/src/queryClient.ts +155 -434
- package/src/queryObserver.ts +155 -192
- package/src/removable.ts +13 -13
- package/src/retryer.ts +5 -5
- package/src/subscribable.ts +1 -1
- package/src/tests/focusManager.test.tsx +25 -25
- package/src/tests/hydration.test.tsx +167 -81
- package/src/tests/infiniteQueryBehavior.test.tsx +209 -17
- package/src/tests/infiniteQueryObserver.test.tsx +6 -2
- package/src/tests/mutationCache.test.tsx +127 -127
- package/src/tests/mutationObserver.test.tsx +1 -31
- package/src/tests/mutations.test.tsx +62 -43
- package/src/tests/onlineManager.test.tsx +12 -4
- package/src/tests/queriesObserver.test.tsx +41 -77
- package/src/tests/query.test.tsx +175 -243
- package/src/tests/queryCache.test.tsx +170 -93
- package/src/tests/queryClient.test.tsx +229 -378
- package/src/tests/queryObserver.test.tsx +23 -147
- package/src/tests/utils.test.tsx +84 -29
- package/src/tests/utils.ts +9 -18
- package/src/types.ts +187 -140
- package/src/utils.ts +31 -124
- package/build/lib/logger.d.ts +0 -8
- package/build/lib/logger.esm.js +0 -4
- package/build/lib/logger.esm.js.map +0 -1
- package/build/lib/logger.js +0 -8
- package/build/lib/logger.js.map +0 -1
- package/build/lib/logger.mjs +0 -4
- package/build/lib/logger.mjs.map +0 -1
- package/build/lib/logger.native.d.ts +0 -6
- package/build/lib/logger.native.esm.js +0 -12
- package/build/lib/logger.native.esm.js.map +0 -1
- package/build/lib/logger.native.js +0 -16
- package/build/lib/logger.native.js.map +0 -1
- package/build/lib/logger.native.mjs +0 -12
- package/build/lib/logger.native.mjs.map +0 -1
- package/src/logger.native.ts +0 -11
- package/src/logger.ts +0 -9
|
@@ -29,7 +29,7 @@ describe('mutationObserver', () => {
|
|
|
29
29
|
const unsubscribe1 = mutation.subscribe(subscription1Handler)
|
|
30
30
|
const unsubscribe2 = mutation.subscribe(subscription2Handler)
|
|
31
31
|
|
|
32
|
-
mutation.mutate()
|
|
32
|
+
mutation.mutate('input')
|
|
33
33
|
|
|
34
34
|
unsubscribe1()
|
|
35
35
|
|
|
@@ -43,34 +43,4 @@ describe('mutationObserver', () => {
|
|
|
43
43
|
// Clean-up
|
|
44
44
|
unsubscribe2()
|
|
45
45
|
})
|
|
46
|
-
|
|
47
|
-
test('should not notify listeners if options.listeners is set to false', async () => {
|
|
48
|
-
const mutation = new MutationObserver(queryClient, {
|
|
49
|
-
mutationFn: async (text: string) => {
|
|
50
|
-
await sleep(20)
|
|
51
|
-
return text
|
|
52
|
-
},
|
|
53
|
-
})
|
|
54
|
-
|
|
55
|
-
const subscriptionHandler = jest.fn()
|
|
56
|
-
const unsubscribe = mutation.subscribe(subscriptionHandler)
|
|
57
|
-
mutation.mutate()
|
|
58
|
-
|
|
59
|
-
await waitFor(() => {
|
|
60
|
-
// 2 calls: loading, success
|
|
61
|
-
expect(subscriptionHandler).toBeCalledTimes(2)
|
|
62
|
-
})
|
|
63
|
-
subscriptionHandler.mockReset()
|
|
64
|
-
|
|
65
|
-
// Force a notification with listeners set to false
|
|
66
|
-
// because there is no existing usage of notify with listeners set to false
|
|
67
|
-
mutation['notify']({ listeners: false })
|
|
68
|
-
|
|
69
|
-
await waitFor(() => {
|
|
70
|
-
// 0 call because no notification has been sent
|
|
71
|
-
expect(subscriptionHandler).toBeCalledTimes(0)
|
|
72
|
-
})
|
|
73
|
-
|
|
74
|
-
unsubscribe()
|
|
75
|
-
})
|
|
76
46
|
})
|
|
@@ -15,15 +15,6 @@ describe('mutations', () => {
|
|
|
15
15
|
queryClient.clear()
|
|
16
16
|
})
|
|
17
17
|
|
|
18
|
-
test('mutate should trigger a mutation', async () => {
|
|
19
|
-
const result = await executeMutation(queryClient, {
|
|
20
|
-
mutationFn: async (text: string) => text,
|
|
21
|
-
variables: 'todo',
|
|
22
|
-
})
|
|
23
|
-
|
|
24
|
-
expect(result).toBe(result)
|
|
25
|
-
})
|
|
26
|
-
|
|
27
18
|
test('mutate should accept null values', async () => {
|
|
28
19
|
let variables
|
|
29
20
|
|
|
@@ -34,26 +25,29 @@ describe('mutations', () => {
|
|
|
34
25
|
},
|
|
35
26
|
})
|
|
36
27
|
|
|
37
|
-
mutation.mutate(null)
|
|
38
|
-
|
|
39
|
-
await sleep(10)
|
|
28
|
+
await mutation.mutate(null)
|
|
40
29
|
|
|
41
30
|
expect(variables).toBe(null)
|
|
42
31
|
})
|
|
43
32
|
|
|
44
33
|
test('setMutationDefaults should be able to set defaults', async () => {
|
|
45
34
|
const key = queryKey()
|
|
35
|
+
const fn = jest.fn()
|
|
46
36
|
|
|
47
37
|
queryClient.setMutationDefaults(key, {
|
|
48
|
-
mutationFn:
|
|
38
|
+
mutationFn: fn,
|
|
49
39
|
})
|
|
50
40
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
41
|
+
await executeMutation(
|
|
42
|
+
queryClient,
|
|
43
|
+
{
|
|
44
|
+
mutationKey: key,
|
|
45
|
+
},
|
|
46
|
+
'vars',
|
|
47
|
+
)
|
|
55
48
|
|
|
56
|
-
expect(
|
|
49
|
+
expect(fn).toHaveBeenCalledTimes(1)
|
|
50
|
+
expect(fn).toHaveBeenCalledWith('vars')
|
|
57
51
|
})
|
|
58
52
|
|
|
59
53
|
test('mutation should set correct success states', async () => {
|
|
@@ -63,7 +57,6 @@ describe('mutations', () => {
|
|
|
63
57
|
return text
|
|
64
58
|
},
|
|
65
59
|
onMutate: (text) => text,
|
|
66
|
-
variables: 'todo',
|
|
67
60
|
})
|
|
68
61
|
|
|
69
62
|
expect(mutation.getCurrentResult()).toEqual({
|
|
@@ -74,13 +67,14 @@ describe('mutations', () => {
|
|
|
74
67
|
failureReason: null,
|
|
75
68
|
isError: false,
|
|
76
69
|
isIdle: true,
|
|
77
|
-
|
|
70
|
+
isPending: false,
|
|
78
71
|
isPaused: false,
|
|
79
72
|
isSuccess: false,
|
|
80
73
|
mutate: expect.any(Function),
|
|
81
74
|
reset: expect.any(Function),
|
|
82
75
|
status: 'idle',
|
|
83
76
|
variables: undefined,
|
|
77
|
+
submittedAt: 0,
|
|
84
78
|
})
|
|
85
79
|
|
|
86
80
|
const states: MutationState<string, unknown, string, string>[] = []
|
|
@@ -89,7 +83,7 @@ describe('mutations', () => {
|
|
|
89
83
|
states.push(state)
|
|
90
84
|
})
|
|
91
85
|
|
|
92
|
-
mutation.mutate()
|
|
86
|
+
mutation.mutate('todo')
|
|
93
87
|
|
|
94
88
|
await sleep(0)
|
|
95
89
|
|
|
@@ -101,13 +95,14 @@ describe('mutations', () => {
|
|
|
101
95
|
failureReason: null,
|
|
102
96
|
isError: false,
|
|
103
97
|
isIdle: false,
|
|
104
|
-
|
|
98
|
+
isPending: true,
|
|
105
99
|
isPaused: false,
|
|
106
100
|
isSuccess: false,
|
|
107
101
|
mutate: expect.any(Function),
|
|
108
102
|
reset: expect.any(Function),
|
|
109
|
-
status: '
|
|
103
|
+
status: 'pending',
|
|
110
104
|
variables: 'todo',
|
|
105
|
+
submittedAt: expect.any(Number),
|
|
111
106
|
})
|
|
112
107
|
|
|
113
108
|
await sleep(5)
|
|
@@ -120,13 +115,14 @@ describe('mutations', () => {
|
|
|
120
115
|
failureReason: null,
|
|
121
116
|
isError: false,
|
|
122
117
|
isIdle: false,
|
|
123
|
-
|
|
118
|
+
isPending: true,
|
|
124
119
|
isPaused: false,
|
|
125
120
|
isSuccess: false,
|
|
126
121
|
mutate: expect.any(Function),
|
|
127
122
|
reset: expect.any(Function),
|
|
128
|
-
status: '
|
|
123
|
+
status: 'pending',
|
|
129
124
|
variables: 'todo',
|
|
125
|
+
submittedAt: expect.any(Number),
|
|
130
126
|
})
|
|
131
127
|
|
|
132
128
|
await sleep(20)
|
|
@@ -139,24 +135,24 @@ describe('mutations', () => {
|
|
|
139
135
|
failureReason: null,
|
|
140
136
|
isError: false,
|
|
141
137
|
isIdle: false,
|
|
142
|
-
|
|
138
|
+
isPending: false,
|
|
143
139
|
isPaused: false,
|
|
144
140
|
isSuccess: true,
|
|
145
141
|
mutate: expect.any(Function),
|
|
146
142
|
reset: expect.any(Function),
|
|
147
143
|
status: 'success',
|
|
148
144
|
variables: 'todo',
|
|
145
|
+
submittedAt: expect.any(Number),
|
|
149
146
|
})
|
|
150
147
|
})
|
|
151
148
|
|
|
152
149
|
test('mutation should set correct error states', async () => {
|
|
153
150
|
const mutation = new MutationObserver(queryClient, {
|
|
154
|
-
mutationFn: async () => {
|
|
151
|
+
mutationFn: async (_: string) => {
|
|
155
152
|
await sleep(20)
|
|
156
|
-
return Promise.reject('err')
|
|
153
|
+
return Promise.reject(new Error('err'))
|
|
157
154
|
},
|
|
158
155
|
onMutate: (text) => text,
|
|
159
|
-
variables: 'todo',
|
|
160
156
|
retry: 1,
|
|
161
157
|
retryDelay: 1,
|
|
162
158
|
})
|
|
@@ -167,7 +163,7 @@ describe('mutations', () => {
|
|
|
167
163
|
states.push(state)
|
|
168
164
|
})
|
|
169
165
|
|
|
170
|
-
mutation.mutate().catch(() => undefined)
|
|
166
|
+
mutation.mutate('todo').catch(() => undefined)
|
|
171
167
|
|
|
172
168
|
await sleep(0)
|
|
173
169
|
|
|
@@ -179,13 +175,14 @@ describe('mutations', () => {
|
|
|
179
175
|
failureReason: null,
|
|
180
176
|
isError: false,
|
|
181
177
|
isIdle: false,
|
|
182
|
-
|
|
178
|
+
isPending: true,
|
|
183
179
|
isPaused: false,
|
|
184
180
|
isSuccess: false,
|
|
185
181
|
mutate: expect.any(Function),
|
|
186
182
|
reset: expect.any(Function),
|
|
187
|
-
status: '
|
|
183
|
+
status: 'pending',
|
|
188
184
|
variables: 'todo',
|
|
185
|
+
submittedAt: expect.any(Number),
|
|
189
186
|
})
|
|
190
187
|
|
|
191
188
|
await sleep(10)
|
|
@@ -198,13 +195,14 @@ describe('mutations', () => {
|
|
|
198
195
|
failureReason: null,
|
|
199
196
|
isError: false,
|
|
200
197
|
isIdle: false,
|
|
201
|
-
|
|
198
|
+
isPending: true,
|
|
202
199
|
isPaused: false,
|
|
203
200
|
isSuccess: false,
|
|
204
201
|
mutate: expect.any(Function),
|
|
205
202
|
reset: expect.any(Function),
|
|
206
|
-
status: '
|
|
203
|
+
status: 'pending',
|
|
207
204
|
variables: 'todo',
|
|
205
|
+
submittedAt: expect.any(Number),
|
|
208
206
|
})
|
|
209
207
|
|
|
210
208
|
await sleep(20)
|
|
@@ -214,16 +212,17 @@ describe('mutations', () => {
|
|
|
214
212
|
data: undefined,
|
|
215
213
|
error: null,
|
|
216
214
|
failureCount: 1,
|
|
217
|
-
failureReason: 'err',
|
|
215
|
+
failureReason: new Error('err'),
|
|
218
216
|
isError: false,
|
|
219
217
|
isIdle: false,
|
|
220
|
-
|
|
218
|
+
isPending: true,
|
|
221
219
|
isPaused: false,
|
|
222
220
|
isSuccess: false,
|
|
223
221
|
mutate: expect.any(Function),
|
|
224
222
|
reset: expect.any(Function),
|
|
225
|
-
status: '
|
|
223
|
+
status: 'pending',
|
|
226
224
|
variables: 'todo',
|
|
225
|
+
submittedAt: expect.any(Number),
|
|
227
226
|
})
|
|
228
227
|
|
|
229
228
|
await sleep(30)
|
|
@@ -231,18 +230,19 @@ describe('mutations', () => {
|
|
|
231
230
|
expect(states[3]).toEqual({
|
|
232
231
|
context: 'todo',
|
|
233
232
|
data: undefined,
|
|
234
|
-
error: 'err',
|
|
233
|
+
error: new Error('err'),
|
|
235
234
|
failureCount: 2,
|
|
236
|
-
failureReason: 'err',
|
|
235
|
+
failureReason: new Error('err'),
|
|
237
236
|
isError: true,
|
|
238
237
|
isIdle: false,
|
|
239
|
-
|
|
238
|
+
isPending: false,
|
|
240
239
|
isPaused: false,
|
|
241
240
|
isSuccess: false,
|
|
242
241
|
mutate: expect.any(Function),
|
|
243
242
|
reset: expect.any(Function),
|
|
244
243
|
status: 'error',
|
|
245
244
|
variables: 'todo',
|
|
245
|
+
submittedAt: expect.any(Number),
|
|
246
246
|
})
|
|
247
247
|
})
|
|
248
248
|
|
|
@@ -274,8 +274,9 @@ describe('mutations', () => {
|
|
|
274
274
|
failureCount: 1,
|
|
275
275
|
failureReason: 'err',
|
|
276
276
|
isPaused: true,
|
|
277
|
-
status: '
|
|
277
|
+
status: 'pending',
|
|
278
278
|
variables: 'todo',
|
|
279
|
+
submittedAt: 1,
|
|
279
280
|
},
|
|
280
281
|
)
|
|
281
282
|
|
|
@@ -286,8 +287,9 @@ describe('mutations', () => {
|
|
|
286
287
|
failureCount: 1,
|
|
287
288
|
failureReason: 'err',
|
|
288
289
|
isPaused: true,
|
|
289
|
-
status: '
|
|
290
|
+
status: 'pending',
|
|
290
291
|
variables: 'todo',
|
|
292
|
+
submittedAt: 1,
|
|
291
293
|
})
|
|
292
294
|
|
|
293
295
|
await queryClient.resumePausedMutations()
|
|
@@ -301,6 +303,7 @@ describe('mutations', () => {
|
|
|
301
303
|
isPaused: false,
|
|
302
304
|
status: 'success',
|
|
303
305
|
variables: 'todo',
|
|
306
|
+
submittedAt: 1,
|
|
304
307
|
})
|
|
305
308
|
|
|
306
309
|
expect(onMutate).not.toHaveBeenCalled()
|
|
@@ -340,7 +343,23 @@ describe('mutations', () => {
|
|
|
340
343
|
} catch (err) {
|
|
341
344
|
error = err
|
|
342
345
|
}
|
|
343
|
-
expect(error).toEqual('No mutationFn found')
|
|
346
|
+
expect(error).toEqual(new Error('No mutationFn found'))
|
|
347
|
+
})
|
|
348
|
+
|
|
349
|
+
test('mutate update the mutation state even without an active subscription', async () => {
|
|
350
|
+
const onSuccess = jest.fn()
|
|
351
|
+
const onSettled = jest.fn()
|
|
352
|
+
|
|
353
|
+
const mutation = new MutationObserver(queryClient, {
|
|
354
|
+
mutationFn: async () => {
|
|
355
|
+
return 'update'
|
|
356
|
+
},
|
|
357
|
+
})
|
|
358
|
+
|
|
359
|
+
await mutation.mutate(undefined, { onSuccess, onSettled })
|
|
360
|
+
expect(mutation.getCurrentResult().data).toEqual('update')
|
|
361
|
+
expect(onSuccess).not.toHaveBeenCalled()
|
|
362
|
+
expect(onSettled).not.toHaveBeenCalled()
|
|
344
363
|
})
|
|
345
364
|
|
|
346
365
|
test('mutate update the mutation state even without an active subscription', async () => {
|
|
@@ -56,26 +56,34 @@ describe('onlineManager', () => {
|
|
|
56
56
|
expect(remove2Spy).not.toHaveBeenCalled()
|
|
57
57
|
})
|
|
58
58
|
|
|
59
|
-
test('cleanup should
|
|
59
|
+
test('cleanup (removeEventListener) should not be called if window is not defined', async () => {
|
|
60
60
|
const restoreIsServer = setIsServer(true)
|
|
61
61
|
|
|
62
|
+
const removeEventListenerSpy = jest.spyOn(globalThis, 'removeEventListener')
|
|
63
|
+
|
|
62
64
|
const unsubscribe = onlineManager.subscribe(() => undefined)
|
|
63
|
-
expect(onlineManager['cleanup']).toBeUndefined()
|
|
64
65
|
|
|
65
66
|
unsubscribe()
|
|
67
|
+
|
|
68
|
+
expect(removeEventListenerSpy).not.toHaveBeenCalled()
|
|
69
|
+
|
|
66
70
|
restoreIsServer()
|
|
67
71
|
})
|
|
68
72
|
|
|
69
|
-
test('cleanup should
|
|
73
|
+
test('cleanup (removeEventListener) should not be called if window.addEventListener is not defined', async () => {
|
|
70
74
|
const { addEventListener } = globalThis.window
|
|
71
75
|
|
|
72
76
|
// @ts-expect-error
|
|
73
77
|
globalThis.window.addEventListener = undefined
|
|
74
78
|
|
|
79
|
+
const removeEventListenerSpy = jest.spyOn(globalThis, 'removeEventListener')
|
|
80
|
+
|
|
75
81
|
const unsubscribe = onlineManager.subscribe(() => undefined)
|
|
76
|
-
expect(onlineManager['cleanup']).toBeUndefined()
|
|
77
82
|
|
|
78
83
|
unsubscribe()
|
|
84
|
+
|
|
85
|
+
expect(removeEventListenerSpy).not.toHaveBeenCalled()
|
|
86
|
+
|
|
79
87
|
globalThis.window.addEventListener = addEventListener
|
|
80
88
|
})
|
|
81
89
|
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { waitFor } from '@testing-library/react'
|
|
2
|
-
import { sleep, queryKey, createQueryClient
|
|
2
|
+
import { sleep, queryKey, createQueryClient } from './utils'
|
|
3
3
|
import type { QueryClient, QueryObserverResult } from '..'
|
|
4
|
-
import { QueriesObserver
|
|
5
|
-
import type { QueryKey } from '..'
|
|
4
|
+
import { QueriesObserver } from '..'
|
|
6
5
|
|
|
7
6
|
describe('queriesObserver', () => {
|
|
8
7
|
let queryClient: QueryClient
|
|
@@ -35,6 +34,8 @@ describe('queriesObserver', () => {
|
|
|
35
34
|
})
|
|
36
35
|
|
|
37
36
|
test('should still return value for undefined query key', async () => {
|
|
37
|
+
const consoleMock = jest.spyOn(console, 'error')
|
|
38
|
+
consoleMock.mockImplementation(() => undefined)
|
|
38
39
|
const key1 = queryKey()
|
|
39
40
|
const queryFn1 = jest.fn().mockReturnValue(1)
|
|
40
41
|
const queryFn2 = jest.fn().mockReturnValue(2)
|
|
@@ -50,13 +51,11 @@ describe('queriesObserver', () => {
|
|
|
50
51
|
unsubscribe()
|
|
51
52
|
expect(observerResult).toMatchObject([{ data: 1 }, { data: 2 }])
|
|
52
53
|
|
|
53
|
-
expect(
|
|
54
|
-
expect(
|
|
55
|
-
|
|
56
|
-
)
|
|
57
|
-
expect(mockLogger.error).toHaveBeenCalledWith(
|
|
58
|
-
'Passing a custom logger has been deprecated and will be removed in the next major version.',
|
|
54
|
+
expect(consoleMock).toHaveBeenCalledTimes(1)
|
|
55
|
+
expect(consoleMock).toHaveBeenCalledWith(
|
|
56
|
+
"As of v4, queryKey needs to be an Array. If you are using a string like 'repoData', please change it to an Array, e.g. ['repoData']",
|
|
59
57
|
)
|
|
58
|
+
consoleMock.mockRestore()
|
|
60
59
|
})
|
|
61
60
|
|
|
62
61
|
test('should update when a query updates', async () => {
|
|
@@ -79,20 +78,20 @@ describe('queriesObserver', () => {
|
|
|
79
78
|
unsubscribe()
|
|
80
79
|
expect(results.length).toBe(6)
|
|
81
80
|
expect(results[0]).toMatchObject([
|
|
82
|
-
{ status: '
|
|
83
|
-
{ status: '
|
|
81
|
+
{ status: 'pending', fetchStatus: 'idle', data: undefined },
|
|
82
|
+
{ status: 'pending', fetchStatus: 'idle', data: undefined },
|
|
84
83
|
])
|
|
85
84
|
expect(results[1]).toMatchObject([
|
|
86
|
-
{ status: '
|
|
87
|
-
{ status: '
|
|
85
|
+
{ status: 'pending', fetchStatus: 'fetching', data: undefined },
|
|
86
|
+
{ status: 'pending', fetchStatus: 'idle', data: undefined },
|
|
88
87
|
])
|
|
89
88
|
expect(results[2]).toMatchObject([
|
|
90
|
-
{ status: '
|
|
91
|
-
{ status: '
|
|
89
|
+
{ status: 'pending', fetchStatus: 'fetching', data: undefined },
|
|
90
|
+
{ status: 'pending', fetchStatus: 'fetching', data: undefined },
|
|
92
91
|
])
|
|
93
92
|
expect(results[3]).toMatchObject([
|
|
94
93
|
{ status: 'success', data: 1 },
|
|
95
|
-
{ status: '
|
|
94
|
+
{ status: 'pending', fetchStatus: 'fetching', data: undefined },
|
|
96
95
|
])
|
|
97
96
|
expect(results[4]).toMatchObject([
|
|
98
97
|
{ status: 'success', data: 1 },
|
|
@@ -122,27 +121,27 @@ describe('queriesObserver', () => {
|
|
|
122
121
|
observer.setQueries([{ queryKey: key2, queryFn: queryFn2 }])
|
|
123
122
|
await sleep(1)
|
|
124
123
|
const queryCache = queryClient.getQueryCache()
|
|
125
|
-
expect(queryCache.find(key1,
|
|
126
|
-
expect(queryCache.find(key2,
|
|
124
|
+
expect(queryCache.find({ queryKey: key1, type: 'active' })).toBeUndefined()
|
|
125
|
+
expect(queryCache.find({ queryKey: key2, type: 'active' })).toBeDefined()
|
|
127
126
|
unsubscribe()
|
|
128
|
-
expect(queryCache.find(key1,
|
|
129
|
-
expect(queryCache.find(key2,
|
|
127
|
+
expect(queryCache.find({ queryKey: key1, type: 'active' })).toBeUndefined()
|
|
128
|
+
expect(queryCache.find({ queryKey: key2, type: 'active' })).toBeUndefined()
|
|
130
129
|
expect(results.length).toBe(6)
|
|
131
130
|
expect(results[0]).toMatchObject([
|
|
132
|
-
{ status: '
|
|
133
|
-
{ status: '
|
|
131
|
+
{ status: 'pending', fetchStatus: 'idle', data: undefined },
|
|
132
|
+
{ status: 'pending', fetchStatus: 'idle', data: undefined },
|
|
134
133
|
])
|
|
135
134
|
expect(results[1]).toMatchObject([
|
|
136
|
-
{ status: '
|
|
137
|
-
{ status: '
|
|
135
|
+
{ status: 'pending', fetchStatus: 'fetching', data: undefined },
|
|
136
|
+
{ status: 'pending', fetchStatus: 'idle', data: undefined },
|
|
138
137
|
])
|
|
139
138
|
expect(results[2]).toMatchObject([
|
|
140
|
-
{ status: '
|
|
141
|
-
{ status: '
|
|
139
|
+
{ status: 'pending', fetchStatus: 'fetching', data: undefined },
|
|
140
|
+
{ status: 'pending', fetchStatus: 'fetching', data: undefined },
|
|
142
141
|
])
|
|
143
142
|
expect(results[3]).toMatchObject([
|
|
144
143
|
{ status: 'success', data: 1 },
|
|
145
|
-
{ status: '
|
|
144
|
+
{ status: 'pending', fetchStatus: 'fetching', data: undefined },
|
|
146
145
|
])
|
|
147
146
|
expect(results[4]).toMatchObject([
|
|
148
147
|
{ status: 'success', data: 1 },
|
|
@@ -174,20 +173,20 @@ describe('queriesObserver', () => {
|
|
|
174
173
|
unsubscribe()
|
|
175
174
|
expect(results.length).toBe(6)
|
|
176
175
|
expect(results[0]).toMatchObject([
|
|
177
|
-
{ status: '
|
|
178
|
-
{ status: '
|
|
176
|
+
{ status: 'pending', fetchStatus: 'idle', data: undefined },
|
|
177
|
+
{ status: 'pending', fetchStatus: 'idle', data: undefined },
|
|
179
178
|
])
|
|
180
179
|
expect(results[1]).toMatchObject([
|
|
181
|
-
{ status: '
|
|
182
|
-
{ status: '
|
|
180
|
+
{ status: 'pending', fetchStatus: 'fetching', data: undefined },
|
|
181
|
+
{ status: 'pending', fetchStatus: 'idle', data: undefined },
|
|
183
182
|
])
|
|
184
183
|
expect(results[2]).toMatchObject([
|
|
185
|
-
{ status: '
|
|
186
|
-
{ status: '
|
|
184
|
+
{ status: 'pending', fetchStatus: 'fetching', data: undefined },
|
|
185
|
+
{ status: 'pending', fetchStatus: 'fetching', data: undefined },
|
|
187
186
|
])
|
|
188
187
|
expect(results[3]).toMatchObject([
|
|
189
188
|
{ status: 'success', data: 1 },
|
|
190
|
-
{ status: '
|
|
189
|
+
{ status: 'pending', fetchStatus: 'fetching', data: undefined },
|
|
191
190
|
])
|
|
192
191
|
expect(results[4]).toMatchObject([
|
|
193
192
|
{ status: 'success', data: 1 },
|
|
@@ -222,20 +221,20 @@ describe('queriesObserver', () => {
|
|
|
222
221
|
unsubscribe()
|
|
223
222
|
expect(results.length).toBe(5)
|
|
224
223
|
expect(results[0]).toMatchObject([
|
|
225
|
-
{ status: '
|
|
226
|
-
{ status: '
|
|
224
|
+
{ status: 'pending', fetchStatus: 'idle', data: undefined },
|
|
225
|
+
{ status: 'pending', fetchStatus: 'idle', data: undefined },
|
|
227
226
|
])
|
|
228
227
|
expect(results[1]).toMatchObject([
|
|
229
|
-
{ status: '
|
|
230
|
-
{ status: '
|
|
228
|
+
{ status: 'pending', fetchStatus: 'fetching', data: undefined },
|
|
229
|
+
{ status: 'pending', fetchStatus: 'idle', data: undefined },
|
|
231
230
|
])
|
|
232
231
|
expect(results[2]).toMatchObject([
|
|
233
|
-
{ status: '
|
|
234
|
-
{ status: '
|
|
232
|
+
{ status: 'pending', fetchStatus: 'fetching', data: undefined },
|
|
233
|
+
{ status: 'pending', fetchStatus: 'fetching', data: undefined },
|
|
235
234
|
])
|
|
236
235
|
expect(results[3]).toMatchObject([
|
|
237
236
|
{ status: 'success', data: 1 },
|
|
238
|
-
{ status: '
|
|
237
|
+
{ status: 'pending', fetchStatus: 'fetching', data: undefined },
|
|
239
238
|
])
|
|
240
239
|
expect(results[4]).toMatchObject([
|
|
241
240
|
{ status: 'success', data: 1 },
|
|
@@ -280,7 +279,7 @@ describe('queriesObserver', () => {
|
|
|
280
279
|
unsubscribe1()
|
|
281
280
|
|
|
282
281
|
await waitFor(() => {
|
|
283
|
-
// 1 call:
|
|
282
|
+
// 1 call: pending
|
|
284
283
|
expect(subscription1Handler).toBeCalledTimes(1)
|
|
285
284
|
// 1 call: success
|
|
286
285
|
expect(subscription2Handler).toBeCalledTimes(1)
|
|
@@ -289,39 +288,4 @@ describe('queriesObserver', () => {
|
|
|
289
288
|
// Clean-up
|
|
290
289
|
unsubscribe2()
|
|
291
290
|
})
|
|
292
|
-
|
|
293
|
-
test('onUpdate should not update the result for an unknown observer', async () => {
|
|
294
|
-
const key1 = queryKey()
|
|
295
|
-
const key2 = queryKey()
|
|
296
|
-
|
|
297
|
-
const queriesObserver = new QueriesObserver(queryClient, [
|
|
298
|
-
{
|
|
299
|
-
queryKey: key1,
|
|
300
|
-
queryFn: () => 1,
|
|
301
|
-
},
|
|
302
|
-
])
|
|
303
|
-
|
|
304
|
-
const newQueryObserver = new QueryObserver<
|
|
305
|
-
unknown,
|
|
306
|
-
unknown,
|
|
307
|
-
unknown,
|
|
308
|
-
unknown,
|
|
309
|
-
QueryKey
|
|
310
|
-
>(queryClient, {
|
|
311
|
-
queryKey: key2,
|
|
312
|
-
queryFn: () => 2,
|
|
313
|
-
})
|
|
314
|
-
|
|
315
|
-
// Force onUpdate with an unknown QueryObserver
|
|
316
|
-
// because no existing use case has been found in the lib
|
|
317
|
-
queriesObserver['onUpdate'](
|
|
318
|
-
newQueryObserver,
|
|
319
|
-
// The current queries observer result is re-used here
|
|
320
|
-
// to use a typescript friendly result
|
|
321
|
-
queriesObserver.getCurrentResult()[0]!,
|
|
322
|
-
)
|
|
323
|
-
|
|
324
|
-
// Should not alter the result
|
|
325
|
-
expect(queriesObserver.getCurrentResult()[-1]).toBeUndefined()
|
|
326
|
-
})
|
|
327
291
|
})
|