Skip to content

Commit c058c14

Browse files
committed
test: unit tests for queries
1 parent 361cba4 commit c058c14

File tree

2 files changed

+223
-18
lines changed

2 files changed

+223
-18
lines changed

src/mobx-infinite-query.test.ts

Lines changed: 210 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
QueryKey,
77
RefetchOptions,
88
} from '@tanstack/query-core';
9+
import { when } from 'mobx';
910
import { describe, expect, it, vi } from 'vitest';
1011

1112
import { MobxInfiniteQuery } from './mobx-inifinite-query';
@@ -23,7 +24,7 @@ class MobxInfiniteQueryMock<
2324
TPageParam = unknown,
2425
> extends MobxInfiniteQuery<TData, TError, TQueryKey, TPageParam> {
2526
spies = {
26-
queryFn: vi.fn(),
27+
queryFn: null as unknown as ReturnType<typeof vi.fn>,
2728
setData: vi.fn(),
2829
update: vi.fn(),
2930
dispose: vi.fn(),
@@ -45,11 +46,14 @@ class MobxInfiniteQueryMock<
4546
...options,
4647
queryClient: new QueryClient({}),
4748
// @ts-ignore
48-
queryFn: vi.fn(options.queryFn),
49+
queryFn: vi.fn((...args: any[]) => {
50+
// @ts-ignore
51+
const result = options.queryFn?.(...args);
52+
return result;
53+
}),
4954
});
5055

51-
// @ts-ignore
52-
this.spies.queryFn = this.options.queryFn;
56+
this.spies.queryFn = this.options.queryFn as any;
5357

5458
this.onDone(this.spies.onDone);
5559
this.onError(this.spies.onError);
@@ -170,7 +174,7 @@ describe('MobxInfiniteQuery', () => {
170174
const mobxQuery = new MobxInfiniteQueryMock({
171175
queryKey: ['test'],
172176
getNextPageParam: () => null,
173-
queryFn: () => {},
177+
queryFn: async () => 'data',
174178
});
175179

176180
expect(mobxQuery.spies.queryFn).toBeCalledTimes(1);
@@ -182,28 +186,226 @@ describe('MobxInfiniteQuery', () => {
182186
signal: mobxQuery.spies.queryFn.mock.calls[0][0].signal,
183187
});
184188

189+
await when(() => !mobxQuery.result.isLoading);
190+
191+
expect(mobxQuery.result).toStrictEqual({
192+
...mobxQuery.result,
193+
data: {
194+
pageParams: [undefined],
195+
pages: ['data'],
196+
},
197+
error: null,
198+
errorUpdateCount: 0,
199+
errorUpdatedAt: 0,
200+
failureCount: 0,
201+
failureReason: null,
202+
fetchStatus: 'idle',
203+
hasNextPage: false,
204+
hasPreviousPage: false,
205+
isError: false,
206+
isFetchNextPageError: false,
207+
isFetchPreviousPageError: false,
208+
isFetched: true,
209+
isFetchedAfterMount: true,
210+
isFetching: false,
211+
isFetchingNextPage: false,
212+
isFetchingPreviousPage: false,
213+
isInitialLoading: false,
214+
isLoading: false,
215+
isLoadingError: false,
216+
isPaused: false,
217+
isPending: false,
218+
isPlaceholderData: false,
219+
isRefetchError: false,
220+
isRefetching: false,
221+
isStale: true,
222+
isSuccess: true,
223+
status: 'success',
224+
});
225+
185226
mobxQuery.dispose();
186227
});
187228

188229
it('should call queryFn after fetchNextPage call', async () => {
189230
const mobxQuery = new MobxInfiniteQueryMock({
190231
queryKey: ['test'],
191232
initialPageParam: 1,
192-
enabled: true,
193-
enableOnDemand: true,
194233
getNextPageParam: (_, _1, lastPageParam) => lastPageParam + 1,
195234
queryFn: () => {
196235
return [1, 2, 3];
197236
},
198237
});
199238

200-
expect(mobxQuery.result.hasNextPage).toBeTruthy();
239+
expect(mobxQuery.result).toStrictEqual({
240+
...mobxQuery.result,
241+
data: undefined,
242+
dataUpdatedAt: 0,
243+
error: null,
244+
errorUpdateCount: 0,
245+
errorUpdatedAt: 0,
246+
failureCount: 0,
247+
failureReason: null,
248+
fetchStatus: 'fetching',
249+
hasNextPage: false,
250+
hasPreviousPage: false,
251+
isError: false,
252+
isFetchNextPageError: false,
253+
isFetchPreviousPageError: false,
254+
isFetched: false,
255+
isFetchedAfterMount: false,
256+
isFetching: true,
257+
isFetchingNextPage: false,
258+
isFetchingPreviousPage: false,
259+
isInitialLoading: true,
260+
isLoading: true,
261+
isLoadingError: false,
262+
isPaused: false,
263+
isPending: true,
264+
isPlaceholderData: false,
265+
isRefetchError: false,
266+
isRefetching: false,
267+
isStale: true,
268+
isSuccess: false,
269+
status: 'pending',
270+
});
201271

202272
await mobxQuery.fetchNextPage();
203273

204274
expect(mobxQuery.spies.fetchNextPage).toBeCalledTimes(1);
205275
expect(mobxQuery.spies.queryFn).toBeCalledTimes(1);
206276

277+
expect(mobxQuery.result).toStrictEqual({
278+
...mobxQuery.result,
279+
data: {
280+
pageParams: [1],
281+
pages: [[1, 2, 3]],
282+
},
283+
error: null,
284+
errorUpdateCount: 0,
285+
errorUpdatedAt: 0,
286+
failureCount: 0,
287+
failureReason: null,
288+
fetchStatus: 'idle',
289+
hasNextPage: true,
290+
hasPreviousPage: false,
291+
isError: false,
292+
isFetchNextPageError: false,
293+
isFetchPreviousPageError: false,
294+
isFetched: true,
295+
isFetchedAfterMount: true,
296+
isFetching: false,
297+
isFetchingNextPage: false,
298+
isFetchingPreviousPage: false,
299+
isInitialLoading: false,
300+
isLoading: false,
301+
isLoadingError: false,
302+
isPaused: false,
303+
isPending: false,
304+
isPlaceholderData: false,
305+
isRefetchError: false,
306+
isRefetching: false,
307+
isStale: true,
308+
isSuccess: true,
309+
status: 'success',
310+
});
311+
312+
mobxQuery.dispose();
313+
});
314+
it('should call queryFn after fetchNextPage call (x3 times)', async () => {
315+
const mobxQuery = new MobxInfiniteQueryMock({
316+
queryKey: ['test'],
317+
initialPageParam: 1,
318+
getNextPageParam: (_, _1, lastPageParam) => lastPageParam + 1,
319+
queryFn: ({ pageParam, queryKey }) => {
320+
return { data: pageParam, queryKey };
321+
},
322+
});
323+
324+
expect(mobxQuery.result).toStrictEqual({
325+
...mobxQuery.result,
326+
data: undefined,
327+
dataUpdatedAt: 0,
328+
error: null,
329+
errorUpdateCount: 0,
330+
errorUpdatedAt: 0,
331+
failureCount: 0,
332+
failureReason: null,
333+
fetchStatus: 'fetching',
334+
hasNextPage: false,
335+
hasPreviousPage: false,
336+
isError: false,
337+
isFetchNextPageError: false,
338+
isFetchPreviousPageError: false,
339+
isFetched: false,
340+
isFetchedAfterMount: false,
341+
isFetching: true,
342+
isFetchingNextPage: false,
343+
isFetchingPreviousPage: false,
344+
isInitialLoading: true,
345+
isLoading: true,
346+
isLoadingError: false,
347+
isPaused: false,
348+
isPending: true,
349+
isPlaceholderData: false,
350+
isRefetchError: false,
351+
isRefetching: false,
352+
isStale: true,
353+
isSuccess: false,
354+
status: 'pending',
355+
});
356+
357+
await mobxQuery.fetchNextPage();
358+
await mobxQuery.fetchNextPage();
359+
await mobxQuery.fetchNextPage();
360+
361+
expect(mobxQuery.result).toStrictEqual({
362+
...mobxQuery.result,
363+
data: {
364+
pageParams: [1, 2, 3],
365+
pages: [
366+
{
367+
data: 1,
368+
queryKey: ['test'],
369+
},
370+
{
371+
data: 2,
372+
queryKey: ['test'],
373+
},
374+
{
375+
data: 3,
376+
queryKey: ['test'],
377+
},
378+
],
379+
},
380+
error: null,
381+
errorUpdateCount: 0,
382+
errorUpdatedAt: 0,
383+
failureCount: 0,
384+
failureReason: null,
385+
fetchStatus: 'idle',
386+
hasNextPage: true,
387+
hasPreviousPage: false,
388+
isError: false,
389+
isFetchNextPageError: false,
390+
isFetchPreviousPageError: false,
391+
isFetched: true,
392+
isFetchedAfterMount: true,
393+
isFetching: false,
394+
isFetchingNextPage: false,
395+
isFetchingPreviousPage: false,
396+
isInitialLoading: false,
397+
isLoading: false,
398+
isLoadingError: false,
399+
isPaused: false,
400+
isPending: false,
401+
isPlaceholderData: false,
402+
isRefetchError: false,
403+
isRefetching: false,
404+
isStale: true,
405+
isSuccess: true,
406+
status: 'success',
407+
});
408+
207409
mobxQuery.dispose();
208410
});
209411
});

src/mobx-query.test.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class MobxQueryMock<
2020
TQueryKey extends QueryKey = any,
2121
> extends MobxQuery<TData, TError, TQueryKey> {
2222
spies = {
23-
queryFn: vi.fn(),
23+
queryFn: null as unknown as ReturnType<typeof vi.fn>,
2424
setData: vi.fn(),
2525
update: vi.fn(),
2626
dispose: vi.fn(),
@@ -37,11 +37,14 @@ class MobxQueryMock<
3737
...options,
3838
queryClient: new QueryClient({}),
3939
// @ts-ignore
40-
queryFn: vi.fn(options.queryFn),
40+
queryFn: vi.fn((...args: any[]) => {
41+
// @ts-ignore
42+
const result = options.queryFn?.(...args);
43+
return result;
44+
}),
4145
});
4246

43-
// @ts-ignore
44-
this.spies.queryFn = this.options.queryFn;
47+
this.spies.queryFn = this.options.queryFn as any;
4548

4649
this.onDone(this.spies.onDone);
4750
this.onError(this.spies.onError);
@@ -69,8 +72,7 @@ class MobxQueryMock<
6972
>,
7073
): void {
7174
const result = super.update(options);
72-
this.spies.update.mockReturnValue(result);
73-
this.spies.update(options);
75+
this.spies.update.mockReturnValue(result)(options);
7476
return result;
7577
}
7678

@@ -79,15 +81,13 @@ class MobxQueryMock<
7981
options?: SetDataOptions | undefined,
8082
): TData | undefined {
8183
const result = super.setData(updater, options);
82-
this.spies.setData.mockReturnValue(result);
83-
this.spies.setData(updater, options);
84+
this.spies.setData.mockReturnValue(result)(updater, options);
8485
return result;
8586
}
8687

8788
dispose(): void {
8889
const result = super.dispose();
89-
this.spies.dispose.mockReturnValue(result);
90-
this.spies.dispose();
90+
this.spies.dispose.mockReturnValue(result)();
9191
return result;
9292
}
9393
}
@@ -151,6 +151,8 @@ describe('MobxQuery', () => {
151151
queryKey: () => ['test', boxCounter.get()] as const,
152152
});
153153

154+
await when(() => !mobxQuery._rawResult.isLoading);
155+
154156
runInAction(() => {
155157
boxCounter.set(1);
156158
});
@@ -159,6 +161,7 @@ describe('MobxQuery', () => {
159161

160162
expect(mobxQuery.spies.queryFn).toBeCalledTimes(2);
161163
expect(mobxQuery.spies.queryFn).nthReturnedWith(1, 0);
164+
expect(mobxQuery.spies.queryFn).nthReturnedWith(2, 1);
162165

163166
mobxQuery.dispose();
164167
});

0 commit comments

Comments
 (0)