Skip to content

Commit

Permalink
fix: duplicate results on findAllComponents with non-function slots
Browse files Browse the repository at this point in the history
  • Loading branch information
freakzlike authored and cexbrayat committed Aug 25, 2022
1 parent 60ff6bd commit 0a4156f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/utils/find.ts
Expand Up @@ -142,7 +142,7 @@ function findAllVNodes(
const { activeBranch } = node.suspense
aggregateChildren(nodes, [activeBranch])
}
if (matches(node, selector)) {
if (matches(node, selector) && !matchingNodes.includes(node)) {
matchingNodes.push(node)
}
}
Expand Down
42 changes: 41 additions & 1 deletion tests/findAllComponents.spec.ts
@@ -1,7 +1,7 @@
import { describe, expect, it } from 'vitest'
import { mount } from '../src'
import Hello from './components/Hello.vue'
import { DefineComponent, defineComponent } from 'vue'
import { DefineComponent, defineComponent, h } from 'vue'

const compC = defineComponent({
name: 'ComponentC',
Expand Down Expand Up @@ -80,4 +80,44 @@ describe('findAllComponents', () => {
.name
).toEqual('NestedChild')
})

it('findAllComponents with function slots', () => {
const ComponentA = defineComponent({
template: '<div><slot /></div>'
})
const ComponentB = defineComponent({
template: '<span>Text</span>'
})

const wrapper = mount({
components: { ComponentA, ComponentB },
render() {
return h(ComponentA, {}, () => [1, 2, 3].map(() => h(ComponentB)))
}
})
expect(wrapper.findAll('span')).toHaveLength(3)
expect(wrapper.findAllComponents(ComponentB)).toHaveLength(3)
})

it('findAllComponents with non-function slots', () => {
const ComponentA = defineComponent({
template: '<div><slot /></div>'
})
const ComponentB = defineComponent({
template: '<span>Text</span>'
})

const wrapper = mount({
components: { ComponentA, ComponentB },
render() {
return h(
ComponentA,
{},
[1, 2, 3].map(() => h(ComponentB))
)
}
})
expect(wrapper.findAll('span')).toHaveLength(3)
expect(wrapper.findAllComponents(ComponentB)).toHaveLength(3)
})
})

0 comments on commit 0a4156f

Please sign in to comment.