@tanstack/react-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/lib/__tests__/ssr.test.d.ts +0 -3
- package/build/lib/__tests__/utils.d.ts +2 -3
- package/build/umd/index.development.js +51 -51
- 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 +3 -3
- package/src/__tests__/HydrationBoundary.test.tsx +4 -3
- package/src/__tests__/QueryClientProvider.test.tsx +2 -1
- package/src/__tests__/QueryResetErrorBoundary.test.tsx +12 -11
- package/src/__tests__/ssr-hydration.test.tsx +11 -10
- package/src/__tests__/ssr.test.tsx +4 -7
- package/src/__tests__/suspense.test.tsx +14 -13
- package/src/__tests__/useInfiniteQuery.test.tsx +18 -16
- package/src/__tests__/useInfiniteQuery.type.test.tsx +81 -1
- package/src/__tests__/useMutation.test.tsx +21 -20
- package/src/__tests__/useMutationState.test.tsx +1 -58
- package/src/__tests__/useQueries.test.tsx +13 -73
- package/src/__tests__/useQuery.test.tsx +30 -36
- package/src/__tests__/utils.tsx +3 -2
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
/// <reference types="jest" />
|
|
2
1
|
/// <reference types="node" />
|
|
3
2
|
import * as React from 'react';
|
|
4
3
|
import { render } from '@testing-library/react';
|
|
@@ -10,8 +9,8 @@ export declare const Blink: ({ duration, children, }: {
|
|
|
10
9
|
children: React.ReactNode;
|
|
11
10
|
}) => JSX.Element;
|
|
12
11
|
export declare function createQueryClient(config?: QueryClientConfig): QueryClient;
|
|
13
|
-
export declare function mockVisibilityState(value: DocumentVisibilityState):
|
|
14
|
-
export declare function mockNavigatorOnLine(value: boolean):
|
|
12
|
+
export declare function mockVisibilityState(value: DocumentVisibilityState): import("vitest/dist/index-1cfc7f58").S<[], DocumentVisibilityState>;
|
|
13
|
+
export declare function mockNavigatorOnLine(value: boolean): import("vitest/dist/index-1cfc7f58").S<[], boolean>;
|
|
15
14
|
export declare function queryKey(): Array<string>;
|
|
16
15
|
export declare function sleep(timeout: number): Promise<void>;
|
|
17
16
|
export declare function setActTimeout(fn: () => void, ms?: number): NodeJS.Timeout;
|
|
@@ -1473,12 +1473,15 @@
|
|
|
1473
1473
|
function infiniteQueryBehavior() {
|
|
1474
1474
|
return {
|
|
1475
1475
|
onFetch: context => {
|
|
1476
|
-
context.fetchFn = () => {
|
|
1476
|
+
context.fetchFn = async () => {
|
|
1477
1477
|
const options = context.options;
|
|
1478
1478
|
const direction = context.fetchOptions?.meta?.fetchMore?.direction;
|
|
1479
1479
|
const oldPages = context.state.data?.pages || [];
|
|
1480
1480
|
const oldPageParams = context.state.data?.pageParams || [];
|
|
1481
|
-
|
|
1481
|
+
const empty = {
|
|
1482
|
+
pages: [],
|
|
1483
|
+
pageParams: []
|
|
1484
|
+
};
|
|
1482
1485
|
let cancelled = false;
|
|
1483
1486
|
const addSignalProperty = object => {
|
|
1484
1487
|
Object.defineProperty(object, 'signal', {
|
|
@@ -1498,25 +1501,14 @@
|
|
|
1498
1501
|
|
|
1499
1502
|
// Get query function
|
|
1500
1503
|
const queryFn = context.options.queryFn || (() => Promise.reject(new Error('Missing queryFn')));
|
|
1501
|
-
const buildNewPages = (pages, param, page, previous) => {
|
|
1502
|
-
const {
|
|
1503
|
-
maxPages
|
|
1504
|
-
} = context.options;
|
|
1505
|
-
if (previous) {
|
|
1506
|
-
newPageParams = addToStart(newPageParams, param, maxPages);
|
|
1507
|
-
return addToStart(pages, page, maxPages);
|
|
1508
|
-
}
|
|
1509
|
-
newPageParams = addToEnd(newPageParams, param, maxPages);
|
|
1510
|
-
return addToEnd(pages, page, maxPages);
|
|
1511
|
-
};
|
|
1512
1504
|
|
|
1513
1505
|
// Create function to fetch a page
|
|
1514
|
-
const fetchPage = (
|
|
1506
|
+
const fetchPage = async (data, param, previous) => {
|
|
1515
1507
|
if (cancelled) {
|
|
1516
1508
|
return Promise.reject();
|
|
1517
1509
|
}
|
|
1518
|
-
if (typeof param === 'undefined' && pages.length) {
|
|
1519
|
-
return Promise.resolve(
|
|
1510
|
+
if (typeof param === 'undefined' && data.pages.length) {
|
|
1511
|
+
return Promise.resolve(data);
|
|
1520
1512
|
}
|
|
1521
1513
|
const queryFnContext = {
|
|
1522
1514
|
queryKey: context.queryKey,
|
|
@@ -1524,69 +1516,79 @@
|
|
|
1524
1516
|
meta: context.options.meta
|
|
1525
1517
|
};
|
|
1526
1518
|
addSignalProperty(queryFnContext);
|
|
1527
|
-
const
|
|
1528
|
-
const
|
|
1529
|
-
|
|
1519
|
+
const page = await queryFn(queryFnContext);
|
|
1520
|
+
const {
|
|
1521
|
+
maxPages
|
|
1522
|
+
} = context.options;
|
|
1523
|
+
const addTo = previous ? addToStart : addToEnd;
|
|
1524
|
+
return {
|
|
1525
|
+
pages: addTo(data.pages, page, maxPages),
|
|
1526
|
+
pageParams: addTo(data.pageParams, param, maxPages)
|
|
1527
|
+
};
|
|
1530
1528
|
};
|
|
1531
|
-
let
|
|
1529
|
+
let result;
|
|
1532
1530
|
|
|
1533
1531
|
// Fetch first page?
|
|
1534
1532
|
if (!oldPages.length) {
|
|
1535
|
-
|
|
1533
|
+
result = await fetchPage(empty, options.defaultPageParam);
|
|
1536
1534
|
}
|
|
1537
1535
|
|
|
1538
1536
|
// fetch next / previous page?
|
|
1539
1537
|
else if (direction) {
|
|
1540
1538
|
const previous = direction === 'backward';
|
|
1541
|
-
const
|
|
1542
|
-
|
|
1539
|
+
const pageParamFn = previous ? getPreviousPageParam : getNextPageParam;
|
|
1540
|
+
const oldData = {
|
|
1541
|
+
pages: oldPages,
|
|
1542
|
+
pageParams: oldPageParams
|
|
1543
|
+
};
|
|
1544
|
+
const param = pageParamFn(options, oldData);
|
|
1545
|
+
result = await fetchPage(oldData, param, previous);
|
|
1543
1546
|
}
|
|
1544
1547
|
|
|
1545
1548
|
// Refetch pages
|
|
1546
1549
|
else {
|
|
1547
|
-
newPageParams = [];
|
|
1548
|
-
|
|
1549
1550
|
// Fetch first page
|
|
1550
|
-
|
|
1551
|
+
result = await fetchPage(empty, oldPageParams[0]);
|
|
1551
1552
|
|
|
1552
1553
|
// Fetch remaining pages
|
|
1553
1554
|
for (let i = 1; i < oldPages.length; i++) {
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
return fetchPage(pages, param);
|
|
1557
|
-
});
|
|
1555
|
+
const param = getNextPageParam(options, result);
|
|
1556
|
+
result = await fetchPage(result, param);
|
|
1558
1557
|
}
|
|
1559
1558
|
}
|
|
1560
|
-
|
|
1561
|
-
pages,
|
|
1562
|
-
pageParams: newPageParams
|
|
1563
|
-
}));
|
|
1564
|
-
return finalPromise;
|
|
1559
|
+
return result;
|
|
1565
1560
|
};
|
|
1566
1561
|
}
|
|
1567
1562
|
};
|
|
1568
1563
|
}
|
|
1569
|
-
function getNextPageParam(options,
|
|
1570
|
-
|
|
1564
|
+
function getNextPageParam(options, {
|
|
1565
|
+
pages,
|
|
1566
|
+
pageParams
|
|
1567
|
+
}) {
|
|
1568
|
+
const lastIndex = pages.length - 1;
|
|
1569
|
+
return options.getNextPageParam(pages[lastIndex], pages, pageParams[lastIndex], pageParams);
|
|
1571
1570
|
}
|
|
1572
|
-
function getPreviousPageParam(options,
|
|
1573
|
-
|
|
1571
|
+
function getPreviousPageParam(options, {
|
|
1572
|
+
pages,
|
|
1573
|
+
pageParams
|
|
1574
|
+
}) {
|
|
1575
|
+
return options.getPreviousPageParam?.(pages[0], pages, pageParams[0], pageParams);
|
|
1574
1576
|
}
|
|
1575
1577
|
|
|
1576
1578
|
/**
|
|
1577
1579
|
* Checks if there is a next page.
|
|
1578
1580
|
*/
|
|
1579
|
-
function hasNextPage(options,
|
|
1580
|
-
if (!
|
|
1581
|
-
return typeof getNextPageParam(options,
|
|
1581
|
+
function hasNextPage(options, data) {
|
|
1582
|
+
if (!data) return false;
|
|
1583
|
+
return typeof getNextPageParam(options, data) !== 'undefined';
|
|
1582
1584
|
}
|
|
1583
1585
|
|
|
1584
1586
|
/**
|
|
1585
1587
|
* Checks if there is a previous page.
|
|
1586
1588
|
*/
|
|
1587
|
-
function hasPreviousPage(options,
|
|
1588
|
-
if (!
|
|
1589
|
-
return typeof getPreviousPageParam(options,
|
|
1589
|
+
function hasPreviousPage(options, data) {
|
|
1590
|
+
if (!data || !options.getPreviousPageParam) return false;
|
|
1591
|
+
return typeof getPreviousPageParam(options, data) !== 'undefined';
|
|
1590
1592
|
}
|
|
1591
1593
|
|
|
1592
1594
|
// CLASS
|
|
@@ -2444,7 +2446,7 @@
|
|
|
2444
2446
|
options.behavior = infiniteQueryBehavior();
|
|
2445
2447
|
return super.getOptimisticResult(options);
|
|
2446
2448
|
}
|
|
2447
|
-
fetchNextPage(options
|
|
2449
|
+
fetchNextPage(options) {
|
|
2448
2450
|
return this.fetch({
|
|
2449
2451
|
...options,
|
|
2450
2452
|
meta: {
|
|
@@ -2454,9 +2456,7 @@
|
|
|
2454
2456
|
}
|
|
2455
2457
|
});
|
|
2456
2458
|
}
|
|
2457
|
-
fetchPreviousPage({
|
|
2458
|
-
...options
|
|
2459
|
-
} = {}) {
|
|
2459
|
+
fetchPreviousPage(options) {
|
|
2460
2460
|
return this.fetch({
|
|
2461
2461
|
...options,
|
|
2462
2462
|
meta: {
|
|
@@ -2481,8 +2481,8 @@
|
|
|
2481
2481
|
...result,
|
|
2482
2482
|
fetchNextPage: this.fetchNextPage,
|
|
2483
2483
|
fetchPreviousPage: this.fetchPreviousPage,
|
|
2484
|
-
hasNextPage: hasNextPage(options, state.data
|
|
2485
|
-
hasPreviousPage: hasPreviousPage(options, state.data
|
|
2484
|
+
hasNextPage: hasNextPage(options, state.data),
|
|
2485
|
+
hasPreviousPage: hasPreviousPage(options, state.data),
|
|
2486
2486
|
isFetchingNextPage,
|
|
2487
2487
|
isFetchingPreviousPage,
|
|
2488
2488
|
isRefetching: isRefetching && !isFetchingNextPage && !isFetchingPreviousPage
|