Skip to content

Commit d9d2efe

Browse files
committed
fix #184
1 parent 308ce0d commit d9d2efe

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

src/utils/find.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ function findAllVNodes(
111111
if (node.component) {
112112
// match children of the wrapping component
113113
aggregateChildren(nodes, node.component.subTree.children)
114+
aggregateChildren(nodes, [node.component.subTree])
114115
}
115116
if (node.suspense) {
116117
// match children if component is Suspense
@@ -123,11 +124,13 @@ function findAllVNodes(
123124
aggregateChildren(nodes, fallbackTree.children)
124125
}
125126
}
126-
if (matches(node, selector)) {
127+
if (matches(node, selector) && matchingNodes.indexOf(node) < 0) {
127128
matchingNodes.push(node)
128129
}
129130
}
130131

132+
// console.log(matchingNodes)
133+
131134
return matchingNodes
132135
}
133136

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { mount } from '../src'
2+
import { defineComponent, h } from 'vue'
3+
4+
describe('findAllComponentsInSubTree', () => {
5+
it('finds all components', () => {
6+
const compB = defineComponent({
7+
name: 'compB',
8+
render: () => h('div', { class: 'B' }, 'TextB')
9+
})
10+
const compA = defineComponent({
11+
name: 'compA',
12+
template: '<div><slot /></div>'
13+
})
14+
const starter = defineComponent({
15+
name: 'starter',
16+
template: '<comp-a><comp-b /><comp-b /><comp-b /></comp-a>',
17+
components: { compA, compB }
18+
})
19+
const App = defineComponent({
20+
name: 'App',
21+
render: () => h(starter),
22+
components: { starter }
23+
})
24+
25+
const wrapper = mount(App)
26+
// find by DOM selector
27+
expect(wrapper.findComponent(compA).exists()).toBe(true)
28+
expect(wrapper.findComponent(compB).exists()).toBe(true)
29+
expect(wrapper.findComponent(starter).exists()).toBe(true)
30+
})
31+
32+
it('finds all components', () => {
33+
const compC = defineComponent({
34+
name: 'compC',
35+
render: () => h('div', { class: 'B' }, 'TextB')
36+
})
37+
const compB = defineComponent({
38+
name: 'compB',
39+
render: () => h('div', { class: 'B' }, 'TextB')
40+
})
41+
const compA = defineComponent({
42+
name: 'compA',
43+
template: '<div><slot /><slot name="b" /></div>'
44+
})
45+
const starter = defineComponent({
46+
name: 'starter',
47+
template:
48+
'<comp-a><comp-b /><comp-b /><comp-b /><comp-c v-slot:b /></comp-a>',
49+
components: { compA, compB, compC }
50+
})
51+
const App = defineComponent({
52+
name: 'App',
53+
template: '<starter />',
54+
components: { starter }
55+
})
56+
57+
const wrapper = mount(App)
58+
// find by DOM selector
59+
expect(wrapper.findComponent(compA).exists()).toBe(true)
60+
expect(wrapper.findComponent(compB).exists()).toBe(true)
61+
expect(wrapper.findComponent(compC).exists()).toBe(true)
62+
expect(wrapper.findComponent(starter).exists()).toBe(true)
63+
})
64+
})

0 commit comments

Comments
 (0)