/
CSSPropertyOperations-test.js
270 lines (236 loc) · 7.85 KB
/
CSSPropertyOperations-test.js
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/**
* Copyright 2013-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @emails react-core
*/
'use strict';
var React = require('react');
var ReactDOM = require('react-dom');
var ReactDOMServer = require('react-dom/server');
describe('CSSPropertyOperations', () => {
var CSSPropertyOperations;
beforeEach(() => {
jest.resetModules();
CSSPropertyOperations = require('CSSPropertyOperations');
});
it('should create markup for simple styles', () => {
expect(
CSSPropertyOperations.createMarkupForStyles({
backgroundColor: '#3b5998',
display: 'none',
}),
).toBe('background-color:#3b5998;display:none;');
});
it('should ignore undefined styles', () => {
expect(
CSSPropertyOperations.createMarkupForStyles({
backgroundColor: undefined,
display: 'none',
}),
).toBe('display:none;');
});
it('should ignore null styles', () => {
expect(
CSSPropertyOperations.createMarkupForStyles({
backgroundColor: null,
display: 'none',
}),
).toBe('display:none;');
});
it('should return null for no styles', () => {
expect(
CSSPropertyOperations.createMarkupForStyles({
backgroundColor: null,
display: null,
}),
).toBe(null);
});
it('should automatically append `px` to relevant styles', () => {
expect(
CSSPropertyOperations.createMarkupForStyles({
left: 0,
margin: 16,
opacity: 0.5,
padding: '4px',
}),
).toBe('left:0;margin:16px;opacity:0.5;padding:4px;');
});
it('should trim values', () => {
expect(
CSSPropertyOperations.createMarkupForStyles({
left: '16 ',
opacity: 0.5,
right: ' 4 ',
}),
).toBe('left:16;opacity:0.5;right:4;');
});
it('should not append `px` to styles that might need a number', () => {
var CSSProperty = require('CSSProperty');
var unitlessProperties = Object.keys(CSSProperty.isUnitlessNumber);
unitlessProperties.forEach(function(property) {
var styles = {};
styles[property] = 1;
expect(CSSPropertyOperations.createMarkupForStyles(styles)).toMatch(
/:1;$/,
);
});
});
it('should create vendor-prefixed markup correctly', () => {
expect(
CSSPropertyOperations.createMarkupForStyles({
msTransition: 'none',
MozTransition: 'none',
}),
).toBe('-ms-transition:none;-moz-transition:none;');
});
it('should set style attribute when styles exist', () => {
var styles = {
backgroundColor: '#000',
display: 'none',
};
var div = <div style={styles} />;
var root = document.createElement('div');
div = ReactDOM.render(div, root);
expect(/style=".*"/.test(root.innerHTML)).toBe(true);
});
it('should not set style attribute when no styles exist', () => {
var styles = {
backgroundColor: null,
display: null,
};
var div = <div style={styles} />;
var html = ReactDOMServer.renderToString(div);
expect(/style=/.test(html)).toBe(false);
});
it('should warn when using hyphenated style names', () => {
class Comp extends React.Component {
static displayName = 'Comp';
render() {
return <div style={{'background-color': 'crimson'}} />;
}
}
spyOn(console, 'error');
var root = document.createElement('div');
ReactDOM.render(<Comp />, root);
expectDev(console.error.calls.count()).toBe(1);
expectDev(console.error.calls.argsFor(0)[0]).toEqual(
'Warning: Unsupported style property background-color. Did you mean backgroundColor?' +
'\n\nCheck the render method of `Comp`.',
);
});
it('should warn when updating hyphenated style names', () => {
class Comp extends React.Component {
static displayName = 'Comp';
render() {
return <div style={this.props.style} />;
}
}
spyOn(console, 'error');
var styles = {
'-ms-transform': 'translate3d(0, 0, 0)',
'-webkit-transform': 'translate3d(0, 0, 0)',
};
var root = document.createElement('div');
ReactDOM.render(<Comp />, root);
ReactDOM.render(<Comp style={styles} />, root);
expectDev(console.error.calls.count()).toBe(2);
expectDev(console.error.calls.argsFor(0)[0]).toEqual(
'Warning: Unsupported style property -ms-transform. Did you mean msTransform?' +
'\n\nCheck the render method of `Comp`.',
);
expectDev(console.error.calls.argsFor(1)[0]).toEqual(
'Warning: Unsupported style property -webkit-transform. Did you mean WebkitTransform?' +
'\n\nCheck the render method of `Comp`.',
);
});
it('warns when miscapitalizing vendored style names', () => {
class Comp extends React.Component {
static displayName = 'Comp';
render() {
return (
<div
style={{
msTransform: 'translate3d(0, 0, 0)',
oTransform: 'translate3d(0, 0, 0)',
webkitTransform: 'translate3d(0, 0, 0)',
}}
/>
);
}
}
spyOn(console, 'error');
var root = document.createElement('div');
ReactDOM.render(<Comp />, root);
// msTransform is correct already and shouldn't warn
expectDev(console.error.calls.count()).toBe(2);
expectDev(console.error.calls.argsFor(0)[0]).toEqual(
'Warning: Unsupported vendor-prefixed style property oTransform. ' +
'Did you mean OTransform?\n\nCheck the render method of `Comp`.',
);
expectDev(console.error.calls.argsFor(1)[0]).toEqual(
'Warning: Unsupported vendor-prefixed style property webkitTransform. ' +
'Did you mean WebkitTransform?\n\nCheck the render method of `Comp`.',
);
});
it('should warn about style having a trailing semicolon', () => {
class Comp extends React.Component {
static displayName = 'Comp';
render() {
return (
<div
style={{
fontFamily: 'Helvetica, arial',
backgroundImage: 'url(foo;bar)',
backgroundColor: 'blue;',
color: 'red; ',
}}
/>
);
}
}
spyOn(console, 'error');
var root = document.createElement('div');
ReactDOM.render(<Comp />, root);
expectDev(console.error.calls.count()).toBe(2);
expectDev(console.error.calls.argsFor(0)[0]).toEqual(
"Warning: Style property values shouldn't contain a semicolon." +
'\n\nCheck the render method of `Comp`. Try "backgroundColor: blue" instead.',
);
expectDev(console.error.calls.argsFor(1)[0]).toEqual(
"Warning: Style property values shouldn't contain a semicolon." +
'\n\nCheck the render method of `Comp`. Try "color: red" instead.',
);
});
it('should warn about style containing a NaN value', () => {
class Comp extends React.Component {
static displayName = 'Comp';
render() {
return <div style={{fontSize: NaN}} />;
}
}
spyOn(console, 'error');
var root = document.createElement('div');
ReactDOM.render(<Comp />, root);
expectDev(console.error.calls.count()).toBe(1);
expectDev(console.error.calls.argsFor(0)[0]).toEqual(
'Warning: `NaN` is an invalid value for the `fontSize` css style property.' +
'\n\nCheck the render method of `Comp`.',
);
});
it('should not warn when setting CSS variables', () => {
class Comp extends React.Component {
render() {
return <div style={{'--foo-primary': 'red', backgroundColor: 'red'}} />;
}
}
spyOn(console, 'error');
var root = document.createElement('div');
ReactDOM.render(<Comp />, root);
expectDev(console.error.calls.count()).toBe(0);
});
});