From fc336b8dc0d83e1487b53d62b7874156bcc0a80d Mon Sep 17 00:00:00 2001 From: "Kent C. Dodds" Date: Thu, 25 Jun 2020 16:10:34 -0600 Subject: [PATCH] fix: do not wrap the whole function in the event wrapper (#389) Closes #387 --- src/blur.js | 6 ++---- src/clear.js | 3 --- src/click.js | 4 ---- src/focus.js | 5 ++--- src/hover.js | 4 ---- src/paste.js | 5 ++--- src/select-options.js | 5 ++--- src/tab.js | 3 +-- src/type.js | 4 +--- src/upload.js | 2 -- src/utils.js | 19 +++++++------------ 11 files changed, 17 insertions(+), 43 deletions(-) diff --git a/src/blur.js b/src/blur.js index 305a8b56..f8dee334 100644 --- a/src/blur.js +++ b/src/blur.js @@ -1,5 +1,5 @@ import {fireEvent} from '@testing-library/dom' -import {getActiveElement, isFocusable, wrapInEventWrapper} from './utils' +import {getActiveElement, isFocusable, eventWrapper} from './utils' function blur(element, init) { if (!isFocusable(element)) return @@ -7,10 +7,8 @@ function blur(element, init) { const wasActive = getActiveElement(element.ownerDocument) === element if (!wasActive) return - element.blur() + eventWrapper(() => element.blur()) fireEvent.focusOut(element, init) } -blur = wrapInEventWrapper(blur) - export {blur} diff --git a/src/clear.js b/src/clear.js index a604e5a8..965d1a6c 100644 --- a/src/clear.js +++ b/src/clear.js @@ -1,5 +1,4 @@ import {type} from './type' -import {wrapInEventWrapper} from './utils' function clear(element) { if (element.tagName !== 'INPUT' && element.tagName !== 'TEXTAREA') { @@ -28,6 +27,4 @@ function clear(element) { } } -clear = wrapInEventWrapper(clear) - export {clear} diff --git a/src/click.js b/src/click.js index 7a5f1889..ee5fc6ec 100644 --- a/src/click.js +++ b/src/click.js @@ -2,7 +2,6 @@ import {fireEvent} from '@testing-library/dom' import { getMouseEventOptions, isLabelWithInternallyDisabledControl, - wrapInEventWrapper, } from './utils' import {hover} from './hover' import {blur} from './blur' @@ -104,7 +103,4 @@ function dblClick(element, init) { fireEvent.dblClick(element, getMouseEventOptions('dblclick', init, 2)) } -click = wrapInEventWrapper(click) -dblClick = wrapInEventWrapper(dblClick) - export {click, dblClick} diff --git a/src/focus.js b/src/focus.js index 2e4f7c5f..1df8818f 100644 --- a/src/focus.js +++ b/src/focus.js @@ -1,5 +1,5 @@ import {fireEvent} from '@testing-library/dom' -import {getActiveElement, isFocusable, wrapInEventWrapper} from './utils' +import {getActiveElement, isFocusable, eventWrapper} from './utils' function focus(element, init) { if (!isFocusable(element)) return @@ -7,9 +7,8 @@ function focus(element, init) { const isAlreadyActive = getActiveElement(element.ownerDocument) === element if (isAlreadyActive) return - element.focus() + eventWrapper(() => element.focus()) fireEvent.focusIn(element, init) } -focus = wrapInEventWrapper(focus) export {focus} diff --git a/src/hover.js b/src/hover.js index 7c81a34f..e2a3bb80 100644 --- a/src/hover.js +++ b/src/hover.js @@ -2,7 +2,6 @@ import {fireEvent} from '@testing-library/dom' import { isLabelWithInternallyDisabledControl, getMouseEventOptions, - wrapInEventWrapper, } from './utils' function hover(element, init) { @@ -35,7 +34,4 @@ function unhover(element, init) { } } -hover = wrapInEventWrapper(hover) -unhover = wrapInEventWrapper(unhover) - export {hover, unhover} diff --git a/src/paste.js b/src/paste.js index a0bdd26a..ce88ea26 100644 --- a/src/paste.js +++ b/src/paste.js @@ -2,7 +2,7 @@ import {fireEvent} from '@testing-library/dom' import { setSelectionRangeIfNecessary, calculateNewValue, - wrapInEventWrapper, + eventWrapper, } from './utils' function paste( @@ -17,7 +17,7 @@ function paste( `the current element is of type ${element.tagName} and doesn't have a valid value`, ) } - element.focus() + eventWrapper(() => element.focus()) // by default, a new element has it's selection start and end at 0 // but most of the time when people call "paste", they expect it to paste @@ -53,6 +53,5 @@ function paste( }) } } -paste = wrapInEventWrapper(paste) export {paste} diff --git a/src/select-options.js b/src/select-options.js index 88121a95..a63cd238 100644 --- a/src/select-options.js +++ b/src/select-options.js @@ -1,5 +1,4 @@ import {createEvent, getConfig, fireEvent} from '@testing-library/dom' -import {wrapInEventWrapper} from './utils' import {click} from './click' import {focus} from './focus' @@ -74,7 +73,7 @@ function selectOptionsBase(newValue, select, values, init) { } } -const selectOptions = wrapInEventWrapper(selectOptionsBase.bind(null, true)) -const deselectOptions = wrapInEventWrapper(selectOptionsBase.bind(null, false)) +const selectOptions = selectOptionsBase.bind(null, true) +const deselectOptions = selectOptionsBase.bind(null, false) export {selectOptions, deselectOptions} diff --git a/src/tab.js b/src/tab.js index fd5706f4..96ae333a 100644 --- a/src/tab.js +++ b/src/tab.js @@ -1,5 +1,5 @@ import {fireEvent} from '@testing-library/dom' -import {getActiveElement, FOCUSABLE_SELECTOR, wrapInEventWrapper} from './utils' +import {getActiveElement, FOCUSABLE_SELECTOR} from './utils' import {focus} from './focus' import {blur} from './blur' @@ -112,7 +112,6 @@ function tab({shift = false, focusTrap} = {}) { fireEvent.keyUp(keyUpTarget, {...shiftKeyInit, shiftKey: false}) } } -tab = wrapInEventWrapper(tab) export {tab} diff --git a/src/type.js b/src/type.js index dac1343e..a8ed5c92 100644 --- a/src/type.js +++ b/src/type.js @@ -27,9 +27,7 @@ async function type(element, text, {delay = 0, ...options} = {}) { result = await typeImpl(element, text, {delay, ...options}) }) } else { - getDOMTestingLibraryConfig().eventWrapper(() => { - result = typeImpl(element, text, {delay, ...options}) - }) + result = typeImpl(element, text, {delay, ...options}) } return result } diff --git a/src/upload.js b/src/upload.js index 5c97466a..87bd447f 100644 --- a/src/upload.js +++ b/src/upload.js @@ -1,5 +1,4 @@ import {fireEvent, createEvent} from '@testing-library/dom' -import {wrapInEventWrapper} from './utils' import {click} from './click' import {blur} from './blur' import {focus} from './focus' @@ -48,6 +47,5 @@ function upload(element, fileOrFiles, init) { ...init, }) } -upload = wrapInEventWrapper(upload) export {upload} diff --git a/src/utils.js b/src/utils.js index 932acbbf..bd1e1e6c 100644 --- a/src/utils.js +++ b/src/utils.js @@ -171,17 +171,12 @@ function isFocusable(element) { ) } -function wrapInEventWrapper(fn) { - function wrapper(...args) { - let result - getConfig().eventWrapper(() => { - result = fn(...args) - }) - return result - } - // give it a helpful name for debugging - Object.defineProperty(wrapper, 'name', {value: `${fn.name}Wrapper`}) - return wrapper +function eventWrapper(cb) { + let result + getConfig().eventWrapper(() => { + result = cb() + }) + return result } export { @@ -192,5 +187,5 @@ export { getActiveElement, calculateNewValue, setSelectionRangeIfNecessary, - wrapInEventWrapper, + eventWrapper, }