/
style-map_test.ts
210 lines (191 loc) · 7.09 KB
/
style-map_test.ts
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/**
* @license
* Copyright 2018 Google LLC
* SPDX-License-Identifier: BSD-3-Clause
*/
import {AttributePart, html, render} from 'lit-html';
import {directive} from 'lit-html/directive.js';
import {StyleInfo, styleMap} from 'lit-html/directives/style-map.js';
import {assert} from '@esm-bundle/chai';
const ua = window.navigator.userAgent;
const isChrome41 = ua.indexOf('Chrome/41') > 0;
const isIE = ua.indexOf('Trident/') > 0;
const testIfSupportsCSSVariables = (test: any) =>
isIE || isChrome41 ? test.skip : test;
suite('styleMap', () => {
let container: HTMLDivElement;
function renderStyleMap(cssInfo: StyleInfo) {
render(html`<div style="${styleMap(cssInfo)}"></div>`, container);
}
function renderStyleMapStatic(cssInfo: StyleInfo) {
render(
html`<div style="height: 1px; ${styleMap(cssInfo)} color: red"></div>`,
container
);
}
setup(() => {
container = document.createElement('div');
});
test('render() only properties', () => {
// Get the StyleMapDirective class indirectly, since it's not exported
const result = styleMap({});
// This property needs to remain unminified.
const StyleMapDirective = result['_$litDirective$'];
// Extend StyleMapDirective so we can test its render() method
class TestStyleMapDirective extends StyleMapDirective {
override update(
_part: AttributePart,
[styleInfo]: Parameters<this['render']>
) {
return this.render(styleInfo);
}
}
const testStyleMap = directive(TestStyleMapDirective);
render(
html`<div
style=${testStyleMap({
color: 'red',
backgroundColor: 'blue',
webkitAppearance: 'none',
['padding-left']: '4px',
})}
></div>`,
container
);
const div = container.firstElementChild as HTMLDivElement;
const style = div.style;
assert.equal(style.color, 'red');
assert.equal(style.backgroundColor, 'blue');
assert.include(['none', undefined], style.webkitAppearance);
assert.equal(style.paddingLeft, '4px');
});
test('first render skips undefined properties', () => {
renderStyleMap({marginTop: undefined, marginBottom: null});
const el = container.firstElementChild as HTMLElement;
// Note calling `setAttribute('style', '') does results in
// `getAttribute('style') === null` on IE11; test cssText instead
assert.equal(el.style.cssText, '');
assert.equal(el.style.marginTop, '');
assert.equal(el.style.marginBottom, '');
});
test('adds and updates properties', () => {
renderStyleMap({
marginTop: '2px',
'padding-bottom': '4px',
opacity: '0.5',
'z-index': 10,
});
const el = container.firstElementChild as HTMLElement;
assert.equal(el.style.marginTop, '2px');
assert.equal(el.style.paddingBottom, '4px');
assert.equal(el.style.opacity, '0.5');
assert.equal(el.style.zIndex, '10');
renderStyleMap({
marginTop: '4px',
paddingBottom: '8px',
opacity: '0.55',
'z-index': 1,
});
assert.equal(el.style.marginTop, '4px');
assert.equal(el.style.paddingBottom, '8px');
assert.equal(el.style.opacity, '0.55');
assert.equal(el.style.zIndex, '1');
});
test('removes properties', () => {
renderStyleMap({
marginTop: '2px',
'padding-bottom': '4px',
borderRadius: '5px',
borderColor: 'blue',
});
const el = container.firstElementChild as HTMLElement;
assert.equal(el.style.marginTop, '2px');
assert.equal(el.style.paddingBottom, '4px');
assert.equal(el.style.borderRadius, '5px');
assert.equal(el.style.borderColor, 'blue');
renderStyleMap({borderRadius: undefined, borderColor: null});
assert.equal(el.style.marginTop, '');
assert.equal(el.style.paddingBottom, '');
assert.equal(el.style.borderRadius, '');
assert.equal(el.style.borderColor, '');
});
test('works with static properties', () => {
renderStyleMapStatic({marginTop: '2px', 'padding-bottom': '4px'});
const el = container.firstElementChild as HTMLElement;
assert.equal(el.style.height, '1px');
assert.equal(el.style.color, 'red');
assert.equal(el.style.marginTop, '2px');
assert.equal(el.style.paddingBottom, '4px');
renderStyleMapStatic({});
assert.equal(el.style.height, '1px');
assert.equal(el.style.color, 'red');
assert.equal(el.style.marginTop, '');
assert.equal(el.style.paddingBottom, '');
});
testIfSupportsCSSVariables(test)('adds and removes CSS variables', () => {
renderStyleMap({'--size': '2px'});
const el = container.firstElementChild as HTMLElement;
assert.equal(el.style.getPropertyValue('--size'), '2px');
renderStyleMap({'--size': '4px'});
assert.equal(el.style.getPropertyValue('--size'), '4px');
renderStyleMap({});
assert.equal(el.style.getPropertyValue('--size'), '');
});
test('adds priority in updated properties', () => {
renderStyleMap({color: 'blue !important'});
const el = container.firstElementChild as HTMLElement;
assert.equal(el.style.getPropertyValue('color'), 'blue');
assert.equal(el.style.getPropertyPriority('color'), 'important');
renderStyleMap({color: 'green !important'});
assert.equal(el.style.getPropertyValue('color'), 'green');
assert.equal(el.style.getPropertyPriority('color'), 'important');
renderStyleMap({color: 'red'});
assert.equal(el.style.getPropertyValue('color'), 'red');
assert.equal(el.style.getPropertyPriority('color'), '');
renderStyleMap({});
assert.equal(el.style.getPropertyValue('color'), '');
});
test('works when used with the same object', () => {
const styleInfo: StyleInfo = {marginTop: '2px', 'padding-bottom': '4px'};
renderStyleMap(styleInfo);
const el = container.firstElementChild as HTMLElement;
assert.equal(el.style.marginTop, '2px');
assert.equal(el.style.paddingBottom, '4px');
styleInfo.marginTop = '6px';
styleInfo['padding-bottom'] = '8px';
renderStyleMap(styleInfo);
assert.equal(el.style.marginTop, '6px');
assert.equal(el.style.paddingBottom, '8px');
});
test('works when same object adds and removes properties', () => {
const styleInfo: StyleInfo = {marginTop: '2px', 'padding-bottom': '4px'};
renderStyleMap(styleInfo);
const el = container.firstElementChild as HTMLElement;
assert.equal(el.style.marginTop, '2px');
assert.equal(el.style.paddingBottom, '4px');
assert.equal(el.style.color, '');
delete styleInfo['marginTop'];
styleInfo.color = 'green';
renderStyleMap(styleInfo);
assert.equal(el.style.marginTop, '');
assert.equal(el.style.color, 'green');
});
test('throws when used on non-style attribute', () => {
assert.throws(() => {
render(html`<div id="${styleMap({})}"></div>`, container);
});
});
test('throws when used in attribute with more than 1 part', () => {
assert.throws(() => {
render(
html`<div style="${'height: 2px;'} ${styleMap({})}"></div>`,
container
);
});
});
test('throws when used in ChildPart', () => {
assert.throws(() => {
render(html`<div>${styleMap({})}</div>`, container);
});
});
});