@tanstack/query-core 5.0.0-alpha.3 → 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/infiniteQueryBehavior.d.ts +2 -2
- package/build/lib/infiniteQueryBehavior.esm.js +47 -45
- package/build/lib/infiniteQueryBehavior.esm.js.map +1 -1
- package/build/lib/infiniteQueryBehavior.js +47 -45
- package/build/lib/infiniteQueryBehavior.js.map +1 -1
- package/build/lib/infiniteQueryBehavior.mjs +47 -45
- package/build/lib/infiniteQueryBehavior.mjs.map +1 -1
- package/build/lib/infiniteQueryObserver.d.ts +6 -6
- package/build/lib/infiniteQueryObserver.esm.js +4 -6
- package/build/lib/infiniteQueryObserver.esm.js.map +1 -1
- package/build/lib/infiniteQueryObserver.js +4 -6
- package/build/lib/infiniteQueryObserver.js.map +1 -1
- package/build/lib/infiniteQueryObserver.mjs +4 -6
- package/build/lib/infiniteQueryObserver.mjs.map +1 -1
- package/build/lib/query.esm.js.map +1 -1
- package/build/lib/query.js.map +1 -1
- package/build/lib/query.mjs.map +1 -1
- package/build/lib/tests/utils.d.ts +4 -5
- package/build/lib/types.d.ts +3 -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 +2 -2
- package/src/infiniteQueryBehavior.ts +51 -60
- package/src/infiniteQueryObserver.ts +15 -10
- package/src/query.ts +1 -1
- package/src/tests/focusManager.test.tsx +12 -14
- package/src/tests/hydration.test.tsx +15 -14
- package/src/tests/infiniteQueryBehavior.test.tsx +7 -9
- package/src/tests/infiniteQueryObserver.test.tsx +62 -1
- package/src/tests/mutationCache.test.tsx +11 -10
- package/src/tests/mutationObserver.test.tsx +3 -2
- package/src/tests/mutations.test.tsx +11 -10
- package/src/tests/notifyManager.test.tsx +7 -6
- package/src/tests/onlineManager.test.tsx +12 -17
- package/src/tests/queriesObserver.test.tsx +18 -17
- package/src/tests/query.test.tsx +18 -17
- package/src/tests/queryCache.test.tsx +14 -13
- package/src/tests/queryClient.test.tsx +49 -48
- package/src/tests/queryObserver.test.tsx +10 -9
- package/src/tests/utils.test.tsx +2 -1
- package/src/tests/utils.ts +5 -4
- package/src/types.ts +7 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/query-core",
|
|
3
|
-
"version": "5.0.0-alpha.
|
|
3
|
+
"version": "5.0.0-alpha.5",
|
|
4
4
|
"description": "The framework agnostic core that powers TanStack Query",
|
|
5
5
|
"author": "tannerlinsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"clean": "rimraf ./build",
|
|
32
32
|
"test:eslint": "eslint --ext .ts,.tsx ./src",
|
|
33
33
|
"test:types": "tsc",
|
|
34
|
-
"test:lib": "
|
|
34
|
+
"test:lib": "vitest run --coverage",
|
|
35
35
|
"test:lib:dev": "pnpm run test:lib --watch",
|
|
36
36
|
"build:types": "tsc --build"
|
|
37
37
|
}
|
|
@@ -14,12 +14,12 @@ export function infiniteQueryBehavior<
|
|
|
14
14
|
>(): QueryBehavior<TQueryFnData, TError, InfiniteData<TData>> {
|
|
15
15
|
return {
|
|
16
16
|
onFetch: (context) => {
|
|
17
|
-
context.fetchFn = () => {
|
|
17
|
+
context.fetchFn = async () => {
|
|
18
18
|
const options = context.options as InfiniteQueryPageParamsOptions<TData>
|
|
19
19
|
const direction = context.fetchOptions?.meta?.fetchMore?.direction
|
|
20
20
|
const oldPages = context.state.data?.pages || []
|
|
21
21
|
const oldPageParams = context.state.data?.pageParams || []
|
|
22
|
-
|
|
22
|
+
const empty = { pages: [], pageParams: [] }
|
|
23
23
|
let cancelled = false
|
|
24
24
|
|
|
25
25
|
const addSignalProperty = (object: unknown) => {
|
|
@@ -43,35 +43,18 @@ export function infiniteQueryBehavior<
|
|
|
43
43
|
context.options.queryFn ||
|
|
44
44
|
(() => Promise.reject(new Error('Missing queryFn')))
|
|
45
45
|
|
|
46
|
-
const buildNewPages = (
|
|
47
|
-
pages: unknown[],
|
|
48
|
-
param: unknown,
|
|
49
|
-
page: unknown,
|
|
50
|
-
previous?: boolean,
|
|
51
|
-
) => {
|
|
52
|
-
const { maxPages } = context.options
|
|
53
|
-
|
|
54
|
-
if (previous) {
|
|
55
|
-
newPageParams = addToStart(newPageParams, param, maxPages)
|
|
56
|
-
return addToStart(pages, page, maxPages)
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
newPageParams = addToEnd(newPageParams, param, maxPages)
|
|
60
|
-
return addToEnd(pages, page, maxPages)
|
|
61
|
-
}
|
|
62
|
-
|
|
63
46
|
// Create function to fetch a page
|
|
64
|
-
const fetchPage = (
|
|
65
|
-
|
|
47
|
+
const fetchPage = async (
|
|
48
|
+
data: InfiniteData<unknown>,
|
|
66
49
|
param: unknown,
|
|
67
50
|
previous?: boolean,
|
|
68
|
-
): Promise<unknown
|
|
51
|
+
): Promise<InfiniteData<unknown>> => {
|
|
69
52
|
if (cancelled) {
|
|
70
53
|
return Promise.reject()
|
|
71
54
|
}
|
|
72
55
|
|
|
73
|
-
if (typeof param === 'undefined' && pages.length) {
|
|
74
|
-
return Promise.resolve(
|
|
56
|
+
if (typeof param === 'undefined' && data.pages.length) {
|
|
57
|
+
return Promise.resolve(data)
|
|
75
58
|
}
|
|
76
59
|
|
|
77
60
|
const queryFnContext: Omit<
|
|
@@ -85,55 +68,52 @@ export function infiniteQueryBehavior<
|
|
|
85
68
|
|
|
86
69
|
addSignalProperty(queryFnContext)
|
|
87
70
|
|
|
88
|
-
const
|
|
71
|
+
const page = await queryFn(
|
|
89
72
|
queryFnContext as QueryFunctionContext<QueryKey, unknown>,
|
|
90
73
|
)
|
|
91
74
|
|
|
92
|
-
const
|
|
93
|
-
|
|
94
|
-
)
|
|
75
|
+
const { maxPages } = context.options
|
|
76
|
+
const addTo = previous ? addToStart : addToEnd
|
|
95
77
|
|
|
96
|
-
return
|
|
78
|
+
return {
|
|
79
|
+
pages: addTo(data.pages, page, maxPages),
|
|
80
|
+
pageParams: addTo(data.pageParams, param, maxPages),
|
|
81
|
+
}
|
|
97
82
|
}
|
|
98
83
|
|
|
99
|
-
let
|
|
84
|
+
let result: InfiniteData<unknown>
|
|
100
85
|
|
|
101
86
|
// Fetch first page?
|
|
102
87
|
if (!oldPages.length) {
|
|
103
|
-
|
|
88
|
+
result = await fetchPage(empty, options.defaultPageParam)
|
|
104
89
|
}
|
|
105
90
|
|
|
106
91
|
// fetch next / previous page?
|
|
107
92
|
else if (direction) {
|
|
108
93
|
const previous = direction === 'backward'
|
|
109
|
-
const
|
|
110
|
-
|
|
111
|
-
:
|
|
112
|
-
|
|
94
|
+
const pageParamFn = previous ? getPreviousPageParam : getNextPageParam
|
|
95
|
+
const oldData = {
|
|
96
|
+
pages: oldPages,
|
|
97
|
+
pageParams: oldPageParams,
|
|
98
|
+
}
|
|
99
|
+
const param = pageParamFn(options, oldData)
|
|
100
|
+
|
|
101
|
+
result = await fetchPage(oldData, param, previous)
|
|
113
102
|
}
|
|
114
103
|
|
|
115
104
|
// Refetch pages
|
|
116
105
|
else {
|
|
117
|
-
newPageParams = []
|
|
118
|
-
|
|
119
106
|
// Fetch first page
|
|
120
|
-
|
|
107
|
+
result = await fetchPage(empty, oldPageParams[0])
|
|
121
108
|
|
|
122
109
|
// Fetch remaining pages
|
|
123
110
|
for (let i = 1; i < oldPages.length; i++) {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
return fetchPage(pages, param)
|
|
127
|
-
})
|
|
111
|
+
const param = getNextPageParam(options, result)
|
|
112
|
+
result = await fetchPage(result, param)
|
|
128
113
|
}
|
|
129
114
|
}
|
|
130
115
|
|
|
131
|
-
|
|
132
|
-
pages,
|
|
133
|
-
pageParams: newPageParams,
|
|
134
|
-
}))
|
|
135
|
-
|
|
136
|
-
return finalPromise
|
|
116
|
+
return result
|
|
137
117
|
}
|
|
138
118
|
},
|
|
139
119
|
}
|
|
@@ -141,36 +121,47 @@ export function infiniteQueryBehavior<
|
|
|
141
121
|
|
|
142
122
|
function getNextPageParam(
|
|
143
123
|
options: InfiniteQueryPageParamsOptions<any>,
|
|
144
|
-
pages: unknown
|
|
124
|
+
{ pages, pageParams }: InfiniteData<unknown>,
|
|
145
125
|
): unknown | undefined {
|
|
146
|
-
|
|
126
|
+
const lastIndex = pages.length - 1
|
|
127
|
+
return options.getNextPageParam(
|
|
128
|
+
pages[lastIndex],
|
|
129
|
+
pages,
|
|
130
|
+
pageParams[lastIndex],
|
|
131
|
+
pageParams,
|
|
132
|
+
)
|
|
147
133
|
}
|
|
148
134
|
|
|
149
135
|
function getPreviousPageParam(
|
|
150
136
|
options: InfiniteQueryPageParamsOptions<any>,
|
|
151
|
-
pages: unknown
|
|
137
|
+
{ pages, pageParams }: InfiniteData<unknown>,
|
|
152
138
|
): unknown | undefined {
|
|
153
|
-
return options.getPreviousPageParam?.(
|
|
139
|
+
return options.getPreviousPageParam?.(
|
|
140
|
+
pages[0],
|
|
141
|
+
pages,
|
|
142
|
+
pageParams[0],
|
|
143
|
+
pageParams,
|
|
144
|
+
)
|
|
154
145
|
}
|
|
155
146
|
|
|
156
147
|
/**
|
|
157
148
|
* Checks if there is a next page.
|
|
158
149
|
*/
|
|
159
150
|
export function hasNextPage(
|
|
160
|
-
options: InfiniteQueryPageParamsOptions<any>,
|
|
161
|
-
|
|
151
|
+
options: InfiniteQueryPageParamsOptions<any, any>,
|
|
152
|
+
data?: InfiniteData<unknown>,
|
|
162
153
|
): boolean {
|
|
163
|
-
if (!
|
|
164
|
-
return typeof getNextPageParam(options,
|
|
154
|
+
if (!data) return false
|
|
155
|
+
return typeof getNextPageParam(options, data) !== 'undefined'
|
|
165
156
|
}
|
|
166
157
|
|
|
167
158
|
/**
|
|
168
159
|
* Checks if there is a previous page.
|
|
169
160
|
*/
|
|
170
161
|
export function hasPreviousPage(
|
|
171
|
-
options: InfiniteQueryPageParamsOptions<any>,
|
|
172
|
-
|
|
162
|
+
options: InfiniteQueryPageParamsOptions<any, any>,
|
|
163
|
+
data?: InfiniteData<unknown>,
|
|
173
164
|
): boolean {
|
|
174
|
-
if (!
|
|
175
|
-
return typeof getPreviousPageParam(options,
|
|
165
|
+
if (!data || !options.getPreviousPageParam) return false
|
|
166
|
+
return typeof getPreviousPageParam(options, data) !== 'undefined'
|
|
176
167
|
}
|
|
@@ -28,6 +28,7 @@ export class InfiniteQueryObserver<
|
|
|
28
28
|
TData = InfiniteData<TQueryFnData>,
|
|
29
29
|
TQueryData = TQueryFnData,
|
|
30
30
|
TQueryKey extends QueryKey = QueryKey,
|
|
31
|
+
TPageParam = unknown,
|
|
31
32
|
> extends QueryObserver<
|
|
32
33
|
TQueryFnData,
|
|
33
34
|
TError,
|
|
@@ -56,7 +57,8 @@ export class InfiniteQueryObserver<
|
|
|
56
57
|
TError,
|
|
57
58
|
TData,
|
|
58
59
|
TQueryData,
|
|
59
|
-
TQueryKey
|
|
60
|
+
TQueryKey,
|
|
61
|
+
TPageParam
|
|
60
62
|
>,
|
|
61
63
|
) {
|
|
62
64
|
super(client, options)
|
|
@@ -74,7 +76,8 @@ export class InfiniteQueryObserver<
|
|
|
74
76
|
TError,
|
|
75
77
|
TData,
|
|
76
78
|
TQueryData,
|
|
77
|
-
TQueryKey
|
|
79
|
+
TQueryKey,
|
|
80
|
+
TPageParam
|
|
78
81
|
>,
|
|
79
82
|
notifyOptions?: NotifyOptions,
|
|
80
83
|
): void {
|
|
@@ -93,7 +96,8 @@ export class InfiniteQueryObserver<
|
|
|
93
96
|
TError,
|
|
94
97
|
TData,
|
|
95
98
|
TQueryData,
|
|
96
|
-
TQueryKey
|
|
99
|
+
TQueryKey,
|
|
100
|
+
TPageParam
|
|
97
101
|
>,
|
|
98
102
|
): InfiniteQueryObserverResult<TData, TError> {
|
|
99
103
|
options.behavior = infiniteQueryBehavior()
|
|
@@ -104,7 +108,7 @@ export class InfiniteQueryObserver<
|
|
|
104
108
|
}
|
|
105
109
|
|
|
106
110
|
fetchNextPage(
|
|
107
|
-
options
|
|
111
|
+
options?: FetchNextPageOptions,
|
|
108
112
|
): Promise<InfiniteQueryObserverResult<TData, TError>> {
|
|
109
113
|
return this.fetch({
|
|
110
114
|
...options,
|
|
@@ -114,9 +118,9 @@ export class InfiniteQueryObserver<
|
|
|
114
118
|
})
|
|
115
119
|
}
|
|
116
120
|
|
|
117
|
-
fetchPreviousPage(
|
|
118
|
-
|
|
119
|
-
|
|
121
|
+
fetchPreviousPage(
|
|
122
|
+
options?: FetchPreviousPageOptions,
|
|
123
|
+
): Promise<InfiniteQueryObserverResult<TData, TError>> {
|
|
120
124
|
return this.fetch({
|
|
121
125
|
...options,
|
|
122
126
|
meta: {
|
|
@@ -132,7 +136,8 @@ export class InfiniteQueryObserver<
|
|
|
132
136
|
TError,
|
|
133
137
|
TData,
|
|
134
138
|
TQueryData,
|
|
135
|
-
TQueryKey
|
|
139
|
+
TQueryKey,
|
|
140
|
+
TPageParam
|
|
136
141
|
>,
|
|
137
142
|
): InfiniteQueryObserverResult<TData, TError> {
|
|
138
143
|
const { state } = query
|
|
@@ -150,8 +155,8 @@ export class InfiniteQueryObserver<
|
|
|
150
155
|
...result,
|
|
151
156
|
fetchNextPage: this.fetchNextPage,
|
|
152
157
|
fetchPreviousPage: this.fetchPreviousPage,
|
|
153
|
-
hasNextPage: hasNextPage(options, state.data
|
|
154
|
-
hasPreviousPage: hasPreviousPage(options, state.data
|
|
158
|
+
hasNextPage: hasNextPage(options, state.data),
|
|
159
|
+
hasPreviousPage: hasPreviousPage(options, state.data),
|
|
155
160
|
isFetchingNextPage,
|
|
156
161
|
isFetchingPreviousPage,
|
|
157
162
|
isRefetching:
|
package/src/query.ts
CHANGED
|
@@ -453,7 +453,7 @@ export class Query<
|
|
|
453
453
|
|
|
454
454
|
// Try to fetch the data
|
|
455
455
|
this.#retryer = createRetryer({
|
|
456
|
-
fn: context.fetchFn as () => TData
|
|
456
|
+
fn: context.fetchFn as () => Promise<TData>,
|
|
457
457
|
abort: abortController.abort.bind(abortController),
|
|
458
458
|
onSuccess: (data) => {
|
|
459
459
|
if (typeof data === 'undefined') {
|
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
import { sleep } from '../utils'
|
|
2
2
|
import { FocusManager } from '../focusManager'
|
|
3
3
|
import { setIsServer } from './utils'
|
|
4
|
+
import { vi } from 'vitest'
|
|
4
5
|
|
|
5
6
|
describe('focusManager', () => {
|
|
6
7
|
let focusManager: FocusManager
|
|
7
8
|
beforeEach(() => {
|
|
8
|
-
|
|
9
|
+
vi.resetModules()
|
|
9
10
|
focusManager = new FocusManager()
|
|
10
11
|
})
|
|
11
12
|
|
|
12
13
|
it('should call previous remove handler when replacing an event listener', () => {
|
|
13
|
-
const remove1Spy =
|
|
14
|
-
const remove2Spy =
|
|
14
|
+
const remove1Spy = vi.fn()
|
|
15
|
+
const remove2Spy = vi.fn()
|
|
15
16
|
|
|
16
17
|
focusManager.setEventListener(() => remove1Spy)
|
|
17
18
|
focusManager.setEventListener(() => remove2Spy)
|
|
@@ -39,7 +40,7 @@ describe('focusManager', () => {
|
|
|
39
40
|
})
|
|
40
41
|
|
|
41
42
|
it('should not notify listeners on focus if already focused', async () => {
|
|
42
|
-
const subscriptionSpy =
|
|
43
|
+
const subscriptionSpy = vi.fn()
|
|
43
44
|
const unsubscribe = focusManager.subscribe(subscriptionSpy)
|
|
44
45
|
|
|
45
46
|
focusManager.setFocused(true)
|
|
@@ -66,7 +67,7 @@ describe('focusManager', () => {
|
|
|
66
67
|
test('cleanup (removeEventListener) should not be called if window is not defined', async () => {
|
|
67
68
|
const restoreIsServer = setIsServer(true)
|
|
68
69
|
|
|
69
|
-
const removeEventListenerSpy =
|
|
70
|
+
const removeEventListenerSpy = vi.spyOn(globalThis, 'removeEventListener')
|
|
70
71
|
|
|
71
72
|
const unsubscribe = focusManager.subscribe(() => undefined)
|
|
72
73
|
|
|
@@ -83,7 +84,7 @@ describe('focusManager', () => {
|
|
|
83
84
|
// @ts-expect-error
|
|
84
85
|
globalThis.window.addEventListener = undefined
|
|
85
86
|
|
|
86
|
-
const removeEventListenerSpy =
|
|
87
|
+
const removeEventListenerSpy = vi.spyOn(globalThis, 'removeEventListener')
|
|
87
88
|
|
|
88
89
|
const unsubscribe = focusManager.subscribe(() => undefined)
|
|
89
90
|
|
|
@@ -95,8 +96,8 @@ describe('focusManager', () => {
|
|
|
95
96
|
})
|
|
96
97
|
|
|
97
98
|
it('should replace default window listener when a new event listener is set', async () => {
|
|
98
|
-
const unsubscribeSpy =
|
|
99
|
-
const handlerSpy =
|
|
99
|
+
const unsubscribeSpy = vi.fn().mockImplementation(() => undefined)
|
|
100
|
+
const handlerSpy = vi.fn().mockImplementation(() => unsubscribeSpy)
|
|
100
101
|
|
|
101
102
|
focusManager.setEventListener(() => handlerSpy())
|
|
102
103
|
|
|
@@ -115,12 +116,9 @@ describe('focusManager', () => {
|
|
|
115
116
|
})
|
|
116
117
|
|
|
117
118
|
test('should call removeEventListener when last listener unsubscribes', () => {
|
|
118
|
-
const addEventListenerSpy =
|
|
119
|
-
globalThis.window,
|
|
120
|
-
'addEventListener',
|
|
121
|
-
)
|
|
119
|
+
const addEventListenerSpy = vi.spyOn(globalThis.window, 'addEventListener')
|
|
122
120
|
|
|
123
|
-
const removeEventListenerSpy =
|
|
121
|
+
const removeEventListenerSpy = vi.spyOn(
|
|
124
122
|
globalThis.window,
|
|
125
123
|
'removeEventListener',
|
|
126
124
|
)
|
|
@@ -136,7 +134,7 @@ describe('focusManager', () => {
|
|
|
136
134
|
})
|
|
137
135
|
|
|
138
136
|
test('should keep setup function even if last listener unsubscribes', () => {
|
|
139
|
-
const setupSpy =
|
|
137
|
+
const setupSpy = vi.fn().mockImplementation(() => () => undefined)
|
|
140
138
|
|
|
141
139
|
focusManager.setEventListener(setupSpy)
|
|
142
140
|
|
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
} from './utils'
|
|
7
7
|
import { QueryCache } from '../queryCache'
|
|
8
8
|
import { dehydrate, hydrate } from '../hydration'
|
|
9
|
+
import { vi } from 'vitest'
|
|
9
10
|
|
|
10
11
|
async function fetchData<TData>(value: TData, ms?: number): Promise<TData> {
|
|
11
12
|
await sleep(ms || 0)
|
|
@@ -67,7 +68,7 @@ describe('dehydration and rehydration', () => {
|
|
|
67
68
|
key: [{ nestedKey: 1 }],
|
|
68
69
|
})
|
|
69
70
|
|
|
70
|
-
const fetchDataAfterHydration =
|
|
71
|
+
const fetchDataAfterHydration = vi.fn<unknown[], unknown>()
|
|
71
72
|
await hydrationClient.prefetchQuery({
|
|
72
73
|
queryKey: ['string'],
|
|
73
74
|
queryFn: fetchDataAfterHydration,
|
|
@@ -192,7 +193,7 @@ describe('dehydration and rehydration', () => {
|
|
|
192
193
|
})?.state.data,
|
|
193
194
|
).toBe('string')
|
|
194
195
|
|
|
195
|
-
const fetchDataAfterHydration =
|
|
196
|
+
const fetchDataAfterHydration = vi.fn<unknown[], unknown>()
|
|
196
197
|
await hydrationClient.prefetchQuery({
|
|
197
198
|
queryKey: ['string', { key: ['string'], key2: 0 }],
|
|
198
199
|
queryFn: fetchDataAfterHydration,
|
|
@@ -205,7 +206,7 @@ describe('dehydration and rehydration', () => {
|
|
|
205
206
|
})
|
|
206
207
|
|
|
207
208
|
test('should only hydrate successful queries by default', async () => {
|
|
208
|
-
const consoleMock =
|
|
209
|
+
const consoleMock = vi.spyOn(console, 'error')
|
|
209
210
|
consoleMock.mockImplementation(() => undefined)
|
|
210
211
|
|
|
211
212
|
const queryCache = new QueryCache()
|
|
@@ -341,18 +342,18 @@ describe('dehydration and rehydration', () => {
|
|
|
341
342
|
})
|
|
342
343
|
|
|
343
344
|
test('should be able to dehydrate mutations and continue on hydration', async () => {
|
|
344
|
-
const consoleMock =
|
|
345
|
+
const consoleMock = vi.spyOn(console, 'error')
|
|
345
346
|
consoleMock.mockImplementation(() => undefined)
|
|
346
347
|
const onlineMock = mockNavigatorOnLine(false)
|
|
347
348
|
|
|
348
|
-
const serverAddTodo =
|
|
349
|
+
const serverAddTodo = vi
|
|
349
350
|
.fn()
|
|
350
351
|
.mockImplementation(() => Promise.reject(new Error('offline')))
|
|
351
|
-
const serverOnMutate =
|
|
352
|
+
const serverOnMutate = vi.fn().mockImplementation((variables) => {
|
|
352
353
|
const optimisticTodo = { id: 1, text: variables.text }
|
|
353
354
|
return { optimisticTodo }
|
|
354
355
|
})
|
|
355
|
-
const serverOnSuccess =
|
|
356
|
+
const serverOnSuccess = vi.fn()
|
|
356
357
|
|
|
357
358
|
const serverClient = createQueryClient()
|
|
358
359
|
|
|
@@ -386,14 +387,14 @@ describe('dehydration and rehydration', () => {
|
|
|
386
387
|
const parsed = JSON.parse(stringified)
|
|
387
388
|
const client = createQueryClient()
|
|
388
389
|
|
|
389
|
-
const clientAddTodo =
|
|
390
|
+
const clientAddTodo = vi.fn().mockImplementation((variables) => {
|
|
390
391
|
return { id: 2, text: variables.text }
|
|
391
392
|
})
|
|
392
|
-
const clientOnMutate =
|
|
393
|
+
const clientOnMutate = vi.fn().mockImplementation((variables) => {
|
|
393
394
|
const optimisticTodo = { id: 1, text: variables.text }
|
|
394
395
|
return { optimisticTodo }
|
|
395
396
|
})
|
|
396
|
-
const clientOnSuccess =
|
|
397
|
+
const clientOnSuccess = vi.fn()
|
|
397
398
|
|
|
398
399
|
client.setMutationDefaults(['addTodo'], {
|
|
399
400
|
mutationFn: clientAddTodo,
|
|
@@ -422,10 +423,10 @@ describe('dehydration and rehydration', () => {
|
|
|
422
423
|
})
|
|
423
424
|
|
|
424
425
|
test('should not dehydrate mutations if dehydrateMutations is set to false', async () => {
|
|
425
|
-
const consoleMock =
|
|
426
|
+
const consoleMock = vi.spyOn(console, 'error')
|
|
426
427
|
consoleMock.mockImplementation(() => undefined)
|
|
427
428
|
|
|
428
|
-
const serverAddTodo =
|
|
429
|
+
const serverAddTodo = vi
|
|
429
430
|
.fn()
|
|
430
431
|
.mockImplementation(() => Promise.reject(new Error('offline')))
|
|
431
432
|
|
|
@@ -454,10 +455,10 @@ describe('dehydration and rehydration', () => {
|
|
|
454
455
|
})
|
|
455
456
|
|
|
456
457
|
test('should not dehydrate mutation if mutation state is set to pause', async () => {
|
|
457
|
-
const consoleMock =
|
|
458
|
+
const consoleMock = vi.spyOn(console, 'error')
|
|
458
459
|
consoleMock.mockImplementation(() => undefined)
|
|
459
460
|
|
|
460
|
-
const serverAddTodo =
|
|
461
|
+
const serverAddTodo = vi
|
|
461
462
|
.fn()
|
|
462
463
|
.mockImplementation(() => Promise.reject(new Error('offline')))
|
|
463
464
|
|
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
import { waitFor } from '@testing-library/react'
|
|
2
|
-
import type {
|
|
3
|
-
|
|
4
|
-
InfiniteQueryObserverResult,
|
|
5
|
-
} from '@tanstack/query-core'
|
|
6
|
-
import { InfiniteQueryObserver, CancelledError } from '@tanstack/query-core'
|
|
2
|
+
import type { QueryClient, InfiniteQueryObserverResult } from '..'
|
|
3
|
+
import { InfiniteQueryObserver, CancelledError } from '..'
|
|
7
4
|
import { createQueryClient, queryKey, sleep } from './utils'
|
|
5
|
+
import { vi } from 'vitest'
|
|
8
6
|
|
|
9
7
|
describe('InfiniteQueryBehavior', () => {
|
|
10
8
|
let queryClient: QueryClient
|
|
@@ -50,7 +48,7 @@ describe('InfiniteQueryBehavior', () => {
|
|
|
50
48
|
const key = queryKey()
|
|
51
49
|
let abortSignal: AbortSignal | null = null
|
|
52
50
|
|
|
53
|
-
const queryFnSpy =
|
|
51
|
+
const queryFnSpy = vi.fn().mockImplementation(({ pageParam, signal }) => {
|
|
54
52
|
abortSignal = signal
|
|
55
53
|
return pageParam
|
|
56
54
|
})
|
|
@@ -185,7 +183,7 @@ describe('InfiniteQueryBehavior', () => {
|
|
|
185
183
|
const key = queryKey()
|
|
186
184
|
let abortSignal: AbortSignal | null = null
|
|
187
185
|
|
|
188
|
-
const queryFnSpy =
|
|
186
|
+
const queryFnSpy = vi.fn().mockImplementation(({ pageParam, signal }) => {
|
|
189
187
|
abortSignal = signal
|
|
190
188
|
sleep(10)
|
|
191
189
|
return pageParam
|
|
@@ -236,7 +234,7 @@ describe('InfiniteQueryBehavior', () => {
|
|
|
236
234
|
const key = queryKey()
|
|
237
235
|
let abortSignal: AbortSignal | null = null
|
|
238
236
|
|
|
239
|
-
let queryFnSpy =
|
|
237
|
+
let queryFnSpy = vi.fn().mockImplementation(({ pageParam, signal }) => {
|
|
240
238
|
abortSignal = signal
|
|
241
239
|
return pageParam
|
|
242
240
|
})
|
|
@@ -284,7 +282,7 @@ describe('InfiniteQueryBehavior', () => {
|
|
|
284
282
|
signal: abortSignal,
|
|
285
283
|
})
|
|
286
284
|
|
|
287
|
-
queryFnSpy =
|
|
285
|
+
queryFnSpy = vi.fn().mockImplementation(({ pageParam = 1, signal }) => {
|
|
288
286
|
abortSignal = signal
|
|
289
287
|
sleep(10)
|
|
290
288
|
return pageParam
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { createQueryClient, queryKey, sleep } from './utils'
|
|
2
2
|
import type { QueryClient } from '..'
|
|
3
3
|
import { InfiniteQueryObserver } from '..'
|
|
4
|
+
import { vi } from 'vitest'
|
|
4
5
|
|
|
5
6
|
describe('InfiniteQueryObserver', () => {
|
|
6
7
|
let queryClient: QueryClient
|
|
@@ -43,7 +44,7 @@ describe('InfiniteQueryObserver', () => {
|
|
|
43
44
|
}
|
|
44
45
|
|
|
45
46
|
const key = queryKey()
|
|
46
|
-
const queryFn =
|
|
47
|
+
const queryFn = vi.fn(() => 1)
|
|
47
48
|
const observer = new InfiniteQueryObserver(queryClient, {
|
|
48
49
|
meta,
|
|
49
50
|
queryKey: key,
|
|
@@ -66,4 +67,64 @@ describe('InfiniteQueryObserver', () => {
|
|
|
66
67
|
})
|
|
67
68
|
expect(queryFn).toBeCalledWith(expect.objectContaining({ meta }))
|
|
68
69
|
})
|
|
70
|
+
|
|
71
|
+
test('getNextPagParam and getPreviousPageParam should receive current pageParams', async () => {
|
|
72
|
+
const key = queryKey()
|
|
73
|
+
let single: string[] = []
|
|
74
|
+
let all: string[] = []
|
|
75
|
+
const observer = new InfiniteQueryObserver(queryClient, {
|
|
76
|
+
queryKey: key,
|
|
77
|
+
queryFn: ({ pageParam }) => String(pageParam),
|
|
78
|
+
defaultPageParam: 1,
|
|
79
|
+
getNextPageParam: (_, __, lastPageParam, allPageParams) => {
|
|
80
|
+
single.push('next' + lastPageParam)
|
|
81
|
+
all.push('next' + allPageParams.join(','))
|
|
82
|
+
return lastPageParam + 1
|
|
83
|
+
},
|
|
84
|
+
getPreviousPageParam: (_, __, firstPageParam, allPageParams) => {
|
|
85
|
+
single.push('prev' + firstPageParam)
|
|
86
|
+
all.push('prev' + allPageParams.join(','))
|
|
87
|
+
return firstPageParam - 1
|
|
88
|
+
},
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
await observer.fetchNextPage()
|
|
92
|
+
await observer.fetchPreviousPage()
|
|
93
|
+
|
|
94
|
+
expect(single).toEqual(['next1', 'prev1', 'prev1', 'next1', 'prev0'])
|
|
95
|
+
expect(all).toEqual(['next1', 'prev1', 'prev1', 'next0,1', 'prev0,1'])
|
|
96
|
+
|
|
97
|
+
single = []
|
|
98
|
+
all = []
|
|
99
|
+
|
|
100
|
+
await observer.refetch()
|
|
101
|
+
|
|
102
|
+
expect(single).toEqual(['next0', 'next1', 'prev0'])
|
|
103
|
+
expect(all).toEqual(['next0', 'next0,1', 'prev0,1'])
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
test('should stop refetching if undefined is returned from getNextPageParam', async () => {
|
|
107
|
+
const key = queryKey()
|
|
108
|
+
let next: number | undefined = 2
|
|
109
|
+
const queryFn = vi.fn<any, any>(({ pageParam }) => String(pageParam))
|
|
110
|
+
const observer = new InfiniteQueryObserver(queryClient, {
|
|
111
|
+
queryKey: key,
|
|
112
|
+
queryFn,
|
|
113
|
+
defaultPageParam: 1,
|
|
114
|
+
getNextPageParam: () => next,
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
await observer.fetchNextPage()
|
|
118
|
+
await observer.fetchNextPage()
|
|
119
|
+
|
|
120
|
+
expect(observer.getCurrentResult().data?.pages).toEqual(['1', '2'])
|
|
121
|
+
expect(queryFn).toBeCalledTimes(2)
|
|
122
|
+
|
|
123
|
+
next = undefined
|
|
124
|
+
|
|
125
|
+
await observer.refetch()
|
|
126
|
+
|
|
127
|
+
expect(observer.getCurrentResult().data?.pages).toEqual(['1'])
|
|
128
|
+
expect(queryFn).toBeCalledTimes(3)
|
|
129
|
+
})
|
|
69
130
|
})
|
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import { waitFor } from '@testing-library/react'
|
|
2
2
|
import { queryKey, sleep, executeMutation, createQueryClient } from './utils'
|
|
3
3
|
import { MutationCache, MutationObserver } from '..'
|
|
4
|
+
import { vi } from 'vitest'
|
|
4
5
|
|
|
5
6
|
describe('mutationCache', () => {
|
|
6
7
|
describe('MutationCacheConfig error callbacks', () => {
|
|
7
8
|
test('should call onError and onSettled when a mutation errors', async () => {
|
|
8
9
|
const key = queryKey()
|
|
9
|
-
const onError =
|
|
10
|
-
const onSuccess =
|
|
11
|
-
const onSettled =
|
|
10
|
+
const onError = vi.fn()
|
|
11
|
+
const onSuccess = vi.fn()
|
|
12
|
+
const onSettled = vi.fn()
|
|
12
13
|
const testCache = new MutationCache({ onError, onSuccess, onSettled })
|
|
13
14
|
const testClient = createQueryClient({ mutationCache: testCache })
|
|
14
15
|
|
|
@@ -86,9 +87,9 @@ describe('mutationCache', () => {
|
|
|
86
87
|
describe('MutationCacheConfig success callbacks', () => {
|
|
87
88
|
test('should call onSuccess and onSettled when a mutation is successful', async () => {
|
|
88
89
|
const key = queryKey()
|
|
89
|
-
const onError =
|
|
90
|
-
const onSuccess =
|
|
91
|
-
const onSettled =
|
|
90
|
+
const onError = vi.fn()
|
|
91
|
+
const onSuccess = vi.fn()
|
|
92
|
+
const onSettled = vi.fn()
|
|
92
93
|
const testCache = new MutationCache({ onError, onSuccess, onSettled })
|
|
93
94
|
const testClient = createQueryClient({ mutationCache: testCache })
|
|
94
95
|
|
|
@@ -163,7 +164,7 @@ describe('mutationCache', () => {
|
|
|
163
164
|
describe('MutationCacheConfig.onMutate', () => {
|
|
164
165
|
test('should be called before a mutation executes', async () => {
|
|
165
166
|
const key = queryKey()
|
|
166
|
-
const onMutate =
|
|
167
|
+
const onMutate = vi.fn()
|
|
167
168
|
const testCache = new MutationCache({ onMutate })
|
|
168
169
|
const testClient = createQueryClient({ mutationCache: testCache })
|
|
169
170
|
|
|
@@ -286,7 +287,7 @@ describe('mutationCache', () => {
|
|
|
286
287
|
test('should remove unused mutations after gcTime has elapsed', async () => {
|
|
287
288
|
const testCache = new MutationCache()
|
|
288
289
|
const testClient = createQueryClient({ mutationCache: testCache })
|
|
289
|
-
const onSuccess =
|
|
290
|
+
const onSuccess = vi.fn()
|
|
290
291
|
await executeMutation(
|
|
291
292
|
testClient,
|
|
292
293
|
{
|
|
@@ -329,7 +330,7 @@ describe('mutationCache', () => {
|
|
|
329
330
|
|
|
330
331
|
test('should be garbage collected later when unsubscribed and mutation is pending', async () => {
|
|
331
332
|
const queryClient = createQueryClient()
|
|
332
|
-
const onSuccess =
|
|
333
|
+
const onSuccess = vi.fn()
|
|
333
334
|
const observer = new MutationObserver(queryClient, {
|
|
334
335
|
gcTime: 10,
|
|
335
336
|
mutationFn: async () => {
|
|
@@ -355,7 +356,7 @@ describe('mutationCache', () => {
|
|
|
355
356
|
|
|
356
357
|
test('should call callbacks even with gcTime 0 and mutation still pending', async () => {
|
|
357
358
|
const queryClient = createQueryClient()
|
|
358
|
-
const onSuccess =
|
|
359
|
+
const onSuccess = vi.fn()
|
|
359
360
|
const observer = new MutationObserver(queryClient, {
|
|
360
361
|
gcTime: 0,
|
|
361
362
|
mutationFn: async () => {
|