Skip to content

test(components/molecule/select): fix flaky onblur test #2397

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 21, 2022

Conversation

alfdocimo
Copy link
Contributor

Category/Component

TASK:

Types of changes

  • πŸͺ² Bug fix (non-breaking change which fixes an issue)
  • ✨ New feature (non-breaking change which adds functionality)
  • πŸ’₯ Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • 🧾 Documentation
  • πŸ“· Demo
  • πŸ§ͺ Test
  • 🧠 Refactor
  • πŸ’„ Styles

Description, Motivation and Context

Avoid weird race condition with userEvent.tab() by firing focusIn and focusOut events

Screenshots - Animations

@github-actions
Copy link

STATEMENTS BRANCHES FUNCTIONS LINES
≍ ≍ 0.45↓ ≍ 0.42↓ ≍ 0.61↓ ≍ 0.61↓
% 71.65 57.36 58.95 73.42
ABS 2659 / 3711 1495 / 2606 484 / 821 2564 / 3492


// Then
sinon.assert.calledOnce(spy)
sinon.assert.called(spy)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't calledOnce a better choice for this kind of test? being it more specific, the test is more precise, IMHO. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! will update this. Thanks! (:

userEvents.click(textBox)
userEvents.tab()
fireEvent.focusIn(textBox)
fireEvent.focusOut(textBox)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://testing-library.com/docs/guide-events

have a look at this... perhaps that's why you're feeling it's a bit flaky on your local environment? πŸ€”

perhaps using imperative focus / blur is more robust:
https://testing-library.com/docs/guide-events#alternatives

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting! Thanks for sharing! πŸ’‘

I have given it a read and updated this with the imperative focus()

Seems like for some reason, calling imperatively blur() does not seem to work as you'd expect πŸ€”
Captura de Pantalla 2022-10-14 a las 10 11 29
Captura de Pantalla 2022-10-14 a las 10 11 08

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh that's sad... πŸ€”

as an interim solution... how do you feel about doing focus first on textBox, as you're already doing, and then on some other element so that it hopefully fires the blur event on the textbox? I know it's a bit hacky but perhaps more robust than the original fireEvent focus and blur.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better use click and tab, Its what user is going to tackle, dont care if it blurs or focus on his/her POV

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, click and tab should be what we aim for, which was my original intention

userEvents.click(textBox)
userEvents.tab()

After trying quite different things, like rendering another component and clicking that component

      const component = props => (
        <>
          <div>
            <button>Hello!</button>
          </div>
          <Component {...props} />
        </>
      )

      const setup = setupEnvironment(component)

I've realized that this behavior happens whether the browser that runs the tests is opened/closed. 🀯

ezgif com-gif-maker (1)

only with fireEvent.blur() it fires the event from the node, regardless of wether the Karma browser is opened or not...

@github-actions
Copy link

STATEMENTS BRANCHES FUNCTIONS LINES
≍ ≍ 0.45↓ ≍ 0.42↓ ≍ 0.61↓ ≍ 0.61↓
% 71.65 57.36 58.95 73.42
ABS 2659 / 3711 1495 / 2606 484 / 821 2564 / 3492

@alfdocimo alfdocimo merged commit af45f3f into master Oct 21, 2022
@alfdocimo alfdocimo deleted the fix/flaky-onblur-test branch October 21, 2022 11:16
@alfdocimo
Copy link
Contributor Author

Merging this PR.
We are aware that there are some issues with focusing in and out of the Karma browser when running the tests. This is why firing an event directly seemed to fix the test. As a temporary workaround we're using fireEvent to trigger the blur event from the node

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants