Skip to content

Commit d32fd24

Browse files
committed
Create local version of fireEvent
1 parent b98df9b commit d32fd24

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/vue-testing-library.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
getQueriesForElement,
55
prettyDOM,
66
wait,
7-
fireEvent
7+
fireEvent as dtlFireEvent
88
} from '@testing-library/dom'
99

1010
const mountedWrappers = new Set()
@@ -91,10 +91,16 @@ function cleanupAtWrapper(wrapper) {
9191
mountedWrappers.delete(wrapper)
9292
}
9393

94-
Object.keys(fireEvent).forEach(fn => {
95-
fireEvent[`_${fn}`] = fireEvent[fn]
96-
fireEvent[fn] = async (...params) => {
97-
fireEvent[`_${fn}`](...params)
94+
// Vue Testing Library's version of fireEvent will call DOM Testing Library's
95+
// version of fireEvent plus wait for one tick of the event loop so that...
96+
async function fireEvent(...args) {
97+
dtlFireEvent(...args)
98+
await wait()
99+
}
100+
101+
Object.keys(dtlFireEvent).forEach(key => {
102+
fireEvent[key] = async (...args) => {
103+
dtlFireEvent[key](...args)
98104
await wait()
99105
}
100106
})
@@ -104,6 +110,9 @@ fireEvent.touch = async elem => {
104110
await fireEvent.blur(elem)
105111
}
106112

113+
// Small utility to provide a better experience when working with v-model.
114+
// Related upstream issue: https://github.com/vuejs/vue-test-utils/issues/345#issuecomment-380588199
115+
// Examples: https://github.com/testing-library/vue-testing-library/blob/master/tests/__tests__/form.js
107116
fireEvent.update = async (elem, value) => {
108117
const tagName = elem.tagName
109118
const type = elem.type
@@ -143,4 +152,4 @@ fireEvent.update = async (elem, value) => {
143152
}
144153

145154
export * from '@testing-library/dom'
146-
export { cleanup, render }
155+
export { cleanup, render, fireEvent }

0 commit comments

Comments
 (0)