Replies: 1 comment 2 replies
-
This feels like a positive change. My greatest curiosity though would be if we could remove the focus requirement as that has been a constant "gotcha". |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Both Cypress and RTL have the concept of "typing" input directly into the keyboard rather than associating input with a single component as we do with our
fillIn()
action. In other words, to simulate filling out a password form in cypress, you could, with the focus on the username field say:https://docs.cypress.io/api/commands/type.html
Similarly in RTL, you would say:
https://github.com/testing-library/user-event#typeelement-text-options
This approach has several advantages. The first is purely practical. It allows you to define keyboard interactions that span input elements. So while we could enhance the
fillIn
helper to generate control character events, it would be weird to submit a login form by "filling in" the just the username field:Some interactions aren't targeted at a field at all. What about if I want to test dismissing a modal dialog by pressing the
ESC
key? In that case, it's doesn't matter which element has focus. Choosing anything other than the current focus to do that feels arbitrary.... and could even fail to catch bugs if the application's key handler is misconfigured and catches key events from the element the test case is dispatching do, but not to catch key events that are dispatched from the document's current natural focus.The second advantage is philosophical in that it is more aligned with what users actually do and how they think. They don't enter text directly into a component. They enter a stream of character inputs into the keyboard which then inserts characters on their behalf onto the component with focus.
What if we had a separate Keybord interactor whose element was whatever happened to have the document focus?
which could then be used as:
This isn't to say that individual components don't have
fillIn
actions, but they can delegate to the keyboard interactor where appropriate:This lets us have the best of both worlds.
Beta Was this translation helpful? Give feedback.
All reactions