Open History to see the last commit
1import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2import { onScopeDispose, reactive, ref } from 'vue-demi'
3import { createKey, sleep } from '@tanstack/query-test-utils'
4import { useQuery } from '../useQuery'
5import { useIsFetching } from '../useIsFetching'
6β
7vi.mock(import('../useQueryClient'))
8β
9describe('useIsFetching', () => {
10 beforeEach(() => {
11 vi.useFakeTimers()
12 })
13β
14 afterEach(() => {
15 vi.useRealTimers()
16 })
17β
18 it('should properly return isFetching state', async () => {
19 const key1 = createKey()
20 const key2 = createKey()
21 const { isFetching: isFetchingQuery } = useQuery({
22 queryKey: key1,
23 queryFn: () => sleep(0).then(() => 'Some data'),
24 })
25 useQuery({
26 queryKey: key2,
27 queryFn: () => sleep(0).then(() => 'Some data'),
28 })
29 const isFetching = useIsFetching()
30β
31 expect(isFetchingQuery.value).toBe(true)
32 expect(isFetching.value).toBe(2)
33 await vi.advanceTimersByTimeAsync(0)
34 expect(isFetchingQuery.value).toBe(false)
35 expect(isFetching.value).toBe(0)
36 })
37β
38 it('should stop listening to changes on onScopeDispose', async () => {
39 const key = createKey()
40 const onScopeDisposeMock = vi.mocked(onScopeDispose)
41 onScopeDisposeMock.mockImplementation((fn) => fn())
42β
43 const { status } = useQuery({
44 queryKey: key,
45 queryFn: () => sleep(0).then(() => 'Some data'),
46 })
47 const isFetching = useIsFetching()
48β
49 expect(status.value).toBe('pending')
50 expect(isFetching.value).toBe(1)
51 await vi.advanceTimersByTimeAsync(0)
52 expect(status.value).toBe('pending')
53 expect(isFetching.value).toBe(1)
54 await vi.advanceTimersByTimeAsync(0)
55 expect(status.value).toBe('pending')
56 expect(isFetching.value).toBe(1)
57β
58 onScopeDisposeMock.mockReset()
59 })
60β
61 it('should properly update filters', async () => {
62 const key = createKey()
63 const filter = reactive({ stale: false, queryKey: key })
64 useQuery({
65 queryKey: key,
66 queryFn: () => sleep(10).then(() => 'Some data'),
67 })
68 const isFetching = useIsFetching(filter)
69β
70 expect(isFetching.value).toBe(0)
71β
72 filter.stale = true
73 await vi.advanceTimersByTimeAsync(0)
74 expect(isFetching.value).toBe(1)
75 })
76β
77 it('should work with options getter and be reactive', async () => {
78 const key = createKey()
79 const staleRef = ref(false)
80 useQuery({
81 queryKey: key,
82 queryFn: () => sleep(10).then(() => 'Some data'),
83 })
84 const isFetching = useIsFetching(() => ({
85 stale: staleRef.value,
86 queryKey: key,
87 }))
88β
89 expect(isFetching.value).toBe(0)
90β
91 staleRef.value = true
92 await vi.advanceTimersByTimeAsync(0)
93 expect(isFetching.value).toBe(1)
94 })
95β
96 it('should warn when used outside of setup function in development mode', () => {
97 vi.stubEnv('NODE_ENV', 'development')
98 const consoleWarnMock = vi
99 .spyOn(console, 'warn')
100 .mockImplementation(() => {})
101β
102 useIsFetching()
103β
104 expect(consoleWarnMock).toHaveBeenCalledWith(
105 'vue-query composable like "useQuery()" should only be used inside a "setup()" function or a running effect scope. They might otherwise lead to memory leaks.',
106 )
107 })
108})