Skip to content

Commit d064eaa

Browse files
committed
fix: throw error if functional find not supported
closes #296
1 parent c5cab72 commit d064eaa

File tree

3 files changed

+43
-8
lines changed

3 files changed

+43
-8
lines changed

src/lib/consts.js

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
import Vue from 'vue'
2+
13
export const NAME_SELECTOR = 'NAME_SELECTOR'
24
export const COMPONENT_SELECTOR = 'COMPONENT_SELECTOR'
35
export const REF_SELECTOR = 'REF_SELECTOR'
46
export const DOM_SELECTOR = 'DOM_SELECTOR'
7+
export const VUE_VERSION = Number(`${Vue.version.split('.')[0]}.${Vue.version.split('.')[1]}`)
8+
export const FUNCTIONAL_OPTIONS = VUE_VERSION >= 2.5 ? 'fnOptions' : 'functionalOptions'

src/lib/find-vue-components.js

+16-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
// @flow
22
import {
3-
COMPONENT_SELECTOR
3+
COMPONENT_SELECTOR,
4+
FUNCTIONAL_OPTIONS,
5+
VUE_VERSION
46
} from './consts'
7+
import {
8+
throwError
9+
} from './util'
510

611
function findAllVueComponentsFromVm (
712
vm: Component,
@@ -35,7 +40,7 @@ function findAllFunctionalComponentsFromVnode (
3540
vnode: Component,
3641
components: Array<Component> = []
3742
): Array<Component> {
38-
if (vnode.fnOptions) {
43+
if (vnode[FUNCTIONAL_OPTIONS] || vnode.functionalContext) {
3944
components.push(vnode)
4045
}
4146
if (vnode.children) {
@@ -61,11 +66,15 @@ export function vmCtorMatchesSelector (component: Component, selector: Object) {
6166
}
6267

6368
export function vmFunctionalCtorMatchesSelector (component: VNode, Ctor: Object) {
64-
if (!component.fnOptions) {
69+
if (VUE_VERSION < 2.3) {
70+
throwError('find for functional components is not support in Vue < 2.3')
71+
}
72+
73+
if (!component[FUNCTIONAL_OPTIONS]) {
6574
return false
6675
}
67-
const Ctors = Object.keys(component.fnOptions._Ctor)
68-
return Ctors.some(c => Ctor[c] === component.fnOptions._Ctor[c])
76+
const Ctors = Object.keys(component[FUNCTIONAL_OPTIONS]._Ctor)
77+
return Ctors.some(c => Ctor[c] === component[FUNCTIONAL_OPTIONS]._Ctor[c])
6978
}
7079

7180
export default function findVueComponents (
@@ -74,10 +83,10 @@ export default function findVueComponents (
7483
selector: Object
7584
): Array<Component> {
7685
if (selector.functional) {
77-
const components = root._vnode
86+
const nodes = root._vnode
7887
? findAllFunctionalComponentsFromVnode(root._vnode)
7988
: findAllFunctionalComponentsFromVnode(root)
80-
return components.filter(component => vmFunctionalCtorMatchesSelector(component, selector._Ctor))
89+
return nodes.filter(node => vmFunctionalCtorMatchesSelector(node, selector._Ctor))
8190
}
8291
const components = root._isVue
8392
? findAllVueComponentsFromVm(root)

test/unit/specs/mount/Wrapper/find.spec.js

+23-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import ComponentWithVFor from '~resources/components/component-with-v-for.vue'
88
import Component from '~resources/components/component.vue'
99
import FunctionalComponent from '~resources/components/functional-component.vue'
1010
import ComponentAsAClass from '~resources/components/component-as-a-class.vue'
11-
import { functionalSFCsSupported } from '~resources/test-utils'
11+
import { functionalSFCsSupported, vueVersion } from '~resources/test-utils'
1212

1313
describe('find', () => {
1414
it('returns a Wrapper matching tag selector passed', () => {
@@ -141,6 +141,28 @@ describe('find', () => {
141141
expect(wrapper.find(FunctionalComponent).vm).to.equal(undefined)
142142
})
143143

144+
it('returns Wrapper of Vue Component matching functional component with name', () => {
145+
const TestFunctionalComponent = {
146+
render: h => h('div'),
147+
functional: true,
148+
name: 'test-functional-component'
149+
}
150+
const TestComponent = {
151+
template: '<div><test-functional-component /></div>',
152+
components: {
153+
TestFunctionalComponent
154+
}
155+
}
156+
const wrapper = mount(TestComponent)
157+
if (vueVersion < 2.3) {
158+
const message = '[vue-test-utils]: find for functional components is not support in Vue < 2.3'
159+
const fn = () => wrapper.find(TestFunctionalComponent)
160+
expect(fn).to.throw().with.property('message', message)
161+
} else {
162+
expect(wrapper.find(TestFunctionalComponent).exists()).to.equal(true)
163+
}
164+
})
165+
144166
it('returns correct number of Vue Wrappers when component has a v-for', () => {
145167
const items = [{ id: 1 }, { id: 2 }, { id: 3 }]
146168
const wrapper = mount(ComponentWithVFor, { propsData: { items }})

0 commit comments

Comments
 (0)