The Wayback Machine - https://web.archive.org/web/20201212162943/https://github.com/vuejs/vue-test-utils/issues/1727
Skip to content
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

Calling .props() on a child component returns props for the parent #1727

Open
bkuzma opened this issue Nov 4, 2020 · 0 comments
Open

Calling .props() on a child component returns props for the parent #1727

bkuzma opened this issue Nov 4, 2020 · 0 comments

Comments

@bkuzma
Copy link

@bkuzma bkuzma commented Nov 4, 2020

Subject of the issue

Let's say you have a Vue component Foo which simply renders another component Bar. If you shallow mount Foo, then find() Bar via a data attribute and call props() on the resulting wrapper, you'll get the props of Foo rather than Bar.

Steps to reproduce

I'm not sure how to set up a code sandbox with a test runner but here's a simplified example:

# Foo.vue

<template>
    <Bar data-testid="bar" :bar-thing="fooThing"></Bar>
</template>

<script>
import Bar from '@/components/Bar'

export default {
    components: {
        Bar,
    },
    props: {
        fooThing: {
            type: Number,
            default: 0,
        },
    },
}
</script>

# Foo.spec.js

it('sets props correctly on Bar', () => {
    const wrapper = shallowMount(Foo, {
        propsData: {
            fooThing: 100,
        },
    })

    console.log(wrapper.find('[data-testid="bar"]').props())
})

Expected behaviour

You would expect to see Bar's props logged to the console:

{ barThing: 100 }

Actual behaviour

Instead you see Foo's props logged to the console:

{ fooThing: 100 }

Possible Solution

I noticed that if Foo is changed so that it wraps the Bar instance in a div, we get the results we expect:

<template>
    <div>
        <Bar data-testid="bar" :bar-thing="4"></Bar>
    </div>
</template>

Alternatively, if we find Bar by importing it and using findComponent(Bar), we get the results we expect.

Neither of these solutions is great in my view because the first requires extra markup and the second will give us issues if we have more than one instance of Bar and we want to use a data attribute selector. In my case, my Bar is actually a Vuetify v-dialog component, and when I try to import it and use findComponent, I get babel issues in Jest.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant
You can’t perform that action at this time.