Skip to content

Commit a64d7a1

Browse files
committed
test: refactor use-swr-loading.test.tsx
1 parent 4f9299c commit a64d7a1

File tree

1 file changed

+19
-26
lines changed

1 file changed

+19
-26
lines changed

test/use-swr-loading.test.tsx

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import { act, render } from '@testing-library/react'
1+
import { act, render, screen } from '@testing-library/react'
22
import React from 'react'
33
import useSWR from '../src'
4-
import { sleep } from './utils'
4+
import { createResponse, sleep } from './utils'
55

66
describe('useSWR - loading', () => {
7-
const loadData = () => new Promise(res => setTimeout(() => res('data'), 100))
8-
97
it('should return loading state', async () => {
108
let renderCount = 0
119
function Page() {
12-
const { data, isValidating } = useSWR('is-validating-1', loadData)
10+
const { data, isValidating } = useSWR('is-validating-1', () =>
11+
createResponse('data')
12+
)
1313
renderCount++
1414
return (
1515
<div>
@@ -18,11 +18,10 @@ describe('useSWR - loading', () => {
1818
)
1919
}
2020

21-
const { container } = render(<Page />)
22-
expect(container.textContent).toMatchInlineSnapshot(`"hello, , loading"`)
21+
render(<Page />)
22+
screen.getByText('hello, , loading')
2323

24-
await act(() => sleep(110))
25-
expect(container.textContent).toMatchInlineSnapshot(`"hello, data, ready"`)
24+
await screen.findByText('hello, data, ready')
2625
// data isValidating
2726
// -> undefined, true
2827
// -> data, false
@@ -33,17 +32,14 @@ describe('useSWR - loading', () => {
3332
let renderCount = 0
3433
function Page() {
3534
// we never access `isValidating`, so it will not trigger rerendering
36-
const { data } = useSWR('is-validating-2', loadData)
35+
const { data } = useSWR('is-validating-2', () => createResponse('data'))
3736
renderCount++
3837
return <div>hello, {data}</div>
3938
}
4039

41-
const { container } = render(<Page />)
42-
43-
await act(() => sleep(110))
44-
45-
expect(container.textContent).toMatchInlineSnapshot(`"hello, data"`)
40+
render(<Page />)
4641

42+
await screen.findByText('hello, data')
4743
// data
4844
// -> undefined
4945
// -> data
@@ -53,25 +49,22 @@ describe('useSWR - loading', () => {
5349
it('should avoid extra rerenders while fetching', async () => {
5450
let renderCount = 0,
5551
dataLoaded = false
56-
const loadDataWithLog = () =>
57-
new Promise(res =>
58-
setTimeout(() => {
59-
dataLoaded = true
60-
res('data')
61-
}, 100)
62-
)
6352

6453
function Page() {
6554
// we never access anything
66-
useSWR('is-validating-3', loadDataWithLog)
55+
useSWR('is-validating-3', async () => {
56+
const res = await createResponse('data')
57+
dataLoaded = true
58+
return res
59+
})
6760
renderCount++
6861
return <div>hello</div>
6962
}
7063

71-
const { container } = render(<Page />)
72-
expect(container.textContent).toMatchInlineSnapshot(`"hello"`)
64+
render(<Page />)
65+
screen.getByText('hello')
7366

74-
await act(() => sleep(110)) // wait
67+
await act(() => sleep(100)) // wait
7568
// it doesn't re-render, but fetch was triggered
7669
expect(renderCount).toEqual(1)
7770
expect(dataLoaded).toEqual(true)

0 commit comments

Comments
 (0)