Skip to content

Commit fc336b8

Browse files
authored
fix: do not wrap the whole function in the event wrapper (testing-library#389)
Closes testing-library#387
1 parent 8161fa2 commit fc336b8

File tree

11 files changed

+17
-43
lines changed

11 files changed

+17
-43
lines changed

src/blur.js

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

44
function blur(element, init) {
55
if (!isFocusable(element)) return
66

77
const wasActive = getActiveElement(element.ownerDocument) === element
88
if (!wasActive) return
99

10-
element.blur()
10+
eventWrapper(() => element.blur())
1111
fireEvent.focusOut(element, init)
1212
}
1313

14-
blur = wrapInEventWrapper(blur)
15-
1614
export {blur}

src/clear.js

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

43
function clear(element) {
54
if (element.tagName !== 'INPUT' && element.tagName !== 'TEXTAREA') {
@@ -28,6 +27,4 @@ function clear(element) {
2827
}
2928
}
3029

31-
clear = wrapInEventWrapper(clear)
32-
3330
export {clear}

src/click.js

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

107-
click = wrapInEventWrapper(click)
108-
dblClick = wrapInEventWrapper(dblClick)
109-
110106
export {click, dblClick}

src/focus.js

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

44
function focus(element, init) {
55
if (!isFocusable(element)) return
66

77
const isAlreadyActive = getActiveElement(element.ownerDocument) === element
88
if (isAlreadyActive) return
99

10-
element.focus()
10+
eventWrapper(() => element.focus())
1111
fireEvent.focusIn(element, init)
1212
}
13-
focus = wrapInEventWrapper(focus)
1413

1514
export {focus}

src/hover.js

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

87
function hover(element, init) {
@@ -35,7 +34,4 @@ function unhover(element, init) {
3534
}
3635
}
3736

38-
hover = wrapInEventWrapper(hover)
39-
unhover = wrapInEventWrapper(unhover)
40-
4137
export {hover, unhover}

src/paste.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {fireEvent} from '@testing-library/dom'
22
import {
33
setSelectionRangeIfNecessary,
44
calculateNewValue,
5-
wrapInEventWrapper,
5+
eventWrapper,
66
} from './utils'
77

88
function paste(
@@ -17,7 +17,7 @@ function paste(
1717
`the current element is of type ${element.tagName} and doesn't have a valid value`,
1818
)
1919
}
20-
element.focus()
20+
eventWrapper(() => element.focus())
2121

2222
// by default, a new element has it's selection start and end at 0
2323
// but most of the time when people call "paste", they expect it to paste
@@ -53,6 +53,5 @@ function paste(
5353
})
5454
}
5555
}
56-
paste = wrapInEventWrapper(paste)
5756

5857
export {paste}

src/select-options.js

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

@@ -74,7 +73,7 @@ function selectOptionsBase(newValue, select, values, init) {
7473
}
7574
}
7675

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

8079
export {selectOptions, deselectOptions}

src/tab.js

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

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

117116
export {tab}
118117

src/type.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ async function type(element, text, {delay = 0, ...options} = {}) {
2727
result = await typeImpl(element, text, {delay, ...options})
2828
})
2929
} else {
30-
getDOMTestingLibraryConfig().eventWrapper(() => {
31-
result = typeImpl(element, text, {delay, ...options})
32-
})
30+
result = typeImpl(element, text, {delay, ...options})
3331
}
3432
return result
3533
}

src/upload.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {fireEvent, createEvent} from '@testing-library/dom'
2-
import {wrapInEventWrapper} from './utils'
32
import {click} from './click'
43
import {blur} from './blur'
54
import {focus} from './focus'
@@ -48,6 +47,5 @@ function upload(element, fileOrFiles, init) {
4847
...init,
4948
})
5049
}
51-
upload = wrapInEventWrapper(upload)
5250

5351
export {upload}

src/utils.js

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -171,17 +171,12 @@ function isFocusable(element) {
171171
)
172172
}
173173

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
174+
function eventWrapper(cb) {
175+
let result
176+
getConfig().eventWrapper(() => {
177+
result = cb()
178+
})
179+
return result
185180
}
186181

187182
export {
@@ -192,5 +187,5 @@ export {
192187
getActiveElement,
193188
calculateNewValue,
194189
setSelectionRangeIfNecessary,
195-
wrapInEventWrapper,
190+
eventWrapper,
196191
}

0 commit comments

Comments
 (0)