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

issue-249 feat: stub global components #260

Merged
merged 3 commits into from
Nov 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/RouterLinkStub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ export const RouterLinkStub = defineComponent({
},

render() {
return h('a', undefined, this.$slots.default())
return h('a', undefined, this.$slots?.default?.())
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is possible to use router-link <router-link /> (however unlikely)

}
})
20 changes: 19 additions & 1 deletion src/mount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ import { createWrapper, VueWrapper } from './vueWrapper'
import { attachEmitListener } from './emitMixin'
import { createDataMixin } from './dataMixin'
import { MOUNT_COMPONENT_REF, MOUNT_PARENT_NAME } from './constants'
import { stubComponents } from './stubs'
import { createStub, stubComponents } from './stubs'
import { hyphenate } from './utils/vueShared'

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

// users expect stubs to work with globally registered
// compnents, too, such as <router-link> and <router-view>
// so we register those globally.
// https://github.com/vuejs/vue-test-utils-next/issues/249
if (global?.stubs) {
for (const [name, stub] of Object.entries(global.stubs)) {
const tag = hyphenate(name)
if (stub === true) {
// default stub.
app.component(tag, createStub({ name, props: {} }))
} else {
// user has provided a custom implementation.
app.component(tag, stub)
}
}
}

// mount the app!
const vm = app.mount(el)

Expand Down
2 changes: 1 addition & 1 deletion src/stubs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function getSlots(ctx: ComponentPublicInstance): Slots | undefined {
return !config.renderStubDefaultSlot ? undefined : ctx.$slots
}

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

Expand Down
29 changes: 27 additions & 2 deletions tests/mountingOptions/stubs.global.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { h, ComponentOptions } from 'vue'
import { h, ComponentOptions, defineComponent } from 'vue'

import { config, mount } from '../../src'
import { config, mount, RouterLinkStub } from '../../src'
import Hello from '../components/Hello.vue'
import ComponentWithoutName from '../components/ComponentWithoutName.vue'
import ComponentWithSlots from '../components/ComponentWithSlots.vue'
Expand Down Expand Up @@ -37,6 +37,31 @@ describe('mounting options: stubs', () => {
expect(wrapper.html()).toBe('<div></div><foo-stub></foo-stub>')
})

// https://github.com/vuejs/vue-test-utils-next/issues/249
it('applies stubs globally', () => {
const Comp = defineComponent({
template: '<div><foo /><router-link to="/foo" /><router-view /></div>'
})
const wrapper = mount(Comp, {
global: {
stubs: {
Foo: true,
RouterLink: RouterLinkStub,
RouterView: defineComponent({
render() {
return h('span')
}
})
}
}
})

expect(wrapper.html()).toBe(
'<div><foo-stub></foo-stub><a></a><span></span></div>'
)
Copy link
Member

@cexbrayat cexbrayat Nov 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I'm a bit unclear on how everything is related, can you maybe add a test where we try to find the component registered as a stub? I'd like to be sure that getComponent and friends work in that case as well.

I think a real use-case would be to stub RouterLink with RouterLinkStub, find it, and check its props to see if the component is creating links with the appropriate values. So I'd like to make sure we cover this 😅

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done: 20c7694

Is this what you mean?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect, thanks Lachlan!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, want to give me a big old ✅

expect(wrapper.getComponent(RouterLinkStub).vm.to).toBe('/foo')
})

it('stubs a functional component by its variable declaration name', () => {
const FunctionalFoo = (props) => h('p', props, 'Foo Text')

Expand Down