-
Notifications
You must be signed in to change notification settings - Fork 399
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2915ac3
commit 4aab717
Showing
15 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
packages/integration-karma/test/rendering/style-collisions/index.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { createElement } from 'lwc'; | ||
import Light1 from 'x/light1'; | ||
import Light2 from 'x/light2'; | ||
import Shadow1 from 'x/shadow1'; | ||
import Shadow2 from 'x/shadow2'; | ||
import ShadowParentLightChild from 'x/shadowParentLightChild'; | ||
|
||
// Under the hood, we may de-dup styles based on their string contents. So if two | ||
// components happen to have exactly the same CSS, we have to confirm that unrendering | ||
// one component doesn't cause the other's component's CSS to be removed from the DOM. | ||
describe('style collisions', () => { | ||
const cases = [ | ||
{ | ||
name: 'light', | ||
components: [Light1, Light2], | ||
}, | ||
{ | ||
name: 'shadow', | ||
components: [Shadow1, Shadow2], | ||
}, | ||
]; | ||
|
||
const getDivColor = (elm) => { | ||
return getComputedStyle((elm.shadowRoot || elm).querySelector('div')).color; | ||
}; | ||
|
||
const getNumGlobalStylesheets = () => { | ||
let count = document.head.querySelectorAll('style').length; | ||
if (document.adoptedStyleSheets) { | ||
count += document.adoptedStyleSheets.length; | ||
} | ||
return count; | ||
}; | ||
|
||
const getNumStylesheetsForElement = (elm) => { | ||
let count = 0; | ||
if (elm.shadowRoot) { | ||
if (elm.shadowRoot.adoptedStyleSheets) { | ||
count += elm.shadowRoot.adoptedStyleSheets.length; | ||
} | ||
count += elm.shadowRoot.querySelectorAll('style').length; | ||
} | ||
return count; | ||
}; | ||
|
||
cases.forEach(({ name, components: [Component1, Component2] }) => { | ||
describe(name, () => { | ||
it('removes styles from the DOM when two components have exact same styles', () => { | ||
const elm1 = createElement('x-one', { is: Component1 }); | ||
const elm2 = createElement('x-two', { is: Component2 }); | ||
|
||
document.body.appendChild(elm1); | ||
document.body.appendChild(elm2); | ||
|
||
return Promise.resolve() | ||
.then(() => { | ||
expect(getDivColor(elm1)).toEqual('rgb(255, 0, 0)'); | ||
expect(getDivColor(elm2)).toEqual('rgb(255, 0, 0)'); | ||
document.body.removeChild(elm1); | ||
}) | ||
.then(() => { | ||
expect(getDivColor(elm2)).toEqual('rgb(255, 0, 0)'); | ||
document.body.removeChild(elm2); | ||
expect(getNumGlobalStylesheets()).toEqual(0); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('shadow parent, light child', () => { | ||
it('removes styles from the DOM when two components have the exact same styles', () => { | ||
const elm = createElement('x-parent', { is: ShadowParentLightChild }); | ||
document.body.appendChild(elm); | ||
|
||
elm.child1Shown = true; | ||
elm.child2Shown = true; | ||
return Promise.resolve() | ||
.then(() => { | ||
const [child1, child2] = elm.shadowRoot.querySelectorAll('x-light1,x-light2'); | ||
expect(getDivColor(child1)).toEqual('rgb(255, 0, 0)'); | ||
expect(getDivColor(child2)).toEqual('rgb(255, 0, 0)'); | ||
elm.child1Shown = false; | ||
}) | ||
.then(() => { | ||
expect(getDivColor(elm.shadowRoot.querySelector('x-light2'))).toEqual( | ||
'rgb(255, 0, 0)' | ||
); | ||
elm.child2Shown = false; | ||
}) | ||
.then(() => { | ||
expect(getNumStylesheetsForElement(elm)).toEqual(0); | ||
}); | ||
}); | ||
}); | ||
}); |
3 changes: 3 additions & 0 deletions
3
packages/integration-karma/test/rendering/style-collisions/x/light1/light1.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
div { | ||
color: red; | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/integration-karma/test/rendering/style-collisions/x/light1/light1.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<template lwc:render-mode="light"> | ||
<div>a</div> | ||
</template> |
5 changes: 5 additions & 0 deletions
5
packages/integration-karma/test/rendering/style-collisions/x/light1/light1.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { LightningElement } from 'lwc'; | ||
|
||
export default class extends LightningElement { | ||
static renderMode = 'light'; | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/integration-karma/test/rendering/style-collisions/x/light2/light2.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
div { | ||
color: red; | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/integration-karma/test/rendering/style-collisions/x/light2/light2.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<template lwc:render-mode="light"> | ||
<div>a</div> | ||
</template> |
5 changes: 5 additions & 0 deletions
5
packages/integration-karma/test/rendering/style-collisions/x/light2/light2.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { LightningElement } from 'lwc'; | ||
|
||
export default class extends LightningElement { | ||
static renderMode = 'light'; | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/integration-karma/test/rendering/style-collisions/x/shadow1/shadow1.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
div { | ||
color: red; | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/integration-karma/test/rendering/style-collisions/x/shadow1/shadow1.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<template> | ||
<div>a</div> | ||
</template> |
3 changes: 3 additions & 0 deletions
3
packages/integration-karma/test/rendering/style-collisions/x/shadow1/shadow1.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { LightningElement } from 'lwc'; | ||
|
||
export default class extends LightningElement {} |
3 changes: 3 additions & 0 deletions
3
packages/integration-karma/test/rendering/style-collisions/x/shadow2/shadow2.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
div { | ||
color: red; | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/integration-karma/test/rendering/style-collisions/x/shadow2/shadow2.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<template> | ||
<div>a</div> | ||
</template> |
3 changes: 3 additions & 0 deletions
3
packages/integration-karma/test/rendering/style-collisions/x/shadow2/shadow2.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { LightningElement } from 'lwc'; | ||
|
||
export default class extends LightningElement {} |
8 changes: 8 additions & 0 deletions
8
...arma/test/rendering/style-collisions/x/shadowParentLightChild/shadowParentLightChild.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<template> | ||
<template if:true={child1Shown}> | ||
<x-light1></x-light1> | ||
</template> | ||
<template if:true={child2Shown}> | ||
<x-light2></x-light2> | ||
</template> | ||
</template> |
6 changes: 6 additions & 0 deletions
6
...-karma/test/rendering/style-collisions/x/shadowParentLightChild/shadowParentLightChild.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { LightningElement, api } from 'lwc'; | ||
|
||
export default class extends LightningElement { | ||
@api child1Shown = false; | ||
@api child2Shown = false; | ||
} |