File tree 2 files changed +25
-4
lines changed
2 files changed +25
-4
lines changed Original file line number Diff line number Diff line change 4
4
* single-use custom hooks. Typically those are better tested by testing
5
5
* the component that is using it.
6
6
*/
7
- import { testHook , cleanup } from 'react-testing-library'
7
+ import { testHook , act , cleanup } from 'react-testing-library'
8
8
9
9
import useCounter from '../react-hooks'
10
10
@@ -29,7 +29,9 @@ test('provides an `increment` function', () => {
29
29
testHook ( ( ) => ( { count, increment} = useCounter ( { step : 2 } ) ) )
30
30
31
31
expect ( count ) . toBe ( 0 )
32
- increment ( )
32
+ act ( ( ) => {
33
+ increment ( )
34
+ } )
33
35
expect ( count ) . toBe ( 2 )
34
36
} )
35
37
@@ -38,7 +40,9 @@ test('provides an `decrement` function', () => {
38
40
testHook ( ( ) => ( { count, decrement} = useCounter ( { step : 2 } ) ) )
39
41
40
42
expect ( count ) . toBe ( 0 )
41
- decrement ( )
43
+ act ( ( ) => {
44
+ decrement ( )
45
+ } )
42
46
expect ( count ) . toBe ( - 2 )
43
47
} )
44
48
@@ -47,6 +51,8 @@ test('accepts a default initial value for `step`', () => {
47
51
testHook ( ( ) => ( { count, increment} = useCounter ( { } ) ) )
48
52
49
53
expect ( count ) . toBe ( 0 )
50
- increment ( )
54
+ act ( ( ) => {
55
+ increment ( )
56
+ } )
51
57
expect ( count ) . toBe ( 1 )
52
58
} )
Original file line number Diff line number Diff line change @@ -163,3 +163,18 @@ test('onChange works', () => {
163
163
fireEvent . change ( input , { target : { value : 'a' } } )
164
164
expect ( handleChange ) . toHaveBeenCalledTimes ( 1 )
165
165
} )
166
+
167
+ test ( 'calling `fireEvent` directly works too' , ( ) => {
168
+ const handleEvent = jest . fn ( )
169
+ const {
170
+ container : { firstChild : button } ,
171
+ } = render ( < button onClick = { handleEvent } /> )
172
+ fireEvent (
173
+ button ,
174
+ new Event ( 'MouseEvent' , {
175
+ bubbles : true ,
176
+ cancelable : true ,
177
+ button : 0 ,
178
+ } ) ,
179
+ )
180
+ } )
You can’t perform that action at this time.
0 commit comments