Skip to content

Commit 6ec97eb

Browse files
committed
test(react-query/useIsFetching): simplify 'queryFn' and add 'expect' using 'toBeInTheDocument
1 parent ac5d858 commit 6ec97eb

File tree

1 file changed

+10
-36
lines changed

1 file changed

+10
-36
lines changed

packages/react-query/src/__tests__/useIsFetching.test.tsx

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@ describe('useIsFetching', () => {
3030

3131
useQuery({
3232
queryKey: key,
33-
queryFn: async () => {
34-
await sleep(50)
35-
return 'test'
36-
},
33+
queryFn: () => sleep(50).then(() => 'test'),
3734
enabled: ready,
3835
})
3936

@@ -54,10 +51,8 @@ describe('useIsFetching', () => {
5451
expect(getByText('isFetching: 0')).toBeInTheDocument()
5552

5653
fireEvent.click(getByRole('button', { name: /setReady/i }))
57-
5854
await vi.advanceTimersByTimeAsync(0)
5955
expect(getByText('isFetching: 1')).toBeInTheDocument()
60-
6156
await vi.advanceTimersByTimeAsync(51)
6257
expect(getByText('isFetching: 0')).toBeInTheDocument()
6358
})
@@ -80,21 +75,15 @@ describe('useIsFetching', () => {
8075
function FirstQuery() {
8176
useQuery({
8277
queryKey: key1,
83-
queryFn: async () => {
84-
await sleep(100)
85-
return 'data'
86-
},
78+
queryFn: () => sleep(100).then(() => 'data'),
8779
})
8880
return null
8981
}
9082

9183
function SecondQuery() {
9284
useQuery({
9385
queryKey: key2,
94-
queryFn: async () => {
95-
await sleep(100)
96-
return 'data'
97-
},
86+
queryFn: () => sleep(100).then(() => 'data'),
9887
})
9988
return null
10089
}
@@ -133,21 +122,15 @@ describe('useIsFetching', () => {
133122
function One() {
134123
useQuery({
135124
queryKey: key1,
136-
queryFn: async () => {
137-
await sleep(10)
138-
return 'test'
139-
},
125+
queryFn: () => sleep(10).then(() => 'test'),
140126
})
141127
return null
142128
}
143129

144130
function Two() {
145131
useQuery({
146132
queryKey: key2,
147-
queryFn: async () => {
148-
await sleep(20)
149-
return 'test'
150-
},
133+
queryFn: () => sleep(20).then(() => 'test'),
151134
})
152135
return null
153136
}
@@ -174,15 +157,13 @@ describe('useIsFetching', () => {
174157

175158
const { getByText, getByRole } = renderWithClient(queryClient, <Page />)
176159

177-
getByText('isFetching: 0')
160+
expect(getByText('isFetching: 0')).toBeInTheDocument()
178161

179162
fireEvent.click(getByRole('button', { name: /setStarted/i }))
180-
181163
await vi.advanceTimersByTimeAsync(0)
182-
getByText('isFetching: 1')
183-
164+
expect(getByText('isFetching: 1')).toBeInTheDocument()
184165
await vi.advanceTimersByTimeAsync(11)
185-
getByText('isFetching: 0')
166+
expect(getByText('isFetching: 0')).toBeInTheDocument()
186167

187168
// at no point should we have isFetching: 2
188169
expect(isFetchingArray).toEqual(expect.not.arrayContaining([2]))
@@ -195,10 +176,7 @@ describe('useIsFetching', () => {
195176
function Page() {
196177
useQuery({
197178
queryKey: key,
198-
queryFn: async () => {
199-
await sleep(10)
200-
return 'test'
201-
},
179+
queryFn: () => sleep(10).then(() => 'test'),
202180
})
203181

204182
const isFetching = useIsFetching()
@@ -214,7 +192,6 @@ describe('useIsFetching', () => {
214192

215193
await vi.advanceTimersByTimeAsync(0)
216194
expect(rendered.getByText('isFetching: 1')).toBeInTheDocument()
217-
218195
await vi.advanceTimersByTimeAsync(11)
219196
expect(rendered.getByText('isFetching: 0')).toBeInTheDocument()
220197
})
@@ -227,10 +204,7 @@ describe('useIsFetching', () => {
227204
useQuery(
228205
{
229206
queryKey: key,
230-
queryFn: async () => {
231-
await sleep(10)
232-
return 'test'
233-
},
207+
queryFn: () => sleep(10).then(() => 'test'),
234208
},
235209
queryClient,
236210
)

0 commit comments

Comments
 (0)