Skip to content

fix: stubs components inlined in render function #2017

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

Merged
merged 3 commits into from
Jan 27, 2023
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
5 changes: 1 addition & 4 deletions docs/.vuepress/theme/Layout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
</p>
<p>
To read docs for Vue Test Utils for Vue 3,
<a
href="https://test-utils.vuejs.org/"
v-text="'click here'"
/>.
<a href="https://test-utils.vuejs.org/" v-text="'click here'" />.
</p>
</div>
</div>
Expand Down
6 changes: 3 additions & 3 deletions packages/create-instance/create-component-stubs.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ function resolveComponent(obj: Object, component: string): Object {
)
}

function getCoreProperties(componentOptions: Component): Object {
function getCoreProperties(componentOptions: Component, name?: string): Object {
return {
attrs: componentOptions.attrs,
name: componentOptions.name,
name: componentOptions.name || name,
model: componentOptions.model,
props: componentOptions.props,
on: componentOptions.on,
Expand Down Expand Up @@ -108,7 +108,7 @@ export function createStubFromComponent(
}

return {
...getCoreProperties(componentOptions),
...getCoreProperties(componentOptions, name),
$_vueTestUtils_original: originalComponent,
$_doNotStubChildren: true,
render(h, context) {
Expand Down
10 changes: 9 additions & 1 deletion packages/create-instance/patch-create-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,16 @@ export function patchCreateElement(_Vue, stubs, stubAllComponents) {
}

if (isConstructor(el) || isComponentOptions(el)) {
const componentOptions = isConstructor(el) ? el.options : el
const elName = componentOptions.name

const stubbedComponent = resolveComponent(elName, stubs)
if (stubbedComponent) {
return originalCreateElement(stubbedComponent, ...args)
}

if (stubAllComponents) {
const stub = createStubFromComponent(el, el.name || 'anonymous', _Vue)
const stub = createStubFromComponent(el, elName || 'anonymous', _Vue)
return originalCreateElement(stub, ...args)
}
const Constructor = shouldExtend(el, _Vue) ? extend(el, _Vue) : el
Expand Down
86 changes: 86 additions & 0 deletions test/specs/mounting-options/stubs.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -704,4 +704,90 @@ describeWithShallowAndMount('options.stub', mountingMethod => {
`</div>`
)
})

it('stubs component inlined in render function with custom stub', () => {
let childComponentCreated = false
let childComponent2Created = false
const ChildComponent = Vue.extend({
name: 'child-component',
template: '<div />',
created() {
childComponentCreated = true
}
})
const ChildComponent2 = {
name: 'child-component-2',
template: '<div />',
created() {
childComponent2Created = true
}
}

const ParentComponent = {
name: 'parent-component',
render(h) {
return h('div', [h(ChildComponent), h(ChildComponent2)])
}
}

const wrapper = mountingMethod(ParentComponent, {
stubs: {
ChildComponent: {
name: 'child-component',
template: '<div class="stub" />'
},
ChildComponent2: {
name: 'child-component-2',
template: '<div class="stub-2" />'
}
}
})

expect(childComponentCreated).toBe(false)
expect(childComponent2Created).toBe(false)

expect(wrapper.find('.stub').exists()).toBe(true)
expect(wrapper.find('.stub-2').exists()).toBe(true)
expect(wrapper.find(ChildComponent).exists()).toBe(true)
expect(wrapper.find(ChildComponent2).exists()).toBe(true)
})

it('stubs component inlined in render function with default stub', () => {
let childComponentCreated = false
let childComponent2Created = false
const ChildComponent = Vue.extend({
name: 'child-component',
template: '<div />',
created() {
childComponentCreated = true
}
})
const ChildComponent2 = {
name: 'child-component-2',
template: '<div />',
created() {
childComponent2Created = true
}
}

const ParentComponent = {
name: 'parent-component',
render(h) {
return h('div', [h(ChildComponent), h(ChildComponent2)])
}
}

const wrapper = mountingMethod(ParentComponent, {
stubs: {
ChildComponent: true,
ChildComponent2: true
}
})

expect(childComponentCreated).toBe(false)
expect(childComponent2Created).toBe(false)

expect(wrapper.find(ChildComponent).exists()).toBe(true)
expect(wrapper.find(ChildComponent2).exists()).toBe(true)
})
})