Skip to content

Commit cd1b708

Browse files
authored
docs: Add docs and test for stubbing an async component (vuejs#516)
* docs: Add section for stubbing an async component * test: Add a test case for stubbing an async component with name
1 parent b2a1edc commit cd1b708

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

docs/guide/advanced/stubs-shallow-mount.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,42 @@ test('shallow stubs out all child components', () => {
135135
If you used VTU V1, you may remember this as `shallowMount`. That method is still available, too - it's the same as writing `shallow: true`.
136136
:::
137137

138+
## Stubbing an async component
139+
140+
In case you want to stub out an async component, then make sure to provide a name for the component and use this name as stubs key.
141+
142+
```js
143+
// AsyncComponent.js
144+
export default defineComponent({
145+
name: 'AsyncComponent',
146+
template: '<span>AsyncComponent</span>'
147+
})
148+
149+
// App.js
150+
const App = defineComponent({
151+
components: {
152+
MyComponent: defineAsyncComponent(() => import('./AsyncComponent'))
153+
},
154+
template: '<MyComponent/>'
155+
})
156+
157+
// App.spec.js
158+
test('stubs async component', async () => {
159+
const wrapper = mount(App, {
160+
global: {
161+
stubs: {
162+
// Besure to use the name from AsyncComponent and not "MyComponent"
163+
AsyncComponent: true
164+
}
165+
}
166+
})
167+
168+
await flushPromises()
169+
170+
expect(wrapper.html()).toBe('<async-component-stub></async-component-stub>')
171+
})
172+
```
173+
138174
## Default Slots and `shallow`
139175

140176
Since `shallow` stubs out all the content of a components, any `<slot>` won't get rendered when using `shallow`. While this is not a problem in most cases, there are some scenarios where this isn't ideal.

tests/mountingOptions/stubs.global.spec.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { h, ComponentOptions, defineComponent } from 'vue'
1+
import { h, ComponentOptions, defineComponent, defineAsyncComponent } from 'vue'
22

3-
import { config, mount, RouterLinkStub } from '../../src'
3+
import { config, flushPromises, mount, RouterLinkStub } from '../../src'
44
import Hello from '../components/Hello.vue'
55
import ComponentWithoutName from '../components/ComponentWithoutName.vue'
66
import ComponentWithSlots from '../components/ComponentWithSlots.vue'
@@ -387,6 +387,31 @@ describe('mounting options: stubs', () => {
387387
expect(wrapper.find('#content').exists()).toBe(true)
388388
})
389389

390+
it('stubs async component with name', async () => {
391+
const AsyncComponent = defineComponent({
392+
name: 'AsyncComponent',
393+
template: '<span>AsyncComponent</span>'
394+
})
395+
const TestComponent = defineComponent({
396+
components: {
397+
MyComponent: defineAsyncComponent(async () => AsyncComponent)
398+
},
399+
template: '<MyComponent/>'
400+
})
401+
402+
const wrapper = mount(TestComponent, {
403+
global: {
404+
stubs: {
405+
AsyncComponent: true
406+
}
407+
}
408+
})
409+
410+
await flushPromises()
411+
412+
expect(wrapper.html()).toBe('<async-component-stub></async-component-stub>')
413+
})
414+
390415
describe('stub slots', () => {
391416
const Component = {
392417
name: 'Parent',

0 commit comments

Comments
 (0)