Skip to content

Commit

Permalink
fix: method should be updated when triggering
Browse files Browse the repository at this point in the history
  • Loading branch information
briwa authored and eddyerburgh committed May 17, 2018
1 parent dc12334 commit 3922ab7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
7 changes: 7 additions & 0 deletions packages/test-utils/src/wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 13 additions & 7 deletions test/specs/wrapper/setMethods.spec.js
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -10,18 +11,23 @@ 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('<div><p></p></div>')
const wrapper = mountingMethod(compiled)
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
})
})

0 comments on commit 3922ab7

Please sign in to comment.