Skip to content

Commit

Permalink
fix: wrapper.text method
Browse files Browse the repository at this point in the history
This change fixes wrapper.text to handle suspense components with comments in a slot.
When suspense component had comments in a slot, wrapper.text returned empty.
  • Loading branch information
harunari0928 committed Nov 3, 2023
1 parent d30d29a commit d2a39fe
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/baseWrapper.ts
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
14 changes: 13 additions & 1 deletion 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'

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

it('returns correct text for suspense component with comments in a slot', () => {
const wrapper = mount({
render: () => h(Suspense, {}, {

Check failure on line 99 in tests/text.spec.ts

View workflow job for this annotation

GitHub Actions / build (18)

Replace `·h(Suspense,·{},` with `⏎········h(⏎··········Suspense,⏎··········{},⏎·········`

Check failure on line 99 in tests/text.spec.ts

View workflow job for this annotation

GitHub Actions / build (20)

Replace `·h(Suspense,·{},` with `⏎········h(⏎··········Suspense,⏎··········{},⏎·········`
default: () => h(defineComponent({

Check failure on line 100 in tests/text.spec.ts

View workflow job for this annotation

GitHub Actions / build (18)

Replace `········default:·()·=>·h(` with `············default:·()·=>⏎··············h(⏎················`

Check failure on line 100 in tests/text.spec.ts

View workflow job for this annotation

GitHub Actions / build (20)

Replace `········default:·()·=>·h(` with `············default:·()·=>⏎··············h(⏎················`
template: `<!-- some comments --><div>Text content</div>`

Check failure on line 101 in tests/text.spec.ts

View workflow job for this annotation

GitHub Actions / build (18)

Insert `········`

Check failure on line 101 in tests/text.spec.ts

View workflow job for this annotation

GitHub Actions / build (20)

Insert `········`
}))

Check failure on line 102 in tests/text.spec.ts

View workflow job for this annotation

GitHub Actions / build (18)

Replace `········})` with `················})⏎··············`

Check failure on line 102 in tests/text.spec.ts

View workflow job for this annotation

GitHub Actions / build (20)

Replace `········})` with `················})⏎··············`
})

Check failure on line 103 in tests/text.spec.ts

View workflow job for this annotation

GitHub Actions / build (18)

Replace `}` with `····}⏎········`

Check failure on line 103 in tests/text.spec.ts

View workflow job for this annotation

GitHub Actions / build (20)

Replace `}` with `····}⏎········`
})

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

0 comments on commit d2a39fe

Please sign in to comment.