diff --git a/src/domWrapper.ts b/src/domWrapper.ts index a4bfee3ec..5c5522f62 100644 --- a/src/domWrapper.ts +++ b/src/domWrapper.ts @@ -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 extends BaseWrapper { protected readonly subTree: VNode | null | undefined = null @@ -25,17 +27,36 @@ export class DOMWrapper extends BaseWrapper { 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 @@ -70,6 +91,9 @@ export class DOMWrapper extends BaseWrapper { ): DOMWrapper[] findAll(selector: string): DOMWrapper[] findAll(selector: string): DOMWrapper[] { + if (this.hasFragmentRoot) { + return this.findAllDOMElements(selector).map(e => createDOMWrapper(e)) + } if (!(this.wrapperElement instanceof Element)) { return [] } @@ -80,6 +104,8 @@ export class DOMWrapper extends BaseWrapper { 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)) } diff --git a/tests/element.spec.ts b/tests/element.spec.ts index 58b4f2e85..6b44dd98d 100644 --- a/tests/element.spec.ts +++ b/tests/element.spec.ts @@ -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' @@ -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: '
foobar
' + }) + const func = mount(Parent).findComponent(Func) + + expect(func.html()).toBe( + '

title

\nfoo\nbar' + ) + 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: '
' + }) + 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: '
' + }) + 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')