-
Notifications
You must be signed in to change notification settings - Fork 112
Testing emit from $root #169
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
Comments
Hi! Can you share a component/test example? |
Sure! The export default {
name: 'ComponentToTest',
created () {
this.$root.$on('event', () => {
// This runs an axios request and conditionally displays a component based on the response
this.fetchFromServerAndDisplayElement()
})
}
...
} Template of <template>
<component-to-display v-if="requestReturnedTrue">
displayThis
</component-to-display>
</template> Then, in my test it('should display a component on root emit', function () {
const { getByText } = render(ComponentToTest, ...)
// Here I somehow want to test that the component responds to the event
// emitted via $root.$emit
expect(getByText('displayThis')).toBeInTheDocument()
}) |
Hi! Vue Testing Library offers a third parameter which gives you access to the vue instance (https://github.com/testing-library/vue-testing-library/blob/master/src/vue-testing-library.js#L51-L53): render(Component, {}, vue => {}) maybe you can leverage it to trigger the event? let me know how it goes! |
Been digging through the instance and haven't been able to find anything that helps me trigger the event unfortunately. I've also attempted to mock |
Let me know if this is a feature the library needs and I'll send in a PR! |
need to get the vue instance object too! in some special environmens, I need to mock some methods on vm |
Can you just wrap the component? const Wrapper = (comp) => {
return {
render: (h) => h('div', h(comp))
}
} Now you can do I also noticed |
Thank you! Can't believe I didn't think of this sooner. This was my solution: it('should respond to root emit', function () {
const Wrapper = {
mounted () {
this.$root.$emit('emit-to-test')
},
render (h) {
return h('div', [ h(ComponentToTest) ])
}
}
// ...assert stuff
})
Thanks for the heads up! |
Any recommendations on testing functionality that depends on listening to events emitted via
$root.$emit
? Like you would viawrapper.vm.$root.$emit
for example usingvue-test-utils
. Haven't been able to find anything in the docs.Thanks for a great library!
The text was updated successfully, but these errors were encountered: