Skip to content

focusIn, focusOut, and resize events #179

Closed
@sarenji

Description

@sarenji

Describe the feature you'd like:

fireEvent.focusIn and fireEvent.focusOut seem to be missing. I didn't know if there was a reasoning for it, so I wanted to double-check with y'all. Here's the documentation on focusin: https://developer.mozilla.org/en-US/docs/Web/Events/focusin

Additionally, fireEvent.resize is missing. This one is a bit weirder, though, so rather than suggest anything, I'm asking if it belongs in here.

Suggested implementation:

Note that focusIn and focusOut bubble, unlike focus and blur.

// https://github.com/kentcdodds/dom-testing-library/blob/master/src/events.js
const eventMap = {
  // ...
  focusIn: {
    EventType: 'FocusEvent',
    defaultInit: {bubbles: true, cancelable: false},
  },
  focusOut: {
    EventType: 'FocusEvent',
    defaultInit: {bubbles: true, cancelable: false},
  },
  // ...
}

In the case of resize, it's a little weird. You want to call it on a Window. The magic function that gets assigned to all these events tries to find the window via const window = node.ownerDocument.defaultView, which does an angry tableflip if you pass it a window in the first place. So some ceremony can be skipped:

fireEvent.resize = (window) => {
  window.dispatchEvent(new Event('resize'));
};

But seeing as it's a little different than the rest and a one-liner I'm not bothered if y'all think adding resize is a bad idea.

I'm happy to make a PR for either.

Describe alternatives you've considered:

We can just use the low-level version (i.e. just fireEvent) in all cases, but it'd be nice to have the convenience functions for a consistent API.

Teachability, Documentation, Adoption, Migration Strategy:

Documentation is sparse; I think the library just links to src/events.js.

Thoughts? 😄

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions