Skip to content
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

feat: Allow null type for node on fireEvent and its shortcuts #1307

Closed
wants to merge 1 commit into from

Conversation

jdufresne
Copy link
Contributor

What:

Allow the null type for node argument on fireEvent and its shortcuts.

Why:

A common pattern in some testing code is:

const btn = document.getElementById('my-button')
fireEvent.click(btn)

However, document.getElementById() may return null and then TypeScript will report an error:

error TS2345: Argument of type 'HTMLElement | null' is not assignable to parameter of type 'Document | Node | Element | Window'.

To resolve the TypeScript error, library users can assert or check the returned value with something like:

const btn = document.getElementById('my-button')
if (!btn) {
   throw new Error("Unable to find 'my-button')
}
fireEvent.click(btn)

But dom-testing-library is already doing this check, so let fireEvent accept null as a convenience to call sites.

The error already reported by dom-testing-library is:

Unable to fire a "click" event - please provide a DOM element.

Some might suggest that using document.getElementById() should be replaced by a dom-testing-library query method, but that is non-trivial on legacy codebases with many tests trying to adopt and introduce dom-testing-library.

How:

Augmented the types of fireEvent & friends to allow null for the node argument.

Checklist:

  • Documentation added to the
    docs site N/A
  • Tests
  • TypeScript definitions updated
  • Ready to be merged

A common pattern in some testing code is:

```js
const btn = document.getElementById('my-button')
fireEvent.click(btn)
```

However, `document.getElementById()` may return null and then TypeScript
will report an error:

```
error TS2345: Argument of type 'HTMLElement | null' is not assignable to parameter of type 'Document | Node | Element | Window'.
```

To resolve the TypeScript error, user can assert or check the returned
value wit something like:

```js
const btn = document.getElementById('my-button')
if (!btn) {
   throw new Error("Unable to find 'my-button')
}
fireEvent.click(btn)
```

But dom-testing-library is already doing this check, so let fireEvent
accept null as a convenience to call sites.

The error already reported by dom-testing-library is:

```
Unable to fire a "click" event - please provide a DOM element.
```
Copy link

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit ff95b7a:

Sandbox Source
react-testing-library-examples Configuration

Copy link
Member

@eps1lon eps1lon left a comment

Choose a reason for hiding this comment

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

It should error at compile time since that's also a runtime error. If you know getElementById(elementID) returns an element, use non-null assertions instead: fireEvent.click(getElementById(elementID)!)

@jdufresne
Copy link
Contributor Author

For me, that runtime error is desired as that gets converted into a test failure by Jest.

Using the TypeScript ! is an escape hatch to bypass type checking. Generally speaking, I try to avoid these escape hatches as much as possible and instead adjust the types to better reflect the state and capability of things.

In this case, the fireEvent function is prepared to handle null. True, it handles it by throwing an error, but it is handled none-the-less and so, IMO, isn't invalid input from a typing perspective.

My goal is to reduce the burden on the call site within tests as the same expected outcome happens: either continue with the non-null value or fail the test. Asking every call site to add ! reintroduces a burden (albeit a small one).

If this proposal is against the principles of this library, no worries, feel free to close.

@eps1lon
Copy link
Member

eps1lon commented May 1, 2024

The principle is that runtime errors should be caught at compile time.

@eps1lon eps1lon closed this May 1, 2024
@jdufresne jdufresne deleted the events-allow-null branch May 4, 2024 14:52
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.

None yet

2 participants