Skip to content

Commit c2e8330

Browse files
sukvvonmanudeli
andauthored
test(solid-query/useIsFetching): simplify 'queryFn' and add 'expect' for 'isFetching' state transitions (#9480)
Co-authored-by: Jonghyeon Ko <[email protected]>
1 parent 9376835 commit c2e8330

File tree

1 file changed

+21
-32
lines changed

1 file changed

+21
-32
lines changed

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

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ describe('useIsFetching', () => {
2828

2929
function IsFetching() {
3030
const isFetching = useIsFetching()
31+
3132
return <div>isFetching: {isFetching()}</div>
3233
}
3334

@@ -36,10 +37,7 @@ describe('useIsFetching', () => {
3637

3738
useQuery(() => ({
3839
queryKey: key,
39-
queryFn: async () => {
40-
await sleep(50)
41-
return 'test'
42-
},
40+
queryFn: () => sleep(50).then(() => 'test'),
4341
enabled: ready(),
4442
}))
4543

@@ -62,8 +60,8 @@ describe('useIsFetching', () => {
6260
))
6361

6462
expect(rendered.getByText('isFetching: 0')).toBeInTheDocument()
63+
6564
fireEvent.click(rendered.getByRole('button', { name: /setReady/i }))
66-
await vi.advanceTimersByTimeAsync(0)
6765
expect(rendered.getByText('isFetching: 1')).toBeInTheDocument()
6866
await vi.advanceTimersByTimeAsync(50)
6967
expect(rendered.getByText('isFetching: 0')).toBeInTheDocument()
@@ -80,31 +78,29 @@ describe('useIsFetching', () => {
8078

8179
function IsFetching() {
8280
const isFetching = useIsFetching()
81+
8382
createRenderEffect(() => {
8483
isFetchingArray.push(isFetching())
8584
})
85+
8686
return null
8787
}
8888

8989
function FirstQuery() {
9090
useQuery(() => ({
9191
queryKey: key1,
92-
queryFn: async () => {
93-
await sleep(150)
94-
return 'data'
95-
},
92+
queryFn: () => sleep(150).then(() => 'data'),
9693
}))
94+
9795
return null
9896
}
9997

10098
function SecondQuery() {
10199
useQuery(() => ({
102100
queryKey: key2,
103-
queryFn: async () => {
104-
await sleep(200)
105-
return 'data'
106-
},
101+
queryFn: () => sleep(200).then(() => 'data'),
107102
}))
103+
108104
return null
109105
}
110106

@@ -133,8 +129,10 @@ describe('useIsFetching', () => {
133129
<Page />
134130
</QueryClientProvider>
135131
))
132+
136133
// unlike react, Updating renderSecond wont cause a rerender for FirstQuery
137134
await vi.advanceTimersByTimeAsync(300)
135+
138136
expect(isFetchingArray).toEqual([0, 1, 2, 1, 0])
139137
})
140138

@@ -148,22 +146,18 @@ describe('useIsFetching', () => {
148146
function One() {
149147
useQuery(() => ({
150148
queryKey: key1,
151-
queryFn: async () => {
152-
await sleep(10)
153-
return 'test'
154-
},
149+
queryFn: () => sleep(10).then(() => 'test'),
155150
}))
151+
156152
return null
157153
}
158154

159155
function Two() {
160156
useQuery(() => ({
161157
queryKey: key2,
162-
queryFn: async () => {
163-
await sleep(20)
164-
return 'test'
165-
},
158+
queryFn: () => sleep(20).then(() => 'test'),
166159
}))
160+
167161
return null
168162
}
169163

@@ -198,11 +192,12 @@ describe('useIsFetching', () => {
198192
))
199193

200194
expect(rendered.getByText('isFetching: 0')).toBeInTheDocument()
195+
201196
fireEvent.click(rendered.getByRole('button', { name: /setStarted/i }))
202-
await vi.advanceTimersByTimeAsync(0)
203197
expect(rendered.getByText('isFetching: 1')).toBeInTheDocument()
204198
await vi.advanceTimersByTimeAsync(20)
205199
expect(rendered.getByText('isFetching: 0')).toBeInTheDocument()
200+
206201
// at no point should we have isFetching: 2
207202
expect(isFetchingArray).toEqual(expect.not.arrayContaining([2]))
208203
})
@@ -214,10 +209,7 @@ describe('useIsFetching', () => {
214209
function Page() {
215210
useQuery(() => ({
216211
queryKey: key,
217-
queryFn: async () => {
218-
await sleep(10)
219-
return 'test'
220-
},
212+
queryFn: () => sleep(10).then(() => 'test'),
221213
}))
222214

223215
const isFetching = useIsFetching()
@@ -235,7 +227,6 @@ describe('useIsFetching', () => {
235227
</QueryClientProvider>
236228
))
237229

238-
await vi.advanceTimersByTimeAsync(0)
239230
expect(rendered.getByText('isFetching: 1')).toBeInTheDocument()
240231
await vi.advanceTimersByTimeAsync(10)
241232
expect(rendered.getByText('isFetching: 0')).toBeInTheDocument()
@@ -249,10 +240,7 @@ describe('useIsFetching', () => {
249240
useQuery(
250241
() => ({
251242
queryKey: key,
252-
queryFn: async () => {
253-
await sleep(10)
254-
return 'test'
255-
},
243+
queryFn: () => sleep(10).then(() => 'test'),
256244
}),
257245
() => queryClient,
258246
)
@@ -268,7 +256,8 @@ describe('useIsFetching', () => {
268256

269257
const rendered = render(() => <Page></Page>)
270258

271-
await vi.advanceTimersByTimeAsync(0)
272259
expect(rendered.getByText('isFetching: 1')).toBeInTheDocument()
260+
await vi.advanceTimersByTimeAsync(10)
261+
expect(rendered.getByText('isFetching: 0')).toBeInTheDocument()
273262
})
274263
})

0 commit comments

Comments
 (0)