Skip to content

store not available in child components #122

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

Closed
mediafreakch opened this issue Mar 4, 2020 · 7 comments
Closed

store not available in child components #122

mediafreakch opened this issue Mar 4, 2020 · 7 comments
Labels
bug Something isn't working

Comments

@mediafreakch
Copy link
Contributor

mediafreakch commented Mar 4, 2020

Describe the bug
store is undefined in child components.

To Reproduce
Steps to reproduce the behavior:

  1. Clone https://github.com/mediafreakch/vue-testing-library-bug
  2. run npm install
  3. run npm test

The second test fails.

Expected behavior

Second test passes.

Screenshots

Related information:

  • @testing-library/vue version: 4.1.0
  • Vue version: 2.6.11
  • node version: 12.13.1
  • npm (or yarn) version: npm 6.12.1

Relevant code or config (if any)

Additional context

Unfortunately the example doesn't run in Codesandbox. There is an issue with they're bundler.
You need to clone the repo and run it locally to reproduce the bug.

I managed to get the same app tested using pure @vue/test-utils. I will link the corresponding codesandbox example as soon as I have recovered it. Codesandbox example of @vue/test-utils

@mediafreakch
Copy link
Contributor Author

After all I've read, could this be due to a clash of Babel versions?

vue-testing-library: @babel/[email protected]
vue-test-utils: @babel/[email protected]

Version 7.8.4 includes dynamic-import-node plugin, while the latter does not.
I wouldn't know how to fix though nor if that really can be a factor...

@beeequeue
Copy link

Can confirm this happening, can't write any tests for components that use vuex. 😞

It's not the babel version. I tried a resolution for it to 7.10.2 and it didn't change anything.

The store is available deep in the private apis root._vnode.componentInstance.$store

@afontcu
Copy link
Member

afontcu commented Jul 8, 2020

Hi! 👋

Just gave the OP repro a go. Not sure why, but removing the first mockReturnValueOnce and only running the "should render the chart component" test makes it pass. Same thing if I set the viewInfo mock per test, instead of relying on beforeEach.

In other words – this works:

  beforeEach(() => {
    actions = {
      fetchViewInfo: jest.fn().mockResolvedValue(null),
    }
  })

  it("should render the chart component", async () => {
    store = {
      actions,
      getters: {
        viewInfo: () =>
          jest.fn().mockReturnValueOnce({
            title: "Test Chart",
          }),
      },
    }

    const { findByText } = render(GenericView, {
      store,
      propsData: {
        kind: "Dashboard",
      },
    })

    await findByText("Test Chart") // changed Vue.nextTick() + getByText with findByText, but's that unrelated
  })

@mediafreakch
Copy link
Contributor Author

mediafreakch commented Jul 9, 2020

@afontcu Like this you basically render the Dashboard component, but where props.viewData.items is undefined. So the for loop for the nested <GenericView> component is never invoked. Right?

@afontcu
Copy link
Member

afontcu commented Jul 9, 2020

Yeah, I guess you're right… I gotta say the component is quite complex and right now I don't have the bandwidth to deep dive on this 😅 It is really weird (and I'd say unlikely?) that the store is not available in child components – I've been testing such scenarios myself, and Vue Testing Lib is only importing Vuex and registering the store as a whole in the Vue instance.

If you could come up with a smaller, simpler reproduction case that would mean a lot 🙌

@mediafreakch
Copy link
Contributor Author

mediafreakch commented Jul 15, 2020

@afontcu I updated my repository and tried to simplify the example: https://github.com/mediafreakch/vue-testing-library-bug .

It still demonstrates the same problem: The dynamically imported GenericView.vue component inside Portfolio.vue doesn't have access to the store (when running the tests). Everything works as expected when running in the browser.

I also wanted to let you know, that I posted the same problem at vue-test-utils, as it is reproducible there too: vuejs/vue-test-utils#1486 . Not sure where it makes more sense to dig in...

@afontcu
Copy link
Member

afontcu commented Jul 16, 2020

Hi! Thanks for this 🙌

Didn't notice you posted the same issue in Vue Test Utils. That means this is an upstream issue, and as soon as we find a fix in Vue Test Utils, it will work properly here in Vue Testing Lib, too :)

I'll go ahead and close this up!

@afontcu afontcu closed this as completed Jul 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants