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
38 changes: 32 additions & 6 deletions src/domWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import {
createDOMWrapper,
registerFactory
} from './wrapperFactory'
import type { RefSelector } from './types'
import type { RefSelector, VueElement } from './types'
import { isRefSelector } from './utils'
import { createWrapperError } from './errorWrapper'
import { getRootNodes } from './utils/getRootNodes'
import { isElement } from './utils/isElement'

export class DOMWrapper<NodeType extends Node> extends BaseWrapper<NodeType> {
protected readonly subTree: VNode | null | undefined = null
Expand All @@ -25,17 +27,36 @@ export class DOMWrapper<NodeType extends Node> extends BaseWrapper<NodeType> {
config.plugins.DOMWrapper.extend(this)
}

private get hasFragmentRoot(): boolean {
return (
this.subTree?.type === Fragment && Array.isArray(this.subTree?.children)
)
}

// the anchor has no component reference, walk up from a root element
private getFragmentComponent() {
const rootElement = getRootNodes(this.subTree!).find(isElement) as
| VueElement
| undefined
let component = rootElement?.__vueParentComponent
while (component && component.subTree.el !== this.element) {
component = component.parent ?? undefined
}
return component
}

getRootNodes() {
if (
this.subTree?.type === Fragment &&
Array.isArray(this.subTree?.children)
) {
return this.subTree.children.map(node => (node as any)?.el)
if (this.hasFragmentRoot) {
// the wrapped element is only the fragment anchor, collect nested roots
return getRootNodes(this.getFragmentComponent()?.subTree ?? this.subTree!)
}
return [this.wrapperElement]
}

getCurrentComponent() {
if (this.hasFragmentRoot) {
return this.getFragmentComponent()
}
let component = this.element.__vueParentComponent
while (component?.parent?.vnode.el === this.element) {
component = component.parent
Expand Down Expand Up @@ -70,6 +91,9 @@ export class DOMWrapper<NodeType extends Node> extends BaseWrapper<NodeType> {
): DOMWrapper<SVGElementTagNameMap[K]>[]
findAll<T extends Element>(selector: string): DOMWrapper<T>[]
findAll(selector: string): DOMWrapper<Element>[] {
if (this.hasFragmentRoot) {
return this.findAllDOMElements(selector).map(e => createDOMWrapper(e))
}
if (!(this.wrapperElement instanceof Element)) {
return []
}
Expand All @@ -80,6 +104,8 @@ export class DOMWrapper<NodeType extends Node> extends BaseWrapper<NodeType> {

findAllComponents(selector: any): any {
const results = super.findAllComponents(selector)
// the current component is the functional one, so results are already scoped
if (this.hasFragmentRoot) return results
return results.filter((r: WrapperLike) => this.element.contains(r.element))
}

Expand Down
75 changes: 74 additions & 1 deletion tests/element.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { describe, expect, it, vi } from 'vitest'
import { defineComponent, h } from 'vue'
import {
type FunctionalComponent,
defineComponent,
h,
nextTick,
ref
} from 'vue'

import { mount } from '../src'

Expand Down Expand Up @@ -90,6 +96,73 @@ describe('element', () => {
expect(wrapper.findComponent(Func).text()).toBe('foobar')
})

it('returns correct output for functional component with nested array roots', () => {
const Func: FunctionalComponent = (_, { slots }) => [
h('h1', {}, 'title'),
slots.default?.()
]

const Parent = defineComponent({
components: { Func },
template: '<div><Func><span>foo</span><span>bar</span></Func></div>'
})
const func = mount(Parent).findComponent(Func)

expect(func.html()).toBe(
'<h1>title</h1>\n<span>foo</span>\n<span>bar</span>'
)
expect(func.text()).toBe('titlefoobar')
})

it('finds elements inside functional component with multiple roots', () => {
const Func = () => [
h('div', { class: 'foo' }, [h('span', {}, 'nested')]),
h('div', { class: 'bar' }, 'bar')
]

const Parent = defineComponent({
components: { Func },
template: '<div><Func/><div class="outside" /></div>'
})
const func = mount(Parent).findComponent(Func)

expect(func.get('span').text()).toBe('nested')
expect(func.findAll('div')).toHaveLength(2)
expect(func.find('.outside').exists()).toBe(false)
})

it('finds components inside functional component with multiple roots', () => {
const Child = defineComponent({ name: 'Child', render: () => h('i') })
const MultiRoot = defineComponent({ render: () => [h('b'), h('b')] })
const Func = () => [h(Child), h(MultiRoot)]

const Parent = defineComponent({
components: { Func, Child },
template: '<div><Func/><Child/></div>'
})
const func = mount(Parent).findComponent(Func)

expect(func.findAllComponents(Child)).toHaveLength(1)
expect(func.findComponent(MultiRoot).exists()).toBe(true)
})

it('finds components inside functional component with multiple roots after re-render', async () => {
const Child = defineComponent({ name: 'Child', render: () => h('i') })
const Func: FunctionalComponent<{ count: number }> = props =>
Array.from({ length: props.count }, (_, i) => h(Child, { key: i }))
const count = ref(1)
const Parent = defineComponent({
render: () => h('div', [h(Func, { count: count.value })])
})
const func = mount(Parent).findComponent(Func)

count.value = 2
await nextTick()

expect(func.findAllComponents(Child)).toHaveLength(2)
expect(func.findAll('i')).toHaveLength(2)
})

it('text() reads DOM content for single-root components with empty vnode children', () => {
const Child = defineComponent({
render: () => h('button')
Expand Down
Loading