@tanstack/solid-query 5.0.0-alpha.4 → 5.0.0-alpha.5
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/source/__tests__/QueryClientProvider.test.jsx +2 -1
- package/build/source/__tests__/createInfiniteQuery.test.jsx +11 -10
- package/build/source/__tests__/createMutation.test.jsx +19 -18
- package/build/source/__tests__/createQueries.test.jsx +2 -17
- package/build/source/__tests__/createQuery.test.jsx +26 -25
- package/build/source/__tests__/suspense.test.jsx +6 -5
- package/build/source/__tests__/useIsMutating.test.jsx +23 -22
- package/build/source/__tests__/utils.jsx +3 -2
- package/build/types/__tests__/utils.d.ts +2 -3
- package/package.json +4 -4
- package/src/__tests__/QueryClientProvider.test.tsx +2 -1
- package/src/__tests__/createInfiniteQuery.test.tsx +20 -18
- package/src/__tests__/createMutation.test.tsx +19 -18
- package/src/__tests__/createQueries.test.tsx +2 -26
- package/src/__tests__/createQuery.test.tsx +27 -25
- package/src/__tests__/suspense.test.tsx +6 -5
- package/src/__tests__/useIsMutating.test.tsx +29 -33
- package/src/__tests__/utils.tsx +3 -2
|
@@ -3,6 +3,7 @@ import { queryKey } from './utils';
|
|
|
3
3
|
import { QueryCache } from '@tanstack/query-core';
|
|
4
4
|
import { createQuery, QueryClientProvider, useQueryClient } from '..';
|
|
5
5
|
import { createQueryClient, sleep } from './utils';
|
|
6
|
+
import { vi } from 'vitest';
|
|
6
7
|
describe('QueryClientProvider', () => {
|
|
7
8
|
it('sets a specific cache for all queries to use', async () => {
|
|
8
9
|
const key = queryKey();
|
|
@@ -106,7 +107,7 @@ describe('QueryClientProvider', () => {
|
|
|
106
107
|
});
|
|
107
108
|
describe('useQueryClient', () => {
|
|
108
109
|
it('should throw an error if no query client has been set', () => {
|
|
109
|
-
const consoleMock =
|
|
110
|
+
const consoleMock = vi
|
|
110
111
|
.spyOn(console, 'error')
|
|
111
112
|
.mockImplementation(() => undefined);
|
|
112
113
|
function Page() {
|
|
@@ -3,6 +3,7 @@ import { createQueryClient, sleep } from './utils';
|
|
|
3
3
|
import { createEffect, createRenderEffect, createSignal, For, Index, Match, Switch, } from 'solid-js';
|
|
4
4
|
import { createInfiniteQuery, QueryCache, QueryClientProvider, keepPreviousData, } from '..';
|
|
5
5
|
import { Blink, queryKey, setActTimeout } from './utils';
|
|
6
|
+
import { vi } from 'vitest';
|
|
6
7
|
const pageSize = 10;
|
|
7
8
|
const fetchItems = async (page, ts, noNext, noPrev) => {
|
|
8
9
|
await sleep(10);
|
|
@@ -582,9 +583,9 @@ describe('useInfiniteQuery', () => {
|
|
|
582
583
|
const start = 10;
|
|
583
584
|
const onAborts = [];
|
|
584
585
|
const abortListeners = [];
|
|
585
|
-
const fetchPage =
|
|
586
|
-
const onAbort =
|
|
587
|
-
const abortListener =
|
|
586
|
+
const fetchPage = vi.fn(async ({ pageParam, signal }) => {
|
|
587
|
+
const onAbort = vi.fn();
|
|
588
|
+
const abortListener = vi.fn();
|
|
588
589
|
onAborts.push(onAbort);
|
|
589
590
|
abortListeners.push(abortListener);
|
|
590
591
|
signal.onabort = onAbort;
|
|
@@ -648,9 +649,9 @@ describe('useInfiniteQuery', () => {
|
|
|
648
649
|
const start = 10;
|
|
649
650
|
const onAborts = [];
|
|
650
651
|
const abortListeners = [];
|
|
651
|
-
const fetchPage =
|
|
652
|
-
const onAbort =
|
|
653
|
-
const abortListener =
|
|
652
|
+
const fetchPage = vi.fn(async ({ pageParam, signal }) => {
|
|
653
|
+
const onAbort = vi.fn();
|
|
654
|
+
const abortListener = vi.fn();
|
|
654
655
|
onAborts.push(onAbort);
|
|
655
656
|
abortListeners.push(abortListener);
|
|
656
657
|
signal.onabort = onAbort;
|
|
@@ -1131,7 +1132,7 @@ describe('useInfiniteQuery', () => {
|
|
|
1131
1132
|
</>}>
|
|
1132
1133
|
<Match when={state.status === 'pending'}>Loading...</Match>
|
|
1133
1134
|
<Match when={state.status === 'error'}>
|
|
1134
|
-
<span>Error: {state.error
|
|
1135
|
+
<span>Error: {state.error?.message}</span>
|
|
1135
1136
|
</Match>
|
|
1136
1137
|
</Switch>
|
|
1137
1138
|
</div>);
|
|
@@ -1221,7 +1222,7 @@ describe('useInfiniteQuery', () => {
|
|
|
1221
1222
|
</>}>
|
|
1222
1223
|
<Match when={state.status === 'pending'}>Loading...</Match>
|
|
1223
1224
|
<Match when={state.status === 'error'}>
|
|
1224
|
-
<span>Error: {state.error
|
|
1225
|
+
<span>Error: {state.error?.message}</span>
|
|
1225
1226
|
</Match>
|
|
1226
1227
|
</Switch>
|
|
1227
1228
|
</div>);
|
|
@@ -1264,10 +1265,10 @@ describe('useInfiniteQuery', () => {
|
|
|
1264
1265
|
});
|
|
1265
1266
|
it('should cancel the query function when there are no more subscriptions', async () => {
|
|
1266
1267
|
const key = queryKey();
|
|
1267
|
-
let cancelFn =
|
|
1268
|
+
let cancelFn = vi.fn();
|
|
1268
1269
|
const queryFn = ({ signal }) => {
|
|
1269
1270
|
const promise = new Promise((resolve, reject) => {
|
|
1270
|
-
cancelFn =
|
|
1271
|
+
cancelFn = vi.fn(() => reject('Cancelled'));
|
|
1271
1272
|
signal?.addEventListener('abort', cancelFn);
|
|
1272
1273
|
sleep(20).then(() => resolve('OK'));
|
|
1273
1274
|
});
|
|
@@ -3,6 +3,7 @@ import { createEffect, createRenderEffect, createSignal, ErrorBoundary, } from '
|
|
|
3
3
|
import { fireEvent, render, screen, waitFor } from 'solid-testing-library';
|
|
4
4
|
import { createMutation, MutationCache, QueryCache, QueryClientProvider, } from '..';
|
|
5
5
|
import { createQueryClient, mockNavigatorOnLine, queryKey, setActTimeout, sleep, } from './utils';
|
|
6
|
+
import { vi } from 'vitest';
|
|
6
7
|
describe('createMutation', () => {
|
|
7
8
|
const queryCache = new QueryCache();
|
|
8
9
|
const mutationCache = new MutationCache();
|
|
@@ -63,8 +64,8 @@ describe('createMutation', () => {
|
|
|
63
64
|
});
|
|
64
65
|
it('should be able to call `onSuccess` and `onSettled` after each successful mutate', async () => {
|
|
65
66
|
const [count, setCount] = createSignal(0);
|
|
66
|
-
const onSuccessMock =
|
|
67
|
-
const onSettledMock =
|
|
67
|
+
const onSuccessMock = vi.fn();
|
|
68
|
+
const onSettledMock = vi.fn();
|
|
68
69
|
function Page() {
|
|
69
70
|
const mutation = createMutation(() => ({
|
|
70
71
|
mutationFn: (vars) => Promise.resolve(vars.count),
|
|
@@ -110,7 +111,7 @@ describe('createMutation', () => {
|
|
|
110
111
|
});
|
|
111
112
|
it('should set correct values for `failureReason` and `failureCount` on multiple mutate calls', async () => {
|
|
112
113
|
const [count, setCount] = createSignal(0);
|
|
113
|
-
const mutateFn =
|
|
114
|
+
const mutateFn = vi.fn();
|
|
114
115
|
mutateFn.mockImplementationOnce(() => {
|
|
115
116
|
return Promise.reject(new Error('Error test Jonas'));
|
|
116
117
|
});
|
|
@@ -152,8 +153,8 @@ describe('createMutation', () => {
|
|
|
152
153
|
await waitFor(() => screen.getByText('Failed because null'));
|
|
153
154
|
});
|
|
154
155
|
it('should be able to call `onError` and `onSettled` after each failed mutate', async () => {
|
|
155
|
-
const onErrorMock =
|
|
156
|
-
const onSettledMock =
|
|
156
|
+
const onErrorMock = vi.fn();
|
|
157
|
+
const onSettledMock = vi.fn();
|
|
157
158
|
const [count, setCount] = createSignal(0);
|
|
158
159
|
function Page() {
|
|
159
160
|
const mutation = createMutation(() => ({
|
|
@@ -388,7 +389,7 @@ describe('createMutation', () => {
|
|
|
388
389
|
});
|
|
389
390
|
it('should call onMutate even if paused', async () => {
|
|
390
391
|
const onlineMock = mockNavigatorOnLine(false);
|
|
391
|
-
const onMutate =
|
|
392
|
+
const onMutate = vi.fn();
|
|
392
393
|
let count = 0;
|
|
393
394
|
function Page() {
|
|
394
395
|
const mutation = createMutation(() => ({
|
|
@@ -617,8 +618,8 @@ describe('createMutation', () => {
|
|
|
617
618
|
});
|
|
618
619
|
});
|
|
619
620
|
it('should pass meta to mutation', async () => {
|
|
620
|
-
const errorMock =
|
|
621
|
-
const successMock =
|
|
621
|
+
const errorMock = vi.fn();
|
|
622
|
+
const successMock = vi.fn();
|
|
622
623
|
const queryClientMutationMeta = createQueryClient({
|
|
623
624
|
mutationCache: new MutationCache({
|
|
624
625
|
onSuccess: (_, __, ___, mutation) => {
|
|
@@ -664,10 +665,10 @@ describe('createMutation', () => {
|
|
|
664
665
|
expect(errorMock).toHaveBeenCalledWith(metaErrorMessage);
|
|
665
666
|
});
|
|
666
667
|
it('should call cache callbacks when unmounted', async () => {
|
|
667
|
-
const onSuccess =
|
|
668
|
-
const onSuccessMutate =
|
|
669
|
-
const onSettled =
|
|
670
|
-
const onSettledMutate =
|
|
668
|
+
const onSuccess = vi.fn();
|
|
669
|
+
const onSuccessMutate = vi.fn();
|
|
670
|
+
const onSettled = vi.fn();
|
|
671
|
+
const onSettledMutate = vi.fn();
|
|
671
672
|
const mutationKey = queryKey();
|
|
672
673
|
let count = 0;
|
|
673
674
|
function Page() {
|
|
@@ -718,10 +719,10 @@ describe('createMutation', () => {
|
|
|
718
719
|
expect(onSettledMutate).toHaveBeenCalledTimes(0);
|
|
719
720
|
});
|
|
720
721
|
it('should call mutate callbacks only for the last observer', async () => {
|
|
721
|
-
const onSuccess =
|
|
722
|
-
const onSuccessMutate =
|
|
723
|
-
const onSettled =
|
|
724
|
-
const onSettledMutate =
|
|
722
|
+
const onSuccess = vi.fn();
|
|
723
|
+
const onSuccessMutate = vi.fn();
|
|
724
|
+
const onSettled = vi.fn();
|
|
725
|
+
const onSettledMutate = vi.fn();
|
|
725
726
|
let count = 0;
|
|
726
727
|
function Page() {
|
|
727
728
|
const mutation = createMutation(() => ({
|
|
@@ -762,7 +763,7 @@ describe('createMutation', () => {
|
|
|
762
763
|
});
|
|
763
764
|
it('should go to error state if onSuccess callback errors', async () => {
|
|
764
765
|
const error = new Error('error from onSuccess');
|
|
765
|
-
const onError =
|
|
766
|
+
const onError = vi.fn();
|
|
766
767
|
function Page() {
|
|
767
768
|
const mutation = createMutation(() => ({
|
|
768
769
|
mutationFn: async (_text) => {
|
|
@@ -815,7 +816,7 @@ describe('createMutation', () => {
|
|
|
815
816
|
it('should go to error state if onSettled callback errors', async () => {
|
|
816
817
|
const error = new Error('error from onSettled');
|
|
817
818
|
const mutateFnError = new Error('mutateFnError');
|
|
818
|
-
const onError =
|
|
819
|
+
const onError = vi.fn();
|
|
819
820
|
function Page() {
|
|
820
821
|
const mutation = createMutation(() => ({
|
|
821
822
|
mutationFn: async (_text) => {
|
|
@@ -3,6 +3,7 @@ import * as QueriesObserverModule from '../../../query-core/src/queriesObserver'
|
|
|
3
3
|
import { createRenderEffect, createSignal } from 'solid-js';
|
|
4
4
|
import { createQueries, QueriesObserver, QueryCache, QueryClientProvider, } from '..';
|
|
5
5
|
import { createQueryClient, expectType, expectTypeNotAny, queryKey, sleep, } from './utils';
|
|
6
|
+
import { vi } from 'vitest';
|
|
6
7
|
describe('useQueries', () => {
|
|
7
8
|
const queryCache = new QueryCache();
|
|
8
9
|
const queryClient = createQueryClient({ queryCache });
|
|
@@ -620,7 +621,7 @@ describe('useQueries', () => {
|
|
|
620
621
|
return () => void 0;
|
|
621
622
|
}
|
|
622
623
|
}
|
|
623
|
-
const QueriesObserverSpy =
|
|
624
|
+
const QueriesObserverSpy = vi
|
|
624
625
|
.spyOn(QueriesObserverModule, 'QueriesObserver')
|
|
625
626
|
.mockImplementation((fn) => {
|
|
626
627
|
return new QueriesObserverMock(fn);
|
|
@@ -657,20 +658,4 @@ describe('useQueries', () => {
|
|
|
657
658
|
await sleep(20);
|
|
658
659
|
QueriesObserverSpy.mockRestore();
|
|
659
660
|
});
|
|
660
|
-
it('should use provided custom queryClient', async () => {
|
|
661
|
-
const key = queryKey();
|
|
662
|
-
const queryFn = () => {
|
|
663
|
-
return Promise.resolve('custom client');
|
|
664
|
-
};
|
|
665
|
-
function Page() {
|
|
666
|
-
const state = createQueries(() => ({
|
|
667
|
-
queries: [{ queryKey: key, queryFn }],
|
|
668
|
-
}), () => queryClient);
|
|
669
|
-
return (<div>
|
|
670
|
-
<h1>Status: {state[0].data}</h1>
|
|
671
|
-
</div>);
|
|
672
|
-
}
|
|
673
|
-
render(() => <Page />);
|
|
674
|
-
await waitFor(() => screen.getByText('Status: custom client'));
|
|
675
|
-
});
|
|
676
661
|
});
|
|
@@ -3,6 +3,7 @@ import { createEffect, createMemo, createRenderEffect, createSignal, ErrorBounda
|
|
|
3
3
|
import { fireEvent, render, screen, waitFor } from 'solid-testing-library';
|
|
4
4
|
import { createQuery, QueryCache, QueryClientProvider, keepPreviousData, } from '..';
|
|
5
5
|
import { Blink, createQueryClient, expectType, mockNavigatorOnLine, mockVisibilityState, queryKey, setActTimeout, sleep, } from './utils';
|
|
6
|
+
import { vi } from 'vitest';
|
|
6
7
|
describe('createQuery', () => {
|
|
7
8
|
const queryCache = new QueryCache();
|
|
8
9
|
const queryClient = createQueryClient({ queryCache });
|
|
@@ -361,7 +362,7 @@ describe('createQuery', () => {
|
|
|
361
362
|
it('should call onSuccess after a query has been fetched', async () => {
|
|
362
363
|
const key = queryKey();
|
|
363
364
|
const states = [];
|
|
364
|
-
const onSuccess =
|
|
365
|
+
const onSuccess = vi.fn();
|
|
365
366
|
function Page() {
|
|
366
367
|
const state = createQuery(() => ({
|
|
367
368
|
queryKey: key,
|
|
@@ -387,7 +388,7 @@ describe('createQuery', () => {
|
|
|
387
388
|
it('should call onSuccess after a disabled query has been fetched', async () => {
|
|
388
389
|
const key = queryKey();
|
|
389
390
|
const states = [];
|
|
390
|
-
const onSuccess =
|
|
391
|
+
const onSuccess = vi.fn();
|
|
391
392
|
function Page() {
|
|
392
393
|
const state = createQuery(() => ({
|
|
393
394
|
queryKey: key,
|
|
@@ -416,7 +417,7 @@ describe('createQuery', () => {
|
|
|
416
417
|
it('should not call onSuccess if a component has unmounted', async () => {
|
|
417
418
|
const key = queryKey();
|
|
418
419
|
const states = [];
|
|
419
|
-
const onSuccess =
|
|
420
|
+
const onSuccess = vi.fn();
|
|
420
421
|
function Page() {
|
|
421
422
|
const [show, setShow] = createSignal(true);
|
|
422
423
|
createEffect(() => {
|
|
@@ -448,7 +449,7 @@ describe('createQuery', () => {
|
|
|
448
449
|
it('should call onError after a query has been fetched with an error', async () => {
|
|
449
450
|
const key = queryKey();
|
|
450
451
|
const states = [];
|
|
451
|
-
const onError =
|
|
452
|
+
const onError = vi.fn();
|
|
452
453
|
function Page() {
|
|
453
454
|
const state = createQuery(() => ({
|
|
454
455
|
queryKey: key,
|
|
@@ -471,7 +472,7 @@ describe('createQuery', () => {
|
|
|
471
472
|
});
|
|
472
473
|
it('should not call onError when receiving a CancelledError', async () => {
|
|
473
474
|
const key = queryKey();
|
|
474
|
-
const onError =
|
|
475
|
+
const onError = vi.fn();
|
|
475
476
|
function Page() {
|
|
476
477
|
const state = createQuery(() => ({
|
|
477
478
|
queryKey: key,
|
|
@@ -497,7 +498,7 @@ describe('createQuery', () => {
|
|
|
497
498
|
it('should call onSettled after a query has been fetched', async () => {
|
|
498
499
|
const key = queryKey();
|
|
499
500
|
const states = [];
|
|
500
|
-
const onSettled =
|
|
501
|
+
const onSettled = vi.fn();
|
|
501
502
|
function Page() {
|
|
502
503
|
const state = createQuery(() => ({
|
|
503
504
|
queryKey: key,
|
|
@@ -520,7 +521,7 @@ describe('createQuery', () => {
|
|
|
520
521
|
it('should call onSettled after a query has been fetched with an error', async () => {
|
|
521
522
|
const key = queryKey();
|
|
522
523
|
const states = [];
|
|
523
|
-
const onSettled =
|
|
524
|
+
const onSettled = vi.fn();
|
|
524
525
|
function Page() {
|
|
525
526
|
const state = createQuery(() => ({
|
|
526
527
|
queryKey: key,
|
|
@@ -1887,7 +1888,7 @@ describe('createQuery', () => {
|
|
|
1887
1888
|
});
|
|
1888
1889
|
it('should not refetch query on focus when `enabled` is set to `false`', async () => {
|
|
1889
1890
|
const key = queryKey();
|
|
1890
|
-
const queryFn =
|
|
1891
|
+
const queryFn = vi.fn().mockReturnValue('data');
|
|
1891
1892
|
function Page() {
|
|
1892
1893
|
const { data = 'default' } = createQuery(() => ({
|
|
1893
1894
|
queryKey: key,
|
|
@@ -2523,7 +2524,7 @@ describe('createQuery', () => {
|
|
|
2523
2524
|
});
|
|
2524
2525
|
it('should retry specified number of times', async () => {
|
|
2525
2526
|
const key = queryKey();
|
|
2526
|
-
const queryFn =
|
|
2527
|
+
const queryFn = vi.fn();
|
|
2527
2528
|
queryFn.mockImplementation(() => {
|
|
2528
2529
|
return Promise.reject(new Error('Error test Barrett'));
|
|
2529
2530
|
});
|
|
@@ -2552,7 +2553,7 @@ describe('createQuery', () => {
|
|
|
2552
2553
|
});
|
|
2553
2554
|
it('should not retry if retry function `false`', async () => {
|
|
2554
2555
|
const key = queryKey();
|
|
2555
|
-
const queryFn =
|
|
2556
|
+
const queryFn = vi.fn();
|
|
2556
2557
|
queryFn.mockImplementationOnce(() => {
|
|
2557
2558
|
return Promise.reject(new Error('Error test Tanner'));
|
|
2558
2559
|
});
|
|
@@ -2585,7 +2586,7 @@ describe('createQuery', () => {
|
|
|
2585
2586
|
});
|
|
2586
2587
|
it('should extract retryDelay from error', async () => {
|
|
2587
2588
|
const key = queryKey();
|
|
2588
|
-
const queryFn =
|
|
2589
|
+
const queryFn = vi.fn();
|
|
2589
2590
|
queryFn.mockImplementation(() => {
|
|
2590
2591
|
return Promise.reject({ delay: 50 });
|
|
2591
2592
|
});
|
|
@@ -2747,9 +2748,9 @@ describe('createQuery', () => {
|
|
|
2747
2748
|
it('should refetch if stale after a prefetch', async () => {
|
|
2748
2749
|
const key = queryKey();
|
|
2749
2750
|
const states = [];
|
|
2750
|
-
const queryFn =
|
|
2751
|
+
const queryFn = vi.fn();
|
|
2751
2752
|
queryFn.mockImplementation(() => 'data');
|
|
2752
|
-
const prefetchQueryFn =
|
|
2753
|
+
const prefetchQueryFn = vi.fn();
|
|
2753
2754
|
prefetchQueryFn.mockImplementation(() => 'not yet...');
|
|
2754
2755
|
await queryClient.prefetchQuery({
|
|
2755
2756
|
queryKey: key,
|
|
@@ -2773,9 +2774,9 @@ describe('createQuery', () => {
|
|
|
2773
2774
|
});
|
|
2774
2775
|
it('should not refetch if not stale after a prefetch', async () => {
|
|
2775
2776
|
const key = queryKey();
|
|
2776
|
-
const queryFn =
|
|
2777
|
+
const queryFn = vi.fn();
|
|
2777
2778
|
queryFn.mockImplementation(() => 'data');
|
|
2778
|
-
const prefetchQueryFn =
|
|
2779
|
+
const prefetchQueryFn = vi.fn();
|
|
2779
2780
|
prefetchQueryFn.mockImplementation(async () => {
|
|
2780
2781
|
await sleep(10);
|
|
2781
2782
|
return 'not yet...';
|
|
@@ -2977,7 +2978,7 @@ describe('createQuery', () => {
|
|
|
2977
2978
|
});
|
|
2978
2979
|
it('it should support enabled:false in query object syntax', async () => {
|
|
2979
2980
|
const key = queryKey();
|
|
2980
|
-
const queryFn =
|
|
2981
|
+
const queryFn = vi.fn();
|
|
2981
2982
|
queryFn.mockImplementation(() => 'data');
|
|
2982
2983
|
function Page() {
|
|
2983
2984
|
const { fetchStatus } = createQuery(() => ({
|
|
@@ -3028,7 +3029,7 @@ describe('createQuery', () => {
|
|
|
3028
3029
|
<Page />
|
|
3029
3030
|
</QueryClientProvider>));
|
|
3030
3031
|
await waitFor(() => screen.getByText('fetched data'));
|
|
3031
|
-
const setTimeoutSpy =
|
|
3032
|
+
const setTimeoutSpy = vi.spyOn(window, 'setTimeout');
|
|
3032
3033
|
result.unmount();
|
|
3033
3034
|
expect(setTimeoutSpy).not.toHaveBeenCalled();
|
|
3034
3035
|
});
|
|
@@ -3046,14 +3047,14 @@ describe('createQuery', () => {
|
|
|
3046
3047
|
<Page />
|
|
3047
3048
|
</QueryClientProvider>));
|
|
3048
3049
|
await waitFor(() => screen.getByText('fetched data'));
|
|
3049
|
-
const setTimeoutSpy =
|
|
3050
|
+
const setTimeoutSpy = vi.spyOn(window, 'setTimeout');
|
|
3050
3051
|
result.unmount();
|
|
3051
3052
|
expect(setTimeoutSpy).toHaveBeenLastCalledWith(expect.any(Function), 1000 * 60 * 10);
|
|
3052
3053
|
});
|
|
3053
3054
|
it('should not cause memo churn when data does not change', async () => {
|
|
3054
3055
|
const key = queryKey();
|
|
3055
|
-
const queryFn =
|
|
3056
|
-
const memoFn =
|
|
3056
|
+
const queryFn = vi.fn().mockReturnValue('data');
|
|
3057
|
+
const memoFn = vi.fn();
|
|
3057
3058
|
function Page() {
|
|
3058
3059
|
const result = createQuery(() => ({
|
|
3059
3060
|
queryKey: key,
|
|
@@ -3234,7 +3235,7 @@ describe('createQuery', () => {
|
|
|
3234
3235
|
});
|
|
3235
3236
|
it('should refetch if any query instance becomes enabled', async () => {
|
|
3236
3237
|
const key = queryKey();
|
|
3237
|
-
const queryFn =
|
|
3238
|
+
const queryFn = vi.fn().mockReturnValue('data');
|
|
3238
3239
|
function Disabled() {
|
|
3239
3240
|
createQuery(() => ({ queryKey: key, queryFn, enabled: false }));
|
|
3240
3241
|
return null;
|
|
@@ -3504,10 +3505,10 @@ describe('createQuery', () => {
|
|
|
3504
3505
|
});
|
|
3505
3506
|
it('should cancel the query function when there are no more subscriptions', async () => {
|
|
3506
3507
|
const key = queryKey();
|
|
3507
|
-
let cancelFn =
|
|
3508
|
+
let cancelFn = vi.fn();
|
|
3508
3509
|
const queryFn = ({ signal }) => {
|
|
3509
3510
|
const promise = new Promise((resolve, reject) => {
|
|
3510
|
-
cancelFn =
|
|
3511
|
+
cancelFn = vi.fn(() => reject('Cancelled'));
|
|
3511
3512
|
signal?.addEventListener('abort', cancelFn);
|
|
3512
3513
|
sleep(20).then(() => resolve('OK'));
|
|
3513
3514
|
});
|
|
@@ -3767,7 +3768,7 @@ describe('createQuery', () => {
|
|
|
3767
3768
|
expect(renders).toBe(hashes);
|
|
3768
3769
|
});
|
|
3769
3770
|
it('should refetch when changed enabled to true in error state', async () => {
|
|
3770
|
-
const queryFn =
|
|
3771
|
+
const queryFn = vi.fn();
|
|
3771
3772
|
queryFn.mockImplementation(async () => {
|
|
3772
3773
|
await sleep(10);
|
|
3773
3774
|
return Promise.reject(new Error('Suspense Error Bingo'));
|
|
@@ -4514,7 +4515,7 @@ describe('createQuery', () => {
|
|
|
4514
4515
|
});
|
|
4515
4516
|
it('setQueryData - should not call onSuccess callback of active observers', async () => {
|
|
4516
4517
|
const key = queryKey();
|
|
4517
|
-
const onSuccess =
|
|
4518
|
+
const onSuccess = vi.fn();
|
|
4518
4519
|
function Page() {
|
|
4519
4520
|
const state = createQuery(() => ({
|
|
4520
4521
|
queryKey: key,
|
|
@@ -2,6 +2,7 @@ import { fireEvent, render, screen, waitFor } from 'solid-testing-library';
|
|
|
2
2
|
import { createRenderEffect, createSignal, ErrorBoundary, on, Show, Suspense, } from 'solid-js';
|
|
3
3
|
import { createInfiniteQuery, createQuery, QueryCache, QueryClientProvider, } from '..';
|
|
4
4
|
import { createQueryClient, queryKey, sleep } from './utils';
|
|
5
|
+
import { vi } from 'vitest';
|
|
5
6
|
describe("useQuery's in Suspense mode", () => {
|
|
6
7
|
const queryCache = new QueryCache();
|
|
7
8
|
const queryClient = createQueryClient({ queryCache });
|
|
@@ -91,7 +92,7 @@ describe("useQuery's in Suspense mode", () => {
|
|
|
91
92
|
});
|
|
92
93
|
it('should not call the queryFn twice when used in Suspense mode', async () => {
|
|
93
94
|
const key = queryKey();
|
|
94
|
-
const queryFn =
|
|
95
|
+
const queryFn = vi.fn();
|
|
95
96
|
queryFn.mockImplementation(() => {
|
|
96
97
|
sleep(10);
|
|
97
98
|
return 'data';
|
|
@@ -141,7 +142,7 @@ describe("useQuery's in Suspense mode", () => {
|
|
|
141
142
|
});
|
|
142
143
|
it('should call onSuccess on the first successful call', async () => {
|
|
143
144
|
const key = queryKey();
|
|
144
|
-
const successFn =
|
|
145
|
+
const successFn = vi.fn();
|
|
145
146
|
function Page() {
|
|
146
147
|
createQuery(() => ({
|
|
147
148
|
queryKey: [key],
|
|
@@ -166,8 +167,8 @@ describe("useQuery's in Suspense mode", () => {
|
|
|
166
167
|
});
|
|
167
168
|
it('should call every onSuccess handler within a suspense boundary', async () => {
|
|
168
169
|
const key = queryKey();
|
|
169
|
-
const successFn1 =
|
|
170
|
-
const successFn2 =
|
|
170
|
+
const successFn1 = vi.fn();
|
|
171
|
+
const successFn2 = vi.fn();
|
|
171
172
|
function FirstComponent() {
|
|
172
173
|
createQuery(() => ({
|
|
173
174
|
queryKey: key,
|
|
@@ -514,7 +515,7 @@ describe("useQuery's in Suspense mode", () => {
|
|
|
514
515
|
});
|
|
515
516
|
it('should not call the queryFn when not enabled', async () => {
|
|
516
517
|
const key = queryKey();
|
|
517
|
-
const queryFn =
|
|
518
|
+
const queryFn = vi.fn();
|
|
518
519
|
queryFn.mockImplementation(async () => {
|
|
519
520
|
await sleep(10);
|
|
520
521
|
return '23';
|
|
@@ -5,6 +5,7 @@ import { createEffect, createRenderEffect, createSignal, Show } from 'solid-js';
|
|
|
5
5
|
import { render } from 'solid-testing-library';
|
|
6
6
|
import * as MutationCacheModule from '../../../query-core/src/mutationCache';
|
|
7
7
|
import { setActTimeout } from './utils';
|
|
8
|
+
import { vi } from 'vitest';
|
|
8
9
|
describe('useIsMutating', () => {
|
|
9
10
|
it('should return the number of fetching mutations', async () => {
|
|
10
11
|
const isMutatings = [];
|
|
@@ -126,6 +127,27 @@ describe('useIsMutating', () => {
|
|
|
126
127
|
// Again, No unnecessary re-renders like React
|
|
127
128
|
await waitFor(() => expect(isMutatings).toEqual([0, 1, 0]));
|
|
128
129
|
});
|
|
130
|
+
it('should use provided custom queryClient', async () => {
|
|
131
|
+
const queryClient = createQueryClient();
|
|
132
|
+
function Page() {
|
|
133
|
+
const isMutating = useIsMutating(undefined, () => queryClient);
|
|
134
|
+
const { mutate } = createMutation(() => ({
|
|
135
|
+
mutationKey: ['mutation1'],
|
|
136
|
+
mutationFn: async () => {
|
|
137
|
+
await sleep(10);
|
|
138
|
+
return 'data';
|
|
139
|
+
},
|
|
140
|
+
}), () => queryClient);
|
|
141
|
+
createEffect(() => {
|
|
142
|
+
mutate();
|
|
143
|
+
});
|
|
144
|
+
return (<div>
|
|
145
|
+
<div>mutating: {isMutating}</div>
|
|
146
|
+
</div>);
|
|
147
|
+
}
|
|
148
|
+
render(() => <Page></Page>);
|
|
149
|
+
await waitFor(() => screen.findByText('mutating: 1'));
|
|
150
|
+
});
|
|
129
151
|
it('should not change state if unmounted', async () => {
|
|
130
152
|
// We have to mock the MutationCache to not unsubscribe
|
|
131
153
|
// the listener when the component is unmounted
|
|
@@ -135,7 +157,7 @@ describe('useIsMutating', () => {
|
|
|
135
157
|
return () => void 0;
|
|
136
158
|
}
|
|
137
159
|
}
|
|
138
|
-
const MutationCacheSpy =
|
|
160
|
+
const MutationCacheSpy = vi
|
|
139
161
|
.spyOn(MutationCacheModule, 'MutationCache')
|
|
140
162
|
.mockImplementation((fn) => {
|
|
141
163
|
return new MutationCacheMock(fn);
|
|
@@ -173,25 +195,4 @@ describe('useIsMutating', () => {
|
|
|
173
195
|
await sleep(20);
|
|
174
196
|
MutationCacheSpy.mockRestore();
|
|
175
197
|
});
|
|
176
|
-
it('should use provided custom queryClient', async () => {
|
|
177
|
-
const queryClient = createQueryClient();
|
|
178
|
-
function Page() {
|
|
179
|
-
const isMutating = useIsMutating(undefined, () => queryClient);
|
|
180
|
-
const { mutate } = createMutation(() => ({
|
|
181
|
-
mutationKey: ['mutation1'],
|
|
182
|
-
mutationFn: async () => {
|
|
183
|
-
await sleep(10);
|
|
184
|
-
return 'data';
|
|
185
|
-
},
|
|
186
|
-
}), () => queryClient);
|
|
187
|
-
createEffect(() => {
|
|
188
|
-
mutate();
|
|
189
|
-
});
|
|
190
|
-
return (<div>
|
|
191
|
-
<div>mutating: {isMutating}</div>
|
|
192
|
-
</div>);
|
|
193
|
-
}
|
|
194
|
-
render(() => <Page></Page>);
|
|
195
|
-
await waitFor(() => screen.findByText('mutating: 1'));
|
|
196
|
-
});
|
|
197
198
|
});
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { QueryClient } from '@tanstack/query-core';
|
|
2
2
|
import { createEffect, createSignal, onCleanup, Show } from 'solid-js';
|
|
3
|
+
import { vi } from 'vitest';
|
|
3
4
|
let queryKeyCount = 0;
|
|
4
5
|
export function queryKey() {
|
|
5
6
|
queryKeyCount++;
|
|
@@ -20,10 +21,10 @@ export function createQueryClient(config) {
|
|
|
20
21
|
return new QueryClient(config);
|
|
21
22
|
}
|
|
22
23
|
export function mockVisibilityState(value) {
|
|
23
|
-
return
|
|
24
|
+
return vi.spyOn(document, 'visibilityState', 'get').mockReturnValue(value);
|
|
24
25
|
}
|
|
25
26
|
export function mockNavigatorOnLine(value) {
|
|
26
|
-
return
|
|
27
|
+
return vi.spyOn(navigator, 'onLine', 'get').mockReturnValue(value);
|
|
27
28
|
}
|
|
28
29
|
export function sleep(timeout) {
|
|
29
30
|
return new Promise((resolve, _reject) => {
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
/// <reference types="jest" />
|
|
2
1
|
/// <reference types="node" />
|
|
3
2
|
import type { QueryClientConfig } from '@tanstack/query-core';
|
|
4
3
|
import { QueryClient } from '@tanstack/query-core';
|
|
@@ -8,8 +7,8 @@ export declare const Blink: (props: {
|
|
|
8
7
|
duration: number;
|
|
9
8
|
} & ParentProps) => import("solid-js").JSX.Element;
|
|
10
9
|
export declare function createQueryClient(config?: QueryClientConfig): QueryClient;
|
|
11
|
-
export declare function mockVisibilityState(value: DocumentVisibilityState):
|
|
12
|
-
export declare function mockNavigatorOnLine(value: boolean):
|
|
10
|
+
export declare function mockVisibilityState(value: DocumentVisibilityState): import("vitest/dist/index-1cfc7f58").S<[], DocumentVisibilityState>;
|
|
11
|
+
export declare function mockNavigatorOnLine(value: boolean): import("vitest/dist/index-1cfc7f58").S<[], boolean>;
|
|
13
12
|
export declare function sleep(timeout: number): Promise<void>;
|
|
14
13
|
export declare function setActTimeout(fn: () => void, ms?: number): NodeJS.Timeout;
|
|
15
14
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/solid-query",
|
|
3
|
-
"version": "5.0.0-alpha.
|
|
3
|
+
"version": "5.0.0-alpha.5",
|
|
4
4
|
"description": "Primitives for managing, caching and syncing asynchronous and remote data in Solid",
|
|
5
5
|
"author": "tannerlinsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -40,10 +40,10 @@
|
|
|
40
40
|
"src"
|
|
41
41
|
],
|
|
42
42
|
"devDependencies": {
|
|
43
|
-
"solid
|
|
43
|
+
"vite-plugin-solid": "^2.3.9"
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
|
-
"@tanstack/query-core": "5.0.0-alpha.
|
|
46
|
+
"@tanstack/query-core": "5.0.0-alpha.5"
|
|
47
47
|
},
|
|
48
48
|
"peerDependencies": {
|
|
49
49
|
"solid-js": "^1.6.2"
|
|
@@ -53,7 +53,7 @@
|
|
|
53
53
|
"clean": "rimraf ./build",
|
|
54
54
|
"test:eslint": "eslint --ext .ts,.tsx ./src",
|
|
55
55
|
"test:types": "tsc",
|
|
56
|
-
"test:lib": "
|
|
56
|
+
"test:lib": "vitest run --coverage",
|
|
57
57
|
"test:lib:dev": "pnpm run test:lib --watch",
|
|
58
58
|
"build:types": "tsc --build"
|
|
59
59
|
}
|
|
@@ -4,6 +4,7 @@ import { queryKey } from './utils'
|
|
|
4
4
|
import { QueryCache } from '@tanstack/query-core'
|
|
5
5
|
import { createQuery, QueryClientProvider, useQueryClient } from '..'
|
|
6
6
|
import { createQueryClient, sleep } from './utils'
|
|
7
|
+
import { vi } from 'vitest'
|
|
7
8
|
|
|
8
9
|
describe('QueryClientProvider', () => {
|
|
9
10
|
it('sets a specific cache for all queries to use', async () => {
|
|
@@ -145,7 +146,7 @@ describe('QueryClientProvider', () => {
|
|
|
145
146
|
|
|
146
147
|
describe('useQueryClient', () => {
|
|
147
148
|
it('should throw an error if no query client has been set', () => {
|
|
148
|
-
const consoleMock =
|
|
149
|
+
const consoleMock = vi
|
|
149
150
|
.spyOn(console, 'error')
|
|
150
151
|
.mockImplementation(() => undefined)
|
|
151
152
|
|
|
@@ -23,6 +23,8 @@ import {
|
|
|
23
23
|
keepPreviousData,
|
|
24
24
|
} from '..'
|
|
25
25
|
import { Blink, queryKey, setActTimeout } from './utils'
|
|
26
|
+
import { vi } from 'vitest'
|
|
27
|
+
import type { Mock } from 'vitest'
|
|
26
28
|
|
|
27
29
|
interface Result {
|
|
28
30
|
items: number[]
|
|
@@ -712,14 +714,14 @@ describe('useInfiniteQuery', () => {
|
|
|
712
714
|
it('should silently cancel an ongoing fetchNextPage request when another fetchNextPage is invoked', async () => {
|
|
713
715
|
const key = queryKey()
|
|
714
716
|
const start = 10
|
|
715
|
-
const onAborts:
|
|
716
|
-
const abortListeners:
|
|
717
|
-
const fetchPage =
|
|
718
|
-
|
|
719
|
-
|
|
717
|
+
const onAborts: Mock<any, any>[] = []
|
|
718
|
+
const abortListeners: Mock<any, any>[] = []
|
|
719
|
+
const fetchPage = vi.fn<
|
|
720
|
+
[QueryFunctionContext<typeof key, number>],
|
|
721
|
+
Promise<number>
|
|
720
722
|
>(async ({ pageParam, signal }) => {
|
|
721
|
-
const onAbort =
|
|
722
|
-
const abortListener =
|
|
723
|
+
const onAbort = vi.fn()
|
|
724
|
+
const abortListener = vi.fn()
|
|
723
725
|
onAborts.push(onAbort)
|
|
724
726
|
abortListeners.push(abortListener)
|
|
725
727
|
signal.onabort = onAbort
|
|
@@ -794,14 +796,14 @@ describe('useInfiniteQuery', () => {
|
|
|
794
796
|
it('should not cancel an ongoing fetchNextPage request when another fetchNextPage is invoked if `cancelRefetch: false` is used ', async () => {
|
|
795
797
|
const key = queryKey()
|
|
796
798
|
const start = 10
|
|
797
|
-
const onAborts:
|
|
798
|
-
const abortListeners:
|
|
799
|
-
const fetchPage =
|
|
800
|
-
|
|
801
|
-
|
|
799
|
+
const onAborts: Mock<any, any>[] = []
|
|
800
|
+
const abortListeners: Mock<any, any>[] = []
|
|
801
|
+
const fetchPage = vi.fn<
|
|
802
|
+
[QueryFunctionContext<typeof key, number>],
|
|
803
|
+
Promise<number>
|
|
802
804
|
>(async ({ pageParam, signal }) => {
|
|
803
|
-
const onAbort =
|
|
804
|
-
const abortListener =
|
|
805
|
+
const onAbort = vi.fn()
|
|
806
|
+
const abortListener = vi.fn()
|
|
805
807
|
onAborts.push(onAbort)
|
|
806
808
|
abortListeners.push(abortListener)
|
|
807
809
|
signal.onabort = onAbort
|
|
@@ -1396,7 +1398,7 @@ describe('useInfiniteQuery', () => {
|
|
|
1396
1398
|
>
|
|
1397
1399
|
<Match when={state.status === 'pending'}>Loading...</Match>
|
|
1398
1400
|
<Match when={state.status === 'error'}>
|
|
1399
|
-
<span>Error: {state.error
|
|
1401
|
+
<span>Error: {state.error?.message}</span>
|
|
1400
1402
|
</Match>
|
|
1401
1403
|
</Switch>
|
|
1402
1404
|
</div>
|
|
@@ -1523,7 +1525,7 @@ describe('useInfiniteQuery', () => {
|
|
|
1523
1525
|
>
|
|
1524
1526
|
<Match when={state.status === 'pending'}>Loading...</Match>
|
|
1525
1527
|
<Match when={state.status === 'error'}>
|
|
1526
|
-
<span>Error: {state.error
|
|
1528
|
+
<span>Error: {state.error?.message}</span>
|
|
1527
1529
|
</Match>
|
|
1528
1530
|
</Switch>
|
|
1529
1531
|
</div>
|
|
@@ -1587,11 +1589,11 @@ describe('useInfiniteQuery', () => {
|
|
|
1587
1589
|
|
|
1588
1590
|
it('should cancel the query function when there are no more subscriptions', async () => {
|
|
1589
1591
|
const key = queryKey()
|
|
1590
|
-
let cancelFn:
|
|
1592
|
+
let cancelFn: Mock = vi.fn()
|
|
1591
1593
|
|
|
1592
1594
|
const queryFn = ({ signal }: { signal?: AbortSignal }) => {
|
|
1593
1595
|
const promise = new Promise<string>((resolve, reject) => {
|
|
1594
|
-
cancelFn =
|
|
1596
|
+
cancelFn = vi.fn(() => reject('Cancelled'))
|
|
1595
1597
|
signal?.addEventListener('abort', cancelFn)
|
|
1596
1598
|
sleep(20).then(() => resolve('OK'))
|
|
1597
1599
|
})
|
|
@@ -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 =
|
|
112
|
-
const onSettledMock =
|
|
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 =
|
|
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 =
|
|
235
|
-
const onSettledMock =
|
|
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 =
|
|
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 =
|
|
874
|
-
const successMock =
|
|
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 =
|
|
934
|
-
const onSuccessMutate =
|
|
935
|
-
const onSettled =
|
|
936
|
-
const onSettledMutate =
|
|
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 =
|
|
1010
|
-
const onSuccessMutate =
|
|
1011
|
-
const onSettled =
|
|
1012
|
-
const onSettledMutate =
|
|
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 =
|
|
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 =
|
|
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 =
|
|
743
|
+
const QueriesObserverSpy = vi
|
|
743
744
|
.spyOn(QueriesObserverModule, 'QueriesObserver')
|
|
744
745
|
.mockImplementation((fn) => {
|
|
745
746
|
return new QueriesObserverMock(fn)
|
|
@@ -789,29 +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
|
-
() => ({
|
|
802
|
-
queries: [{ queryKey: key, queryFn }],
|
|
803
|
-
}),
|
|
804
|
-
() => queryClient,
|
|
805
|
-
)
|
|
806
|
-
return (
|
|
807
|
-
<div>
|
|
808
|
-
<h1>Status: {state[0].data}</h1>
|
|
809
|
-
</div>
|
|
810
|
-
)
|
|
811
|
-
}
|
|
812
|
-
|
|
813
|
-
render(() => <Page />)
|
|
814
|
-
|
|
815
|
-
await waitFor(() => screen.getByText('Status: custom client'))
|
|
816
|
-
})
|
|
817
793
|
})
|
|
@@ -33,6 +33,8 @@ import {
|
|
|
33
33
|
setActTimeout,
|
|
34
34
|
sleep,
|
|
35
35
|
} from './utils'
|
|
36
|
+
import { vi } from 'vitest'
|
|
37
|
+
import type { Mock } from 'vitest'
|
|
36
38
|
|
|
37
39
|
describe('createQuery', () => {
|
|
38
40
|
const queryCache = new QueryCache()
|
|
@@ -491,7 +493,7 @@ describe('createQuery', () => {
|
|
|
491
493
|
it('should call onSuccess after a query has been fetched', async () => {
|
|
492
494
|
const key = queryKey()
|
|
493
495
|
const states: CreateQueryResult<string>[] = []
|
|
494
|
-
const onSuccess =
|
|
496
|
+
const onSuccess = vi.fn()
|
|
495
497
|
|
|
496
498
|
function Page() {
|
|
497
499
|
const state = createQuery(() => ({
|
|
@@ -523,7 +525,7 @@ describe('createQuery', () => {
|
|
|
523
525
|
it('should call onSuccess after a disabled query has been fetched', async () => {
|
|
524
526
|
const key = queryKey()
|
|
525
527
|
const states: CreateQueryResult<string>[] = []
|
|
526
|
-
const onSuccess =
|
|
528
|
+
const onSuccess = vi.fn()
|
|
527
529
|
|
|
528
530
|
function Page() {
|
|
529
531
|
const state = createQuery(() => ({
|
|
@@ -561,7 +563,7 @@ describe('createQuery', () => {
|
|
|
561
563
|
it('should not call onSuccess if a component has unmounted', async () => {
|
|
562
564
|
const key = queryKey()
|
|
563
565
|
const states: CreateQueryResult<string>[] = []
|
|
564
|
-
const onSuccess =
|
|
566
|
+
const onSuccess = vi.fn()
|
|
565
567
|
|
|
566
568
|
function Page() {
|
|
567
569
|
const [show, setShow] = createSignal(true)
|
|
@@ -601,7 +603,7 @@ describe('createQuery', () => {
|
|
|
601
603
|
it('should call onError after a query has been fetched with an error', async () => {
|
|
602
604
|
const key = queryKey()
|
|
603
605
|
const states: CreateQueryResult<unknown>[] = []
|
|
604
|
-
const onError =
|
|
606
|
+
const onError = vi.fn()
|
|
605
607
|
|
|
606
608
|
function Page() {
|
|
607
609
|
const state = createQuery(() => ({
|
|
@@ -632,7 +634,7 @@ describe('createQuery', () => {
|
|
|
632
634
|
|
|
633
635
|
it('should not call onError when receiving a CancelledError', async () => {
|
|
634
636
|
const key = queryKey()
|
|
635
|
-
const onError =
|
|
637
|
+
const onError = vi.fn()
|
|
636
638
|
|
|
637
639
|
function Page() {
|
|
638
640
|
const state = createQuery(() => ({
|
|
@@ -666,7 +668,7 @@ describe('createQuery', () => {
|
|
|
666
668
|
it('should call onSettled after a query has been fetched', async () => {
|
|
667
669
|
const key = queryKey()
|
|
668
670
|
const states: CreateQueryResult<string>[] = []
|
|
669
|
-
const onSettled =
|
|
671
|
+
const onSettled = vi.fn()
|
|
670
672
|
|
|
671
673
|
function Page() {
|
|
672
674
|
const state = createQuery(() => ({
|
|
@@ -696,7 +698,7 @@ describe('createQuery', () => {
|
|
|
696
698
|
it('should call onSettled after a query has been fetched with an error', async () => {
|
|
697
699
|
const key = queryKey()
|
|
698
700
|
const states: CreateQueryResult<string>[] = []
|
|
699
|
-
const onSettled =
|
|
701
|
+
const onSettled = vi.fn()
|
|
700
702
|
|
|
701
703
|
function Page() {
|
|
702
704
|
const state = createQuery(() => ({
|
|
@@ -2452,7 +2454,7 @@ describe('createQuery', () => {
|
|
|
2452
2454
|
|
|
2453
2455
|
it('should not refetch query on focus when `enabled` is set to `false`', async () => {
|
|
2454
2456
|
const key = queryKey()
|
|
2455
|
-
const queryFn =
|
|
2457
|
+
const queryFn = vi.fn<unknown[], string>().mockReturnValue('data')
|
|
2456
2458
|
|
|
2457
2459
|
function Page() {
|
|
2458
2460
|
const { data = 'default' } = createQuery(() => ({
|
|
@@ -3296,7 +3298,7 @@ describe('createQuery', () => {
|
|
|
3296
3298
|
it('should retry specified number of times', async () => {
|
|
3297
3299
|
const key = queryKey()
|
|
3298
3300
|
|
|
3299
|
-
const queryFn =
|
|
3301
|
+
const queryFn = vi.fn<unknown[], unknown>()
|
|
3300
3302
|
queryFn.mockImplementation(() => {
|
|
3301
3303
|
return Promise.reject(new Error('Error test Barrett'))
|
|
3302
3304
|
})
|
|
@@ -3337,7 +3339,7 @@ describe('createQuery', () => {
|
|
|
3337
3339
|
it('should not retry if retry function `false`', async () => {
|
|
3338
3340
|
const key = queryKey()
|
|
3339
3341
|
|
|
3340
|
-
const queryFn =
|
|
3342
|
+
const queryFn = vi.fn<unknown[], unknown>()
|
|
3341
3343
|
|
|
3342
3344
|
queryFn.mockImplementationOnce(() => {
|
|
3343
3345
|
return Promise.reject(new Error('Error test Tanner'))
|
|
@@ -3385,7 +3387,7 @@ describe('createQuery', () => {
|
|
|
3385
3387
|
|
|
3386
3388
|
type DelayError = { delay: number }
|
|
3387
3389
|
|
|
3388
|
-
const queryFn =
|
|
3390
|
+
const queryFn = vi.fn<unknown[], unknown>()
|
|
3389
3391
|
queryFn.mockImplementation(() => {
|
|
3390
3392
|
return Promise.reject({ delay: 50 })
|
|
3391
3393
|
})
|
|
@@ -3596,10 +3598,10 @@ describe('createQuery', () => {
|
|
|
3596
3598
|
const key = queryKey()
|
|
3597
3599
|
const states: CreateQueryResult<string>[] = []
|
|
3598
3600
|
|
|
3599
|
-
const queryFn =
|
|
3601
|
+
const queryFn = vi.fn<unknown[], string>()
|
|
3600
3602
|
queryFn.mockImplementation(() => 'data')
|
|
3601
3603
|
|
|
3602
|
-
const prefetchQueryFn =
|
|
3604
|
+
const prefetchQueryFn = vi.fn<unknown[], string>()
|
|
3603
3605
|
prefetchQueryFn.mockImplementation(() => 'not yet...')
|
|
3604
3606
|
|
|
3605
3607
|
await queryClient.prefetchQuery({
|
|
@@ -3633,10 +3635,10 @@ describe('createQuery', () => {
|
|
|
3633
3635
|
it('should not refetch if not stale after a prefetch', async () => {
|
|
3634
3636
|
const key = queryKey()
|
|
3635
3637
|
|
|
3636
|
-
const queryFn =
|
|
3638
|
+
const queryFn = vi.fn<unknown[], string>()
|
|
3637
3639
|
queryFn.mockImplementation(() => 'data')
|
|
3638
3640
|
|
|
3639
|
-
const prefetchQueryFn =
|
|
3641
|
+
const prefetchQueryFn = vi.fn<unknown[], Promise<string>>()
|
|
3640
3642
|
prefetchQueryFn.mockImplementation(async () => {
|
|
3641
3643
|
await sleep(10)
|
|
3642
3644
|
return 'not yet...'
|
|
@@ -3909,7 +3911,7 @@ describe('createQuery', () => {
|
|
|
3909
3911
|
|
|
3910
3912
|
it('it should support enabled:false in query object syntax', async () => {
|
|
3911
3913
|
const key = queryKey()
|
|
3912
|
-
const queryFn =
|
|
3914
|
+
const queryFn = vi.fn<unknown[], string>()
|
|
3913
3915
|
queryFn.mockImplementation(() => 'data')
|
|
3914
3916
|
|
|
3915
3917
|
function Page() {
|
|
@@ -3981,7 +3983,7 @@ describe('createQuery', () => {
|
|
|
3981
3983
|
))
|
|
3982
3984
|
|
|
3983
3985
|
await waitFor(() => screen.getByText('fetched data'))
|
|
3984
|
-
const setTimeoutSpy =
|
|
3986
|
+
const setTimeoutSpy = vi.spyOn(window, 'setTimeout')
|
|
3985
3987
|
|
|
3986
3988
|
result.unmount()
|
|
3987
3989
|
|
|
@@ -4007,7 +4009,7 @@ describe('createQuery', () => {
|
|
|
4007
4009
|
))
|
|
4008
4010
|
|
|
4009
4011
|
await waitFor(() => screen.getByText('fetched data'))
|
|
4010
|
-
const setTimeoutSpy =
|
|
4012
|
+
const setTimeoutSpy = vi.spyOn(window, 'setTimeout')
|
|
4011
4013
|
|
|
4012
4014
|
result.unmount()
|
|
4013
4015
|
|
|
@@ -4019,8 +4021,8 @@ describe('createQuery', () => {
|
|
|
4019
4021
|
|
|
4020
4022
|
it('should not cause memo churn when data does not change', async () => {
|
|
4021
4023
|
const key = queryKey()
|
|
4022
|
-
const queryFn =
|
|
4023
|
-
const memoFn =
|
|
4024
|
+
const queryFn = vi.fn<unknown[], string>().mockReturnValue('data')
|
|
4025
|
+
const memoFn = vi.fn()
|
|
4024
4026
|
|
|
4025
4027
|
function Page() {
|
|
4026
4028
|
const result = createQuery(() => ({
|
|
@@ -4255,7 +4257,7 @@ describe('createQuery', () => {
|
|
|
4255
4257
|
it('should refetch if any query instance becomes enabled', async () => {
|
|
4256
4258
|
const key = queryKey()
|
|
4257
4259
|
|
|
4258
|
-
const queryFn =
|
|
4260
|
+
const queryFn = vi.fn<unknown[], string>().mockReturnValue('data')
|
|
4259
4261
|
|
|
4260
4262
|
function Disabled() {
|
|
4261
4263
|
createQuery(() => ({ queryKey: key, queryFn, enabled: false }))
|
|
@@ -4610,11 +4612,11 @@ describe('createQuery', () => {
|
|
|
4610
4612
|
|
|
4611
4613
|
it('should cancel the query function when there are no more subscriptions', async () => {
|
|
4612
4614
|
const key = queryKey()
|
|
4613
|
-
let cancelFn:
|
|
4615
|
+
let cancelFn: Mock = vi.fn()
|
|
4614
4616
|
|
|
4615
4617
|
const queryFn = ({ signal }: { signal?: AbortSignal }) => {
|
|
4616
4618
|
const promise = new Promise<string>((resolve, reject) => {
|
|
4617
|
-
cancelFn =
|
|
4619
|
+
cancelFn = vi.fn(() => reject('Cancelled'))
|
|
4618
4620
|
signal?.addEventListener('abort', cancelFn)
|
|
4619
4621
|
sleep(20).then(() => resolve('OK'))
|
|
4620
4622
|
})
|
|
@@ -4958,7 +4960,7 @@ describe('createQuery', () => {
|
|
|
4958
4960
|
})
|
|
4959
4961
|
|
|
4960
4962
|
it('should refetch when changed enabled to true in error state', async () => {
|
|
4961
|
-
const queryFn =
|
|
4963
|
+
const queryFn = vi.fn<unknown[], unknown>()
|
|
4962
4964
|
queryFn.mockImplementation(async () => {
|
|
4963
4965
|
await sleep(10)
|
|
4964
4966
|
return Promise.reject(new Error('Suspense Error Bingo'))
|
|
@@ -6052,7 +6054,7 @@ describe('createQuery', () => {
|
|
|
6052
6054
|
|
|
6053
6055
|
it('setQueryData - should not call onSuccess callback of active observers', async () => {
|
|
6054
6056
|
const key = queryKey()
|
|
6055
|
-
const onSuccess =
|
|
6057
|
+
const onSuccess = vi.fn()
|
|
6056
6058
|
|
|
6057
6059
|
function Page() {
|
|
6058
6060
|
const state = createQuery(() => ({
|
|
@@ -20,6 +20,7 @@ import {
|
|
|
20
20
|
QueryClientProvider,
|
|
21
21
|
} from '..'
|
|
22
22
|
import { createQueryClient, queryKey, sleep } from './utils'
|
|
23
|
+
import { vi } from 'vitest'
|
|
23
24
|
|
|
24
25
|
describe("useQuery's in Suspense mode", () => {
|
|
25
26
|
const queryCache = new QueryCache()
|
|
@@ -142,7 +143,7 @@ describe("useQuery's in Suspense mode", () => {
|
|
|
142
143
|
it('should not call the queryFn twice when used in Suspense mode', async () => {
|
|
143
144
|
const key = queryKey()
|
|
144
145
|
|
|
145
|
-
const queryFn =
|
|
146
|
+
const queryFn = vi.fn<unknown[], string>()
|
|
146
147
|
queryFn.mockImplementation(() => {
|
|
147
148
|
sleep(10)
|
|
148
149
|
return 'data'
|
|
@@ -219,7 +220,7 @@ describe("useQuery's in Suspense mode", () => {
|
|
|
219
220
|
it('should call onSuccess on the first successful call', async () => {
|
|
220
221
|
const key = queryKey()
|
|
221
222
|
|
|
222
|
-
const successFn =
|
|
223
|
+
const successFn = vi.fn()
|
|
223
224
|
|
|
224
225
|
function Page() {
|
|
225
226
|
createQuery(() => ({
|
|
@@ -254,8 +255,8 @@ describe("useQuery's in Suspense mode", () => {
|
|
|
254
255
|
it('should call every onSuccess handler within a suspense boundary', async () => {
|
|
255
256
|
const key = queryKey()
|
|
256
257
|
|
|
257
|
-
const successFn1 =
|
|
258
|
-
const successFn2 =
|
|
258
|
+
const successFn1 = vi.fn()
|
|
259
|
+
const successFn2 = vi.fn()
|
|
259
260
|
|
|
260
261
|
function FirstComponent() {
|
|
261
262
|
createQuery(() => ({
|
|
@@ -733,7 +734,7 @@ describe("useQuery's in Suspense mode", () => {
|
|
|
733
734
|
it('should not call the queryFn when not enabled', async () => {
|
|
734
735
|
const key = queryKey()
|
|
735
736
|
|
|
736
|
-
const queryFn =
|
|
737
|
+
const queryFn = vi.fn<unknown[], Promise<string>>()
|
|
737
738
|
queryFn.mockImplementation(async () => {
|
|
738
739
|
await sleep(10)
|
|
739
740
|
return '23'
|
|
@@ -6,6 +6,7 @@ import { createEffect, createRenderEffect, createSignal, Show } from 'solid-js'
|
|
|
6
6
|
import { render } from 'solid-testing-library'
|
|
7
7
|
import * as MutationCacheModule from '../../../query-core/src/mutationCache'
|
|
8
8
|
import { setActTimeout } from './utils'
|
|
9
|
+
import { vi } from 'vitest'
|
|
9
10
|
|
|
10
11
|
describe('useIsMutating', () => {
|
|
11
12
|
it('should return the number of fetching mutations', async () => {
|
|
@@ -157,6 +158,33 @@ describe('useIsMutating', () => {
|
|
|
157
158
|
await waitFor(() => expect(isMutatings).toEqual([0, 1, 0]))
|
|
158
159
|
})
|
|
159
160
|
|
|
161
|
+
it('should use provided custom queryClient', async () => {
|
|
162
|
+
const queryClient = createQueryClient()
|
|
163
|
+
function Page() {
|
|
164
|
+
const isMutating = useIsMutating(undefined, () => queryClient)
|
|
165
|
+
const { mutate } = createMutation(
|
|
166
|
+
() => ({
|
|
167
|
+
mutationKey: ['mutation1'],
|
|
168
|
+
mutationFn: async () => {
|
|
169
|
+
await sleep(10)
|
|
170
|
+
return 'data'
|
|
171
|
+
},
|
|
172
|
+
}),
|
|
173
|
+
() => queryClient,
|
|
174
|
+
)
|
|
175
|
+
createEffect(() => {
|
|
176
|
+
mutate()
|
|
177
|
+
})
|
|
178
|
+
return (
|
|
179
|
+
<div>
|
|
180
|
+
<div>mutating: {isMutating}</div>
|
|
181
|
+
</div>
|
|
182
|
+
)
|
|
183
|
+
}
|
|
184
|
+
render(() => <Page></Page>)
|
|
185
|
+
await waitFor(() => screen.findByText('mutating: 1'))
|
|
186
|
+
})
|
|
187
|
+
|
|
160
188
|
it('should not change state if unmounted', async () => {
|
|
161
189
|
// We have to mock the MutationCache to not unsubscribe
|
|
162
190
|
// the listener when the component is unmounted
|
|
@@ -167,7 +195,7 @@ describe('useIsMutating', () => {
|
|
|
167
195
|
}
|
|
168
196
|
}
|
|
169
197
|
|
|
170
|
-
const MutationCacheSpy =
|
|
198
|
+
const MutationCacheSpy = vi
|
|
171
199
|
.spyOn(MutationCacheModule, 'MutationCache')
|
|
172
200
|
.mockImplementation((fn) => {
|
|
173
201
|
return new MutationCacheMock(fn)
|
|
@@ -217,36 +245,4 @@ describe('useIsMutating', () => {
|
|
|
217
245
|
await sleep(20)
|
|
218
246
|
MutationCacheSpy.mockRestore()
|
|
219
247
|
})
|
|
220
|
-
|
|
221
|
-
it('should use provided custom queryClient', async () => {
|
|
222
|
-
const queryClient = createQueryClient()
|
|
223
|
-
|
|
224
|
-
function Page() {
|
|
225
|
-
const isMutating = useIsMutating(undefined, () => queryClient)
|
|
226
|
-
const { mutate } = createMutation(
|
|
227
|
-
() => ({
|
|
228
|
-
mutationKey: ['mutation1'],
|
|
229
|
-
mutationFn: async () => {
|
|
230
|
-
await sleep(10)
|
|
231
|
-
return 'data'
|
|
232
|
-
},
|
|
233
|
-
}),
|
|
234
|
-
() => queryClient,
|
|
235
|
-
)
|
|
236
|
-
|
|
237
|
-
createEffect(() => {
|
|
238
|
-
mutate()
|
|
239
|
-
})
|
|
240
|
-
|
|
241
|
-
return (
|
|
242
|
-
<div>
|
|
243
|
-
<div>mutating: {isMutating}</div>
|
|
244
|
-
</div>
|
|
245
|
-
)
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
render(() => <Page></Page>)
|
|
249
|
-
|
|
250
|
-
await waitFor(() => screen.findByText('mutating: 1'))
|
|
251
|
-
})
|
|
252
248
|
})
|
package/src/__tests__/utils.tsx
CHANGED
|
@@ -2,6 +2,7 @@ import type { QueryClientConfig } from '@tanstack/query-core'
|
|
|
2
2
|
import { QueryClient } from '@tanstack/query-core'
|
|
3
3
|
import type { ParentProps } from 'solid-js'
|
|
4
4
|
import { createEffect, createSignal, onCleanup, Show } from 'solid-js'
|
|
5
|
+
import { vi } from 'vitest'
|
|
5
6
|
|
|
6
7
|
let queryKeyCount = 0
|
|
7
8
|
export function queryKey(): Array<string> {
|
|
@@ -34,11 +35,11 @@ export function createQueryClient(config?: QueryClientConfig): QueryClient {
|
|
|
34
35
|
}
|
|
35
36
|
|
|
36
37
|
export function mockVisibilityState(value: DocumentVisibilityState) {
|
|
37
|
-
return
|
|
38
|
+
return vi.spyOn(document, 'visibilityState', 'get').mockReturnValue(value)
|
|
38
39
|
}
|
|
39
40
|
|
|
40
41
|
export function mockNavigatorOnLine(value: boolean) {
|
|
41
|
-
return
|
|
42
|
+
return vi.spyOn(navigator, 'onLine', 'get').mockReturnValue(value)
|
|
42
43
|
}
|
|
43
44
|
|
|
44
45
|
export function sleep(timeout: number): Promise<void> {
|