@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
|
@@ -1,10 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
sleep,
|
|
3
|
-
queryKey,
|
|
4
|
-
expectType,
|
|
5
|
-
mockLogger,
|
|
6
|
-
createQueryClient,
|
|
7
|
-
} from './utils'
|
|
1
|
+
import { sleep, queryKey, expectType, createQueryClient } from './utils'
|
|
8
2
|
import type { QueryClient, QueryObserverResult } from '..'
|
|
9
3
|
import { QueryObserver, focusManager } from '..'
|
|
10
4
|
|
|
@@ -46,9 +40,9 @@ describe('queryObserver', () => {
|
|
|
46
40
|
await sleep(1)
|
|
47
41
|
unsubscribe()
|
|
48
42
|
expect(results.length).toBe(4)
|
|
49
|
-
expect(results[0]).toMatchObject({ data: undefined, status: '
|
|
43
|
+
expect(results[0]).toMatchObject({ data: undefined, status: 'pending' })
|
|
50
44
|
expect(results[1]).toMatchObject({ data: 1, status: 'success' })
|
|
51
|
-
expect(results[2]).toMatchObject({ data: undefined, status: '
|
|
45
|
+
expect(results[2]).toMatchObject({ data: undefined, status: 'pending' })
|
|
52
46
|
expect(results[3]).toMatchObject({ data: 2, status: 'success' })
|
|
53
47
|
})
|
|
54
48
|
|
|
@@ -148,7 +142,7 @@ describe('queryObserver', () => {
|
|
|
148
142
|
expect(count).toBe(2)
|
|
149
143
|
expect(results.length).toBe(5)
|
|
150
144
|
expect(results[0]).toMatchObject({
|
|
151
|
-
status: '
|
|
145
|
+
status: 'pending',
|
|
152
146
|
isFetching: true,
|
|
153
147
|
data: undefined,
|
|
154
148
|
})
|
|
@@ -203,7 +197,7 @@ describe('queryObserver', () => {
|
|
|
203
197
|
expect(count).toBe(1)
|
|
204
198
|
expect(results.length).toBe(4)
|
|
205
199
|
expect(results[0]).toMatchObject({
|
|
206
|
-
status: '
|
|
200
|
+
status: 'pending',
|
|
207
201
|
isFetching: true,
|
|
208
202
|
data: undefined,
|
|
209
203
|
})
|
|
@@ -263,7 +257,7 @@ describe('queryObserver', () => {
|
|
|
263
257
|
await observer.refetch()
|
|
264
258
|
unsubscribe()
|
|
265
259
|
expect(results[0]).toMatchObject({
|
|
266
|
-
status: '
|
|
260
|
+
status: 'pending',
|
|
267
261
|
isFetching: true,
|
|
268
262
|
data: undefined,
|
|
269
263
|
})
|
|
@@ -313,7 +307,7 @@ describe('queryObserver', () => {
|
|
|
313
307
|
unsubscribe()
|
|
314
308
|
|
|
315
309
|
expect(results[0]).toMatchObject({
|
|
316
|
-
status: '
|
|
310
|
+
status: 'pending',
|
|
317
311
|
isFetching: true,
|
|
318
312
|
data: undefined,
|
|
319
313
|
error: null,
|
|
@@ -383,7 +377,7 @@ describe('queryObserver', () => {
|
|
|
383
377
|
enabled: false,
|
|
384
378
|
})
|
|
385
379
|
const unsubscribe = observer.subscribe(callback)
|
|
386
|
-
await queryClient.fetchQuery(key, queryFn)
|
|
380
|
+
await queryClient.fetchQuery({ queryKey: key, queryFn })
|
|
387
381
|
unsubscribe()
|
|
388
382
|
expect(queryFn).toHaveBeenCalledTimes(1)
|
|
389
383
|
expect(callback).toHaveBeenCalledTimes(2)
|
|
@@ -401,7 +395,7 @@ describe('queryObserver', () => {
|
|
|
401
395
|
results.push(x)
|
|
402
396
|
})
|
|
403
397
|
observer.setOptions({ enabled: false, staleTime: 10 })
|
|
404
|
-
await queryClient.fetchQuery(key, queryFn)
|
|
398
|
+
await queryClient.fetchQuery({ queryKey: key, queryFn })
|
|
405
399
|
await sleep(100)
|
|
406
400
|
unsubscribe()
|
|
407
401
|
expect(queryFn).toHaveBeenCalledTimes(1)
|
|
@@ -426,7 +420,7 @@ describe('queryObserver', () => {
|
|
|
426
420
|
const unsubscribe2 = observer.subscribe((x) => {
|
|
427
421
|
results2.push(x)
|
|
428
422
|
})
|
|
429
|
-
await queryClient.fetchQuery(key, queryFn)
|
|
423
|
+
await queryClient.fetchQuery({ queryKey: key, queryFn })
|
|
430
424
|
await sleep(50)
|
|
431
425
|
unsubscribe1()
|
|
432
426
|
unsubscribe2()
|
|
@@ -460,25 +454,29 @@ describe('queryObserver', () => {
|
|
|
460
454
|
|
|
461
455
|
test('should clear interval when unsubscribing to a refetchInterval query', async () => {
|
|
462
456
|
const key = queryKey()
|
|
457
|
+
let count = 0
|
|
463
458
|
|
|
464
|
-
const fetchData = () =>
|
|
459
|
+
const fetchData = () => {
|
|
460
|
+
count++
|
|
461
|
+
return Promise.resolve('data')
|
|
462
|
+
}
|
|
465
463
|
const observer = new QueryObserver(queryClient, {
|
|
466
464
|
queryKey: key,
|
|
467
465
|
queryFn: fetchData,
|
|
468
|
-
|
|
469
|
-
refetchInterval:
|
|
466
|
+
gcTime: 0,
|
|
467
|
+
refetchInterval: 10,
|
|
470
468
|
})
|
|
471
469
|
const unsubscribe = observer.subscribe(() => undefined)
|
|
472
|
-
|
|
473
|
-
|
|
470
|
+
expect(count).toBe(1)
|
|
471
|
+
await sleep(15)
|
|
472
|
+
expect(count).toBe(2)
|
|
474
473
|
unsubscribe()
|
|
475
|
-
// @ts-expect-error
|
|
476
|
-
expect(observer.refetchIntervalId).toBeUndefined()
|
|
477
474
|
await sleep(10)
|
|
478
|
-
expect(queryClient.getQueryCache().find(key)).toBeUndefined()
|
|
475
|
+
expect(queryClient.getQueryCache().find({ queryKey: key })).toBeUndefined()
|
|
476
|
+
expect(count).toBe(2)
|
|
479
477
|
})
|
|
480
478
|
|
|
481
|
-
test('uses placeholderData as non-cache data when
|
|
479
|
+
test('uses placeholderData as non-cache data when pending a query with no data', async () => {
|
|
482
480
|
const key = queryKey()
|
|
483
481
|
const observer = new QueryObserver(queryClient, {
|
|
484
482
|
queryKey: key,
|
|
@@ -523,36 +521,6 @@ describe('queryObserver', () => {
|
|
|
523
521
|
expect(firstData).toBe(secondData)
|
|
524
522
|
})
|
|
525
523
|
|
|
526
|
-
test('the retrier should not throw an error when reject if the retrier is already resolved', async () => {
|
|
527
|
-
const key = queryKey()
|
|
528
|
-
let count = 0
|
|
529
|
-
|
|
530
|
-
const observer = new QueryObserver(queryClient, {
|
|
531
|
-
queryKey: key,
|
|
532
|
-
queryFn: () => {
|
|
533
|
-
count++
|
|
534
|
-
return Promise.reject<unknown>(`reject ${count}`)
|
|
535
|
-
},
|
|
536
|
-
retry: 1,
|
|
537
|
-
retryDelay: 20,
|
|
538
|
-
})
|
|
539
|
-
|
|
540
|
-
const unsubscribe = observer.subscribe(() => undefined)
|
|
541
|
-
|
|
542
|
-
// Simulate a race condition when an unsubscribe and a retry occur.
|
|
543
|
-
await sleep(20)
|
|
544
|
-
unsubscribe()
|
|
545
|
-
|
|
546
|
-
// A second reject is triggered for the retry
|
|
547
|
-
// but the retryer has already set isResolved to true
|
|
548
|
-
// so it does nothing and no error is thrown
|
|
549
|
-
|
|
550
|
-
// Should not log an error
|
|
551
|
-
queryClient.clear()
|
|
552
|
-
await sleep(40)
|
|
553
|
-
expect(mockLogger.error).not.toHaveBeenNthCalledWith(1, 'reject 1')
|
|
554
|
-
})
|
|
555
|
-
|
|
556
524
|
test('should throw an error if enabled option type is not valid', async () => {
|
|
557
525
|
const key = queryKey()
|
|
558
526
|
|
|
@@ -649,50 +617,6 @@ describe('queryObserver', () => {
|
|
|
649
617
|
unsubscribe()
|
|
650
618
|
})
|
|
651
619
|
|
|
652
|
-
test('should prefer isDataEqual to structuralSharing', async () => {
|
|
653
|
-
const key = queryKey()
|
|
654
|
-
|
|
655
|
-
const data = { value: 'data' }
|
|
656
|
-
const newData = { value: 'data' }
|
|
657
|
-
|
|
658
|
-
const observer = new QueryObserver(queryClient, {
|
|
659
|
-
queryKey: key,
|
|
660
|
-
queryFn: () => data,
|
|
661
|
-
})
|
|
662
|
-
|
|
663
|
-
const unsubscribe = observer.subscribe(() => undefined)
|
|
664
|
-
|
|
665
|
-
await sleep(10)
|
|
666
|
-
expect(observer.getCurrentResult().data).toBe(data)
|
|
667
|
-
|
|
668
|
-
observer.setOptions({
|
|
669
|
-
queryKey: key,
|
|
670
|
-
queryFn: () => newData,
|
|
671
|
-
isDataEqual: () => true,
|
|
672
|
-
structuralSharing: false,
|
|
673
|
-
})
|
|
674
|
-
|
|
675
|
-
await observer.refetch()
|
|
676
|
-
expect(observer.getCurrentResult().data).toBe(data)
|
|
677
|
-
|
|
678
|
-
unsubscribe()
|
|
679
|
-
})
|
|
680
|
-
|
|
681
|
-
test('select function error using placeholderdata should log an error', () => {
|
|
682
|
-
const key = queryKey()
|
|
683
|
-
|
|
684
|
-
new QueryObserver(queryClient, {
|
|
685
|
-
queryKey: key,
|
|
686
|
-
queryFn: () => 'data',
|
|
687
|
-
placeholderData: 'placeholderdata',
|
|
688
|
-
select: () => {
|
|
689
|
-
throw new Error('error')
|
|
690
|
-
},
|
|
691
|
-
})
|
|
692
|
-
|
|
693
|
-
expect(mockLogger.error).toHaveBeenNthCalledWith(2, new Error('error'))
|
|
694
|
-
})
|
|
695
|
-
|
|
696
620
|
test('should not use replaceEqualDeep for select value when structuralSharing option is true and placeholderdata is defined', () => {
|
|
697
621
|
const key = queryKey()
|
|
698
622
|
|
|
@@ -766,54 +690,6 @@ describe('queryObserver', () => {
|
|
|
766
690
|
expect(observer.getCurrentResult().isPlaceholderData).toBe(false)
|
|
767
691
|
})
|
|
768
692
|
|
|
769
|
-
test('updateResult should not notify cache listeners if cache option is false', async () => {
|
|
770
|
-
const key = queryKey()
|
|
771
|
-
|
|
772
|
-
const data1 = { value: 'data 1' }
|
|
773
|
-
const data2 = { value: 'data 2' }
|
|
774
|
-
|
|
775
|
-
await queryClient.prefetchQuery(key, () => data1)
|
|
776
|
-
const observer = new QueryObserver(queryClient, {
|
|
777
|
-
queryKey: key,
|
|
778
|
-
})
|
|
779
|
-
await queryClient.prefetchQuery(key, () => data2)
|
|
780
|
-
|
|
781
|
-
const spy = jest.fn()
|
|
782
|
-
const unsubscribe = queryClient.getQueryCache().subscribe(spy)
|
|
783
|
-
observer.updateResult({ cache: false })
|
|
784
|
-
|
|
785
|
-
expect(spy).toHaveBeenCalledTimes(0)
|
|
786
|
-
|
|
787
|
-
unsubscribe()
|
|
788
|
-
})
|
|
789
|
-
|
|
790
|
-
test('should not notify observer when the stale timeout expires and the current result is stale', async () => {
|
|
791
|
-
const key = queryKey()
|
|
792
|
-
const queryFn = () => 'data'
|
|
793
|
-
|
|
794
|
-
await queryClient.prefetchQuery(key, queryFn)
|
|
795
|
-
const observer = new QueryObserver(queryClient, {
|
|
796
|
-
queryKey: key,
|
|
797
|
-
queryFn,
|
|
798
|
-
staleTime: 20,
|
|
799
|
-
})
|
|
800
|
-
|
|
801
|
-
const spy = jest.fn()
|
|
802
|
-
const unsubscribe = observer.subscribe(spy)
|
|
803
|
-
await queryClient.refetchQueries(key)
|
|
804
|
-
await sleep(10)
|
|
805
|
-
|
|
806
|
-
// Force isStale to true
|
|
807
|
-
// because no use case has been found to reproduce this condition
|
|
808
|
-
// @ts-ignore
|
|
809
|
-
observer['currentResult'].isStale = true
|
|
810
|
-
spy.mockReset()
|
|
811
|
-
await sleep(30)
|
|
812
|
-
expect(spy).not.toHaveBeenCalled()
|
|
813
|
-
|
|
814
|
-
unsubscribe()
|
|
815
|
-
})
|
|
816
|
-
|
|
817
693
|
test('setOptions should notify cache listeners', async () => {
|
|
818
694
|
const key = queryKey()
|
|
819
695
|
|
package/src/tests/utils.test.tsx
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import {
|
|
2
2
|
replaceEqualDeep,
|
|
3
|
-
|
|
3
|
+
partialMatchKey,
|
|
4
4
|
isPlainObject,
|
|
5
|
-
parseMutationArgs,
|
|
6
5
|
matchMutation,
|
|
7
6
|
scheduleMicrotask,
|
|
8
7
|
sleep,
|
|
9
8
|
isPlainArray,
|
|
9
|
+
addToEnd,
|
|
10
|
+
addToStart,
|
|
10
11
|
} from '../utils'
|
|
11
12
|
import { Mutation } from '../mutation'
|
|
12
13
|
import { createQueryClient } from './utils'
|
|
@@ -67,47 +68,47 @@ describe('core/utils', () => {
|
|
|
67
68
|
})
|
|
68
69
|
})
|
|
69
70
|
|
|
70
|
-
describe('
|
|
71
|
+
describe('partialMatchKey', () => {
|
|
71
72
|
it('should return `true` if a includes b', () => {
|
|
72
|
-
const a = { a: { b: 'b' }, c: 'c', d: [{ d: 'd ' }] }
|
|
73
|
-
const b = { a: { b: 'b' }, c: 'c', d: [] }
|
|
74
|
-
expect(
|
|
73
|
+
const a = [{ a: { b: 'b' }, c: 'c', d: [{ d: 'd ' }] }]
|
|
74
|
+
const b = [{ a: { b: 'b' }, c: 'c', d: [] }]
|
|
75
|
+
expect(partialMatchKey(a, b)).toEqual(true)
|
|
75
76
|
})
|
|
76
77
|
|
|
77
78
|
it('should return `false` if a does not include b', () => {
|
|
78
|
-
const a = { a: { b: 'b' }, c: 'c', d: [] }
|
|
79
|
-
const b = { a: { b: 'b' }, c: 'c', d: [{ d: 'd ' }] }
|
|
80
|
-
expect(
|
|
79
|
+
const a = [{ a: { b: 'b' }, c: 'c', d: [] }]
|
|
80
|
+
const b = [{ a: { b: 'b' }, c: 'c', d: [{ d: 'd ' }] }]
|
|
81
|
+
expect(partialMatchKey(a, b)).toEqual(false)
|
|
81
82
|
})
|
|
82
83
|
|
|
83
84
|
it('should return `true` if array a includes array b', () => {
|
|
84
85
|
const a = [1, 2, 3]
|
|
85
86
|
const b = [1, 2]
|
|
86
|
-
expect(
|
|
87
|
+
expect(partialMatchKey(a, b)).toEqual(true)
|
|
87
88
|
})
|
|
88
89
|
|
|
89
90
|
it('should return `false` if a is null and b is not', () => {
|
|
90
|
-
const a = null
|
|
91
|
-
const b = { a: { b: 'b' }, c: 'c', d: [{ d: 'd ' }] }
|
|
92
|
-
expect(
|
|
91
|
+
const a = [null]
|
|
92
|
+
const b = [{ a: { b: 'b' }, c: 'c', d: [{ d: 'd ' }] }]
|
|
93
|
+
expect(partialMatchKey(a, b)).toEqual(false)
|
|
93
94
|
})
|
|
94
95
|
|
|
95
96
|
it('should return `false` if a contains null and b is not', () => {
|
|
96
|
-
const a = { a: null, c: 'c', d: [] }
|
|
97
|
-
const b = { a: { b: 'b' }, c: 'c', d: [{ d: 'd ' }] }
|
|
98
|
-
expect(
|
|
97
|
+
const a = [{ a: null, c: 'c', d: [] }]
|
|
98
|
+
const b = [{ a: { b: 'b' }, c: 'c', d: [{ d: 'd ' }] }]
|
|
99
|
+
expect(partialMatchKey(a, b)).toEqual(false)
|
|
99
100
|
})
|
|
100
101
|
|
|
101
102
|
it('should return `false` if b is null and a is not', () => {
|
|
102
|
-
const a = { a: { b: 'b' }, c: 'c', d: [] }
|
|
103
|
-
const b = null
|
|
104
|
-
expect(
|
|
103
|
+
const a = [{ a: { b: 'b' }, c: 'c', d: [] }]
|
|
104
|
+
const b = [null]
|
|
105
|
+
expect(partialMatchKey(a, b)).toEqual(false)
|
|
105
106
|
})
|
|
106
107
|
|
|
107
108
|
it('should return `false` if b contains null and a is not', () => {
|
|
108
|
-
const a = { a: { b: 'b' }, c: 'c', d: [] }
|
|
109
|
-
const b = { a: null, c: 'c', d: [{ d: 'd ' }] }
|
|
110
|
-
expect(
|
|
109
|
+
const a = [{ a: { b: 'b' }, c: 'c', d: [] }]
|
|
110
|
+
const b = [{ a: null, c: 'c', d: [{ d: 'd ' }] }]
|
|
111
|
+
expect(partialMatchKey(a, b)).toEqual(false)
|
|
111
112
|
})
|
|
112
113
|
})
|
|
113
114
|
|
|
@@ -327,13 +328,6 @@ describe('core/utils', () => {
|
|
|
327
328
|
})
|
|
328
329
|
})
|
|
329
330
|
|
|
330
|
-
describe('parseMutationArgs', () => {
|
|
331
|
-
it('should return mutation options', () => {
|
|
332
|
-
const options = { mutationKey: ['key'] }
|
|
333
|
-
expect(parseMutationArgs(options)).toMatchObject(options)
|
|
334
|
-
})
|
|
335
|
-
})
|
|
336
|
-
|
|
337
331
|
describe('matchMutation', () => {
|
|
338
332
|
it('should return false if mutationKey options is undefined', () => {
|
|
339
333
|
const filters = { mutationKey: ['key1'] }
|
|
@@ -357,4 +351,65 @@ describe('core/utils', () => {
|
|
|
357
351
|
expect(callback).toHaveBeenCalledTimes(1)
|
|
358
352
|
})
|
|
359
353
|
})
|
|
354
|
+
|
|
355
|
+
describe('addToEnd', () => {
|
|
356
|
+
it('should add item to the end of the array', () => {
|
|
357
|
+
const items = [1, 2, 3]
|
|
358
|
+
const newItems = addToEnd(items, 4)
|
|
359
|
+
expect(newItems).toEqual([1, 2, 3, 4])
|
|
360
|
+
})
|
|
361
|
+
|
|
362
|
+
it('should not exceed max if provided', () => {
|
|
363
|
+
const items = [1, 2, 3]
|
|
364
|
+
const newItems = addToEnd(items, 4, 3)
|
|
365
|
+
expect(newItems).toEqual([2, 3, 4])
|
|
366
|
+
})
|
|
367
|
+
|
|
368
|
+
it('should add item to the end of the array when max = 0', () => {
|
|
369
|
+
const items = [1, 2, 3]
|
|
370
|
+
const item = 4
|
|
371
|
+
const max = 0
|
|
372
|
+
expect(addToEnd(items, item, max)).toEqual([1, 2, 3, 4])
|
|
373
|
+
})
|
|
374
|
+
|
|
375
|
+
it('should add item to the end of the array when max is undefined', () => {
|
|
376
|
+
const items = [1, 2, 3]
|
|
377
|
+
const item = 4
|
|
378
|
+
const max = undefined
|
|
379
|
+
expect(addToEnd(items, item, max)).toEqual([1, 2, 3, 4])
|
|
380
|
+
})
|
|
381
|
+
})
|
|
382
|
+
|
|
383
|
+
describe('addToStart', () => {
|
|
384
|
+
it('should add an item to the start of the array', () => {
|
|
385
|
+
const items = [1, 2, 3]
|
|
386
|
+
const item = 4
|
|
387
|
+
const newItems = addToStart(items, item)
|
|
388
|
+
expect(newItems).toEqual([4, 1, 2, 3])
|
|
389
|
+
})
|
|
390
|
+
|
|
391
|
+
it('should respect the max argument', () => {
|
|
392
|
+
const items = [1, 2, 3]
|
|
393
|
+
const item = 4
|
|
394
|
+
const max = 2
|
|
395
|
+
const newItems = addToStart(items, item, max)
|
|
396
|
+
expect(newItems).toEqual([4, 1, 2])
|
|
397
|
+
})
|
|
398
|
+
|
|
399
|
+
it('should not remove any items if max = 0', () => {
|
|
400
|
+
const items = [1, 2, 3]
|
|
401
|
+
const item = 4
|
|
402
|
+
const max = 0
|
|
403
|
+
const newItems = addToStart(items, item, max)
|
|
404
|
+
expect(newItems).toEqual([4, 1, 2, 3])
|
|
405
|
+
})
|
|
406
|
+
|
|
407
|
+
it('should not remove any items if max is undefined', () => {
|
|
408
|
+
const items = [1, 2, 3]
|
|
409
|
+
const item = 4
|
|
410
|
+
const max = 0
|
|
411
|
+
const newItems = addToStart(items, item, max)
|
|
412
|
+
expect(newItems).toEqual([4, 1, 2, 3])
|
|
413
|
+
})
|
|
414
|
+
})
|
|
360
415
|
})
|
package/src/tests/utils.ts
CHANGED
|
@@ -5,8 +5,7 @@ import { QueryClient } from '@tanstack/query-core'
|
|
|
5
5
|
import * as utils from '../utils'
|
|
6
6
|
|
|
7
7
|
export function createQueryClient(config?: QueryClientConfig): QueryClient {
|
|
8
|
-
|
|
9
|
-
return new QueryClient({ logger: mockLogger, ...config })
|
|
8
|
+
return new QueryClient(config)
|
|
10
9
|
}
|
|
11
10
|
|
|
12
11
|
export function mockVisibilityState(value: DocumentVisibilityState) {
|
|
@@ -17,12 +16,6 @@ export function mockNavigatorOnLine(value: boolean) {
|
|
|
17
16
|
return jest.spyOn(navigator, 'onLine', 'get').mockReturnValue(value)
|
|
18
17
|
}
|
|
19
18
|
|
|
20
|
-
export const mockLogger = {
|
|
21
|
-
log: jest.fn(),
|
|
22
|
-
warn: jest.fn(),
|
|
23
|
-
error: jest.fn(),
|
|
24
|
-
}
|
|
25
|
-
|
|
26
19
|
let queryKeyCount = 0
|
|
27
20
|
export function queryKey(): Array<string> {
|
|
28
21
|
queryKeyCount++
|
|
@@ -48,17 +41,15 @@ export function setActTimeout(fn: () => void, ms?: number) {
|
|
|
48
41
|
*/
|
|
49
42
|
export const expectType = <T>(_: T): void => undefined
|
|
50
43
|
|
|
51
|
-
|
|
52
|
-
* Assert the parameter is not typed as `any`
|
|
53
|
-
*/
|
|
54
|
-
export const expectTypeNotAny = <T>(_: 0 extends 1 & T ? never : T): void =>
|
|
55
|
-
undefined
|
|
56
|
-
|
|
57
|
-
export const executeMutation = (
|
|
44
|
+
export const executeMutation = <TVariables>(
|
|
58
45
|
queryClient: QueryClient,
|
|
59
|
-
options: MutationOptions<any, any,
|
|
60
|
-
|
|
61
|
-
|
|
46
|
+
options: MutationOptions<any, any, TVariables, any>,
|
|
47
|
+
variables: TVariables,
|
|
48
|
+
) => {
|
|
49
|
+
return queryClient
|
|
50
|
+
.getMutationCache()
|
|
51
|
+
.build(queryClient, options)
|
|
52
|
+
.execute(variables)
|
|
62
53
|
}
|
|
63
54
|
|
|
64
55
|
// This monkey-patches the isServer-value from utils,
|