Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81165 views
1
/**
2
* Copyright 2013-2014, Facebook, Inc.
3
* All rights reserved.
4
*
5
* This source code is licensed under the BSD-style license found in the
6
* LICENSE file in the root directory of this source tree. An additional grant
7
* of patent rights can be found in the PATENTS file in the same directory.
8
*
9
* @emails react-core
10
*/
11
12
/*jslint evil: true */
13
14
"use strict";
15
16
var React = require('React');
17
18
describe('CSSPropertyOperations', function() {
19
var CSSPropertyOperations;
20
21
beforeEach(function() {
22
require('mock-modules').dumpCache();
23
CSSPropertyOperations = require('CSSPropertyOperations');
24
});
25
26
it('should create markup for simple styles', function() {
27
expect(CSSPropertyOperations.createMarkupForStyles({
28
backgroundColor: '#3b5998',
29
display: 'none'
30
})).toBe('background-color:#3b5998;display:none;');
31
});
32
33
it('should ignore undefined styles', function() {
34
expect(CSSPropertyOperations.createMarkupForStyles({
35
backgroundColor: undefined,
36
display: 'none'
37
})).toBe('display:none;');
38
});
39
40
it('should ignore null styles', function() {
41
expect(CSSPropertyOperations.createMarkupForStyles({
42
backgroundColor: null,
43
display: 'none'
44
})).toBe('display:none;');
45
});
46
47
it('should return null for no styles', function() {
48
expect(CSSPropertyOperations.createMarkupForStyles({
49
backgroundColor: null,
50
display: null
51
})).toBe(null);
52
});
53
54
it('should automatically append `px` to relevant styles', function() {
55
expect(CSSPropertyOperations.createMarkupForStyles({
56
left: 0,
57
margin: 16,
58
opacity: 0.5,
59
padding: '4px'
60
})).toBe('left:0;margin:16px;opacity:0.5;padding:4px;');
61
});
62
63
it('should trim values so `px` will be appended correctly', function() {
64
expect(CSSPropertyOperations.createMarkupForStyles({
65
margin: '16 ',
66
opacity: 0.5,
67
padding: ' 4 '
68
})).toBe('margin:16px;opacity:0.5;padding:4px;');
69
});
70
71
it('should not append `px` to styles that might need a number', function() {
72
var CSSProperty = require('CSSProperty');
73
var unitlessProperties = Object.keys(CSSProperty.isUnitlessNumber);
74
unitlessProperties.forEach(function(property) {
75
var styles = {};
76
styles[property] = 1;
77
expect(CSSPropertyOperations.createMarkupForStyles(styles))
78
.toMatch(/:1;$/);
79
});
80
});
81
82
it('should create vendor-prefixed markup correctly', function() {
83
expect(CSSPropertyOperations.createMarkupForStyles({
84
msTransition: 'none',
85
MozTransition: 'none'
86
})).toBe('-ms-transition:none;-moz-transition:none;');
87
});
88
89
it('should set style attribute when styles exist', function() {
90
var styles = {
91
backgroundColor: '#000',
92
display: 'none'
93
};
94
var div = <div style={styles} />;
95
var root = document.createElement('div');
96
div = React.render(div, root);
97
expect(/style=".*"/.test(root.innerHTML)).toBe(true);
98
});
99
100
it('should not set style attribute when no styles exist', function() {
101
var styles = {
102
backgroundColor: null,
103
display: null
104
};
105
var div = <div style={styles} />;
106
var root = document.createElement('div');
107
React.render(div, root);
108
expect(/style=".*"/.test(root.innerHTML)).toBe(false);
109
});
110
111
it('should warn when using hyphenated style names', function() {
112
spyOn(console, 'warn');
113
114
expect(CSSPropertyOperations.createMarkupForStyles({
115
'background-color': 'crimson'
116
})).toBe('background-color:crimson;');
117
118
expect(console.warn.argsForCall.length).toBe(1);
119
expect(console.warn.argsForCall[0][0]).toContain('backgroundColor');
120
});
121
122
it('should warn when updating hyphenated style names', function() {
123
spyOn(console, 'warn');
124
125
var root = document.createElement('div');
126
var styles = {
127
'-ms-transform': 'translate3d(0, 0, 0)',
128
'-webkit-transform': 'translate3d(0, 0, 0)'
129
};
130
131
React.render(<div />, root);
132
React.render(<div style={styles} />, root);
133
134
expect(console.warn.argsForCall.length).toBe(2);
135
expect(console.warn.argsForCall[0][0]).toContain('msTransform');
136
expect(console.warn.argsForCall[1][0]).toContain('WebkitTransform');
137
});
138
139
});
140
141