@tanstack/solid-query 5.0.0-alpha.2 → 5.0.0-alpha.20

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.
Files changed (54) hide show
  1. package/build/cjs/index.js +73 -34
  2. package/build/cjs/index.js.map +1 -1
  3. package/build/esm/index.js +75 -37
  4. package/build/esm/index.js.map +1 -1
  5. package/build/source/QueryClient.js +6 -0
  6. package/build/source/__tests__/QueryClientProvider.test.jsx +2 -1
  7. package/build/source/__tests__/createInfiniteQuery.test.jsx +67 -20
  8. package/build/source/__tests__/createMutation.test.jsx +19 -18
  9. package/build/source/__tests__/createQueries.test.jsx +2 -18
  10. package/build/source/__tests__/createQuery.test.jsx +70 -28
  11. package/build/source/__tests__/suspense.test.jsx +6 -5
  12. package/build/source/__tests__/useIsFetching.test.jsx +2 -4
  13. package/build/source/__tests__/useIsMutating.test.jsx +25 -28
  14. package/build/source/__tests__/utils.jsx +4 -3
  15. package/build/source/createBaseQuery.js +45 -19
  16. package/build/source/createQueries.js +5 -5
  17. package/build/source/index.js +1 -0
  18. package/build/source/useIsFetching.js +5 -5
  19. package/build/source/useIsMutating.js +5 -5
  20. package/build/types/QueryClient.d.ts +29 -0
  21. package/build/types/QueryClientProvider.d.ts +1 -1
  22. package/build/types/__tests__/utils.d.ts +3 -4
  23. package/build/types/createBaseQuery.d.ts +3 -2
  24. package/build/types/createInfiniteQuery.d.ts +4 -2
  25. package/build/types/createMutation.d.ts +4 -2
  26. package/build/types/createQueries.d.ts +5 -4
  27. package/build/types/createQuery.d.ts +2 -1
  28. package/build/types/index.d.ts +2 -0
  29. package/build/types/types.d.ts +2 -1
  30. package/build/types/useIsFetching.d.ts +3 -7
  31. package/build/types/useIsMutating.d.ts +3 -7
  32. package/build/umd/index.js +1 -1
  33. package/build/umd/index.js.map +1 -1
  34. package/package.json +5 -5
  35. package/src/QueryClient.ts +84 -0
  36. package/src/QueryClientProvider.tsx +1 -1
  37. package/src/__tests__/QueryClientProvider.test.tsx +2 -1
  38. package/src/__tests__/createInfiniteQuery.test.tsx +95 -34
  39. package/src/__tests__/createMutation.test.tsx +19 -18
  40. package/src/__tests__/createQueries.test.tsx +2 -24
  41. package/src/__tests__/createQuery.test.tsx +86 -29
  42. package/src/__tests__/suspense.test.tsx +6 -5
  43. package/src/__tests__/useIsFetching.test.tsx +2 -4
  44. package/src/__tests__/useIsMutating.test.tsx +32 -40
  45. package/src/__tests__/utils.tsx +4 -3
  46. package/src/createBaseQuery.ts +70 -25
  47. package/src/createInfiniteQuery.ts +3 -2
  48. package/src/createMutation.ts +4 -2
  49. package/src/createQueries.ts +9 -8
  50. package/src/createQuery.ts +4 -2
  51. package/src/index.ts +7 -0
  52. package/src/types.ts +4 -2
  53. package/src/useIsFetching.ts +10 -13
  54. package/src/useIsMutating.ts +10 -11
@@ -10,6 +10,7 @@ import {
10
10
  Index,
11
11
  Match,
12
12
  Switch,
13
+ on,
13
14
  } from 'solid-js'
14
15
  import type {
15
16
  CreateInfiniteQueryResult,
@@ -23,6 +24,8 @@ import {
23
24
  keepPreviousData,
24
25
  } from '..'
25
26
  import { Blink, queryKey, setActTimeout } from './utils'
27
+ import { vi } from 'vitest'
28
+ import type { Mock } from 'vitest'
26
29
 
27
30
  interface Result {
28
31
  items: number[]
@@ -191,7 +194,8 @@ describe('useInfiniteQuery', () => {
191
194
 
192
195
  it('should keep the previous data when placeholderData is set', async () => {
193
196
  const key = queryKey()
194
- const states: CreateInfiniteQueryResult<InfiniteData<string>>[] = []
197
+ const states: Partial<CreateInfiniteQueryResult<InfiniteData<string>>>[] =
198
+ []
195
199
 
196
200
  function Page() {
197
201
  const [order, setOrder] = createSignal('desc')
@@ -210,7 +214,16 @@ describe('useInfiniteQuery', () => {
210
214
  }))
211
215
 
212
216
  createRenderEffect(() => {
213
- states.push({ ...state })
217
+ states.push({
218
+ data: state.data ? JSON.parse(JSON.stringify(state.data)) : undefined,
219
+ hasNextPage: state.hasNextPage,
220
+ hasPreviousPage: state.hasPreviousPage,
221
+ isFetching: state.isFetching,
222
+ isFetchingNextPage: state.isFetchingNextPage,
223
+ isFetchingPreviousPage: state.isFetchingPreviousPage,
224
+ isSuccess: state.isSuccess,
225
+ isPlaceholderData: state.isPlaceholderData,
226
+ })
214
227
  })
215
228
 
216
229
  return (
@@ -373,7 +386,8 @@ describe('useInfiniteQuery', () => {
373
386
 
374
387
  it('should be able to reverse the data', async () => {
375
388
  const key = queryKey()
376
- const states: CreateInfiniteQueryResult<InfiniteData<number>>[] = []
389
+ const states: Partial<CreateInfiniteQueryResult<InfiniteData<number>>>[] =
390
+ []
377
391
 
378
392
  function Page() {
379
393
  const state = createInfiniteQuery(() => ({
@@ -392,9 +406,19 @@ describe('useInfiniteQuery', () => {
392
406
  defaultPageParam: 0,
393
407
  }))
394
408
 
395
- createRenderEffect(() => {
396
- states.push({ ...state })
397
- })
409
+ createRenderEffect(
410
+ on(
411
+ () => ({ ...state }),
412
+ () => {
413
+ states.push({
414
+ data: state.data
415
+ ? JSON.parse(JSON.stringify(state.data))
416
+ : undefined,
417
+ isSuccess: state.isSuccess,
418
+ })
419
+ },
420
+ ),
421
+ )
398
422
 
399
423
  return (
400
424
  <div>
@@ -437,7 +461,8 @@ describe('useInfiniteQuery', () => {
437
461
 
438
462
  it('should be able to fetch a previous page', async () => {
439
463
  const key = queryKey()
440
- const states: CreateInfiniteQueryResult<InfiniteData<number>>[] = []
464
+ const states: Partial<CreateInfiniteQueryResult<InfiniteData<number>>>[] =
465
+ []
441
466
 
442
467
  function Page() {
443
468
  const start = 10
@@ -454,7 +479,15 @@ describe('useInfiniteQuery', () => {
454
479
  }))
455
480
 
456
481
  createRenderEffect(() => {
457
- states.push({ ...state })
482
+ states.push({
483
+ data: state.data ? JSON.parse(JSON.stringify(state.data)) : undefined,
484
+ hasNextPage: state.hasNextPage,
485
+ hasPreviousPage: state.hasPreviousPage,
486
+ isFetching: state.isFetching,
487
+ isFetchingNextPage: state.isFetchingNextPage,
488
+ isFetchingPreviousPage: state.isFetchingPreviousPage,
489
+ isSuccess: state.isSuccess,
490
+ })
458
491
  })
459
492
 
460
493
  createEffect(() => {
@@ -516,7 +549,8 @@ describe('useInfiniteQuery', () => {
516
549
 
517
550
  it('should be able to refetch when providing page params automatically', async () => {
518
551
  const key = queryKey()
519
- const states: CreateInfiniteQueryResult<InfiniteData<number>>[] = []
552
+ const states: Partial<CreateInfiniteQueryResult<InfiniteData<number>>>[] =
553
+ []
520
554
 
521
555
  function Page() {
522
556
  const state = createInfiniteQuery(() => ({
@@ -533,7 +567,13 @@ describe('useInfiniteQuery', () => {
533
567
  }))
534
568
 
535
569
  createRenderEffect(() => {
536
- states.push({ ...state })
570
+ states.push({
571
+ data: state.data ? JSON.parse(JSON.stringify(state.data)) : undefined,
572
+ isFetching: state.isFetching,
573
+ isFetchingNextPage: state.isFetchingNextPage,
574
+ isRefetching: state.isRefetching,
575
+ isFetchingPreviousPage: state.isFetchingPreviousPage,
576
+ })
537
577
  })
538
578
 
539
579
  return (
@@ -630,7 +670,8 @@ describe('useInfiniteQuery', () => {
630
670
 
631
671
  it('should silently cancel any ongoing fetch when fetching more', async () => {
632
672
  const key = queryKey()
633
- const states: CreateInfiniteQueryResult<InfiniteData<number>>[] = []
673
+ const states: Partial<CreateInfiniteQueryResult<InfiniteData<number>>>[] =
674
+ []
634
675
 
635
676
  function Page() {
636
677
  const start = 10
@@ -647,7 +688,13 @@ describe('useInfiniteQuery', () => {
647
688
  }))
648
689
 
649
690
  createRenderEffect(() => {
650
- states.push({ ...state })
691
+ states.push({
692
+ hasNextPage: state.hasNextPage,
693
+ data: state.data ? JSON.parse(JSON.stringify(state.data)) : undefined,
694
+ isFetching: state.isFetching,
695
+ isFetchingNextPage: state.isFetchingNextPage,
696
+ isSuccess: state.isSuccess,
697
+ })
651
698
  })
652
699
 
653
700
  createEffect(() => {
@@ -712,14 +759,14 @@ describe('useInfiniteQuery', () => {
712
759
  it('should silently cancel an ongoing fetchNextPage request when another fetchNextPage is invoked', async () => {
713
760
  const key = queryKey()
714
761
  const start = 10
715
- const onAborts: jest.Mock<any, any>[] = []
716
- const abortListeners: jest.Mock<any, any>[] = []
717
- const fetchPage = jest.fn<
718
- Promise<number>,
719
- [QueryFunctionContext<typeof key, number>]
762
+ const onAborts: Mock<any, any>[] = []
763
+ const abortListeners: Mock<any, any>[] = []
764
+ const fetchPage = vi.fn<
765
+ [QueryFunctionContext<typeof key, number>],
766
+ Promise<number>
720
767
  >(async ({ pageParam, signal }) => {
721
- const onAbort = jest.fn()
722
- const abortListener = jest.fn()
768
+ const onAbort = vi.fn()
769
+ const abortListener = vi.fn()
723
770
  onAborts.push(onAbort)
724
771
  abortListeners.push(abortListener)
725
772
  signal.onabort = onAbort
@@ -794,14 +841,14 @@ describe('useInfiniteQuery', () => {
794
841
  it('should not cancel an ongoing fetchNextPage request when another fetchNextPage is invoked if `cancelRefetch: false` is used ', async () => {
795
842
  const key = queryKey()
796
843
  const start = 10
797
- const onAborts: jest.Mock<any, any>[] = []
798
- const abortListeners: jest.Mock<any, any>[] = []
799
- const fetchPage = jest.fn<
800
- Promise<number>,
801
- [QueryFunctionContext<typeof key, number>]
844
+ const onAborts: Mock<any, any>[] = []
845
+ const abortListeners: Mock<any, any>[] = []
846
+ const fetchPage = vi.fn<
847
+ [QueryFunctionContext<typeof key, number>],
848
+ Promise<number>
802
849
  >(async ({ pageParam, signal }) => {
803
- const onAbort = jest.fn()
804
- const abortListener = jest.fn()
850
+ const onAbort = vi.fn()
851
+ const abortListener = vi.fn()
805
852
  onAborts.push(onAbort)
806
853
  abortListeners.push(abortListener)
807
854
  signal.onabort = onAbort
@@ -976,7 +1023,8 @@ describe('useInfiniteQuery', () => {
976
1023
 
977
1024
  it('should be able to set new pages with the query client', async () => {
978
1025
  const key = queryKey()
979
- const states: CreateInfiniteQueryResult<InfiniteData<number>>[] = []
1026
+ const states: Partial<CreateInfiniteQueryResult<InfiniteData<number>>>[] =
1027
+ []
980
1028
 
981
1029
  function Page() {
982
1030
  const [firstPage, setFirstPage] = createSignal(0)
@@ -994,7 +1042,13 @@ describe('useInfiniteQuery', () => {
994
1042
  }))
995
1043
 
996
1044
  createRenderEffect(() => {
997
- states.push({ ...state })
1045
+ states.push({
1046
+ hasNextPage: state.hasNextPage,
1047
+ data: state.data ? JSON.parse(JSON.stringify(state.data)) : undefined,
1048
+ isFetching: state.isFetching,
1049
+ isFetchingNextPage: state.isFetchingNextPage,
1050
+ isSuccess: state.isSuccess,
1051
+ })
998
1052
  })
999
1053
 
1000
1054
  createEffect(() => {
@@ -1064,7 +1118,8 @@ describe('useInfiniteQuery', () => {
1064
1118
 
1065
1119
  it('should only refetch the first page when initialData is provided', async () => {
1066
1120
  const key = queryKey()
1067
- const states: CreateInfiniteQueryResult<InfiniteData<number>>[] = []
1121
+ const states: Partial<CreateInfiniteQueryResult<InfiniteData<number>>>[] =
1122
+ []
1068
1123
 
1069
1124
  function Page() {
1070
1125
  const state = createInfiniteQuery(() => ({
@@ -1081,7 +1136,13 @@ describe('useInfiniteQuery', () => {
1081
1136
  }))
1082
1137
 
1083
1138
  createRenderEffect(() => {
1084
- states.push({ ...state })
1139
+ states.push({
1140
+ data: JSON.parse(JSON.stringify(state.data)),
1141
+ hasNextPage: state.hasNextPage,
1142
+ isFetching: state.isFetching,
1143
+ isFetchingNextPage: state.isFetchingNextPage,
1144
+ isSuccess: state.isSuccess,
1145
+ })
1085
1146
  })
1086
1147
 
1087
1148
  createEffect(() => {
@@ -1396,7 +1457,7 @@ describe('useInfiniteQuery', () => {
1396
1457
  >
1397
1458
  <Match when={state.status === 'pending'}>Loading...</Match>
1398
1459
  <Match when={state.status === 'error'}>
1399
- <span>Error: {state.error!.message}</span>
1460
+ <span>Error: {state.error?.message}</span>
1400
1461
  </Match>
1401
1462
  </Switch>
1402
1463
  </div>
@@ -1523,7 +1584,7 @@ describe('useInfiniteQuery', () => {
1523
1584
  >
1524
1585
  <Match when={state.status === 'pending'}>Loading...</Match>
1525
1586
  <Match when={state.status === 'error'}>
1526
- <span>Error: {state.error!.message}</span>
1587
+ <span>Error: {state.error?.message}</span>
1527
1588
  </Match>
1528
1589
  </Switch>
1529
1590
  </div>
@@ -1587,11 +1648,11 @@ describe('useInfiniteQuery', () => {
1587
1648
 
1588
1649
  it('should cancel the query function when there are no more subscriptions', async () => {
1589
1650
  const key = queryKey()
1590
- let cancelFn: jest.Mock = jest.fn()
1651
+ let cancelFn: Mock = vi.fn()
1591
1652
 
1592
1653
  const queryFn = ({ signal }: { signal?: AbortSignal }) => {
1593
1654
  const promise = new Promise<string>((resolve, reject) => {
1594
- cancelFn = jest.fn(() => reject('Cancelled'))
1655
+ cancelFn = vi.fn(() => reject('Cancelled'))
1595
1656
  signal?.addEventListener('abort', cancelFn)
1596
1657
  sleep(20).then(() => resolve('OK'))
1597
1658
  })
@@ -20,6 +20,7 @@ import {
20
20
  setActTimeout,
21
21
  sleep,
22
22
  } from './utils'
23
+ import { vi } from 'vitest'
23
24
 
24
25
  describe('createMutation', () => {
25
26
  const queryCache = new QueryCache()
@@ -108,8 +109,8 @@ describe('createMutation', () => {
108
109
 
109
110
  it('should be able to call `onSuccess` and `onSettled` after each successful mutate', async () => {
110
111
  const [count, setCount] = createSignal(0)
111
- const onSuccessMock = jest.fn()
112
- const onSettledMock = jest.fn()
112
+ const onSuccessMock = vi.fn()
113
+ const onSettledMock = vi.fn()
113
114
 
114
115
  function Page() {
115
116
  const mutation = createMutation(() => ({
@@ -174,7 +175,7 @@ describe('createMutation', () => {
174
175
  const [count, setCount] = createSignal(0)
175
176
  type Value = { count: number }
176
177
 
177
- const mutateFn = jest.fn<Promise<Value>, [value: Value]>()
178
+ const mutateFn = vi.fn<[value: Value], Promise<Value>>()
178
179
 
179
180
  mutateFn.mockImplementationOnce(() => {
180
181
  return Promise.reject(new Error('Error test Jonas'))
@@ -231,8 +232,8 @@ describe('createMutation', () => {
231
232
  })
232
233
 
233
234
  it('should be able to call `onError` and `onSettled` after each failed mutate', async () => {
234
- const onErrorMock = jest.fn()
235
- const onSettledMock = jest.fn()
235
+ const onErrorMock = vi.fn()
236
+ const onSettledMock = vi.fn()
236
237
  const [count, setCount] = createSignal(0)
237
238
 
238
239
  function Page() {
@@ -562,7 +563,7 @@ describe('createMutation', () => {
562
563
 
563
564
  it('should call onMutate even if paused', async () => {
564
565
  const onlineMock = mockNavigatorOnLine(false)
565
- const onMutate = jest.fn()
566
+ const onMutate = vi.fn()
566
567
  let count = 0
567
568
 
568
569
  function Page() {
@@ -870,8 +871,8 @@ describe('createMutation', () => {
870
871
  })
871
872
 
872
873
  it('should pass meta to mutation', async () => {
873
- const errorMock = jest.fn()
874
- const successMock = jest.fn()
874
+ const errorMock = vi.fn()
875
+ const successMock = vi.fn()
875
876
 
876
877
  const queryClientMutationMeta = createQueryClient({
877
878
  mutationCache: new MutationCache({
@@ -930,10 +931,10 @@ describe('createMutation', () => {
930
931
  })
931
932
 
932
933
  it('should call cache callbacks when unmounted', async () => {
933
- const onSuccess = jest.fn()
934
- const onSuccessMutate = jest.fn()
935
- const onSettled = jest.fn()
936
- const onSettledMutate = jest.fn()
934
+ const onSuccess = vi.fn()
935
+ const onSuccessMutate = vi.fn()
936
+ const onSettled = vi.fn()
937
+ const onSettledMutate = vi.fn()
937
938
  const mutationKey = queryKey()
938
939
  let count = 0
939
940
 
@@ -1006,10 +1007,10 @@ describe('createMutation', () => {
1006
1007
  })
1007
1008
 
1008
1009
  it('should call mutate callbacks only for the last observer', async () => {
1009
- const onSuccess = jest.fn()
1010
- const onSuccessMutate = jest.fn()
1011
- const onSettled = jest.fn()
1012
- const onSettledMutate = jest.fn()
1010
+ const onSuccess = vi.fn()
1011
+ const onSuccessMutate = vi.fn()
1012
+ const onSettled = vi.fn()
1013
+ const onSettledMutate = vi.fn()
1013
1014
  let count = 0
1014
1015
 
1015
1016
  function Page() {
@@ -1072,7 +1073,7 @@ describe('createMutation', () => {
1072
1073
 
1073
1074
  it('should go to error state if onSuccess callback errors', async () => {
1074
1075
  const error = new Error('error from onSuccess')
1075
- const onError = jest.fn()
1076
+ const onError = vi.fn()
1076
1077
 
1077
1078
  function Page() {
1078
1079
  const mutation = createMutation(() => ({
@@ -1148,7 +1149,7 @@ describe('createMutation', () => {
1148
1149
  it('should go to error state if onSettled callback errors', async () => {
1149
1150
  const error = new Error('error from onSettled')
1150
1151
  const mutateFnError = new Error('mutateFnError')
1151
- const onError = jest.fn()
1152
+ const onError = vi.fn()
1152
1153
 
1153
1154
  function Page() {
1154
1155
  const mutation = createMutation(() => ({
@@ -23,6 +23,7 @@ import {
23
23
  queryKey,
24
24
  sleep,
25
25
  } from './utils'
26
+ import { vi } from 'vitest'
26
27
 
27
28
  describe('useQueries', () => {
28
29
  const queryCache = new QueryCache()
@@ -739,7 +740,7 @@ describe('useQueries', () => {
739
740
  }
740
741
  }
741
742
 
742
- const QueriesObserverSpy = jest
743
+ const QueriesObserverSpy = vi
743
744
  .spyOn(QueriesObserverModule, 'QueriesObserver')
744
745
  .mockImplementation((fn) => {
745
746
  return new QueriesObserverMock(fn)
@@ -789,27 +790,4 @@ describe('useQueries', () => {
789
790
  await sleep(20)
790
791
  QueriesObserverSpy.mockRestore()
791
792
  })
792
-
793
- it('should use provided custom queryClient', async () => {
794
- const key = queryKey()
795
- const queryFn = () => {
796
- return Promise.resolve('custom client')
797
- }
798
-
799
- function Page() {
800
- const state = createQueries(() => ({
801
- queries: [{ queryKey: key, queryFn }],
802
- queryClient,
803
- }))
804
- return (
805
- <div>
806
- <h1>Status: {state[0].data}</h1>
807
- </div>
808
- )
809
- }
810
-
811
- render(() => <Page />)
812
-
813
- await waitFor(() => screen.getByText('Status: custom client'))
814
- })
815
793
  })