Open History to see the last commit
1import { onScopeDispose, reactive } from 'vue-demi'
2β
3import { flushPromises, simpleFetcher } from './test-utils'
4import { useQuery } from '../useQuery'
5import { useIsFetching } from '../useIsFetching'
6β
7jest.mock('../useQueryClient')
8β
9describe('useIsFetching', () => {
10 test('should properly return isFetching state', async () => {
11 const { isFetching: isFetchingQuery } = useQuery({
12 queryKey: ['isFetching1'],
13 queryFn: simpleFetcher,
14 })
15 useQuery({ queryKey: ['isFetching2'], queryFn: simpleFetcher })
16 const isFetching = useIsFetching()
17β
18 expect(isFetchingQuery.value).toStrictEqual(true)
19 expect(isFetching.value).toStrictEqual(2)
20β
21 await flushPromises()
22β
23 expect(isFetchingQuery.value).toStrictEqual(false)
24 expect(isFetching.value).toStrictEqual(0)
25 })
26β
27 test('should stop listening to changes on onScopeDispose', async () => {
28 const onScopeDisposeMock = onScopeDispose as jest.MockedFunction<
29 typeof onScopeDispose
30 >
31 onScopeDisposeMock.mockImplementation((fn) => fn())
32β
33 const { status } = useQuery({
34 queryKey: ['onScopeDispose'],
35 queryFn: simpleFetcher,
36 })
37 const isFetching = useIsFetching()
38β
39 expect(status.value).toStrictEqual('pending')
40 expect(isFetching.value).toStrictEqual(1)
41β
42 await flushPromises()
43β
44 expect(status.value).toStrictEqual('pending')
45 expect(isFetching.value).toStrictEqual(1)
46β
47 await flushPromises()
48β
49 expect(status.value).toStrictEqual('pending')
50 expect(isFetching.value).toStrictEqual(1)
51β
52 onScopeDisposeMock.mockReset()
53 })
54β
55 test('should properly update filters', async () => {
56 const filter = reactive({ stale: false })
57 useQuery({
58 queryKey: ['isFetching'],
59 queryFn: () =>
60 new Promise((resolve) => {
61 setTimeout(() => {
62 return resolve('Some data')
63 }, 100)
64 }),
65 })
66 const isFetching = useIsFetching(filter)
67β
68 expect(isFetching.value).toStrictEqual(0)
69β
70 filter.stale = true
71 await flushPromises()
72β
73 expect(isFetching.value).toStrictEqual(1)
74β
75 await flushPromises(100)
76 })
77})