1
1
import { act , fireEvent , render , screen } from '@testing-library/react'
2
2
import React from 'react'
3
3
import useSWR from '../src'
4
- import { sleep } from './utils'
4
+ import { createResponse , sleep } from './utils'
5
+
6
+ const waitForNextTick = ( ) => act ( ( ) => sleep ( 1 ) )
5
7
6
8
describe ( 'useSWR - revalidate' , ( ) => {
7
9
it ( 'should rerender after triggering revalidation' , async ( ) => {
@@ -11,18 +13,18 @@ describe('useSWR - revalidate', () => {
11
13
const { data, revalidate } = useSWR ( 'dynamic-3' , ( ) => value ++ )
12
14
return < button onClick = { revalidate } > data: { data } </ button >
13
15
}
14
- const { container } = render ( < Page /> )
16
+
17
+ render ( < Page /> )
15
18
16
19
// hydration
17
- expect ( container . firstChild . textContent ) . toMatchInlineSnapshot ( `"data: "` )
20
+ screen . getByText ( 'data:' )
21
+
18
22
// mount
19
23
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' )
26
28
} )
27
29
28
30
it ( 'should revalidate all the hooks with the same key' , async ( ) => {
@@ -37,63 +39,55 @@ describe('useSWR - revalidate', () => {
37
39
</ button >
38
40
)
39
41
}
40
- const { container } = render ( < Page /> )
42
+
43
+ render ( < Page /> )
41
44
42
45
// hydration
43
- expect ( container . firstChild . textContent ) . toMatchInlineSnapshot ( `", "` )
46
+ screen . getByText ( ',' )
47
+
44
48
// mount
45
49
await screen . findByText ( '0, 0' )
46
50
47
- fireEvent . click ( container . firstElementChild )
51
+ fireEvent . click ( screen . getByText ( '0, 0' ) )
48
52
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' )
54
55
} )
55
56
56
57
it ( 'should respect sequences of revalidation calls (cope with race condition)' , async ( ) => {
57
58
let faster = false
58
59
59
60
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 } )
67
63
)
68
64
69
- return < button onClick = { revalidate } > { data } </ button >
65
+ return < button onClick = { revalidate } > data: { data } </ button >
70
66
}
71
67
72
- const { container } = render ( < Page /> )
68
+ render ( < Page /> )
73
69
74
- expect ( container . firstChild . textContent ) . toMatchInlineSnapshot ( `""` )
70
+ // hydration
71
+ screen . getByText ( 'data:' )
75
72
76
73
// trigger the slower revalidation
77
74
faster = false
78
- fireEvent . click ( container . firstElementChild )
75
+ fireEvent . click ( screen . getByText ( 'data:' ) )
79
76
80
- await act ( async ( ) => sleep ( 10 ) )
77
+ await waitForNextTick ( )
81
78
// trigger the faster revalidation
82
79
faster = true
83
- fireEvent . click ( container . firstElementChild )
80
+ fireEvent . click ( screen . getByText ( 'data:' ) )
84
81
85
- await act ( async ( ) => sleep ( 210 ) )
86
- expect ( container . firstChild . textContent ) . toMatchInlineSnapshot ( `"1"` )
82
+ await act ( async ( ) => sleep ( 150 ) )
83
+ screen . getByText ( 'data: 1' )
87
84
} )
88
85
89
86
it ( 'should keep isValidating be true when there are two concurrent requests' , async ( ) => {
90
87
function Page ( ) {
91
88
const { isValidating, revalidate } = useSWR (
92
89
'keep isValidating for concurrent requests' ,
93
- ( ) =>
94
- new Promise ( res => {
95
- setTimeout ( res , 200 )
96
- } ) ,
90
+ ( ) => createResponse ( null , { delay : 100 } ) ,
97
91
{ revalidateOnMount : false }
98
92
)
99
93
@@ -102,43 +96,44 @@ describe('useSWR - revalidate', () => {
102
96
)
103
97
}
104
98
105
- const { container } = render ( < Page /> )
106
- expect ( container . firstChild . textContent ) . toMatchInlineSnapshot ( `" false"` )
99
+ render ( < Page /> )
100
+ screen . getByText ( ' false' )
107
101
108
102
// 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' )
112
106
113
- fireEvent . click ( container . firstElementChild )
114
- await act ( ( ) => sleep ( 110 ) )
107
+ fireEvent . click ( screen . getByText ( 'true' ) )
108
+ await act ( ( ) => sleep ( 70 ) )
115
109
// first revalidation is over, second revalidation is still in progress
116
- expect ( container . firstChild . textContent ) . toMatchInlineSnapshot ( `" true"` )
110
+ screen . getByText ( ' true' )
117
111
118
- await act ( ( ) => sleep ( 100 ) )
119
- expect ( container . firstChild . textContent ) . toMatchInlineSnapshot ( `" false"` )
112
+ await act ( ( ) => sleep ( 70 ) )
113
+ screen . getByText ( ' false' )
120
114
} )
121
115
122
116
it ( 'should respect sequences of revalidation calls although in dedupingInterval' , async ( ) => {
123
117
let count = 0
124
118
function Page ( ) {
125
119
const { data, revalidate } = useSWR (
126
120
'respect sequences of revalidation calls although in dedupingInterval' ,
127
- async ( ) => {
121
+ ( ) => {
128
122
const currCount = ++ count
129
- await sleep ( currCount === 1 ? 60 : 0 )
130
- return currCount
123
+ return createResponse ( currCount , { delay : currCount === 1 ? 50 : 0 } )
131
124
} ,
132
125
{
133
126
dedupingInterval : 30
134
127
}
135
128
)
136
129
return < div onClick = { ( ) => revalidate ( ) } > count: { data } </ div >
137
130
}
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' )
143
138
} )
144
139
} )
0 commit comments