Skip to content

Commit ff93e5e

Browse files
freakzlikecexbrayat
authored andcommitted
feat(wrapper): Add raw option to .html()
1 parent 0b5548d commit ff93e5e

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

docs/api/index.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1378,10 +1378,14 @@ test('getComponent', () => {
13781378
13791379
Returns the HTML of an element.
13801380
1381+
By default the output is formatted with [`js-beautify`](https://github.com/beautify-web/js-beautify)
1382+
to make snapshots more readable. Use `raw: true` option to receive the unformatted html string.
1383+
13811384
**Signature:**
13821385
13831386
```ts
13841387
html(): string
1388+
html(options?: { raw?: boolean }): string
13851389
```
13861390
13871391
**Details:**
@@ -1405,7 +1409,13 @@ import Component from './Component.vue'
14051409
test('html', () => {
14061410
const wrapper = mount(Component)
14071411

1408-
expect(wrapper.html()).toBe('<div><p>Hello world</p></div>')
1412+
expect(wrapper.html()).toBe(
1413+
'<div>\n' +
1414+
' <p>Hello world</p>\n' +
1415+
'</div>'
1416+
)
1417+
1418+
expect(wrapper.html({ raw: true })).toBe('<div><p>Hello world</p></div>')
14091419
})
14101420
```
14111421

src/baseWrapper.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,9 @@ export default abstract class BaseWrapper<ElementType extends Node>
217217
}
218218
abstract setValue(value?: any): Promise<void>
219219

220-
html(): string {
220+
html(options?: { raw?: boolean }): string {
221221
const stringNodes = this.getRootNodes().map((node) => stringifyNode(node))
222+
if (options?.raw) return stringNodes.join('')
222223

223224
return stringNodes
224225
.map((node) =>

tests/html.spec.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ import { defineComponent, h } from 'vue'
33

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

6+
const nestedTemplate =
7+
'<div><div class="element"><span>Text 1</span></div><div>Text 2</div></div>'
8+
const NestedNodes = defineComponent({
9+
template: nestedTemplate
10+
})
11+
612
describe('html', () => {
713
it('returns html when mounting single root node', () => {
814
const Component = defineComponent({
@@ -16,6 +22,29 @@ describe('html', () => {
1622
expect(wrapper.html()).toBe('<div>Text content</div>')
1723
})
1824

25+
it('returns formatted html string', () => {
26+
const wrapper = mount(NestedNodes)
27+
28+
expect(wrapper.html()).toBe(
29+
'<div>\n' +
30+
' <div class="element"><span>Text 1</span></div>\n' +
31+
' <div>Text 2</div>\n' +
32+
'</div>'
33+
)
34+
expect(wrapper.html()).toBe(
35+
'<div>\n' +
36+
' <div class="element"><span>Text 1</span></div>\n' +
37+
' <div>Text 2</div>\n' +
38+
'</div>'
39+
)
40+
})
41+
42+
it('returns raw html string', () => {
43+
const wrapper = mount(NestedNodes)
44+
45+
expect(wrapper.html({ raw: true })).toBe(nestedTemplate)
46+
})
47+
1948
describe('multiple root components', () => {
2049
const originalTemplate = [
2150
'<div>foo</div>',
@@ -35,6 +64,11 @@ describe('html', () => {
3564
expect(wrapper.html()).toBe(originalTemplate.join('\n'))
3665
})
3766

67+
it('returns the raw html when mounting multiple root nodes', () => {
68+
const wrapper = mount(Component)
69+
expect(wrapper.html({ raw: true })).toBe(originalTemplate.join(''))
70+
})
71+
3872
it('returns the html when multiple root component is located inside other component', () => {
3973
const ParentComponent = defineComponent({
4074
components: { MultipleRoots: Component },

0 commit comments

Comments
 (0)