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

Stub does not work for async component #514

Closed
freakzlike opened this issue Apr 1, 2021 · 4 comments · Fixed by #516
Closed

Stub does not work for async component #514

freakzlike opened this issue Apr 1, 2021 · 4 comments · Fixed by #516

Comments

@freakzlike
Copy link
Collaborator

When trying to stub an async component it still renders to original component instead of the stub component.

Version: 2.0.0-rc.4

import { flushPromises, mount } from '@vue/test-utils'
import { defineAsyncComponent, defineComponent } from '@vue/runtime-core'

it('should stub async component', async () => {
  const AsyncComponent = defineComponent({
    template: '<span>AsyncComponent</span>'
  })
  const StubComponent = defineComponent({
    template: '<span>StubComponent</span>'
  })
  const TestComponent = defineComponent({
    template: '<MyComponent/>',
    components: {
      MyComponent: defineAsyncComponent(async () => AsyncComponent)
    }
  })

  const wrapper = mount(TestComponent, {
    global: {
      stubs: {
        MyComponent: StubComponent
      }
    }
  })

  await flushPromises()

  expect(wrapper.html()).toContain('StubComponent')
})

Expected HTML to be <span>StubComponent</span> instead of <span>AsyncComponent</span>.

@freakzlike
Copy link
Collaborator Author

I found out that it is required to set a name in the async component and use this name in global.stubs.

import { flushPromises, mount } from '@vue/test-utils'
import { defineAsyncComponent, defineComponent } from '@vue/runtime-core'

it('should stub async component', async () => {
  const AsyncComponent = defineComponent({
    name: 'AsyncComponent',
    template: '<span>AsyncComponent</span>'
  })
  const StubComponent = defineComponent({
    template: '<span>StubComponent</span>'
  })
  const TestComponent = defineComponent({
    template: '<MyComponent/>',
    components: {
      MyComponent: defineAsyncComponent(async () => AsyncComponent)
    }
  })

  const wrapper = mount(TestComponent, {
    global: {
      stubs: {
        AsyncComponent: StubComponent
      }
    }
  })

  await flushPromises()

  expect(wrapper.html()).toContain('StubComponent')
})

I have created an pull request which adds a section to the docs for stubbing an async component.

See #516

@afontcu
Copy link
Member

afontcu commented Apr 2, 2021

Hi! Thank you for the reproduction and the effort to open up a PR ❤️

I feel this could be handled by VTU internals, instead of asking users to add a name to a component? 🤔

@freakzlike
Copy link
Collaborator Author

@afontcu You are right. I will have a look at it

@freakzlike
Copy link
Collaborator Author

I created a PR to handle async components the same way as "sync" components. See #518

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

Successfully merging a pull request may close this issue.

2 participants