Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/vuejs/vue-test-utils into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
lmiller1990 committed Jan 27, 2023
2 parents be88c2b + 9bfe35d commit 4fb9dec
Show file tree
Hide file tree
Showing 7 changed files with 312 additions and 161 deletions.
5 changes: 1 addition & 4 deletions docs/.vuepress/theme/Layout.vue
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
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
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
8 changes: 4 additions & 4 deletions packages/shared/validators.js
Expand Up @@ -33,7 +33,7 @@ export function isVueComponent(c: any): boolean {
return true
}

if (c === null || typeof c !== 'object') {
if (!isPlainObject(c)) {
return false
}

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

export function isRefSelector(refOptionsObject: any): boolean {
if (
typeof refOptionsObject !== 'object' ||
!isPlainObject(refOptionsObject) ||
Object.keys(refOptionsObject || {}).length !== 1
) {
return false
Expand All @@ -73,7 +73,7 @@ export function isRefSelector(refOptionsObject: any): boolean {
}

export function isNameSelector(nameOptionsObject: any): boolean {
if (typeof nameOptionsObject !== 'object' || nameOptionsObject === null) {
if (!isPlainObject(nameOptionsObject)) {
return false
}

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

export function isComponentOptions(c: any) {
return c !== null && typeof c === 'object' && (c.template || c.render)
return isPlainObject(c) && (c.template || c.render)
}

export function isFunctionalComponent(c: any) {
Expand Down
4 changes: 2 additions & 2 deletions packages/test-utils/src/wrapper.js
Expand Up @@ -19,6 +19,7 @@ import {
warnDeprecated,
isVueWrapper
} from 'shared/util'
import { isPlainObject } from 'shared/validators'
import { isElementVisible } from 'shared/is-visible'
import find from './find'
import createWrapper from './create-wrapper'
Expand Down Expand Up @@ -719,8 +720,7 @@ export default class Wrapper implements BaseWrapper {
Object.keys(data).forEach(key => {
// Don't let people set entire objects, because reactivity won't work
if (
typeof data[key] === 'object' &&
data[key] !== null &&
isPlainObject(data[key]) &&
// $FlowIgnore : Problem with possibly null this.vm
data[key] === this.vm[key]
) {
Expand Down
86 changes: 86 additions & 0 deletions test/specs/mounting-options/stubs.spec.js
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)
})
})

0 comments on commit 4fb9dec

Please sign in to comment.