Skip to content

Commit 34f81c4

Browse files
authored
fix: wrapper.text method (#2231)
This change fixes wrapper.text to handle suspense components with multiple elements in a slot. When suspense component had multiple elements in a slot, wrapper.text returned empty.
1 parent d30d29a commit 34f81c4

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/baseWrapper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ export default abstract class BaseWrapper<ElementType extends Node>
261261
}
262262

263263
text() {
264-
return textContent(this.element)
264+
return this.getRootNodes().map(textContent).join('')
265265
}
266266

267267
exists() {

tests/text.spec.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, it } from 'vitest'
2-
import { defineComponent, h } from 'vue'
2+
import { defineComponent, h, Suspense } from 'vue'
33

44
import { mount } from '../src'
55

@@ -93,4 +93,24 @@ describe('text', () => {
9393
const wrapper = mount(() => h(ReturnSlot, {}, () => h(MultiRootText)))
9494
expect(wrapper.text()).toBe('foobarbaz')
9595
})
96+
97+
it('returns correct text for suspense component has multiple elements in a slot', () => {
98+
const wrapper = mount({
99+
render: () =>
100+
h(
101+
Suspense,
102+
{},
103+
{
104+
default: () =>
105+
h(
106+
defineComponent({
107+
template: `<!-- some comments --><div>Text content</div>`
108+
})
109+
)
110+
}
111+
)
112+
})
113+
114+
expect(wrapper.text()).toBe('Text content')
115+
})
96116
})

0 commit comments

Comments
 (0)