Skip to content

Commit d1edba8

Browse files
author
Kent C. Dodds
committed
test: fix test coverage and testHook tests
Sorry @donavon, this is the best we can do I think :-(
1 parent f91faa9 commit d1edba8

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

examples/__tests__/react-hooks.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
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

99
import 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
})

src/__tests__/events.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
})

0 commit comments

Comments
 (0)