Skip to content

Commit 361cba4

Browse files
committed
test: add tests for MobxQuery, MobxInfiniteQuery; feat: add typings for MobxInfiniteQuery, MobxQuery; fix: MobxQuery enableOnDemand option behaviour
1 parent 82ff7f2 commit 361cba4

File tree

9 files changed

+852
-326
lines changed

9 files changed

+852
-326
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"@vitejs/plugin-react-swc": "3.7.2",
5050
"@vitest/coverage-istanbul": "2.1.6",
5151
"eslint": "8.57.0",
52-
"js2me-eslint-config": "1.0.5",
52+
"js2me-eslint-config": "1.0.6",
5353
"js2me-exports-post-build-script": "2.0.17",
5454
"jsdom": "25.0.1",
5555
"typescript": "5.4.5",

pnpm-lock.yaml

Lines changed: 12 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './mobx-mutation';
2+
export * from './mobx-query.types';
23
export * from './mobx-query';
34
export * from './mobx-inifinite-query';

src/mobx-infinite-query.test.ts

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
import {
2+
DefaultError,
3+
FetchNextPageOptions,
4+
FetchPreviousPageOptions,
5+
QueryClient,
6+
QueryKey,
7+
RefetchOptions,
8+
} from '@tanstack/query-core';
9+
import { describe, expect, it, vi } from 'vitest';
10+
11+
import { MobxInfiniteQuery } from './mobx-inifinite-query';
12+
import {
13+
MobxInfiniteQueryConfig,
14+
MobxInfiniteQueryDynamicOptions,
15+
MobxInfiniteQueryUpdateOptions,
16+
} from './mobx-inifinite-query.types';
17+
import { MobxQueryInvalidateParams } from './mobx-query.types';
18+
19+
class MobxInfiniteQueryMock<
20+
TData,
21+
TError = DefaultError,
22+
TQueryKey extends QueryKey = any,
23+
TPageParam = unknown,
24+
> extends MobxInfiniteQuery<TData, TError, TQueryKey, TPageParam> {
25+
spies = {
26+
queryFn: vi.fn(),
27+
setData: vi.fn(),
28+
update: vi.fn(),
29+
dispose: vi.fn(),
30+
refetch: vi.fn(),
31+
invalidate: vi.fn(),
32+
onDone: vi.fn(),
33+
onError: vi.fn(),
34+
fetchNextPage: vi.fn(),
35+
fetchPreviousPage: vi.fn(),
36+
};
37+
38+
constructor(
39+
options: Omit<
40+
MobxInfiniteQueryConfig<TData, TError, TQueryKey, TPageParam>,
41+
'queryClient'
42+
>,
43+
) {
44+
super({
45+
...options,
46+
queryClient: new QueryClient({}),
47+
// @ts-ignore
48+
queryFn: vi.fn(options.queryFn),
49+
});
50+
51+
// @ts-ignore
52+
this.spies.queryFn = this.options.queryFn;
53+
54+
this.onDone(this.spies.onDone);
55+
this.onError(this.spies.onError);
56+
}
57+
58+
get _rawResult() {
59+
return this._result;
60+
}
61+
62+
refetch(options?: RefetchOptions | undefined) {
63+
this.spies.refetch(options);
64+
return super.refetch(options);
65+
}
66+
67+
invalidate(params?: MobxQueryInvalidateParams | undefined): Promise<void> {
68+
this.spies.invalidate(params);
69+
return super.invalidate();
70+
}
71+
72+
update(
73+
options:
74+
| MobxInfiniteQueryUpdateOptions<TData, TError, TQueryKey, TPageParam>
75+
| MobxInfiniteQueryDynamicOptions<TData, TError, TQueryKey, TPageParam>,
76+
) {
77+
const result = super.update(options);
78+
this.spies.update.mockReturnValue(result);
79+
this.spies.update(options);
80+
return result;
81+
}
82+
83+
async fetchNextPage(options?: FetchNextPageOptions | undefined) {
84+
const result = await super.fetchNextPage(options);
85+
this.spies.fetchNextPage.mockReturnValue(result);
86+
this.spies.fetchNextPage(options);
87+
return result;
88+
}
89+
90+
async fetchPreviousPage(options?: FetchPreviousPageOptions | undefined) {
91+
const result = await super.fetchPreviousPage(options);
92+
this.spies.fetchPreviousPage.mockReturnValue(result);
93+
this.spies.fetchPreviousPage(options);
94+
return result;
95+
}
96+
97+
setData(updater: any, options?: any) {
98+
const result = super.setData(updater, options);
99+
this.spies.setData.mockReturnValue(result);
100+
this.spies.setData(updater, options);
101+
return result;
102+
}
103+
104+
dispose(): void {
105+
const result = super.dispose();
106+
this.spies.dispose.mockReturnValue(result);
107+
this.spies.dispose();
108+
return result;
109+
}
110+
}
111+
112+
describe('MobxInfiniteQuery', () => {
113+
it('should call queryFn without infinite query params', async () => {
114+
const mobxQuery = new MobxInfiniteQueryMock({
115+
queryKey: ['test'],
116+
queryFn: () => {},
117+
});
118+
119+
expect(mobxQuery.spies.queryFn).toBeCalledTimes(1);
120+
expect(mobxQuery.spies.queryFn).toBeCalledWith({
121+
direction: 'forward',
122+
meta: undefined,
123+
pageParam: undefined,
124+
queryKey: ['test'],
125+
signal: mobxQuery.spies.queryFn.mock.calls[0][0].signal,
126+
});
127+
128+
mobxQuery.dispose();
129+
});
130+
131+
it('should call queryFn with initialPageParam', async () => {
132+
const mobxQuery = new MobxInfiniteQueryMock({
133+
queryKey: ['test'],
134+
initialPageParam: 0,
135+
queryFn: () => {},
136+
});
137+
138+
expect(mobxQuery.spies.queryFn).toBeCalledTimes(1);
139+
expect(mobxQuery.spies.queryFn).toBeCalledWith({
140+
direction: 'forward',
141+
meta: undefined,
142+
pageParam: 0,
143+
queryKey: ['test'],
144+
signal: mobxQuery.spies.queryFn.mock.calls[0][0].signal,
145+
});
146+
147+
mobxQuery.dispose();
148+
});
149+
150+
it('should call queryFn with getNextPageParam', async () => {
151+
const mobxQuery = new MobxInfiniteQueryMock({
152+
queryKey: ['test'],
153+
getNextPageParam: () => 1,
154+
queryFn: () => {},
155+
});
156+
157+
expect(mobxQuery.spies.queryFn).toBeCalledTimes(1);
158+
expect(mobxQuery.spies.queryFn).toBeCalledWith({
159+
direction: 'forward',
160+
meta: undefined,
161+
pageParam: undefined,
162+
queryKey: ['test'],
163+
signal: mobxQuery.spies.queryFn.mock.calls[0][0].signal,
164+
});
165+
166+
mobxQuery.dispose();
167+
});
168+
169+
it('should call queryFn with getNextPageParam returning null', async () => {
170+
const mobxQuery = new MobxInfiniteQueryMock({
171+
queryKey: ['test'],
172+
getNextPageParam: () => null,
173+
queryFn: () => {},
174+
});
175+
176+
expect(mobxQuery.spies.queryFn).toBeCalledTimes(1);
177+
expect(mobxQuery.spies.queryFn).toBeCalledWith({
178+
direction: 'forward',
179+
meta: undefined,
180+
pageParam: undefined,
181+
queryKey: ['test'],
182+
signal: mobxQuery.spies.queryFn.mock.calls[0][0].signal,
183+
});
184+
185+
mobxQuery.dispose();
186+
});
187+
188+
it('should call queryFn after fetchNextPage call', async () => {
189+
const mobxQuery = new MobxInfiniteQueryMock({
190+
queryKey: ['test'],
191+
initialPageParam: 1,
192+
enabled: true,
193+
enableOnDemand: true,
194+
getNextPageParam: (_, _1, lastPageParam) => lastPageParam + 1,
195+
queryFn: () => {
196+
return [1, 2, 3];
197+
},
198+
});
199+
200+
expect(mobxQuery.result.hasNextPage).toBeTruthy();
201+
202+
await mobxQuery.fetchNextPage();
203+
204+
expect(mobxQuery.spies.fetchNextPage).toBeCalledTimes(1);
205+
expect(mobxQuery.spies.queryFn).toBeCalledTimes(1);
206+
207+
mobxQuery.dispose();
208+
});
209+
});

0 commit comments

Comments
 (0)