Skip to content

Commit 4fb9dec

Browse files
committed
Merge branch 'dev' of https://github.com/vuejs/vue-test-utils into dev
2 parents be88c2b + 9bfe35d commit 4fb9dec

File tree

7 files changed

+312
-161
lines changed

7 files changed

+312
-161
lines changed

docs/.vuepress/theme/Layout.vue

+1-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@
99
</p>
1010
<p>
1111
To read docs for Vue Test Utils for Vue 3,
12-
<a
13-
href="https://test-utils.vuejs.org/"
14-
v-text="'click here'"
15-
/>.
12+
<a href="https://test-utils.vuejs.org/" v-text="'click here'" />.
1613
</p>
1714
</div>
1815
</div>

packages/create-instance/create-component-stubs.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ function resolveComponent(obj: Object, component: string): Object {
4343
)
4444
}
4545

46-
function getCoreProperties(componentOptions: Component): Object {
46+
function getCoreProperties(componentOptions: Component, name?: string): Object {
4747
return {
4848
attrs: componentOptions.attrs,
49-
name: componentOptions.name,
49+
name: componentOptions.name || name,
5050
model: componentOptions.model,
5151
props: componentOptions.props,
5252
on: componentOptions.on,
@@ -108,7 +108,7 @@ export function createStubFromComponent(
108108
}
109109

110110
return {
111-
...getCoreProperties(componentOptions),
111+
...getCoreProperties(componentOptions, name),
112112
$_vueTestUtils_original: originalComponent,
113113
$_doNotStubChildren: true,
114114
render(h, context) {

packages/create-instance/patch-create-element.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,16 @@ export function patchCreateElement(_Vue, stubs, stubAllComponents) {
6868
}
6969

7070
if (isConstructor(el) || isComponentOptions(el)) {
71+
const componentOptions = isConstructor(el) ? el.options : el
72+
const elName = componentOptions.name
73+
74+
const stubbedComponent = resolveComponent(elName, stubs)
75+
if (stubbedComponent) {
76+
return originalCreateElement(stubbedComponent, ...args)
77+
}
78+
7179
if (stubAllComponents) {
72-
const stub = createStubFromComponent(el, el.name || 'anonymous', _Vue)
80+
const stub = createStubFromComponent(el, elName || 'anonymous', _Vue)
7381
return originalCreateElement(stub, ...args)
7482
}
7583
const Constructor = shouldExtend(el, _Vue) ? extend(el, _Vue) : el

packages/shared/validators.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export function isVueComponent(c: any): boolean {
3333
return true
3434
}
3535

36-
if (c === null || typeof c !== 'object') {
36+
if (!isPlainObject(c)) {
3737
return false
3838
}
3939

@@ -63,7 +63,7 @@ export function componentNeedsCompiling(component: Component): boolean {
6363

6464
export function isRefSelector(refOptionsObject: any): boolean {
6565
if (
66-
typeof refOptionsObject !== 'object' ||
66+
!isPlainObject(refOptionsObject) ||
6767
Object.keys(refOptionsObject || {}).length !== 1
6868
) {
6969
return false
@@ -73,7 +73,7 @@ export function isRefSelector(refOptionsObject: any): boolean {
7373
}
7474

7575
export function isNameSelector(nameOptionsObject: any): boolean {
76-
if (typeof nameOptionsObject !== 'object' || nameOptionsObject === null) {
76+
if (!isPlainObject(nameOptionsObject)) {
7777
return false
7878
}
7979

@@ -89,7 +89,7 @@ export function isDynamicComponent(c: any) {
8989
}
9090

9191
export function isComponentOptions(c: any) {
92-
return c !== null && typeof c === 'object' && (c.template || c.render)
92+
return isPlainObject(c) && (c.template || c.render)
9393
}
9494

9595
export function isFunctionalComponent(c: any) {

packages/test-utils/src/wrapper.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
warnDeprecated,
2020
isVueWrapper
2121
} from 'shared/util'
22+
import { isPlainObject } from 'shared/validators'
2223
import { isElementVisible } from 'shared/is-visible'
2324
import find from './find'
2425
import createWrapper from './create-wrapper'
@@ -719,8 +720,7 @@ export default class Wrapper implements BaseWrapper {
719720
Object.keys(data).forEach(key => {
720721
// Don't let people set entire objects, because reactivity won't work
721722
if (
722-
typeof data[key] === 'object' &&
723-
data[key] !== null &&
723+
isPlainObject(data[key]) &&
724724
// $FlowIgnore : Problem with possibly null this.vm
725725
data[key] === this.vm[key]
726726
) {

test/specs/mounting-options/stubs.spec.js

+86
Original file line numberDiff line numberDiff line change
@@ -704,4 +704,90 @@ describeWithShallowAndMount('options.stub', mountingMethod => {
704704
`</div>`
705705
)
706706
})
707+
708+
it('stubs component inlined in render function with custom stub', () => {
709+
let childComponentCreated = false
710+
let childComponent2Created = false
711+
const ChildComponent = Vue.extend({
712+
name: 'child-component',
713+
template: '<div />',
714+
created() {
715+
childComponentCreated = true
716+
}
717+
})
718+
const ChildComponent2 = {
719+
name: 'child-component-2',
720+
template: '<div />',
721+
created() {
722+
childComponent2Created = true
723+
}
724+
}
725+
726+
const ParentComponent = {
727+
name: 'parent-component',
728+
render(h) {
729+
return h('div', [h(ChildComponent), h(ChildComponent2)])
730+
}
731+
}
732+
733+
const wrapper = mountingMethod(ParentComponent, {
734+
stubs: {
735+
ChildComponent: {
736+
name: 'child-component',
737+
template: '<div class="stub" />'
738+
},
739+
ChildComponent2: {
740+
name: 'child-component-2',
741+
template: '<div class="stub-2" />'
742+
}
743+
}
744+
})
745+
746+
expect(childComponentCreated).toBe(false)
747+
expect(childComponent2Created).toBe(false)
748+
749+
expect(wrapper.find('.stub').exists()).toBe(true)
750+
expect(wrapper.find('.stub-2').exists()).toBe(true)
751+
expect(wrapper.find(ChildComponent).exists()).toBe(true)
752+
expect(wrapper.find(ChildComponent2).exists()).toBe(true)
753+
})
754+
755+
it('stubs component inlined in render function with default stub', () => {
756+
let childComponentCreated = false
757+
let childComponent2Created = false
758+
const ChildComponent = Vue.extend({
759+
name: 'child-component',
760+
template: '<div />',
761+
created() {
762+
childComponentCreated = true
763+
}
764+
})
765+
const ChildComponent2 = {
766+
name: 'child-component-2',
767+
template: '<div />',
768+
created() {
769+
childComponent2Created = true
770+
}
771+
}
772+
773+
const ParentComponent = {
774+
name: 'parent-component',
775+
render(h) {
776+
return h('div', [h(ChildComponent), h(ChildComponent2)])
777+
}
778+
}
779+
780+
const wrapper = mountingMethod(ParentComponent, {
781+
stubs: {
782+
ChildComponent: true,
783+
ChildComponent2: true
784+
}
785+
})
786+
787+
expect(childComponentCreated).toBe(false)
788+
expect(childComponent2Created).toBe(false)
789+
790+
expect(wrapper.find(ChildComponent).exists()).toBe(true)
791+
expect(wrapper.find(ChildComponent2).exists()).toBe(true)
792+
})
707793
})

0 commit comments

Comments
 (0)