Skip to content

fix: method should be updated when triggering #622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 })
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should trigger toggle active again after you call setMethods, to make sure it's been overwritten

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eddyerburgh My bad, totally missed that part. I've updated the PR. Thanks.

wrapper.find('.toggle').trigger('click')
expect(wrapper.vm.isActive).to.be.true
})
})