Skip to content

Commit

Permalink
fix: wrapper.text method (#2231)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
harunari0928 authored Nov 7, 2023
1 parent d30d29a commit 34f81c4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/baseWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ export default abstract class BaseWrapper<ElementType extends Node>
}

text() {
return textContent(this.element)
return this.getRootNodes().map(textContent).join('')
}

exists() {
Expand Down
22 changes: 21 additions & 1 deletion tests/text.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'
import { defineComponent, h } from 'vue'
import { defineComponent, h, Suspense } from 'vue'

import { mount } from '../src'

Expand Down Expand Up @@ -93,4 +93,24 @@ describe('text', () => {
const wrapper = mount(() => h(ReturnSlot, {}, () => h(MultiRootText)))
expect(wrapper.text()).toBe('foobarbaz')
})

it('returns correct text for suspense component has multiple elements in a slot', () => {
const wrapper = mount({
render: () =>
h(
Suspense,
{},
{
default: () =>
h(
defineComponent({
template: `<!-- some comments --><div>Text content</div>`
})
)
}
)
})

expect(wrapper.text()).toBe('Text content')
})
})

0 comments on commit 34f81c4

Please sign in to comment.