Skip to content

Commit 3c870a1

Browse files
committed
fix: throw error if target is set in options
#266
1 parent 256086b commit 3c870a1

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

docs/en/api/wrapper/trigger.md

+13
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,16 @@ wrapper.trigger('click', {
3636

3737
expect(clickHandler.called).toBe(true)
3838
```
39+
- **Setting the event target:**
40+
41+
Under the hood, `trigger` creates an `Event` object and dispatches the event on the Wrapper element.
42+
43+
It's not possible edit the `target` value of an `Event` object, so you can't set `target` in the options object.
44+
45+
To add an attribute to the `target`, you need to set the value of the Wrapper element before calling `trigger`. You can do this with the `element` property.
46+
47+
```js
48+
const input = wrapper.find('input')
49+
input.element.value = 100
50+
input.trigger('click')
51+
```

src/wrappers/wrapper.js

+4
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,10 @@ export default class Wrapper implements BaseWrapper {
516516
throwError('cannot call wrapper.trigger() on a wrapper without an element')
517517
}
518518

519+
if (options.target) {
520+
throwError('you cannot set the target value of an event. See the notes section of the docs for more details—https://vue-test-utils.vuejs.org/en/api/wrapper/trigger.html')
521+
}
522+
519523
const modifiers = {
520524
enter: 13,
521525
tab: 9,

test/unit/specs/mount/Wrapper/trigger.spec.js

+10
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,16 @@ describe('trigger', () => {
103103
expect(info.calledWith(true)).to.equal(true)
104104
})
105105

106+
it('throws error if options contains a target value', () => {
107+
const wrapper = mount({ render: (h) => h('div') })
108+
const div = wrapper.find('div')
109+
const fn = () => div.trigger('click', {
110+
target: {}
111+
})
112+
const message = '[vue-test-utils]: you cannot set the target value of an event. See the notes section of the docs for more details—https://vue-test-utils.vuejs.org/en/api/wrapper/trigger.html'
113+
expect(fn).to.throw().with.property('message', message)
114+
})
115+
106116
it('throws error if wrapper does not contain element', () => {
107117
const wrapper = mount({ render: (h) => h('div') })
108118
const div = wrapper.find('div')

0 commit comments

Comments
 (0)