Skip to content

Commit e4391f2

Browse files
committed
feat(test): add toEqualLightHtml()
1 parent 207e837 commit e4391f2

3 files changed

Lines changed: 121 additions & 4 deletions

File tree

src/runtime/test/shadow.spec.tsx

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import { newSpecPage } from '@stencil/core/testing';
2+
import { Component, h } from '@stencil/core';
3+
4+
5+
@Component({
6+
tag: 'cmp-a',
7+
styles: ':host { color: black }',
8+
shadow: true
9+
})
10+
class CmpA {
11+
render() {
12+
return (
13+
<div>
14+
<slot name='start'></slot>
15+
<span>
16+
<slot/>
17+
</span>
18+
<div class='end'>
19+
<slot name='end'></slot>
20+
</div>
21+
</div>
22+
);
23+
}
24+
}
25+
26+
describe('shadow', () => {
27+
28+
it('render with shadow-dom enabled', async () => {
29+
const page = await newSpecPage({
30+
components: [CmpA],
31+
html: `
32+
<cmp-a>
33+
<span slot="end">End</span>
34+
Text
35+
<span slot="start">Start</span>
36+
</cmp-a>`
37+
});
38+
39+
40+
expect(page.root).toEqualHtml(`
41+
<cmp-a class="hydrated">
42+
<mock:shadow-root>
43+
<div>
44+
<slot name=\"start\"></slot>
45+
<span>
46+
<slot></slot>
47+
</span>
48+
<div class='end'>
49+
<slot name='end'></slot>
50+
</div>
51+
</div>
52+
</mock:shadow-root>
53+
54+
<span slot=\"end\">
55+
End
56+
</span>
57+
Text
58+
<span slot=\"start\">
59+
Start
60+
</span>
61+
</cmp-a>`);
62+
63+
expect(page.root).toEqualLightHtml(`
64+
<cmp-a class="hydrated">
65+
<span slot=\"end\">
66+
End
67+
</span>
68+
Text
69+
<span slot=\"start\">
70+
Start
71+
</span>
72+
</cmp-a>`);
73+
});
74+
75+
it('render scoped html with shadow-dom disabled', async () => {
76+
const page = await newSpecPage({
77+
components: [CmpA],
78+
supportsShadowDom: false,
79+
html: `
80+
<cmp-a>
81+
<span slot="end">End</span>
82+
Text
83+
<span slot="start">Start</span>
84+
</cmp-a>`,
85+
});
86+
87+
const expected = `
88+
<cmp-a class="hydrated sc-cmp-a-h">
89+
<!---->
90+
<div class="sc-cmp-a sc-cmp-a-s">
91+
<span slot=\"start\">
92+
Start
93+
</span>
94+
<span class='sc-cmp-a sc-cmp-a-s'>
95+
Text
96+
</span>
97+
<div class='end sc-cmp-a sc-cmp-a-s'>
98+
<span slot=\"end\">
99+
End
100+
</span>
101+
</div>
102+
</div>
103+
</cmp-a>`;
104+
expect(page.root).toEqualHtml(expected);
105+
expect(page.root).toEqualLightHtml(expected);
106+
});
107+
108+
});

src/testing/matchers/html.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@ import { NODE_TYPES, parseHtmlToFragment, serializeNodeToHtml } from '@mock-doc'
22

33

44
export function toEqualHtml(input: string | HTMLElement | ShadowRoot, shouldEqual: string) {
5+
return compareHtml(input, shouldEqual, true);
6+
}
7+
8+
export function toEqualLightHtml(input: string | HTMLElement | ShadowRoot, shouldEqual: string) {
9+
return compareHtml(input, shouldEqual, false);
10+
}
11+
12+
export function compareHtml(input: string | HTMLElement | ShadowRoot, shouldEqual: string, serializeShadowRoot: boolean) {
513
if (input == null) {
614
throw new Error(`expect toEqualHtml() value is "${input}"`);
715
}
@@ -17,22 +25,22 @@ export function toEqualHtml(input: string | HTMLElement | ShadowRoot, shouldEqua
1725
prettyHtml: true,
1826
outerHtml: true,
1927
excludeTags: ['body'],
20-
serializeShadowRoot: true
28+
serializeShadowRoot
2129
});
2230

2331
} else if ((input as HTMLElement).nodeType === NODE_TYPES.DOCUMENT_FRAGMENT_NODE) {
2432
serializeA = serializeNodeToHtml((input as any), {
2533
prettyHtml: true,
2634
excludeTags: ['style'],
2735
excludeTagContent: ['style'],
28-
serializeShadowRoot: true
36+
serializeShadowRoot
2937
});
3038

3139
} else if (typeof input === 'string') {
3240
const parseA = parseHtmlToFragment(input);
3341
serializeA = serializeNodeToHtml(parseA, {
3442
prettyHtml: true,
35-
serializeShadowRoot: true
43+
serializeShadowRoot
3644
});
3745

3846
} else {

src/testing/matchers/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { toEqualAttribute, toEqualAttributes, toHaveAttribute } from './attributes';
22
import { toHaveReceivedEvent, toHaveReceivedEventDetail, toHaveReceivedEventTimes } from './events';
3-
import { toEqualHtml } from './html';
3+
import { toEqualHtml, toEqualLightHtml } from './html';
44
import { toEqualText } from './text';
55
import { toHaveClass, toHaveClasses, toMatchClasses } from './class-list';
66
import { toMatchScreenshot } from './screenshot';
@@ -10,6 +10,7 @@ export const expectExtend = {
1010
toEqualAttribute,
1111
toEqualAttributes,
1212
toEqualHtml,
13+
toEqualLightHtml,
1314
toEqualText,
1415
toHaveAttribute,
1516
toHaveClass,

0 commit comments

Comments
 (0)