1
- import { act , render , screen , fireEvent } from '@testing-library/react'
1
+ import { act , screen , fireEvent } from '@testing-library/react'
2
2
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'
5
10
6
11
describe ( 'useSWR - configs' , ( ) => {
7
12
it ( 'should read the config fallback from the context' , async ( ) => {
8
13
let value = 0
9
14
const INTERVAL = 100
10
15
const fetcher = ( ) => value ++
16
+ const key = createKey ( )
11
17
12
- function Section ( ) {
13
- const { data } = useSWR ( 'config-0' )
14
- return < div > data: { data } </ div >
15
- }
16
18
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 >
25
21
}
26
- render ( < Page /> )
22
+ renderWithConfig ( < Page /> , {
23
+ fetcher,
24
+ refreshInterval : INTERVAL ,
25
+ dedupingInterval : 0
26
+ } )
27
27
// hydration
28
28
screen . getByText ( 'data:' )
29
29
// mount
@@ -35,23 +35,24 @@ describe('useSWR - configs', () => {
35
35
} )
36
36
37
37
it ( 'should stop revalidations when config.isPaused returns true' , async ( ) => {
38
- const key = 'config-1'
38
+ const key = createKey ( )
39
39
let value = 0
40
40
const fetcher = ( ) => {
41
41
if ( value === 2 ) throw new Error ( )
42
42
return value ++
43
43
}
44
- const revalidate = ( ) => mutate ( key )
44
+ let mutate
45
45
46
46
function Page ( ) {
47
47
const [ paused , setPaused ] = useState ( false )
48
- const { data, error } = useSWR ( key , fetcher , {
48
+ const { data, error, mutate : _mutate } = useSWR ( key , fetcher , {
49
49
revalidateOnMount : true ,
50
50
refreshInterval : 1 ,
51
51
isPaused ( ) {
52
52
return paused
53
53
}
54
54
} )
55
+ mutate = _mutate
55
56
56
57
useEffect ( ( ) => {
57
58
// revalidate on mount and turn to idle
@@ -65,27 +66,27 @@ describe('useSWR - configs', () => {
65
66
)
66
67
}
67
68
68
- render ( < Page /> )
69
+ renderWithConfig ( < Page /> )
69
70
await screen . findByText ( 'data: 0' )
70
71
71
72
// should not be revalidated
72
- await act ( ( ) => revalidate ( ) )
73
+ await act ( ( ) => mutate ( ) )
73
74
screen . getByText ( 'data: 0' )
74
- await act ( ( ) => revalidate ( ) )
75
+ await act ( ( ) => mutate ( ) )
75
76
screen . getByText ( 'data: 0' )
76
77
77
78
// enable isPaused
78
79
fireEvent . click ( screen . getByText ( 'data: 0' ) )
79
80
// should be revalidated
80
- await act ( ( ) => revalidate ( ) )
81
+ await act ( ( ) => mutate ( ) )
81
82
screen . getByText ( 'data: 1' )
82
83
83
84
// disable isPaused
84
85
fireEvent . click ( screen . getByText ( 'data: 1' ) )
85
86
// should not be revalidated
86
- await act ( ( ) => revalidate ( ) )
87
+ await act ( ( ) => mutate ( ) )
87
88
screen . getByText ( 'data: 1' )
88
- await act ( ( ) => revalidate ( ) )
89
+ await act ( ( ) => mutate ( ) )
89
90
screen . getByText ( 'data: 1' )
90
91
} )
91
92
@@ -101,7 +102,7 @@ describe('useSWR - configs', () => {
101
102
return null
102
103
}
103
104
104
- render ( < Page /> )
105
+ renderWithGlobalCache ( < Page /> )
105
106
expect ( SWRConfig . default ) . toEqual ( config )
106
107
} )
107
108
@@ -118,7 +119,7 @@ describe('useSWR - configs', () => {
118
119
const middleware2 : Middleware = useSWRNext => ( k , f , c ) =>
119
120
useSWRNext ( k , f , c )
120
121
121
- render (
122
+ renderWithConfig (
122
123
< SWRConfig
123
124
value = { {
124
125
dedupingInterval : 1 ,
0 commit comments