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