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

Unable to fire event on document #159

Closed
arvigeus opened this issue Nov 22, 2018 · 5 comments
Closed

Unable to fire event on document #159

arvigeus opened this issue Nov 22, 2018 · 5 comments

Comments

@arvigeus
Copy link

  • react-testing-library version: 5.3.0
  • react version: 1.6.6.3
  • node version: 8.11.3
  • yarn version: 1.12.3

Relevant code or config:

fireEvent.keyDown(document.body, {
  key: "Escape",
  keyCode: 27,
  which: 27
});

What happened:

TypeError: Cannot read property 'defaultView' of null

Suggested solution:

https://github.com/kentcdodds/dom-testing-library/blob/master/src/events.js#L324
Should handle document and window elements (possibly in more places)

@kentcdodds
Copy link
Member

Hmmm.... Can you dig deeper because document.body.ownerDocument.defaultView is window:

screen shot 2018-11-22 at 10 32 42 am

@kentcdodds kentcdodds added help wanted Extra attention is needed needs investigation labels Nov 22, 2018
alexkrolick added a commit to alexkrolick/repro-document-fire-event that referenced this issue Dec 9, 2018
@alexkrolick
Copy link
Collaborator

alexkrolick commented Dec 9, 2018

Not able to reproduce this error - see https://github.com/alexkrolick/repro-document-fire-event/blob/master/src/__tests__/index.test.js

Please provide a minimal reproduction in Codepen or a repo if you can come up with one

It seems possible that this error is due to something else, such as the node being queried being undefined/null

import {fireEvent} from 'react-testing-library'
import React from 'react'


it('fireEvent on document.body should not error', () => {
  fireEvent.keyDown(document.body, {
    key: "Escape",
    keyCode: 27,
    which: 27
  })
})

@alexkrolick alexkrolick removed the help wanted Extra attention is needed label Dec 9, 2018
@abe4mvp
Copy link

abe4mvp commented Jan 11, 2019

I am getting this same issue but when im trying to fire an event on document.
fireEvent.keyDown(document, { key: 'ArrowRight'});

also confirmed that
document.ownerDocument is null in the browser

@kentcdodds
Copy link
Member

Hey @abe4mvp,

I think that it would be reasonable to change this:

https://github.com/kentcdodds/dom-testing-library/blob/ab29a47386394fee16db700db8cfd5a6ac03c33e/src/events.js#L332

to this:

// if the node does not have an owner document, then it probably _is_ the owner document
const window = (node.ownerDocument || node).defaultView

If you would be willing to make a pull request for that (including a test) then we can merge it and you should be good to go :)

@kentcdodds
Copy link
Member

🎉 This issue has been resolved in version 3.17.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

No branches or pull requests

4 participants