Repository: kentcdodds/dom-testing-library (js)

Issue:
Unable to fire event on document
- `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:

```js
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)

Pull request:
fix(events): fix firing events in document
**What**:

Fix firing events in the document element.

Fixes #159, as per @kentcdodds suggestion in https://github.com/kentcdodds/dom-testing-library/issues/159#issuecomment-453369641.

**Why**:

Currently firing events in the document element throws a `TypeError: Cannot read property 'defaultView' of null` when trying to use `fireEvent` "shortcurt API" (not sure this is its name :)):

```fireEvent.keyDown(document, { key: 'Escape' })```

It was possible to fire events in the document by using `fireEvent` directly, like so:

```fireEvent(document, new KeyboardEvent('keydown', { key: 'Escape' }))```

<!-- How were these changes implemented? -->

**How**:

Falling back to `node` to get `defaultView` from, when `node.ownerDocument` is `null`.

<!-- Have you done all of these things?  -->

**Checklist**:

<!-- add "N/A" to the end of each line that's irrelevant to your changes -->

<!-- to check an item, place an "x" in the box like so: "- [x] Documentation" -->

- [ ] Documentation added to the [docs site](https://github.com/alexkrolick/testing-library-docs) N/A
- [ ] Typescript definitions updated N/A
- [x] Tests
- [x] Ready to be merged <!-- In your opinion, is this ready to be merged as soon as it's reviewed? -->
- [x] Added myself to contributors table <!-- this is optional, see the contributing guidelines for instructions -->

<!-- feel free to add additional comments -->


Patch:
diff --git a/.all-contributorsrc b/.all-contributorsrc
index 8e42dc9..08a6362 100644
--- a/.all-contributorsrc
+++ b/.all-contributorsrc
@@ -507,6 +507,16 @@
       "contributions": [
         "code"
       ]
+    },
+    {
+      "login": "tnunes",
+      "name": "Tiago Nunes",
+      "avatar_url": "https://avatars1.githubusercontent.com/u/163187?v=4",
+      "profile": "https://github.com/tnunes",
+      "contributions": [
+        "code",
+        "test"
+      ]
     }
   ]
 }
diff --git a/README.md b/README.md
index 643a210..aae15d6 100644
--- a/README.md
+++ b/README.md
@@ -138,7 +138,7 @@ Thanks goes to these people ([emoji key][emojis]):
 | [<img src="https://avatars3.githubusercontent.com/u/881986?v=4" width="100px;" alt="dadamssg"/><br /><sub><b>dadamssg</b></sub>](https://github.com/dadamssg)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=dadamssg "Code") | [<img src="https://avatars1.githubusercontent.com/u/186971?v=4" width="100px;" alt="Neil Kistner"/><br /><sub><b>Neil Kistner</b></sub>](https://neilkistner.com/)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=wyze "Code") | [<img src="https://avatars3.githubusercontent.com/u/1448597?v=4" width="100px;" alt="Ben Chauvette"/><br /><sub><b>Ben Chauvette</b></sub>](http://bdchauvette.net/)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=bdchauvette "Code") | [<img src="https://avatars2.githubusercontent.com/u/777527?v=4" width="100px;" alt="Jeff Baumgardt"/><br /><sub><b>Jeff Baumgardt</b></sub>](https://github.com/JeffBaumgardt)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=JeffBaumgardt "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=JeffBaumgardt "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/4658208?v=4" width="100px;" alt="Matan Kushner"/><br /><sub><b>Matan Kushner</b></sub>](http://matchai.me)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=matchai "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=matchai "Documentation") [🤔](#ideas-matchai "Ideas, Planning, & Feedback") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=matchai "Tests") | [<img src="https://avatars2.githubusercontent.com/u/5779538?v=4" width="100px;" alt="Alex Wendte"/><br /><sub><b>Alex Wendte</b></sub>](http://www.wendtedesigns.com/)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=themostcolm "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=themostcolm "Documentation") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=themostcolm "Tests") | [<img src="https://avatars0.githubusercontent.com/u/2196208?v=4" width="100px;" alt="Tamas Fodor"/><br /><sub><b>Tamas Fodor</b></sub>](https://github.com/ruffle1986)<br />[📖](https://github.com/kentcdodds/dom-testing-library/commits?author=ruffle1986 "Documentation") |
 | [<img src="https://avatars3.githubusercontent.com/u/14793495?v=4" width="100px;" alt="Benjamin Eckardt"/><br /><sub><b>Benjamin Eckardt</b></sub>](https://github.com/BenjaminEckardt)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=BenjaminEckardt "Code") | [<img src="https://avatars3.githubusercontent.com/u/205752?v=4" width="100px;" alt="Ryan Campbell"/><br /><sub><b>Ryan Campbell</b></sub>](https://github.com/campbellr)<br />[📖](https://github.com/kentcdodds/dom-testing-library/commits?author=campbellr "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/1335519?v=4" width="100px;" alt="Taylor Briggs"/><br /><sub><b>Taylor Briggs</b></sub>](https://taylor-briggs.com)<br />[⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=TaylorBriggs "Tests") | [<img src="https://avatars2.githubusercontent.com/u/132233?v=4" width="100px;" alt="John Gozde"/><br /><sub><b>John Gozde</b></sub>](https://github.com/jgoz)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=jgoz "Code") | [<img src="https://avatars2.githubusercontent.com/u/3382565?v=4" width="100px;" alt="C. T. Lin"/><br /><sub><b>C. T. Lin</b></sub>](https://github.com/chentsulin)<br />[📖](https://github.com/kentcdodds/dom-testing-library/commits?author=chentsulin "Documentation") | [<img src="https://avatars3.githubusercontent.com/u/5312329?v=4" width="100px;" alt="Terrence Wong"/><br /><sub><b>Terrence Wong</b></sub>](http://terrencewwong.com)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=terrencewwong "Code") | [<img src="https://avatars0.githubusercontent.com/u/12230408?v=4" width="100px;" alt="Soo Jae Hwang"/><br /><sub><b>Soo Jae Hwang</b></sub>](https://www.ossfinder.com)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=misoguy "Code") |
 | [<img src="https://avatars0.githubusercontent.com/u/19773?v=4" width="100px;" alt="Royston Shufflebotham"/><br /><sub><b>Royston Shufflebotham</b></sub>](https://github.com/RoystonS)<br />[🐛](https://github.com/kentcdodds/dom-testing-library/issues?q=author%3ARoystonS "Bug reports") [💻](https://github.com/kentcdodds/dom-testing-library/commits?author=RoystonS "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=RoystonS "Documentation") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=RoystonS "Tests") | [<img src="https://avatars0.githubusercontent.com/u/591673?v=4" width="100px;" alt="Vadim Brodsky"/><br /><sub><b>Vadim Brodsky</b></sub>](http://www.vadimbrodsky.com)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=VadimBrodsky "Code") | [<img src="https://avatars3.githubusercontent.com/u/499898?v=4" width="100px;" alt="Eunjae Lee"/><br /><sub><b>Eunjae Lee</b></sub>](https://twitter.com/eunjae_lee)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=eunjae-lee "Code") | [<img src="https://avatars2.githubusercontent.com/u/167743?v=4" width="100px;" alt="David Peter"/><br /><sub><b>David Peter</b></sub>](http://davidpeter.me)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=sarenji "Code") | [<img src="https://avatars0.githubusercontent.com/u/13174025?v=4" width="100px;" alt="Shy Alter"/><br /><sub><b>Shy Alter</b></sub>](https://twitter.com/@puemos)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=puemos "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=puemos "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/11966621?v=4" width="100px;" alt="Łukasz Makuch"/><br /><sub><b>Łukasz Makuch</b></sub>](https://lukaszmakuch.pl)<br />[📦](#platform-lukaszmakuch "Packaging/porting to new platform") | [<img src="https://avatars1.githubusercontent.com/u/11150235?v=4" width="100px;" alt="Tyler Haas"/><br /><sub><b>Tyler Haas</b></sub>](https://github.com/tylerthehaas)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=tylerthehaas "Code") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=tylerthehaas "Tests") |
-| [<img src="https://avatars2.githubusercontent.com/u/482561?v=4" width="100px;" alt="Vesa Laakso"/><br /><sub><b>Vesa Laakso</b></sub>](http://vesalaakso.com)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=valscion "Code") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=valscion "Tests") | [<img src="https://avatars0.githubusercontent.com/u/16590492?v=4" width="100px;" alt="Tulsi Sapkota"/><br /><sub><b>Tulsi Sapkota</b></sub>](https://github.com/Tolsee)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=Tolsee "Code") |
+| [<img src="https://avatars2.githubusercontent.com/u/482561?v=4" width="100px;" alt="Vesa Laakso"/><br /><sub><b>Vesa Laakso</b></sub>](http://vesalaakso.com)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=valscion "Code") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=valscion "Tests") | [<img src="https://avatars0.githubusercontent.com/u/16590492?v=4" width="100px;" alt="Tulsi Sapkota"/><br /><sub><b>Tulsi Sapkota</b></sub>](https://github.com/Tolsee)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=Tolsee "Code") | [<img src="https://avatars1.githubusercontent.com/u/163187?v=4" width="100px;" alt="Tiago Nunes"/><br /><sub><b>Tiago Nunes</b></sub>](https://github.com/tnunes)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=tnunes "Code") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=tnunes "Tests") |
 
 <!-- ALL-CONTRIBUTORS-LIST:END -->
 
diff --git a/src/events.js b/src/events.js
index 8eeac9e..af9681e 100644
--- a/src/events.js
+++ b/src/events.js
@@ -329,7 +329,8 @@ Object.keys(eventMap).forEach(key => {
         value: files,
       })
     }
-    const window = node.ownerDocument.defaultView
+    // if the node does not have an owner document, then it probably _is_ the owner document
+    const window = (node.ownerDocument || node).defaultView
     const EventConstructor = window[EventType] || window.Event
     const event = new EventConstructor(eventName, eventInit)
     return fireEvent(node, event)


Test patch:
diff --git a/src/__tests__/events.js b/src/__tests__/events.js
index 198ceba..969a558 100644
--- a/src/__tests__/events.js
+++ b/src/__tests__/events.js
@@ -185,3 +185,11 @@ test('fires events on Window', () => {
   expect(messageSpy).toHaveBeenCalledTimes(1)
   window.removeEventListener('message', messageSpy)
 })
+
+test('fires events on Document', () => {
+  const keyDownSpy = jest.fn()
+  document.addEventListener('keydown', keyDownSpy)
+  fireEvent.keyDown(document, {key: 'Escape'})
+  expect(keyDownSpy).toHaveBeenCalledTimes(1)
+  document.removeEventListener('keydown', keyDownSpy)
+})