Skip to content

Commit fc5aa0a

Browse files
authored
Merge pull request #260 from vuejs/lm/issue-249
issue-249 feat: stub global components
2 parents 0b4f762 + 20c7694 commit fc5aa0a

File tree

4 files changed

+48
-5
lines changed

4 files changed

+48
-5
lines changed

src/components/RouterLinkStub.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ export const RouterLinkStub = defineComponent({
1212
},
1313

1414
render() {
15-
return h('a', undefined, this.$slots.default())
15+
return h('a', undefined, this.$slots?.default?.())
1616
}
1717
})

src/mount.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ import { createWrapper, VueWrapper } from './vueWrapper'
3434
import { attachEmitListener } from './emitMixin'
3535
import { createDataMixin } from './dataMixin'
3636
import { MOUNT_COMPONENT_REF, MOUNT_PARENT_NAME } from './constants'
37-
import { stubComponents } from './stubs'
37+
import { createStub, stubComponents } from './stubs'
38+
import { hyphenate } from './utils/vueShared'
3839

3940
// NOTE this should come from `vue`
4041
type PublicProps = VNodeProps & AllowedComponentProps & ComponentCustomProps
@@ -405,6 +406,23 @@ export function mount(
405406
// stub out Transition and Transition Group by default.
406407
stubComponents(global.stubs, options?.shallow)
407408

409+
// users expect stubs to work with globally registered
410+
// compnents, too, such as <router-link> and <router-view>
411+
// so we register those globally.
412+
// https://github.com/vuejs/vue-test-utils-next/issues/249
413+
if (global?.stubs) {
414+
for (const [name, stub] of Object.entries(global.stubs)) {
415+
const tag = hyphenate(name)
416+
if (stub === true) {
417+
// default stub.
418+
app.component(tag, createStub({ name, props: {} }))
419+
} else {
420+
// user has provided a custom implementation.
421+
app.component(tag, stub)
422+
}
423+
}
424+
}
425+
408426
// mount the app!
409427
const vm = app.mount(el)
410428

src/stubs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function getSlots(ctx: ComponentPublicInstance): Slots | undefined {
2525
return !config.renderStubDefaultSlot ? undefined : ctx.$slots
2626
}
2727

28-
const createStub = ({ name, props }: StubOptions): ComponentOptions => {
28+
export const createStub = ({ name, props }: StubOptions): ComponentOptions => {
2929
const anonName = 'anonymous-stub'
3030
const tag = name ? `${hyphenate(name)}-stub` : anonName
3131

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 } from 'vue'
1+
import { h, ComponentOptions, defineComponent } from 'vue'
22

3-
import { config, mount } from '../../src'
3+
import { config, 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'
@@ -37,6 +37,31 @@ describe('mounting options: stubs', () => {
3737
expect(wrapper.html()).toBe('<div></div><foo-stub></foo-stub>')
3838
})
3939

40+
// https://github.com/vuejs/vue-test-utils-next/issues/249
41+
it('applies stubs globally', () => {
42+
const Comp = defineComponent({
43+
template: '<div><foo /><router-link to="/foo" /><router-view /></div>'
44+
})
45+
const wrapper = mount(Comp, {
46+
global: {
47+
stubs: {
48+
Foo: true,
49+
RouterLink: RouterLinkStub,
50+
RouterView: defineComponent({
51+
render() {
52+
return h('span')
53+
}
54+
})
55+
}
56+
}
57+
})
58+
59+
expect(wrapper.html()).toBe(
60+
'<div><foo-stub></foo-stub><a></a><span></span></div>'
61+
)
62+
expect(wrapper.getComponent(RouterLinkStub).vm.to).toBe('/foo')
63+
})
64+
4065
it('stubs a functional component by its variable declaration name', () => {
4166
const FunctionalFoo = (props) => h('p', props, 'Foo Text')
4267

0 commit comments

Comments
 (0)