Skip to content

Commit 061bb3b

Browse files
committed
test: refactor use-swr-revalidate.test.tsx
1 parent e73d9fe commit 061bb3b

File tree

1 file changed

+49
-54
lines changed

1 file changed

+49
-54
lines changed

test/use-swr-revalidate.test.tsx

Lines changed: 49 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { act, fireEvent, 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'
5+
6+
const waitForNextTick = () => act(() => sleep(1))
57

68
describe('useSWR - revalidate', () => {
79
it('should rerender after triggering revalidation', async () => {
@@ -11,18 +13,18 @@ describe('useSWR - revalidate', () => {
1113
const { data, revalidate } = useSWR('dynamic-3', () => value++)
1214
return <button onClick={revalidate}>data: {data}</button>
1315
}
14-
const { container } = render(<Page />)
16+
17+
render(<Page />)
1518

1619
// hydration
17-
expect(container.firstChild.textContent).toMatchInlineSnapshot(`"data: "`)
20+
screen.getByText('data:')
21+
1822
// mount
1923
await screen.findByText('data: 0')
20-
fireEvent.click(container.firstElementChild)
21-
await act(() => {
22-
// trigger revalidation
23-
return sleep(1)
24-
})
25-
expect(container.firstChild.textContent).toMatchInlineSnapshot(`"data: 1"`)
24+
25+
fireEvent.click(screen.getByText('data: 0'))
26+
await waitForNextTick()
27+
screen.getByText('data: 1')
2628
})
2729

2830
it('should revalidate all the hooks with the same key', async () => {
@@ -37,63 +39,55 @@ describe('useSWR - revalidate', () => {
3739
</button>
3840
)
3941
}
40-
const { container } = render(<Page />)
42+
43+
render(<Page />)
4144

4245
// hydration
43-
expect(container.firstChild.textContent).toMatchInlineSnapshot(`", "`)
46+
screen.getByText(',')
47+
4448
// mount
4549
await screen.findByText('0, 0')
4650

47-
fireEvent.click(container.firstElementChild)
51+
fireEvent.click(screen.getByText('0, 0'))
4852

49-
await act(() => {
50-
// trigger revalidation
51-
return sleep(1)
52-
})
53-
expect(container.firstChild.textContent).toMatchInlineSnapshot(`"1, 1"`)
53+
await waitForNextTick()
54+
screen.getByText('1, 1')
5455
})
5556

5657
it('should respect sequences of revalidation calls (cope with race condition)', async () => {
5758
let faster = false
5859

5960
function Page() {
60-
const { data, revalidate } = useSWR(
61-
'race',
62-
() =>
63-
new Promise(res => {
64-
const value = faster ? 1 : 0
65-
setTimeout(() => res(value), faster ? 100 : 200)
66-
})
61+
const { data, revalidate } = useSWR('race', () =>
62+
createResponse(faster ? 1 : 0, { delay: faster ? 50 : 100 })
6763
)
6864

69-
return <button onClick={revalidate}>{data}</button>
65+
return <button onClick={revalidate}>data: {data}</button>
7066
}
7167

72-
const { container } = render(<Page />)
68+
render(<Page />)
7369

74-
expect(container.firstChild.textContent).toMatchInlineSnapshot(`""`)
70+
// hydration
71+
screen.getByText('data:')
7572

7673
// trigger the slower revalidation
7774
faster = false
78-
fireEvent.click(container.firstElementChild)
75+
fireEvent.click(screen.getByText('data:'))
7976

80-
await act(async () => sleep(10))
77+
await waitForNextTick()
8178
// trigger the faster revalidation
8279
faster = true
83-
fireEvent.click(container.firstElementChild)
80+
fireEvent.click(screen.getByText('data:'))
8481

85-
await act(async () => sleep(210))
86-
expect(container.firstChild.textContent).toMatchInlineSnapshot(`"1"`)
82+
await act(async () => sleep(150))
83+
screen.getByText('data: 1')
8784
})
8885

8986
it('should keep isValidating be true when there are two concurrent requests', async () => {
9087
function Page() {
9188
const { isValidating, revalidate } = useSWR(
9289
'keep isValidating for concurrent requests',
93-
() =>
94-
new Promise(res => {
95-
setTimeout(res, 200)
96-
}),
90+
() => createResponse(null, { delay: 100 }),
9791
{ revalidateOnMount: false }
9892
)
9993

@@ -102,43 +96,44 @@ describe('useSWR - revalidate', () => {
10296
)
10397
}
10498

105-
const { container } = render(<Page />)
106-
expect(container.firstChild.textContent).toMatchInlineSnapshot(`"false"`)
99+
render(<Page />)
100+
screen.getByText('false')
107101

108102
// trigger the first revalidation
109-
fireEvent.click(container.firstElementChild)
110-
await act(() => sleep(100))
111-
expect(container.firstChild.textContent).toMatchInlineSnapshot(`"true"`)
103+
fireEvent.click(screen.getByText('false'))
104+
await act(() => sleep(50))
105+
screen.getByText('true')
112106

113-
fireEvent.click(container.firstElementChild)
114-
await act(() => sleep(110))
107+
fireEvent.click(screen.getByText('true'))
108+
await act(() => sleep(70))
115109
// first revalidation is over, second revalidation is still in progress
116-
expect(container.firstChild.textContent).toMatchInlineSnapshot(`"true"`)
110+
screen.getByText('true')
117111

118-
await act(() => sleep(100))
119-
expect(container.firstChild.textContent).toMatchInlineSnapshot(`"false"`)
112+
await act(() => sleep(70))
113+
screen.getByText('false')
120114
})
121115

122116
it('should respect sequences of revalidation calls although in dedupingInterval', async () => {
123117
let count = 0
124118
function Page() {
125119
const { data, revalidate } = useSWR(
126120
'respect sequences of revalidation calls although in dedupingInterval',
127-
async () => {
121+
() => {
128122
const currCount = ++count
129-
await sleep(currCount === 1 ? 60 : 0)
130-
return currCount
123+
return createResponse(currCount, { delay: currCount === 1 ? 50 : 0 })
131124
},
132125
{
133126
dedupingInterval: 30
134127
}
135128
)
136129
return <div onClick={() => revalidate()}>count: {data}</div>
137130
}
138-
const { container } = render(<Page />)
139-
await act(() => sleep(10))
140-
fireEvent.click(container.firstElementChild)
141-
await act(() => sleep(60))
142-
expect(container.firstChild.textContent).toMatchInlineSnapshot('"count: 2"')
131+
132+
render(<Page />)
133+
134+
await waitForNextTick()
135+
fireEvent.click(screen.getByText('count:'))
136+
await act(() => sleep(70))
137+
screen.getByText('count: 2')
143138
})
144139
})

0 commit comments

Comments
 (0)