Repository: kentcdodds/dom-testing-library
Pull request #246 (2019-04-17 14:12:09)

Fix typings of getQueriesForElement
<!--
Thanks for your interest in the project. Bugs filed and PRs submitted are appreciated!

Please make sure that you are familiar with and follow the Code of Conduct for
this project (found in the CODE_OF_CONDUCT.md file).

Also, please make sure you're familiar with and follow the instructions in the
contributing guidelines (found in the CONTRIBUTING.md file).

If you're new to contributing to open source projects, you might find this free
video course helpful: http://kcd.im/pull-request

Please fill out the information below to expedite the review and (hopefully)
merge of your pull request!
-->

<!-- What changes are being made? (What feature/bug is being fixed here?) -->

**What**:
Fix typings of getQueriesForElement function

<!-- Why are these changes necessary? -->

**Why**:
There are currently two problems:
1. Type of `queriesToBind` parameter is `BoundFunctions<typeof queries> | BoundFunctions<typeof queries>[]` which is IMO incorrect because the function is doing the binding and thus should receive unbound queries.
1. Type parameter of `BoundFunctions` is hardcoded to `typeof queries` which prevents binding of custom queries

<!-- How were these changes implemented? -->

**How**:
1. Parametrised `getQueriesForElement` to enable binding custom queries
1. Changed type of `queriesToBind` to type parameter

<!-- 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
- [x] Typescript definitions updated
- [ ] Tests N/A
- [x] Ready to be merged <!-- In your opinion, is this ready to be merged as soon as it's reviewed? -->

<!-- feel free to add additional comments -->



Patch:
diff --git a/src/events.js b/src/events.js
index af9681e..82c7066 100644
--- a/src/events.js
+++ b/src/events.js
@@ -329,14 +329,32 @@ Object.keys(eventMap).forEach(key => {
         value: files,
       })
     }
-    // if the node does not have an owner document, then it probably _is_ the owner document
-    const window = (node.ownerDocument || node).defaultView
+    const window = getWindowFromNode(node)
     const EventConstructor = window[EventType] || window.Event
     const event = new EventConstructor(eventName, eventInit)
     return fireEvent(node, event)
   }
 })
 
+function getWindowFromNode(node) {
+  // istanbul ignore next I'm not sure what could cause the final else so we'll leave it uncovered.
+  if (node.defaultView) {
+    // node is document
+    return node.defaultView
+  } else if (node.ownerDocument) {
+    // node is a DOM node
+    return node.ownerDocument.defaultView
+  } else if (node.window) {
+    // node is window
+    return node.window
+  } else {
+    // no idea...
+    throw new Error(
+      `Unable to find the "window" object for the given node. fireEvent currently supports firing events on DOM nodes, document, and window. Please file an issue with the code that's causing you to see this error: https://github.com/kentcdodds/dom-testing-library/issues/new`,
+    )
+  }
+}
+
 // function written after some investigation here:
 // https://github.com/facebook/react/issues/10135#issuecomment-401496776
 function setNativeValue(element, value) {
diff --git a/typings/get-queries-for-element.d.ts b/typings/get-queries-for-element.d.ts
index 0db549b..3b173a5 100644
--- a/typings/get-queries-for-element.d.ts
+++ b/typings/get-queries-for-element.d.ts
@@ -13,9 +13,7 @@ export type BoundFunction<T> = T extends (
   : never
 export type BoundFunctions<T> = {[P in keyof T]: BoundFunction<T[P]>}
 
-export function getQueriesForElement(
+export function getQueriesForElement<T = typeof queries>(
   element: HTMLElement,
-  queriesToBind?:
-    | BoundFunctions<typeof queries>
-    | BoundFunctions<typeof queries>[],
-): BoundFunctions<typeof queries>
+  queriesToBind?: T,
+): BoundFunctions<T>


Test patch:
diff --git a/src/__tests__/events.js b/src/__tests__/events.js
index 969a558..f0a28d2 100644
--- a/src/__tests__/events.js
+++ b/src/__tests__/events.js
@@ -186,6 +186,14 @@ test('fires events on Window', () => {
   window.removeEventListener('message', messageSpy)
 })
 
+test('fires shortcut events on Window', () => {
+  const clickSpy = jest.fn()
+  window.addEventListener('click', clickSpy)
+  fireEvent.click(window)
+  expect(clickSpy).toHaveBeenCalledTimes(1)
+  window.removeEventListener('message', clickSpy)
+})
+
 test('fires events on Document', () => {
   const keyDownSpy = jest.fn()
   document.addEventListener('keydown', keyDownSpy)