diff --git a/packages/test-utils/src/wrapper.js b/packages/test-utils/src/wrapper.js index 9f9b6900a..b85af8985 100644 --- a/packages/test-utils/src/wrapper.js +++ b/packages/test-utils/src/wrapper.js @@ -633,6 +633,13 @@ export default class Wrapper implements BaseWrapper { eventObject.keyCode = modifiers[event[1]] } + // If this element's event handler has been reset by setMethod, it won't trigger + // Make sure that this element is updated with the latest event handler + if (this.vnode) { + const context = this.vnode.context + if (context.$options.render) context._update(context._render()) + } + this.element.dispatchEvent(eventObject) if (this.vnode) { orderWatchers(this.vm || this.vnode.context.$root) diff --git a/test/specs/wrapper/setMethods.spec.js b/test/specs/wrapper/setMethods.spec.js index 8137decb7..9a252ee8c 100644 --- a/test/specs/wrapper/setMethods.spec.js +++ b/test/specs/wrapper/setMethods.spec.js @@ -1,5 +1,6 @@ import { compileToFunctions } from 'vue-template-compiler' import ComponentWithMethods from '~resources/components/component-with-methods.vue' +import ComponentWithEvents from '~resources/components/component-with-events.vue' import { describeWithShallowAndMount } from '~resources/utils' describeWithShallowAndMount('setMethods', (mountingMethod) => { @@ -10,13 +11,6 @@ describeWithShallowAndMount('setMethods', (mountingMethod) => { expect(wrapper.vm.someMethod).to.equal(someMethod) }) - it('sets component data and updates nested vm nodes when called on Vue instance', () => { - const wrapper = mountingMethod(ComponentWithMethods) - const someMethod = () => console.log('hey') - wrapper.setMethods({ someMethod }) - expect(wrapper.vm.someMethod).to.equal(someMethod) - }) - it('throws an error if node is not a Vue instance', () => { const message = 'wrapper.setMethods() can only be called on a Vue instance' const compiled = compileToFunctions('

') @@ -24,4 +18,16 @@ describeWithShallowAndMount('setMethods', (mountingMethod) => { const p = wrapper.find('p') expect(() => p.setMethods({ ready: true })).throw(Error, message) }) + + it('should replace methods when tied to an event', () => { + const wrapper = mountingMethod(ComponentWithEvents) + expect(wrapper.vm.isActive).to.be.false + wrapper.find('.toggle').trigger('click') + expect(wrapper.vm.isActive).to.be.true + // Replace the toggle function so that the data supposedly won't change + const toggleActive = () => console.log('overriden') + wrapper.setMethods({ toggleActive }) + wrapper.find('.toggle').trigger('click') + expect(wrapper.vm.isActive).to.be.true + }) })