Skip to content
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
21 changes: 12 additions & 9 deletions src/lib/find-vue-components.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// @flow
import {
COMPONENT_SELECTOR,
FUNCTIONAL_OPTIONS,
VUE_VERSION
} from './consts'
Expand Down Expand Up @@ -52,15 +51,20 @@ function findAllFunctionalComponentsFromVnode (
}

export function vmCtorMatchesName (vm: Component, name: string): boolean {
return (vm.$vnode && vm.$vnode.componentOptions &&
return !!((vm.$vnode && vm.$vnode.componentOptions &&
vm.$vnode.componentOptions.Ctor.options.name === name) ||
(vm._vnode && vm._vnode.functionalOptions &&
vm._vnode.functionalOptions.name === name) ||
vm.$options && vm.$options.name === name
(vm._vnode &&
vm._vnode.functionalOptions &&
vm._vnode.functionalOptions.name === name) ||
vm.$options && vm.$options.name === name ||
vm.options && vm.options.name === name)
}

export function vmCtorMatchesSelector (component: Component, selector: Object) {
const Ctor = selector._Ctor || selector.options && selector.options._Ctor
const Ctor = selector._Ctor || (selector.options && selector.options._Ctor)
if (!Ctor) {
return false
}
const Ctors = Object.keys(Ctor)
return Ctors.some(c => Ctor[c] === component.__proto__.constructor)
}
Expand Down Expand Up @@ -95,15 +99,14 @@ export default function findVueComponents (
node[FUNCTIONAL_OPTIONS].name === selector.name
)
}
const nameSelector = typeof selector === 'function' ? selector.options.name : selector.name
const components = root._isVue
? findAllVueComponentsFromVm(root)
: findAllVueComponentsFromVnode(root)
return components.filter((component) => {
if (!component.$vnode && !component.$options.extends) {
return false
}
return selectorType === COMPONENT_SELECTOR
? vmCtorMatchesSelector(component, selector)
: vmCtorMatchesName(component, selector.name)
return vmCtorMatchesSelector(component, selector) || vmCtorMatchesName(component, nameSelector)
})
}
2 changes: 1 addition & 1 deletion test/resources/components/component-as-a-class.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<div />
<div><slot /></div>
</template>

<script>
Expand Down
8 changes: 8 additions & 0 deletions test/resources/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,11 @@ export function itSkipIf (predicate, spec, cb) {
it(spec, cb)
}
}

export function itDoNotRunIf (predicate, spec, cb) {
if (predicate) {
() => {}
} else {
it(spec, cb)
}
}
6 changes: 3 additions & 3 deletions test/specs/mounting-options/attachToDocument.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { compileToFunctions } from 'vue-template-compiler'
import { mount } from '~vue-test-utils'
import { describeWithShallowAndMount } from '~resources/test-utils'

describe('mount.attachToDocument', () => {
describeWithShallowAndMount('options.attachToDocument', (mountingMethod) => {
it('returns VueWrapper with attachedToDocument set to true when passed attachToDocument in options', () => {
const compiled = compileToFunctions('<div><input /></div>')
const wrapper = mount(compiled, { attachToDocument: true })
const wrapper = mountingMethod(compiled, { attachToDocument: true })
expect(wrapper.options.attachedToDocument).to.equal(true)
})
})
8 changes: 4 additions & 4 deletions test/specs/mounting-options/attrs.spec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { compileToFunctions } from 'vue-template-compiler'
import { mount } from '~vue-test-utils'
import { attrsSupported } from '~resources/test-utils'
import { describeWithShallowAndMount } from '~resources/test-utils'

describe('mount.attrs', () => {
describeWithShallowAndMount('options.attrs', (mountingMethod) => {
it('handles inherit attrs', () => {
if (!attrsSupported()) return
const wrapper = mount(compileToFunctions('<p :id="anAttr" />'), {
const wrapper = mountingMethod(compileToFunctions('<p :id="anAttr" />'), {
attrs: {
anAttr: 'an attribute'
}
Expand All @@ -16,7 +16,7 @@ describe('mount.attrs', () => {
})

it('defines attrs as empty object even when not passed', () => {
const wrapper = mount(compileToFunctions('<p />'))
const wrapper = mountingMethod(compileToFunctions('<p />'))
expect(wrapper.vm.$attrs).to.deep.equal({})
})
})
18 changes: 9 additions & 9 deletions test/specs/mounting-options/context.spec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import Vue from 'vue'
import { mount } from '~vue-test-utils'
import { vueVersion } from '~resources/test-utils'
import { describeWithShallowAndMount } from '~resources/test-utils'

describe('context', () => {
describeWithShallowAndMount('options.context', (mountingMethod) => {
it('mounts functional component when passed context object', () => {
if (vueVersion <= 2.2) {
console.log('WARN: no current way to test functional component is component in v2.1.x')
Expand All @@ -21,7 +21,7 @@ describe('context', () => {
props: { show: true }
}

const wrapper = mount(Component, { context })
const wrapper = mountingMethod(Component, { context })
expect(wrapper.is(Component)).to.equal(true)
})

Expand All @@ -31,7 +31,7 @@ describe('context', () => {
}
const context = {}
const message = '[vue-test-utils]: mount.context can only be used when mounting a functional component'
const fn = () => mount(Component, { context })
const fn = () => mountingMethod(Component, { context })
expect(fn).to.throw().with.property('message', message)
})

Expand All @@ -41,7 +41,7 @@ describe('context', () => {
render: h => h('div')
})
const context = {}
const fn = () => mount(Component, { context })
const fn = () => mountingMethod(Component, { context })
expect(fn).not.to.throw()
})

Expand All @@ -52,7 +52,7 @@ describe('context', () => {
}
const context = 'string'
const message = '[vue-test-utils]: mount.context must be an object'
const fn = () => mount(Component, { context })
const fn = () => mountingMethod(Component, { context })
expect(fn).to.throw().with.property('message', message)
})

Expand All @@ -68,7 +68,7 @@ describe('context', () => {
},
render: (h, { props }) => h('div', props.testProp)
}
const wrapper = mount(Component)
const wrapper = mountingMethod(Component)
expect(wrapper.element.textContent).to.equal(defaultValue)
})

Expand All @@ -79,7 +79,7 @@ describe('context', () => {
return h('div', children)
}
}
const wrapper = mount(Component, {
const wrapper = mountingMethod(Component, {
context: {
children: ['render text']
}
Expand All @@ -94,7 +94,7 @@ describe('context', () => {
return h('div', children)
}
}
const wrapper = mount(Component, {
const wrapper = mountingMethod(Component, {
context: {
children: [h => h('div', 'render component')]
}
Expand Down
8 changes: 4 additions & 4 deletions test/specs/mounting-options/listeners.spec.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { compileToFunctions } from 'vue-template-compiler'
import { mount } from '~vue-test-utils'
import { listenersSupported } from '~resources/test-utils'
import { describeWithShallowAndMount } from '~resources/test-utils'

describe('mount.listeners', () => {
describeWithShallowAndMount('options.listeners', (mountingMethod) => {
it('handles inherit listeners', () => {
if (!listenersSupported()) return
const aListener = () => {}
const wrapper = mount(compileToFunctions('<p :id="aListener" />'), {
const wrapper = mountingMethod(compileToFunctions('<p :id="aListener" />'), {
listeners: {
aListener
}
Expand All @@ -18,7 +18,7 @@ describe('mount.listeners', () => {
})

it('defines listeners as empty object even when not passed', () => {
const wrapper = mount(compileToFunctions('<p />'))
const wrapper = mountingMethod(compileToFunctions('<p />'))
expect(wrapper.vm.$listeners).to.deep.equal({})
})
})
9 changes: 4 additions & 5 deletions test/specs/mounting-options/localVue.spec.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import Vue from 'vue'
import { mount } from '~vue-test-utils'
import Component from '~resources/components/component.vue'
import { describeWithShallowAndMount } from '~resources/test-utils'

describe('mount.localVue', () => {
describeWithShallowAndMount('options.localVue', (mountingMethod) => {
it('mounts component using passed localVue as base Vue', () => {
const localVue = Vue.extend()
localVue.version = '2.3'
const wrapper = mount(Component, { localVue: localVue, mocks: { test: true }})
const wrapper = mountingMethod(Component, { localVue: localVue, mocks: { test: true }})
expect(wrapper.vm.test).to.equal(true)
const freshWrapper = mount(Component)
const freshWrapper = mountingMethod(Component)
expect(typeof freshWrapper.vm.test).to.equal('undefined')
})
})

47 changes: 26 additions & 21 deletions test/specs/mounting-options/mocks.spec.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { mount } from '~vue-test-utils'
import { createLocalVue } from '~vue-test-utils'
import Component from '~resources/components/component.vue'
import ComponentWithVuex from '~resources/components/component-with-vuex.vue'
import {
describeWithShallowAndMount,
itDoNotRunIf
} from '~resources/test-utils'

describe('mount.mocks', () => {
describeWithShallowAndMount('options.mocks', (mountingMethod) => {
it('adds variables to vm when passed as mocks object', () => {
const $store = { store: true }
const $route = { path: 'http://test.com' }
const wrapper = mount(Component, {
const wrapper = mountingMethod(Component, {
mocks: {
$store,
$route
Expand All @@ -20,7 +23,7 @@ describe('mount.mocks', () => {
it('adds variables to vm when passed as mocks object', () => {
const stub = sinon.stub()
const $reactiveMock = { value: 'value' }
const wrapper = mount({
const wrapper = mountingMethod({
template: `
<div>
{{value}}
Expand All @@ -45,23 +48,25 @@ describe('mount.mocks', () => {
expect(wrapper.text()).to.contain('changed value')
})

it('adds variables available to nested vms', () => {
const count = 1
const wrapper = mount({
template: '<div><component-with-vuex /></div>',
components: {
ComponentWithVuex
}
}, {
mocks: { $store: { state: { count, foo: {}}}}
itDoNotRunIf(mountingMethod.name === 'shallow',
'adds variables available to nested vms', () => {
const count = 1
const wrapper = mountingMethod({
template: '<div><component-with-vuex /></div>',
components: {
ComponentWithVuex
}
}, {
mocks: { $store: { state: { count, foo: {}}}}
})
expect(wrapper.text()).contains(count)
})
expect(wrapper.text()).contains(count)
})

it('adds variables available to nested vms using localVue', () => {
itDoNotRunIf(mountingMethod.name === 'shallow',
'adds variables available to nested vms using localVue', () => {
const localVue = createLocalVue()
const count = 1
const wrapper = mount({
const wrapper = mountingMethod({
template: '<div><component-with-vuex /></div>',
components: {
ComponentWithVuex
Expand All @@ -75,13 +80,13 @@ describe('mount.mocks', () => {

it('does not affect global vue class when passed as mocks object', () => {
const $store = { store: true }
const wrapper = mount(Component, {
const wrapper = mountingMethod(Component, {
mocks: {
$store
}
})
expect(wrapper.vm.$store).to.equal($store)
const freshWrapper = mount(Component)
const freshWrapper = mountingMethod(Component)
expect(typeof freshWrapper.vm.$store).to.equal('undefined')
})

Expand All @@ -92,7 +97,7 @@ describe('mount.mocks', () => {
value: 42
})
const $store = 64
mount(Component, {
mountingMethod(Component, {
localVue,
mocks: {
$store
Expand All @@ -110,7 +115,7 @@ describe('mount.mocks', () => {
value: 42
})
const $val = 64
mount(Component, {
mountingMethod(Component, {
localVue,
mocks: {
$val
Expand Down
7 changes: 4 additions & 3 deletions test/specs/mounting-options/provide.spec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { mount } from '~vue-test-utils'
import ComponentWithInject from '~resources/components/component-with-inject.vue'
import { injectSupported } from '~resources/test-utils'
import { describeWithShallowAndMount } from '~resources/test-utils'

describe('mount.provide', () => {
describeWithShallowAndMount('options.provide', (mountingMethod) => {
it('provides objects which is injected by mounted component', () => {
if (!injectSupported()) return

Expand All @@ -16,7 +17,7 @@ describe('mount.provide', () => {
it('provides function which is injected by mounted component', () => {
if (!injectSupported()) return

const wrapper = mount(ComponentWithInject, {
const wrapper = mountingMethod(ComponentWithInject, {
provide () {
return {
fromMount: 'functionValue'
Expand All @@ -30,7 +31,7 @@ describe('mount.provide', () => {
it('supports beforeCreate in component', () => {
if (!injectSupported()) return

const wrapper = mount(ComponentWithInject, {
const wrapper = mountingMethod(ComponentWithInject, {
provide: { fromMount: '_' }
})

Expand Down
Loading