|
1 | | -import { DefaultError, QueryClient, QueryKey } from '@tanstack/query-core'; |
2 | | -import { reaction, when } from 'mobx'; |
| 1 | +import { |
| 2 | + DefaultError, |
| 3 | + QueryClient, |
| 4 | + QueryKey, |
| 5 | + QueryObserverOptions, |
| 6 | + QueryObserverResult, |
| 7 | + RefetchOptions, |
| 8 | + SetDataOptions, |
| 9 | + Updater, |
| 10 | +} from '@tanstack/query-core'; |
| 11 | +import { observable, reaction, runInAction, when } from 'mobx'; |
3 | 12 | import { describe, expect, it, vi } from 'vitest'; |
4 | 13 |
|
5 | | -import { MobxQuery, MobxQueryConfig } from './mobx-query'; |
| 14 | +import { |
| 15 | + MobxQuery, |
| 16 | + MobxQueryConfig, |
| 17 | + MobxQueryInvalidateParams, |
| 18 | +} from './mobx-query'; |
6 | 19 |
|
7 | | -describe('MobxQuery', () => { |
8 | | - const testQueryClient = new QueryClient({}); |
9 | | - class TestMobxQuery< |
10 | | - TData, |
11 | | - TError = DefaultError, |
12 | | - TQueryKey extends QueryKey = any, |
13 | | - > extends MobxQuery<TData, TError, TQueryKey> { |
14 | | - constructor( |
15 | | - options: Omit<MobxQueryConfig<TData, TError, TQueryKey>, 'queryClient'>, |
16 | | - ) { |
17 | | - super({ ...options, queryClient: testQueryClient }); |
18 | | - } |
| 20 | +class MobxQueryMock< |
| 21 | + TData, |
| 22 | + TError = DefaultError, |
| 23 | + TQueryKey extends QueryKey = any, |
| 24 | +> extends MobxQuery<TData, TError, TQueryKey> { |
| 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 | + }; |
| 35 | + |
| 36 | + constructor( |
| 37 | + options: Omit<MobxQueryConfig<TData, TError, TQueryKey>, 'queryClient'>, |
| 38 | + ) { |
| 39 | + super({ |
| 40 | + ...options, |
| 41 | + queryClient: new QueryClient({}), |
| 42 | + // @ts-ignore |
| 43 | + queryFn: vi.fn(options.queryFn), |
| 44 | + }); |
| 45 | + |
| 46 | + // @ts-ignore |
| 47 | + this.spies.queryFn = this.options.queryFn; |
| 48 | + |
| 49 | + this.onDone(this.spies.onDone); |
| 50 | + this.onError(this.spies.onError); |
19 | 51 | } |
20 | 52 |
|
21 | | - it('to be defined', () => { |
22 | | - const mobxQuery = new TestMobxQuery({ |
| 53 | + refetch( |
| 54 | + options?: RefetchOptions | undefined, |
| 55 | + ): Promise<QueryObserverResult<TData, TError>> { |
| 56 | + this.spies.refetch(options); |
| 57 | + return super.refetch(options); |
| 58 | + } |
| 59 | + |
| 60 | + invalidate(params?: MobxQueryInvalidateParams | undefined): Promise<void> { |
| 61 | + this.spies.invalidate(params); |
| 62 | + return super.invalidate(); |
| 63 | + } |
| 64 | + |
| 65 | + update( |
| 66 | + options: Partial< |
| 67 | + QueryObserverOptions<TData, TError, TQueryKey, TData, QueryKey, never> |
| 68 | + >, |
| 69 | + ): void { |
| 70 | + this.spies.update(options); |
| 71 | + return super.update(options); |
| 72 | + } |
| 73 | + |
| 74 | + setData( |
| 75 | + updater: Updater<NoInfer<TData> | undefined, NoInfer<TData> | undefined>, |
| 76 | + options?: SetDataOptions | undefined, |
| 77 | + ): void { |
| 78 | + this.spies.setData(updater, options); |
| 79 | + return super.setData(updater, options); |
| 80 | + } |
| 81 | + |
| 82 | + dispose(): void { |
| 83 | + this.spies.dispose(); |
| 84 | + return super.dispose(); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +describe('MobxQuery', () => { |
| 89 | + it('"result" field to be defined', () => { |
| 90 | + const mobxQuery = new MobxQueryMock({ |
23 | 91 | queryKey: ['test'], |
24 | 92 | queryFn: () => {}, |
25 | 93 | }); |
26 | | - expect(mobxQuery).toBeDefined(); |
| 94 | + expect(mobxQuery.result).toBeDefined(); |
27 | 95 | }); |
28 | 96 |
|
29 | 97 | it('"result" field should be reactive', async () => { |
30 | 98 | let counter = 0; |
31 | | - const mobxQuery = new TestMobxQuery({ |
| 99 | + const mobxQuery = new MobxQueryMock({ |
32 | 100 | queryKey: ['test'], |
33 | | - queryFn: () => { |
34 | | - return ++counter; |
35 | | - }, |
| 101 | + queryFn: () => ++counter, |
36 | 102 | }); |
37 | | - const spy = vi.fn(); |
| 103 | + const reactionSpy = vi.fn(); |
38 | 104 |
|
39 | 105 | const dispose = reaction( |
40 | 106 | () => mobxQuery.result, |
41 | | - (result) => { |
42 | | - spy(result.data); |
43 | | - }, |
| 107 | + (result) => reactionSpy(result), |
44 | 108 | ); |
45 | 109 |
|
46 | 110 | await when(() => mobxQuery.result.isLoading); |
47 | 111 |
|
48 | | - expect(spy).toBeCalledTimes(1); |
49 | | - expect(spy).nthCalledWith(1, 1); |
| 112 | + expect(reactionSpy).toBeCalled(); |
| 113 | + expect(reactionSpy).toBeCalledWith({ ...mobxQuery.result }); |
50 | 114 |
|
51 | 115 | dispose(); |
52 | 116 | }); |
| 117 | + |
| 118 | + describe('"queryKey" reactive parameter', () => { |
| 119 | + it('should rerun queryFn after queryKey change', () => { |
| 120 | + const boxCounter = observable.box(0); |
| 121 | + const mobxQuery = new MobxQueryMock({ |
| 122 | + queryFn: ({ queryKey }) => { |
| 123 | + return queryKey[1]; |
| 124 | + }, |
| 125 | + queryKey: () => ['test', boxCounter.get()] as const, |
| 126 | + }); |
| 127 | + |
| 128 | + runInAction(() => { |
| 129 | + boxCounter.set(1); |
| 130 | + }); |
| 131 | + |
| 132 | + expect(mobxQuery.spies.queryFn).toBeCalledTimes(2); |
| 133 | + expect(mobxQuery.spies.queryFn).nthReturnedWith(1, 0); |
| 134 | + }); |
| 135 | + |
| 136 | + it('should rerun queryFn after queryKey change', () => { |
| 137 | + const boxEnabled = observable.box(false); |
| 138 | + const mobxQuery = new MobxQueryMock({ |
| 139 | + queryFn: () => 10, |
| 140 | + queryKey: () => ['test', boxEnabled.get()] as const, |
| 141 | + enabled: ({ queryKey }) => queryKey[1], |
| 142 | + }); |
| 143 | + |
| 144 | + runInAction(() => { |
| 145 | + boxEnabled.set(true); |
| 146 | + }); |
| 147 | + |
| 148 | + expect(mobxQuery.spies.queryFn).toBeCalledTimes(1); |
| 149 | + expect(mobxQuery.spies.queryFn).nthReturnedWith(1, 10); |
| 150 | + }); |
| 151 | + }); |
| 152 | + |
| 153 | + describe('"options" reactive parameter', () => { |
| 154 | + it('"options.queryKey" should updates query', async () => { |
| 155 | + const boxCounter = observable.box(0); |
| 156 | + let counter = 0; |
| 157 | + const mobxQuery = new MobxQueryMock({ |
| 158 | + queryFn: ({ queryKey }) => { |
| 159 | + counter += queryKey[1] * 10; |
| 160 | + return counter; |
| 161 | + }, |
| 162 | + options: () => ({ |
| 163 | + queryKey: ['test', boxCounter.get()] as const, |
| 164 | + }), |
| 165 | + }); |
| 166 | + |
| 167 | + runInAction(() => { |
| 168 | + boxCounter.set(1); |
| 169 | + }); |
| 170 | + |
| 171 | + expect(mobxQuery.spies.queryFn).toBeCalledTimes(2); |
| 172 | + expect(mobxQuery.spies.queryFn).nthReturnedWith(1, 0); |
| 173 | + expect(mobxQuery.spies.queryFn).nthReturnedWith(2, 10); |
| 174 | + }); |
| 175 | + |
| 176 | + it('"options.enabled" should change "enabled" statement for query (enabled as boolean in options)', async () => { |
| 177 | + const boxEnabled = observable.box(false); |
| 178 | + const mobxQuery = new MobxQueryMock({ |
| 179 | + queryFn: ({ queryKey }) => { |
| 180 | + return queryKey[1]; |
| 181 | + }, |
| 182 | + options: () => ({ |
| 183 | + enabled: boxEnabled.get(), |
| 184 | + queryKey: ['test', boxEnabled.get() ? 10 : 0] as const, |
| 185 | + }), |
| 186 | + }); |
| 187 | + |
| 188 | + runInAction(() => { |
| 189 | + boxEnabled.set(true); |
| 190 | + }); |
| 191 | + |
| 192 | + expect(mobxQuery.spies.queryFn).toBeCalledTimes(1); |
| 193 | + expect(mobxQuery.spies.queryFn).nthReturnedWith(1, 10); |
| 194 | + }); |
| 195 | + |
| 196 | + it('"options.enabled" should change "enabled" statement for query (enabled as query based fn)', async () => { |
| 197 | + const boxEnabled = observable.box(false); |
| 198 | + const mobxQuery = new MobxQueryMock({ |
| 199 | + queryFn: ({ queryKey }) => { |
| 200 | + return queryKey[1]; |
| 201 | + }, |
| 202 | + enabled: ({ queryKey }) => queryKey[1], |
| 203 | + options: () => ({ |
| 204 | + queryKey: ['test', boxEnabled.get()] as const, |
| 205 | + }), |
| 206 | + }); |
| 207 | + |
| 208 | + runInAction(() => { |
| 209 | + boxEnabled.set(true); |
| 210 | + }); |
| 211 | + |
| 212 | + expect(mobxQuery.spies.queryFn).toBeCalledTimes(1); |
| 213 | + expect(mobxQuery.spies.queryFn).nthReturnedWith(1, true); |
| 214 | + }); |
| 215 | + }); |
53 | 216 | }); |
0 commit comments