Skip to content

Commit 1642679

Browse files
committed
test: add more tests
1 parent ef1ce1e commit 1642679

File tree

15 files changed

+149
-0
lines changed

15 files changed

+149
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import { createElement } from 'lwc';
2+
import Light1 from 'x/light1';
3+
import Light2 from 'x/light2';
4+
import Shadow1 from 'x/shadow1';
5+
import Shadow2 from 'x/shadow2';
6+
import ShadowParentLightChild from 'x/shadowParentLightChild';
7+
8+
// Under the hood, we may de-dup styles based on their string contents. So if two
9+
// components happen to have exactly the same CSS, we have to confirm that unrendering
10+
// one component doesn't cause the other's component's CSS to be removed from the DOM.
11+
describe('style collisions', () => {
12+
const cases = [
13+
{
14+
name: 'light',
15+
components: [Light1, Light2],
16+
},
17+
{
18+
name: 'shadow',
19+
components: [Shadow1, Shadow2],
20+
},
21+
];
22+
23+
const getDivColor = (elm) => {
24+
return getComputedStyle((elm.shadowRoot || elm).querySelector('div')).color;
25+
};
26+
27+
const getNumGlobalStylesheets = () => {
28+
let count = document.head.querySelectorAll('style').length;
29+
if (document.adoptedStyleSheets) {
30+
count += document.adoptedStyleSheets.length;
31+
}
32+
return count;
33+
};
34+
35+
const getNumStylesheetsForElement = (elm) => {
36+
let count = 0;
37+
if (elm.shadowRoot) {
38+
if (elm.shadowRoot.adoptedStyleSheets) {
39+
count += elm.shadowRoot.adoptedStyleSheets.length;
40+
}
41+
count += elm.shadowRoot.querySelectorAll('style').length;
42+
}
43+
return count;
44+
};
45+
46+
cases.forEach(({ name, components: [Component1, Component2] }) => {
47+
describe(name, () => {
48+
it('removes styles from the DOM when two components have exact same styles', () => {
49+
const elm1 = createElement('x-one', { is: Component1 });
50+
const elm2 = createElement('x-two', { is: Component2 });
51+
52+
document.body.appendChild(elm1);
53+
document.body.appendChild(elm2);
54+
55+
return Promise.resolve()
56+
.then(() => {
57+
expect(getDivColor(elm1)).toEqual('rgb(255, 0, 0)');
58+
expect(getDivColor(elm2)).toEqual('rgb(255, 0, 0)');
59+
document.body.removeChild(elm1);
60+
})
61+
.then(() => {
62+
expect(getDivColor(elm2)).toEqual('rgb(255, 0, 0)');
63+
document.body.removeChild(elm2);
64+
expect(getNumGlobalStylesheets()).toEqual(0);
65+
});
66+
});
67+
});
68+
});
69+
70+
describe('shadow parent, light child', () => {
71+
it('removes styles from the DOM when two components have the exact same styles', () => {
72+
const elm = createElement('x-parent', { is: ShadowParentLightChild });
73+
document.body.appendChild(elm);
74+
75+
elm.child1Shown = true;
76+
elm.child2Shown = true;
77+
return Promise.resolve()
78+
.then(() => {
79+
const [child1, child2] = elm.shadowRoot.querySelectorAll('x-light1,x-light2');
80+
expect(getDivColor(child1)).toEqual('rgb(255, 0, 0)');
81+
expect(getDivColor(child2)).toEqual('rgb(255, 0, 0)');
82+
elm.child1Shown = false;
83+
})
84+
.then(() => {
85+
expect(getDivColor(elm.shadowRoot.querySelector('x-light2'))).toEqual(
86+
'rgb(255, 0, 0)'
87+
);
88+
elm.child2Shown = false;
89+
})
90+
.then(() => {
91+
expect(getNumStylesheetsForElement(elm)).toEqual(0);
92+
});
93+
});
94+
});
95+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
div {
2+
color: red;
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<template lwc:render-mode="light">
2+
<div>a</div>
3+
</template>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { LightningElement } from 'lwc';
2+
3+
export default class extends LightningElement {
4+
static renderMode = 'light';
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
div {
2+
color: red;
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<template lwc:render-mode="light">
2+
<div>a</div>
3+
</template>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { LightningElement } from 'lwc';
2+
3+
export default class extends LightningElement {
4+
static renderMode = 'light';
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
div {
2+
color: red;
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<template>
2+
<div>a</div>
3+
</template>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { LightningElement } from 'lwc';
2+
3+
export default class extends LightningElement {}

0 commit comments

Comments
 (0)