Skip to content

Commit 3922ab7

Browse files
briwaeddyerburgh
briwa
authored andcommitted
fix: method should be updated when triggering
1 parent dc12334 commit 3922ab7

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

packages/test-utils/src/wrapper.js

+7
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,13 @@ export default class Wrapper implements BaseWrapper {
633633
eventObject.keyCode = modifiers[event[1]]
634634
}
635635

636+
// If this element's event handler has been reset by setMethod, it won't trigger
637+
// Make sure that this element is updated with the latest event handler
638+
if (this.vnode) {
639+
const context = this.vnode.context
640+
if (context.$options.render) context._update(context._render())
641+
}
642+
636643
this.element.dispatchEvent(eventObject)
637644
if (this.vnode) {
638645
orderWatchers(this.vm || this.vnode.context.$root)

test/specs/wrapper/setMethods.spec.js

+13-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { compileToFunctions } from 'vue-template-compiler'
22
import ComponentWithMethods from '~resources/components/component-with-methods.vue'
3+
import ComponentWithEvents from '~resources/components/component-with-events.vue'
34
import { describeWithShallowAndMount } from '~resources/utils'
45

56
describeWithShallowAndMount('setMethods', (mountingMethod) => {
@@ -10,18 +11,23 @@ describeWithShallowAndMount('setMethods', (mountingMethod) => {
1011
expect(wrapper.vm.someMethod).to.equal(someMethod)
1112
})
1213

13-
it('sets component data and updates nested vm nodes when called on Vue instance', () => {
14-
const wrapper = mountingMethod(ComponentWithMethods)
15-
const someMethod = () => console.log('hey')
16-
wrapper.setMethods({ someMethod })
17-
expect(wrapper.vm.someMethod).to.equal(someMethod)
18-
})
19-
2014
it('throws an error if node is not a Vue instance', () => {
2115
const message = 'wrapper.setMethods() can only be called on a Vue instance'
2216
const compiled = compileToFunctions('<div><p></p></div>')
2317
const wrapper = mountingMethod(compiled)
2418
const p = wrapper.find('p')
2519
expect(() => p.setMethods({ ready: true })).throw(Error, message)
2620
})
21+
22+
it('should replace methods when tied to an event', () => {
23+
const wrapper = mountingMethod(ComponentWithEvents)
24+
expect(wrapper.vm.isActive).to.be.false
25+
wrapper.find('.toggle').trigger('click')
26+
expect(wrapper.vm.isActive).to.be.true
27+
// Replace the toggle function so that the data supposedly won't change
28+
const toggleActive = () => console.log('overriden')
29+
wrapper.setMethods({ toggleActive })
30+
wrapper.find('.toggle').trigger('click')
31+
expect(wrapper.vm.isActive).to.be.true
32+
})
2733
})

0 commit comments

Comments
 (0)