Skip to content

Commit 8889776

Browse files
authored
fix: ensure all events are fired in the eventWrapper (testing-library#385)
Closes testing-library#384
1 parent 8b15930 commit 8889776

File tree

12 files changed

+49
-6
lines changed

12 files changed

+49
-6
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"rules": {
5656
"jsx-a11y/click-events-have-key-events": "off",
5757
"jsx-a11y/tabindex-no-positive": "off",
58+
"no-func-assign": "off",
5859
"no-return-assign": "off",
5960
"react/prop-types": "off",
6061
"testing-library/no-dom-import": "off"

src/blur.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {fireEvent} from '@testing-library/dom'
2-
import {getActiveElement, isFocusable} from './utils'
2+
import {getActiveElement, isFocusable, wrapInEventWrapper} from './utils'
33

44
function blur(element, init) {
55
if (!isFocusable(element)) return
@@ -11,4 +11,6 @@ function blur(element, init) {
1111
fireEvent.focusOut(element, init)
1212
}
1313

14+
blur = wrapInEventWrapper(blur)
15+
1416
export {blur}

src/clear.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {type} from './type'
2+
import {wrapInEventWrapper} from './utils'
23

34
function clear(element) {
45
if (element.tagName !== 'INPUT' && element.tagName !== 'TEXTAREA') {
@@ -27,4 +28,6 @@ function clear(element) {
2728
}
2829
}
2930

31+
clear = wrapInEventWrapper(clear)
32+
3033
export {clear}

src/click.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {fireEvent} from '@testing-library/dom'
22
import {
33
getMouseEventOptions,
44
isLabelWithInternallyDisabledControl,
5+
wrapInEventWrapper,
56
} from './utils'
67
import {hover} from './hover'
78
import {blur} from './blur'
@@ -103,4 +104,7 @@ function dblClick(element, init) {
103104
fireEvent.dblClick(element, getMouseEventOptions('dblclick', init, 2))
104105
}
105106

107+
click = wrapInEventWrapper(click)
108+
dblClick = wrapInEventWrapper(dblClick)
109+
106110
export {click, dblClick}

src/focus.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {fireEvent} from '@testing-library/dom'
2-
import {getActiveElement, isFocusable} from './utils'
2+
import {getActiveElement, isFocusable, wrapInEventWrapper} from './utils'
33

44
function focus(element, init) {
55
if (!isFocusable(element)) return
@@ -10,5 +10,6 @@ function focus(element, init) {
1010
element.focus()
1111
fireEvent.focusIn(element, init)
1212
}
13+
focus = wrapInEventWrapper(focus)
1314

1415
export {focus}

src/hover.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {fireEvent} from '@testing-library/dom'
22
import {
33
isLabelWithInternallyDisabledControl,
44
getMouseEventOptions,
5+
wrapInEventWrapper,
56
} from './utils'
67

78
function hover(element, init) {
@@ -34,4 +35,7 @@ function unhover(element, init) {
3435
}
3536
}
3637

38+
hover = wrapInEventWrapper(hover)
39+
unhover = wrapInEventWrapper(unhover)
40+
3741
export {hover, unhover}

src/paste.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import {fireEvent} from '@testing-library/dom'
2-
import {setSelectionRangeIfNecessary, calculateNewValue} from './utils'
2+
import {
3+
setSelectionRangeIfNecessary,
4+
calculateNewValue,
5+
wrapInEventWrapper,
6+
} from './utils'
37

48
function paste(
59
element,
@@ -49,5 +53,6 @@ function paste(
4953
})
5054
}
5155
}
56+
paste = wrapInEventWrapper(paste)
5257

5358
export {paste}

src/select-options.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {createEvent, getConfig, fireEvent} from '@testing-library/dom'
2+
import {wrapInEventWrapper} from './utils'
23
import {click} from './click'
34
import {focus} from './focus'
45

@@ -73,7 +74,7 @@ function selectOptionsBase(newValue, select, values, init) {
7374
}
7475
}
7576

76-
const selectOptions = selectOptionsBase.bind(null, true)
77-
const deselectOptions = selectOptionsBase.bind(null, false)
77+
const selectOptions = wrapInEventWrapper(selectOptionsBase.bind(null, true))
78+
const deselectOptions = wrapInEventWrapper(selectOptionsBase.bind(null, false))
7879

7980
export {selectOptions, deselectOptions}

src/tab.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {fireEvent} from '@testing-library/dom'
2-
import {getActiveElement, FOCUSABLE_SELECTOR} from './utils'
2+
import {getActiveElement, FOCUSABLE_SELECTOR, wrapInEventWrapper} from './utils'
33
import {focus} from './focus'
44
import {blur} from './blur'
55

@@ -112,6 +112,7 @@ function tab({shift = false, focusTrap} = {}) {
112112
fireEvent.keyUp(keyUpTarget, {...shiftKeyInit, shiftKey: false})
113113
}
114114
}
115+
tab = wrapInEventWrapper(tab)
115116

116117
export {tab}
117118

src/type.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ import {
33
fireEvent,
44
getConfig as getDOMTestingLibraryConfig,
55
} from '@testing-library/dom'
6+
67
import {
78
getActiveElement,
89
calculateNewValue,
910
setSelectionRangeIfNecessary,
11+
wrapInEventWrapper,
1012
} from './utils'
1113
import {click} from './click'
1214

@@ -528,6 +530,7 @@ function getEventCallbackMap({
528530
}
529531
}
530532
}
533+
type = wrapInEventWrapper(type)
531534

532535
export {type}
533536

src/upload.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {fireEvent, createEvent} from '@testing-library/dom'
2+
import {wrapInEventWrapper} from './utils'
23
import {click} from './click'
34
import {blur} from './blur'
45
import {focus} from './focus'
@@ -47,5 +48,6 @@ function upload(element, fileOrFiles, init) {
4748
...init,
4849
})
4950
}
51+
upload = wrapInEventWrapper(upload)
5052

5153
export {upload}

src/utils.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import {getConfig} from '@testing-library/dom'
2+
13
function isMousePressEvent(event) {
24
return (
35
event === 'mousedown' ||
@@ -169,6 +171,19 @@ function isFocusable(element) {
169171
)
170172
}
171173

174+
function wrapInEventWrapper(fn) {
175+
function wrapper(...args) {
176+
let result
177+
getConfig().eventWrapper(() => {
178+
result = fn(...args)
179+
})
180+
return result
181+
}
182+
// give it a helpful name for debugging
183+
Object.defineProperty(wrapper, 'name', {value: `${fn.name}Wrapper`})
184+
return wrapper
185+
}
186+
172187
export {
173188
FOCUSABLE_SELECTOR,
174189
isFocusable,
@@ -177,4 +192,5 @@ export {
177192
getActiveElement,
178193
calculateNewValue,
179194
setSelectionRangeIfNecessary,
195+
wrapInEventWrapper,
180196
}

0 commit comments

Comments
 (0)