1
- import { act , render } from '@testing-library/react'
1
+ import { act , 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
5
6
6
describe ( 'useSWR - loading' , ( ) => {
7
- const loadData = ( ) => new Promise ( res => setTimeout ( ( ) => res ( 'data' ) , 100 ) )
8
-
9
7
it ( 'should return loading state' , async ( ) => {
10
8
let renderCount = 0
11
9
function Page ( ) {
12
- const { data, isValidating } = useSWR ( 'is-validating-1' , loadData )
10
+ const { data, isValidating } = useSWR ( 'is-validating-1' , ( ) =>
11
+ createResponse ( 'data' )
12
+ )
13
13
renderCount ++
14
14
return (
15
15
< div >
@@ -18,11 +18,10 @@ describe('useSWR - loading', () => {
18
18
)
19
19
}
20
20
21
- const { container } = render ( < Page /> )
22
- expect ( container . textContent ) . toMatchInlineSnapshot ( `" hello, , loading"` )
21
+ render ( < Page /> )
22
+ screen . getByText ( ' hello, , loading' )
23
23
24
- await act ( ( ) => sleep ( 110 ) )
25
- expect ( container . textContent ) . toMatchInlineSnapshot ( `"hello, data, ready"` )
24
+ await screen . findByText ( 'hello, data, ready' )
26
25
// data isValidating
27
26
// -> undefined, true
28
27
// -> data, false
@@ -33,17 +32,14 @@ describe('useSWR - loading', () => {
33
32
let renderCount = 0
34
33
function Page ( ) {
35
34
// 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' ) )
37
36
renderCount ++
38
37
return < div > hello, { data } </ div >
39
38
}
40
39
41
- const { container } = render ( < Page /> )
42
-
43
- await act ( ( ) => sleep ( 110 ) )
44
-
45
- expect ( container . textContent ) . toMatchInlineSnapshot ( `"hello, data"` )
40
+ render ( < Page /> )
46
41
42
+ await screen . findByText ( 'hello, data' )
47
43
// data
48
44
// -> undefined
49
45
// -> data
@@ -53,25 +49,22 @@ describe('useSWR - loading', () => {
53
49
it ( 'should avoid extra rerenders while fetching' , async ( ) => {
54
50
let renderCount = 0 ,
55
51
dataLoaded = false
56
- const loadDataWithLog = ( ) =>
57
- new Promise ( res =>
58
- setTimeout ( ( ) => {
59
- dataLoaded = true
60
- res ( 'data' )
61
- } , 100 )
62
- )
63
52
64
53
function Page ( ) {
65
54
// 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
+ } )
67
60
renderCount ++
68
61
return < div > hello</ div >
69
62
}
70
63
71
- const { container } = render ( < Page /> )
72
- expect ( container . textContent ) . toMatchInlineSnapshot ( `" hello"` )
64
+ render ( < Page /> )
65
+ screen . getByText ( ' hello' )
73
66
74
- await act ( ( ) => sleep ( 110 ) ) // wait
67
+ await act ( ( ) => sleep ( 100 ) ) // wait
75
68
// it doesn't re-render, but fetch was triggered
76
69
expect ( renderCount ) . toEqual ( 1 )
77
70
expect ( dataLoaded ) . toEqual ( true )
0 commit comments