10000 fix: correctly find functional components (#1835) · vuejs/vue-test-utils@c14d6fd · GitHub
[go: up one dir, main page]

Skip to content

Commit c14d6fd

Browse files
authored
fix: correctly find functional components (#1835)
* fix: correctly find functional components * test: skip test in <2.6
1 parent 97fdf18 commit c14d6fd

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed

packages/test-utils/src/matches.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,14 @@ export function matches(node, selector) {
5656
return element && element.matches && element.matches(selector.value)
5757
}
5858

59-
const componentInstance = node[FUNCTIONAL_OPTIONS] || node.child
59+
const isFunctionalSelector = isConstructor(selector.value)
60+
? selector.value.options.functional
61+
: selector.value.functional
62+
63+
const componentInstance =
64+
(isFunctionalSelector ? node[FUNCTIONAL_OPTIONS] : node.child) ||
65+
node[FUNCTIONAL_OPTIONS] ||
66+
node.child
6067

6168
if (!componentInstance) {
6269
return false

test/specs/create-local-vue.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ describeWithShallowAndMount('createLocalVue', mountingMethod => {
6767
})
6868

6969
itDoNotRunIf(
70-
mountingMethod.name === 'shallowMount',
70+
mountingMethod.name === 'shallowMount' || vueVersion < 2.6,
7171
'Router should work properly with local Vue',
7272
async () => {
7373
const localVue = createLocalVue()

test/specs/wrapper/find.spec.js

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { compileToFunctions } from 'vue-template-compiler'
2-
import { createLocalVue } from 'packages/test-utils/src'
2+
import { createLocalVue, shallowMount } from 'packages/test-utils/src'
33
import Vue from 'vue'
4+
import VueRouter from 'vue-router'
45
import ComponentWithChild from '~resources/components/component-with-child.vue'
56
import ComponentWithoutName from '~resources/components/component-without-name.vue'
67
import ComponentWithSlots from '~resources/components/component-with-slots.vue'
@@ -17,6 +18,58 @@ import {
1718
import { itDoNotRunIf, itSkipIf } from 'conditional-specs'
1819

1920
describeWithShallowAndMount('find', mountingMethod => {
21+
itDoNotRunIf(
22+
mountingMethod.name === 'shallowMount',
23+
'returns a VueWrapper using a <router-view /> component',
24+
async () => {
25+
const localVue = createLocalVue()
26+
localVue.use(VueRouter)
27+
const TestComponentToFind = {
28+
render: h => h('div'),
29+
name: 'test-component-to-find'
30+
}
31+
const routes = [
32+
{
33+
path: '/a/b',
34+
name: 'ab',
35+
component: TestComponentToFind
36+
}
37+
]
38+
const router = new VueRouter({ routes })
39+
const wrapper = mountingMethod(
40+
{
41+
template: '<router-view/>'
42+
},
43+
{
44+
localVue,
45+
router
46+
}
47+
)
48+
49+
await router.push('/a/b')
50+
51+
expect(wrapper.findComponent(TestComponentToFind).exists()).toBe(true)
52+
}
53+
)
54+
55+
it('findComponent in functional component', () => {
56+
const Comp2 = {
57+
name: 'test',
58+
render(h) {
59+
return h('div', 'test')
60+
}
61+
}
62+
const Comp = {
63+
name: 'Comp',
64+
functional: true,
65+
render(h) {
66+
return h(Comp2)
67+
}
68+
}
69+
const wrapper = shallowMount(Comp)
70+
wrapper.getComponent(Comp2)
71+
})
72+
2073
it('returns a Wrapper matching tag selector passed', () => {
2174
const compiled = compileToFunctions('<div><p></p><p></p></div>')
2275
const wrapper = mountingMethod(compiled)

0 commit comments

Comments
 (0)
0