From 34f81c459371ebf26e09e217d7b2c2ded7965869 Mon Sep 17 00:00:00 2001 From: Ryoya <33255443+harunari0928@users.noreply.github.com> Date: Wed, 8 Nov 2023 06:23:53 +0900 Subject: [PATCH] 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. --- src/baseWrapper.ts | 2 +- tests/text.spec.ts | 22 +++++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/baseWrapper.ts b/src/baseWrapper.ts index a141d3a87..71f074bf5 100644 --- a/src/baseWrapper.ts +++ b/src/baseWrapper.ts @@ -261,7 +261,7 @@ export default abstract class BaseWrapper } text() { - return textContent(this.element) + return this.getRootNodes().map(textContent).join('') } exists() { diff --git a/tests/text.spec.ts b/tests/text.spec.ts index a3a33387b..05e58b3db 100644 --- a/tests/text.spec.ts +++ b/tests/text.spec.ts @@ -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' @@ -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: `
Text content
` + }) + ) + } + ) + }) + + expect(wrapper.text()).toBe('Text content') + }) })