|
1 | | -import { describe, expect, it } from 'vitest'; |
2 | | -import { MobxQuery } from './mobx-query'; |
3 | | -import { QueryClient } from '@tanstack/query-core'; |
| 1 | +import { DefaultError, QueryClient, QueryKey } from '@tanstack/query-core'; |
| 2 | +import { reaction, when } from 'mobx'; |
| 3 | +import { describe, expect, it, vi } from 'vitest'; |
4 | 4 |
|
| 5 | +import { MobxQuery, MobxQueryConfig } from './mobx-query'; |
5 | 6 |
|
6 | 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 | + } |
| 19 | + } |
| 20 | + |
7 | 21 | it('to be defined', () => { |
8 | | - const mobxQuery = new MobxQuery({ |
9 | | - queryClient: new QueryClient(), |
| 22 | + const mobxQuery = new TestMobxQuery({ |
10 | 23 | queryKey: ['test'], |
11 | | - queryFn: () => {} |
12 | | - }) |
| 24 | + queryFn: () => {}, |
| 25 | + }); |
13 | 26 | expect(mobxQuery).toBeDefined(); |
14 | | - }) |
15 | | -}); |
| 27 | + }); |
| 28 | + |
| 29 | + it('"result" field should be reactive', async () => { |
| 30 | + let counter = 0; |
| 31 | + const mobxQuery = new TestMobxQuery({ |
| 32 | + queryKey: ['test'], |
| 33 | + queryFn: () => { |
| 34 | + return ++counter; |
| 35 | + }, |
| 36 | + }); |
| 37 | + const spy = vi.fn(); |
| 38 | + |
| 39 | + const dispose = reaction( |
| 40 | + () => mobxQuery.result, |
| 41 | + (result) => { |
| 42 | + spy(result.data); |
| 43 | + }, |
| 44 | + ); |
| 45 | + |
| 46 | + await when(() => mobxQuery.result.isLoading); |
| 47 | + |
| 48 | + expect(spy).toBeCalledTimes(1); |
| 49 | + expect(spy).nthCalledWith(1, 1); |
| 50 | + |
| 51 | + dispose(); |
| 52 | + }); |
| 53 | +}); |
0 commit comments