Skip to content

Commit 0405f56

Browse files
authored
test(fireEvent): Add expected behavior for blur/focus in React (#757)
1 parent b82773c commit 0405f56

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/__tests__/events.js

+27
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,30 @@ test('calling `fireEvent` directly works too', () => {
211211
}),
212212
)
213213
})
214+
215+
test('blur/foucs bubbles in react', () => {
216+
const handleBlur = jest.fn()
217+
const handleBubbledBlur = jest.fn()
218+
const handleFocus = jest.fn()
219+
const handleBubbledFocus = jest.fn()
220+
const {container} = render(
221+
<div onBlur={handleBubbledBlur} onFocus={handleBubbledFocus}>
222+
<button onBlur={handleBlur} onFocus={handleFocus} />
223+
</div>,
224+
)
225+
const button = container.firstChild.firstChild
226+
227+
fireEvent.focus(button)
228+
229+
expect(handleBlur).toHaveBeenCalledTimes(0)
230+
expect(handleBubbledBlur).toHaveBeenCalledTimes(0)
231+
expect(handleFocus).toHaveBeenCalledTimes(1)
232+
expect(handleBubbledFocus).toHaveBeenCalledTimes(1)
233+
234+
fireEvent.blur(button)
235+
236+
expect(handleBlur).toHaveBeenCalledTimes(1)
237+
expect(handleBubbledBlur).toHaveBeenCalledTimes(1)
238+
expect(handleFocus).toHaveBeenCalledTimes(1)
239+
expect(handleBubbledFocus).toHaveBeenCalledTimes(1)
240+
})

0 commit comments

Comments
 (0)