-
Notifications
You must be signed in to change notification settings - Fork 438
Expand file tree
/
Copy pathindex.spec.js
More file actions
146 lines (116 loc) · 5.83 KB
/
index.spec.js
File metadata and controls
146 lines (116 loc) · 5.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { createElement, setFeatureFlagForTest } from 'lwc';
import Native from 'x/native';
import Synthetic from 'x/synthetic';
import StyledLight from 'x/styledLight';
function doubleMicrotask() {
// wait for applyShadowMigrateMode()
return Promise.resolve().then(() => Promise.resolve());
}
function isActuallyNativeShadow(shadowRoot) {
return shadowRoot.constructor.toString().includes('[native code]');
}
function isClaimingToBeSyntheticShadow(shadowRoot) {
return !!shadowRoot.synthetic;
}
// This test only makes sense for true native shadow mode, because if ENABLE_FORCE_SHADOW_MIGRATE_MODE is true,
// then the polyfill should not be loaded at all.
if (process.env.NATIVE_SHADOW && !process.env.MIXED_SHADOW) {
describe('shadow migrate mode', () => {
beforeEach(async () => {
const style = document.createElement('style');
style.textContent = 'h1 { color: blue }';
document.head.appendChild(style);
});
describe('flag on', () => {
beforeEach(() => {
setFeatureFlagForTest('ENABLE_FORCE_SHADOW_MIGRATE_MODE', true);
});
afterEach(() => {
setFeatureFlagForTest('ENABLE_FORCE_SHADOW_MIGRATE_MODE', false);
});
it('uses global styles for synthetic components', async () => {
const elm = createElement('x-synthetic', { is: Synthetic });
document.body.appendChild(elm);
await doubleMicrotask();
expect(isActuallyNativeShadow(elm.shadowRoot)).toBe(true);
expect(isClaimingToBeSyntheticShadow(elm.shadowRoot)).toBe(true);
expect(getComputedStyle(elm.shadowRoot.querySelector('h1')).color).toBe(
'rgb(0, 0, 255)'
);
});
it('does not use global styles for native components', async () => {
const elm = createElement('x-native', { is: Native });
document.body.appendChild(elm);
await doubleMicrotask();
expect(isActuallyNativeShadow(elm.shadowRoot)).toBe(true);
expect(isClaimingToBeSyntheticShadow(elm.shadowRoot)).toBe(false);
expect(getComputedStyle(elm.shadowRoot.querySelector('h1')).color).toBe(
'rgb(0, 0, 0)'
);
});
it('does not apply styles from global light DOM components to synthetic components', async () => {
const light = createElement('x-styled-light', { is: StyledLight });
document.body.appendChild(light);
const elm = createElement('x-synthetic', { is: Synthetic });
document.body.appendChild(elm);
await doubleMicrotask();
expect(getComputedStyle(elm.shadowRoot.querySelector('h1')).opacity).toBe('1');
expect(getComputedStyle(light.querySelector('h1')).opacity).toBe('0.5');
});
it('uses new styles added to the head after component is rendered', async () => {
const elm = createElement('x-synthetic', { is: Synthetic });
document.body.appendChild(elm);
await doubleMicrotask();
expect(getComputedStyle(elm.shadowRoot.querySelector('h1')).color).toBe(
'rgb(0, 0, 255)'
);
const style = document.createElement('style');
style.textContent = `h1 { color: purple; background-color: crimson }`;
document.head.appendChild(style);
await doubleMicrotask();
expect(getComputedStyle(elm.shadowRoot.querySelector('h1')).color).toBe(
'rgb(128, 0, 128)'
);
expect(getComputedStyle(elm.shadowRoot.querySelector('h1')).backgroundColor).toBe(
'rgb(220, 20, 60)'
);
});
it('local styles are defined after global styles', async () => {
const elm = createElement('x-synthetic', { is: Synthetic });
document.body.appendChild(elm);
const style = document.createElement('style');
style.textContent = `h1 { font-family: sans-serif; background-color: green; }`;
document.head.appendChild(style);
await doubleMicrotask();
expect(getComputedStyle(elm.shadowRoot.querySelector('h1')).backgroundColor).toBe(
'rgb(0, 128, 0)'
);
expect(getComputedStyle(elm.shadowRoot.querySelector('h1')).fontFamily).toBe(
'monospace'
);
});
});
describe('flag off', () => {
it('does not use global styles for synthetic components', async () => {
const elm = createElement('x-synthetic', { is: Synthetic });
document.body.appendChild(elm);
await doubleMicrotask();
expect(isActuallyNativeShadow(elm.shadowRoot)).toBe(true);
expect(isClaimingToBeSyntheticShadow(elm.shadowRoot)).toBe(false);
expect(getComputedStyle(elm.shadowRoot.querySelector('h1')).color).toBe(
'rgb(0, 0, 0)'
);
});
it('does not use global styles for native components', async () => {
const elm = createElement('x-native', { is: Native });
document.body.appendChild(elm);
await doubleMicrotask();
expect(isActuallyNativeShadow(elm.shadowRoot)).toBe(true);
expect(isClaimingToBeSyntheticShadow(elm.shadowRoot)).toBe(false);
expect(getComputedStyle(elm.shadowRoot.querySelector('h1')).color).toBe(
'rgb(0, 0, 0)'
);
});
});
});
}