From 0405f56ffef0bcf719fc8bbed856e33417401acf Mon Sep 17 00:00:00 2001 From: Sebastian Silbermann Date: Tue, 4 Aug 2020 21:13:40 +0200 Subject: [PATCH 1/2] test(fireEvent): Add expected behavior for blur/focus in React (#757) --- src/__tests__/events.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/__tests__/events.js b/src/__tests__/events.js index dc529344..bac063de 100644 --- a/src/__tests__/events.js +++ b/src/__tests__/events.js @@ -211,3 +211,30 @@ test('calling `fireEvent` directly works too', () => { }), ) }) + +test('blur/foucs bubbles in react', () => { + const handleBlur = jest.fn() + const handleBubbledBlur = jest.fn() + const handleFocus = jest.fn() + const handleBubbledFocus = jest.fn() + const {container} = render( +
+
, + ) + const button = container.firstChild.firstChild + + fireEvent.focus(button) + + expect(handleBlur).toHaveBeenCalledTimes(0) + expect(handleBubbledBlur).toHaveBeenCalledTimes(0) + expect(handleFocus).toHaveBeenCalledTimes(1) + expect(handleBubbledFocus).toHaveBeenCalledTimes(1) + + fireEvent.blur(button) + + expect(handleBlur).toHaveBeenCalledTimes(1) + expect(handleBubbledBlur).toHaveBeenCalledTimes(1) + expect(handleFocus).toHaveBeenCalledTimes(1) + expect(handleBubbledFocus).toHaveBeenCalledTimes(1) +}) From 9aac1570d8bccfdf4584b22196cc23a479b47aff Mon Sep 17 00:00:00 2001 From: Sebastian Silbermann Date: Wed, 5 Aug 2020 04:02:17 +0200 Subject: [PATCH 2/2] fix(fireEvent): Make sure react dispatches focus/blur events (#758) * test: Run CI with experimental React * stable -> latest * fix(fireEvent): Make sure react dispatches focus/blur events * Remove todo --- .travis.yml | 11 ++++++----- src/fire-event.js | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index c2f47b7a..eae48ee7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,14 +9,14 @@ node_js: - 14 - node env: - - REACT_NEXT=false - - REACT_NEXT=true + - REACT_DIST=latest + - REACT_DIST=next + - REACT_DIST=experimental install: - npm install # as requested by the React team :) # https://reactjs.org/blog/2019/10/22/react-release-channels.html#using-the-next-channel-for-integration-testing - - if [ "$REACT_NEXT" = true ]; then npm install react@next - react-dom@next; fi + - npm install react@$REACT_DIST react-dom@$REACT_DIST script: - npm run validate - npx codecov@3 @@ -27,7 +27,8 @@ branches: jobs: allow_failures: - - env: REACT_NEXT=true + - REACT_DIST=next + - REACT_DIST=experimental include: - stage: release node_js: 14 diff --git a/src/fire-event.js b/src/fire-event.js index 071aff8a..b4e60928 100644 --- a/src/fire-event.js +++ b/src/fire-event.js @@ -41,4 +41,18 @@ fireEvent.select = (node, init) => { fireEvent.keyUp(node, init) } +// React event system tracks native focusout/focusin events for +// running blur/focus handlers +// @link https://github.com/facebook/react/pull/19186 +const blur = fireEvent.blur +const focus = fireEvent.focus +fireEvent.blur = (...args) => { + fireEvent.focusOut(...args) + return blur(...args) +} +fireEvent.focus = (...args) => { + fireEvent.focusIn(...args) + return focus(...args) +} + export {fireEvent}