Open History to see the last commit
1import { onScopeDispose, reactive, ref } from 'vue-demi'
2β
3import { flushPromises, simpleFetcher } from './test-utils'
4import { useQuery } from '../useQuery'
5import { parseFilterArgs, useIsFetching } from '../useIsFetching'
6β
7jest.mock('../useQueryClient')
8β
9describe('useIsFetching', () => {
10 test('should properly return isFetching state', async () => {
11 const { isFetching: isFetchingQuery } = useQuery(
12 ['isFetching1'],
13 simpleFetcher,
14 )
15 useQuery(['isFetching2'], 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(['onScopeDispose'], simpleFetcher)
34 const isFetching = useIsFetching()
35β
36 expect(status.value).toStrictEqual('loading')
37 expect(isFetching.value).toStrictEqual(1)
38β
39 await flushPromises()
40β
41 expect(status.value).toStrictEqual('loading')
42 expect(isFetching.value).toStrictEqual(1)
43β
44 await flushPromises()
45β
46 expect(status.value).toStrictEqual('loading')
47 expect(isFetching.value).toStrictEqual(1)
48β
49 onScopeDisposeMock.mockReset()
50 })
51β
52 test('should properly update filters', async () => {
53 const filter = reactive({ stale: false })
54 useQuery(
55 ['isFetching'],
56 () =>
57 new Promise((resolve) => {
58 setTimeout(() => {
59 return resolve('Some data')
60 }, 100)
61 }),
62 )
63 const isFetching = useIsFetching(filter)
64β
65 expect(isFetching.value).toStrictEqual(0)
66β
67 filter.stale = true
68 await flushPromises()
69β
70 expect(isFetching.value).toStrictEqual(1)
71β
72 await flushPromises(100)
73 })
74β
75 describe('parseFilterArgs', () => {
76 test('should default to empty filters', () => {
77 const result = parseFilterArgs(undefined)
78β
79 expect(result).toEqual({})
80 })
81β
82 test('should merge query key with filters', () => {
83 const filters = { stale: true }
84β
85 const result = parseFilterArgs(['key'], filters)
86 const expected = { ...filters, queryKey: ['key'] }
87β
88 expect(result).toEqual(expected)
89 })
90β
91 test('should unwrap refs arguments', () => {
92 const key = ref(['key'])
93 const filters = ref({ stale: ref(true) })
94β
95 const result = parseFilterArgs(key, filters)
96 const expected = { queryKey: ['key'], stale: true }
97β
98 expect(result).toEqual(expected)
99 })
100 })
101})