File tree Expand file tree Collapse file tree 2 files changed +25
-4
lines changed Expand file tree Collapse file tree 2 files changed +25
-4
lines changed Original file line number Diff line number Diff line change 44 * single-use custom hooks. Typically those are better tested by testing
55 * the component that is using it.
66 */
7- import { testHook , cleanup } from 'react-testing-library'
7+ import { testHook , act , cleanup } from 'react-testing-library'
88
99import useCounter from '../react-hooks'
1010
@@ -29,7 +29,9 @@ test('provides an `increment` function', () => {
2929 testHook ( ( ) => ( { count, increment} = useCounter ( { step : 2 } ) ) )
3030
3131 expect ( count ) . toBe ( 0 )
32- increment ( )
32+ act ( ( ) => {
33+ increment ( )
34+ } )
3335 expect ( count ) . toBe ( 2 )
3436} )
3537
@@ -38,7 +40,9 @@ test('provides an `decrement` function', () => {
3840 testHook ( ( ) => ( { count, decrement} = useCounter ( { step : 2 } ) ) )
3941
4042 expect ( count ) . toBe ( 0 )
41- decrement ( )
43+ act ( ( ) => {
44+ decrement ( )
45+ } )
4246 expect ( count ) . toBe ( - 2 )
4347} )
4448
@@ -47,6 +51,8 @@ test('accepts a default initial value for `step`', () => {
4751 testHook ( ( ) => ( { count, increment} = useCounter ( { } ) ) )
4852
4953 expect ( count ) . toBe ( 0 )
50- increment ( )
54+ act ( ( ) => {
55+ increment ( )
56+ } )
5157 expect ( count ) . toBe ( 1 )
5258} )
Original file line number Diff line number Diff line change @@ -163,3 +163,18 @@ test('onChange works', () => {
163163 fireEvent . change ( input , { target : { value : 'a' } } )
164164 expect ( handleChange ) . toHaveBeenCalledTimes ( 1 )
165165} )
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