1
1
import React from 'react'
2
2
import { render , cleanup } from '../'
3
3
4
- test ( 'cleans up the document' , async ( ) => {
4
+ test ( 'cleans up the document' , ( ) => {
5
5
const spy = jest . fn ( )
6
6
const divId = 'my-div'
7
7
@@ -17,17 +17,17 @@ test('cleans up the document', async () => {
17
17
}
18
18
19
19
render ( < Test /> )
20
- await cleanup ( )
20
+ cleanup ( )
21
21
expect ( document . body ) . toBeEmptyDOMElement ( )
22
22
expect ( spy ) . toHaveBeenCalledTimes ( 1 )
23
23
} )
24
24
25
- test ( 'cleanup does not error when an element is not a child' , async ( ) => {
25
+ test ( 'cleanup does not error when an element is not a child' , ( ) => {
26
26
render ( < div /> , { container : document . createElement ( 'div' ) } )
27
- await cleanup ( )
27
+ cleanup ( )
28
28
} )
29
29
30
- test ( 'cleanup runs effect cleanup functions' , async ( ) => {
30
+ test ( 'cleanup runs effect cleanup functions' , ( ) => {
31
31
const spy = jest . fn ( )
32
32
33
33
const Test = ( ) => {
@@ -37,6 +37,87 @@ test('cleanup runs effect cleanup functions', async () => {
37
37
}
38
38
39
39
render ( < Test /> )
40
- await cleanup ( )
40
+ cleanup ( )
41
41
expect ( spy ) . toHaveBeenCalledTimes ( 1 )
42
42
} )
43
+
44
+ describe ( 'fake timers and missing act warnings' , ( ) => {
45
+ beforeEach ( ( ) => {
46
+ jest . resetAllMocks ( )
47
+ jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => {
48
+ // assert messages explicitly
49
+ } )
50
+ jest . useFakeTimers ( )
51
+ } )
52
+
53
+ afterEach ( ( ) => {
54
+ jest . useRealTimers ( )
55
+ } )
56
+
57
+ test ( 'cleanup does not flush immediates' , ( ) => {
58
+ const microTaskSpy = jest . fn ( )
59
+ function Test ( ) {
60
+ const counter = 1
61
+ const [ , setDeferredCounter ] = React . useState ( null )
62
+ React . useEffect ( ( ) => {
63
+ let cancelled = false
64
+ setImmediate ( ( ) => {
65
+ microTaskSpy ( )
66
+ if ( ! cancelled ) {
67
+ setDeferredCounter ( counter )
68
+ }
69
+ } )
70
+
71
+ return ( ) => {
72
+ cancelled = true
73
+ }
74
+ } , [ counter ] )
75
+
76
+ return null
77
+ }
78
+ render ( < Test /> )
79
+
80
+ cleanup ( )
81
+
82
+ expect ( microTaskSpy ) . toHaveBeenCalledTimes ( 0 )
83
+ // console.error is mocked
84
+ // eslint-disable-next-line no-console
85
+ expect ( console . error ) . toHaveBeenCalledTimes ( 0 )
86
+ } )
87
+
88
+ test ( 'cleanup does not swallow missing act warnings' , ( ) => {
89
+ const deferredStateUpdateSpy = jest . fn ( )
90
+ function Test ( ) {
91
+ const counter = 1
92
+ const [ , setDeferredCounter ] = React . useState ( null )
93
+ React . useEffect ( ( ) => {
94
+ let cancelled = false
95
+ setImmediate ( ( ) => {
96
+ deferredStateUpdateSpy ( )
97
+ if ( ! cancelled ) {
98
+ setDeferredCounter ( counter )
99
+ }
100
+ } )
101
+
102
+ return ( ) => {
103
+ cancelled = true
104
+ }
105
+ } , [ counter ] )
106
+
107
+ return null
108
+ }
109
+ render ( < Test /> )
110
+
111
+ jest . runAllImmediates ( )
112
+ cleanup ( )
113
+
114
+ expect ( deferredStateUpdateSpy ) . toHaveBeenCalledTimes ( 1 )
115
+ // console.error is mocked
116
+ // eslint-disable-next-line no-console
117
+ expect ( console . error ) . toHaveBeenCalledTimes ( 1 )
118
+ // eslint-disable-next-line no-console
119
+ expect ( console . error . mock . calls [ 0 ] [ 0 ] ) . toMatch (
120
+ 'a test was not wrapped in act(...)' ,
121
+ )
122
+ } )
123
+ } )
0 commit comments