question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Event not being caught on child component?

See original GitHub issue

It seems that when I do a dispatch() on a vue-component that is a child element of the component I’m testing, the event is either not fired at all or not caught by the vue event handler.

If I replace the ui-navigation-level component with a simple div, the event is caught.

This is the component (stripped to bare functionality):

<template>
    <ul class="ui-navigation">
        <ui-navigation-level
            :options="options"
            @select="handleSelect"
        ></ui-navigation-level>
    </ul>
</template>

<script>
    import NavigationLevel from './ui-navigation-level.vue'

    export default {
        name: 'ui-navigation',

        props: {
            options: {
                type: Array,
                required: true,
            },
        },

        components: {
            'ui-navigation-level': NavigationLevel,
        },

        methods: {
            handleSelect(option) {
                this.$emit('select', option)
            },
        },
    }
</script>

And the test:

it('handles the select event', () => {
    const handleSelect = sinon.spy(Navigation.methods, 'handleSelect')

    const wrapper = mount(Navigation, {
        propsData: {
            options: [
                /* ... */
            ],
        },
    })

    wrapper.find('.ui-navigation-level')[0].dispatch('select')

    expect(handleSelect).to.be.calledOnce //Result: called 0 times
})

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
mattixittamcommented, Jun 12, 2017

Yup, when finding using the component object, it works!

Hmm, found the (an) issue, I think. This returns undefined:

wrapper.find('.ui-navigation-level')[0].isVueComponent

0reactions
eddyerburghcommented, Jun 12, 2017

Hmm, found the (an) issue, I think. This returns undefined: wrapper.find(‘.ui-navigation-level’)[0].isVueComponent

Vue components and DOM nodes are treated differently. If you use a DOM selector, it will return a vnode - not a vue instance.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Component event not being caught - Salesforce Stack Exchange
A component event will not be caught by child components and while an application event will be caught, it's very expensive and inefficient ......
Read more >
Why is Vue event not being caught in parent? - Stack Overflow
I'm new to Vue and trying to get the hang of the trivial example of sending an event from a child component. I...
Read more >
7. Listen to child component events - Angular and NgRx
The EventEmitter in Angular is used to emit event from child components to parent components and can pass what ever object, array primitive, ......
Read more >
Component Events | Vue.js
Unlike native DOM events, component emitted events do not bubble. You can only listen to the events emitted by a direct child component....
Read more >
Parent not catching events fired in child component-Vue.js
means that the root Vue is listening for the child component to emit a click event, and that doesn't appear to be your...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found