From 92a4daec29894afe22faa31147389042fa37d704 Mon Sep 17 00:00:00 2001 From: Philip Bulley Date: Mon, 10 Feb 2020 17:49:57 +0000 Subject: [PATCH] Add ChildNode to union in fireEvent `element` arg (#42215) * Add ChildNode to union in fireEvent element arg * Add ChildNode test to eventTest() * Changing ChildNode to its super type Node for typescript@3.0 compatibility --- types/testing-library__dom/events.d.ts | 6 +++--- .../testing-library__dom-tests.ts | 12 ++++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/types/testing-library__dom/events.d.ts b/types/testing-library__dom/events.d.ts index 6926565717..d9c50cb790 100644 --- a/types/testing-library__dom/events.d.ts +++ b/types/testing-library__dom/events.d.ts @@ -83,12 +83,12 @@ export type EventType = | 'gotPointerCapture' | 'lostPointerCapture'; -export type FireFunction = (element: Document | Element | Window, event: Event) => boolean; +export type FireFunction = (element: Document | Element | Window | Node, event: Event) => boolean; export type FireObject = { - [K in EventType]: (element: Document | Element | Window, options?: {}) => boolean; + [K in EventType]: (element: Document | Element | Window | Node, options?: {}) => boolean; }; export type CreateObject = { - [K in EventType]: (element: Document | Element | Window, options?: {}) => Event; + [K in EventType]: (element: Document | Element | Window | Node, options?: {}) => Event; }; export const createEvent: CreateObject; diff --git a/types/testing-library__dom/testing-library__dom-tests.ts b/types/testing-library__dom/testing-library__dom-tests.ts index 830914fd7f..8615fe7b1d 100644 --- a/types/testing-library__dom/testing-library__dom-tests.ts +++ b/types/testing-library__dom/testing-library__dom-tests.ts @@ -51,4 +51,16 @@ function eventTest() { location: 'http://www.example.com/?page=1', state: { page: 1 }, }); + + // HTMLElement + const element = document.createElement('div'); + fireEvent.click(getByText(element, 'foo')); + + // ChildNode + const child = document.createElement('div'); + element.appendChild(child); + if (!element.firstChild) { // Narrow Type + throw new Error(`Can't find firstChild`); + } + fireEvent.click(element.firstChild); }