Skip to content

Commit 78a7ba6

Browse files
committed
test: refactor config tests
1 parent df16c89 commit 78a7ba6

File tree

2 files changed

+38
-39
lines changed

2 files changed

+38
-39
lines changed

test/use-swr-config-callbacks.test.tsx

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
1-
import { act, render, screen, fireEvent } from '@testing-library/react'
1+
import { act, screen, fireEvent } from '@testing-library/react'
22
import React from 'react'
33
import useSWR from 'swr'
4-
import { sleep, createResponse } from './utils'
4+
import { sleep, createResponse, renderWithConfig, createKey } from './utils'
55

66
describe('useSWR - config callbacks', () => {
77
it('should trigger the onSuccess event with the latest version of the onSuccess callback', async () => {
88
let state = null
99
let count = 0
10-
10+
const key = createKey()
1111
function Page(props: { text: string }) {
12-
const { data, mutate } = useSWR(
13-
'config callbacks - onSuccess',
14-
() => createResponse(count++),
15-
{ onSuccess: () => (state = props.text) }
16-
)
12+
const { data, mutate } = useSWR(key, () => createResponse(count++), {
13+
onSuccess: () => (state = props.text)
14+
})
1715
return (
1816
<div onClick={() => mutate()}>
1917
hello, {data}, {props.text}
2018
</div>
2119
)
2220
}
23-
const { rerender } = render(<Page text={'a'} />)
21+
const { rerender } = renderWithConfig(<Page text={'a'} />)
2422
// the onSuccess callback does not trigger yet, the state still null.
2523
screen.getByText('hello, , a')
2624
expect(state).toEqual(null)
@@ -64,7 +62,7 @@ describe('useSWR - config callbacks', () => {
6462
)
6563
}
6664

67-
const { rerender } = render(<Page text="a" />)
65+
const { rerender } = renderWithConfig(<Page text="a" />)
6866

6967
screen.getByText('hello, , a')
7068
expect(state).toEqual(null)
@@ -107,7 +105,7 @@ describe('useSWR - config callbacks', () => {
107105
)
108106
}
109107

110-
const { rerender } = render(<Page text="a" />)
108+
const { rerender } = renderWithConfig(<Page text="a" />)
111109
screen.getByText('hello, , a')
112110
expect(state).toEqual(null)
113111

@@ -154,7 +152,7 @@ describe('useSWR - config callbacks', () => {
154152
)
155153
}
156154

157-
const { rerender } = render(<Page text="a" />)
155+
const { rerender } = renderWithConfig(<Page text="a" />)
158156

159157
screen.getByText('hello, , a')
160158
expect(state).toEqual(null)

test/use-swr-config.test.tsx

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
1-
import { act, render, screen, fireEvent } from '@testing-library/react'
1+
import { act, screen, fireEvent } from '@testing-library/react'
22
import React, { useEffect, useState } from 'react'
3-
import useSWR, { mutate, SWRConfig, useSWRConfig, Middleware } from 'swr'
4-
import { sleep } from './utils'
3+
import useSWR, { SWRConfig, useSWRConfig, Middleware } from 'swr'
4+
import {
5+
sleep,
6+
renderWithConfig,
7+
createKey,
8+
renderWithGlobalCache
9+
} from './utils'
510

611
describe('useSWR - configs', () => {
712
it('should read the config fallback from the context', async () => {
813
let value = 0
914
const INTERVAL = 100
1015
const fetcher = () => value++
16+
const key = createKey()
1117

12-
function Section() {
13-
const { data } = useSWR('config-0')
14-
return <div>data: {data}</div>
15-
}
1618
function Page() {
17-
// config provider
18-
return (
19-
<SWRConfig
20-
value={{ fetcher, refreshInterval: INTERVAL, dedupingInterval: 0 }}
21-
>
22-
<Section />
23-
</SWRConfig>
24-
)
19+
const { data } = useSWR(key)
20+
return <div>data: {data}</div>
2521
}
26-
render(<Page />)
22+
renderWithConfig(<Page />, {
23+
fetcher,
24+
refreshInterval: INTERVAL,
25+
dedupingInterval: 0
26+
})
2727
// hydration
2828
screen.getByText('data:')
2929
// mount
@@ -35,23 +35,24 @@ describe('useSWR - configs', () => {
3535
})
3636

3737
it('should stop revalidations when config.isPaused returns true', async () => {
38-
const key = 'config-1'
38+
const key = createKey()
3939
let value = 0
4040
const fetcher = () => {
4141
if (value === 2) throw new Error()
4242
return value++
4343
}
44-
const revalidate = () => mutate(key)
44+
let mutate
4545

4646
function Page() {
4747
const [paused, setPaused] = useState(false)
48-
const { data, error } = useSWR(key, fetcher, {
48+
const { data, error, mutate: _mutate } = useSWR(key, fetcher, {
4949
revalidateOnMount: true,
5050
refreshInterval: 1,
5151
isPaused() {
5252
return paused
5353
}
5454
})
55+
mutate = _mutate
5556

5657
useEffect(() => {
5758
// revalidate on mount and turn to idle
@@ -65,27 +66,27 @@ describe('useSWR - configs', () => {
6566
)
6667
}
6768

68-
render(<Page />)
69+
renderWithConfig(<Page />)
6970
await screen.findByText('data: 0')
7071

7172
// should not be revalidated
72-
await act(() => revalidate())
73+
await act(() => mutate())
7374
screen.getByText('data: 0')
74-
await act(() => revalidate())
75+
await act(() => mutate())
7576
screen.getByText('data: 0')
7677

7778
// enable isPaused
7879
fireEvent.click(screen.getByText('data: 0'))
7980
// should be revalidated
80-
await act(() => revalidate())
81+
await act(() => mutate())
8182
screen.getByText('data: 1')
8283

8384
// disable isPaused
8485
fireEvent.click(screen.getByText('data: 1'))
8586
// should not be revalidated
86-
await act(() => revalidate())
87+
await act(() => mutate())
8788
screen.getByText('data: 1')
88-
await act(() => revalidate())
89+
await act(() => mutate())
8990
screen.getByText('data: 1')
9091
})
9192

@@ -101,7 +102,7 @@ describe('useSWR - configs', () => {
101102
return null
102103
}
103104

104-
render(<Page />)
105+
renderWithGlobalCache(<Page />)
105106
expect(SWRConfig.default).toEqual(config)
106107
})
107108

@@ -118,7 +119,7 @@ describe('useSWR - configs', () => {
118119
const middleware2: Middleware = useSWRNext => (k, f, c) =>
119120
useSWRNext(k, f, c)
120121

121-
render(
122+
renderWithConfig(
122123
<SWRConfig
123124
value={{
124125
dedupingInterval: 1,

0 commit comments

Comments
 (0)