Skip to content

Commit

Permalink
fix: ensure all events are fired in the eventWrapper (#385)
Browse files Browse the repository at this point in the history
Closes #384
  • Loading branch information
kentcdodds committed Jun 25, 2020
1 parent 8b15930 commit 8889776
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 6 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -55,6 +55,7 @@
"rules": {
"jsx-a11y/click-events-have-key-events": "off",
"jsx-a11y/tabindex-no-positive": "off",
"no-func-assign": "off",
"no-return-assign": "off",
"react/prop-types": "off",
"testing-library/no-dom-import": "off"
Expand Down
4 changes: 3 additions & 1 deletion src/blur.js
@@ -1,5 +1,5 @@
import {fireEvent} from '@testing-library/dom'
import {getActiveElement, isFocusable} from './utils'
import {getActiveElement, isFocusable, wrapInEventWrapper} from './utils'

function blur(element, init) {
if (!isFocusable(element)) return
Expand All @@ -11,4 +11,6 @@ function blur(element, init) {
fireEvent.focusOut(element, init)
}

blur = wrapInEventWrapper(blur)

export {blur}
3 changes: 3 additions & 0 deletions src/clear.js
@@ -1,4 +1,5 @@
import {type} from './type'
import {wrapInEventWrapper} from './utils'

function clear(element) {
if (element.tagName !== 'INPUT' && element.tagName !== 'TEXTAREA') {
Expand Down Expand Up @@ -27,4 +28,6 @@ function clear(element) {
}
}

clear = wrapInEventWrapper(clear)

export {clear}
4 changes: 4 additions & 0 deletions src/click.js
Expand Up @@ -2,6 +2,7 @@ import {fireEvent} from '@testing-library/dom'
import {
getMouseEventOptions,
isLabelWithInternallyDisabledControl,
wrapInEventWrapper,
} from './utils'
import {hover} from './hover'
import {blur} from './blur'
Expand Down Expand Up @@ -103,4 +104,7 @@ function dblClick(element, init) {
fireEvent.dblClick(element, getMouseEventOptions('dblclick', init, 2))
}

click = wrapInEventWrapper(click)
dblClick = wrapInEventWrapper(dblClick)

export {click, dblClick}
3 changes: 2 additions & 1 deletion src/focus.js
@@ -1,5 +1,5 @@
import {fireEvent} from '@testing-library/dom'
import {getActiveElement, isFocusable} from './utils'
import {getActiveElement, isFocusable, wrapInEventWrapper} from './utils'

function focus(element, init) {
if (!isFocusable(element)) return
Expand All @@ -10,5 +10,6 @@ function focus(element, init) {
element.focus()
fireEvent.focusIn(element, init)
}
focus = wrapInEventWrapper(focus)

export {focus}
4 changes: 4 additions & 0 deletions src/hover.js
Expand Up @@ -2,6 +2,7 @@ import {fireEvent} from '@testing-library/dom'
import {
isLabelWithInternallyDisabledControl,
getMouseEventOptions,
wrapInEventWrapper,
} from './utils'

function hover(element, init) {
Expand Down Expand Up @@ -34,4 +35,7 @@ function unhover(element, init) {
}
}

hover = wrapInEventWrapper(hover)
unhover = wrapInEventWrapper(unhover)

export {hover, unhover}
7 changes: 6 additions & 1 deletion src/paste.js
@@ -1,5 +1,9 @@
import {fireEvent} from '@testing-library/dom'
import {setSelectionRangeIfNecessary, calculateNewValue} from './utils'
import {
setSelectionRangeIfNecessary,
calculateNewValue,
wrapInEventWrapper,
} from './utils'

function paste(
element,
Expand Down Expand Up @@ -49,5 +53,6 @@ function paste(
})
}
}
paste = wrapInEventWrapper(paste)

export {paste}
5 changes: 3 additions & 2 deletions src/select-options.js
@@ -1,4 +1,5 @@
import {createEvent, getConfig, fireEvent} from '@testing-library/dom'
import {wrapInEventWrapper} from './utils'
import {click} from './click'
import {focus} from './focus'

Expand Down Expand Up @@ -73,7 +74,7 @@ function selectOptionsBase(newValue, select, values, init) {
}
}

const selectOptions = selectOptionsBase.bind(null, true)
const deselectOptions = selectOptionsBase.bind(null, false)
const selectOptions = wrapInEventWrapper(selectOptionsBase.bind(null, true))
const deselectOptions = wrapInEventWrapper(selectOptionsBase.bind(null, false))

export {selectOptions, deselectOptions}
3 changes: 2 additions & 1 deletion src/tab.js
@@ -1,5 +1,5 @@
import {fireEvent} from '@testing-library/dom'
import {getActiveElement, FOCUSABLE_SELECTOR} from './utils'
import {getActiveElement, FOCUSABLE_SELECTOR, wrapInEventWrapper} from './utils'
import {focus} from './focus'
import {blur} from './blur'

Expand Down Expand Up @@ -112,6 +112,7 @@ function tab({shift = false, focusTrap} = {}) {
fireEvent.keyUp(keyUpTarget, {...shiftKeyInit, shiftKey: false})
}
}
tab = wrapInEventWrapper(tab)

export {tab}

Expand Down
3 changes: 3 additions & 0 deletions src/type.js
Expand Up @@ -3,10 +3,12 @@ import {
fireEvent,
getConfig as getDOMTestingLibraryConfig,
} from '@testing-library/dom'

import {
getActiveElement,
calculateNewValue,
setSelectionRangeIfNecessary,
wrapInEventWrapper,
} from './utils'
import {click} from './click'

Expand Down Expand Up @@ -528,6 +530,7 @@ function getEventCallbackMap({
}
}
}
type = wrapInEventWrapper(type)

export {type}

Expand Down
2 changes: 2 additions & 0 deletions src/upload.js
@@ -1,4 +1,5 @@
import {fireEvent, createEvent} from '@testing-library/dom'
import {wrapInEventWrapper} from './utils'
import {click} from './click'
import {blur} from './blur'
import {focus} from './focus'
Expand Down Expand Up @@ -47,5 +48,6 @@ function upload(element, fileOrFiles, init) {
...init,
})
}
upload = wrapInEventWrapper(upload)

export {upload}
16 changes: 16 additions & 0 deletions src/utils.js
@@ -1,3 +1,5 @@
import {getConfig} from '@testing-library/dom'

function isMousePressEvent(event) {
return (
event === 'mousedown' ||
Expand Down Expand Up @@ -169,6 +171,19 @@ 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
}

export {
FOCUSABLE_SELECTOR,
isFocusable,
Expand All @@ -177,4 +192,5 @@ export {
getActiveElement,
calculateNewValue,
setSelectionRangeIfNecessary,
wrapInEventWrapper,
}

0 comments on commit 8889776

Please sign in to comment.