@samitouri / QOS-React-2 / commits / af1af19d39

Sync out transform tests

Paul O’Shannessy committed Jun 19, 2014 at 15:14 UTC af1af19d395ee23f1947eadf57eccf5500db71ba
2 files changed +510
vendor/fbtransform/transforms/__tests__/react-test.js new
+360
@@ -0,0 +1,360 @@
1 +/**
2 + * Copyright 2013-2014 Facebook, Inc.
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + *
16 + * @emails javascript@lists.facebook.com
17 + */
18 +"use strict";
19 +
20 +require('mock-modules').autoMockOff();
21 +
22 +describe('react jsx', function() {
23 + var transformAll = require('../../syntax.js').transformAll;
24 +
25 + var transform = function(code, options, excludes) {
26 + return transformAll(
27 + code,
28 + options,
29 + (excludes || []).concat(['sourcemeta', 'allocate'])
30 + );
31 + };
32 +
33 + it('should convert simple tags', function() {
34 + var code = [
35 + '/**@jsx React.DOM*/',
36 + 'var x = <div></div>;'
37 + ].join('\n');
38 + var result = [
39 + '/**@jsx React.DOM*/',
40 + 'var x = React.DOM.div(null);'
41 + ].join('\n');
42 +
43 + expect(transform(code).code).toEqual(result);
44 + });
45 +
46 + it('should convert simple text', function() {
47 + var code = [
48 + '/**@jsx React.DOM*/\n' +
49 + 'var x = <div>text</div>;'
50 + ].join('\n');
51 + var result = [
52 + '/**@jsx React.DOM*/',
53 + 'var x = React.DOM.div(null, "text");'
54 + ].join('\n');
55 +
56 + expect(transform(code).code).toEqual(result);
57 + });
58 +
59 + it('should have correct comma in nested children', function() {
60 + var code = [
61 + '/**@jsx React.DOM*/',
62 + 'var x = <div>',
63 + ' <div><br /></div>',
64 + ' <Component>{foo}<br />{bar}</Component>',
65 + ' <br />',
66 + '</div>;'
67 + ].join('\n');
68 + var result = [
69 + '/**@jsx React.DOM*/',
70 + 'var x = React.DOM.div(null, ',
71 + ' React.DOM.div(null, React.DOM.br(null)), ',
72 + ' Component(null, foo, React.DOM.br(null), bar), ',
73 + ' React.DOM.br(null)',
74 + ');'
75 + ].join('\n');
76 +
77 + expect(transform(code).code).toEqual(result);
78 + });
79 +
80 + it('should avoid wrapping in extra parens if not needed', function() {
81 + // Try with a single composite child, wrapped in a div.
82 + var code = [
83 + '/**@jsx React.DOM*/',
84 + 'var x = <div>',
85 + ' <Component />',
86 + '</div>;'
87 + ].join('\n');
88 + var result = [
89 + '/**@jsx React.DOM*/',
90 + 'var x = React.DOM.div(null, ',
91 + ' Component(null)',
92 + ');'
93 + ].join('\n');
94 +
95 + expect(transform(code).code).toEqual(result);
96 +
97 + // Try with a single interpolated child, wrapped in a div.
98 + code = [
99 + '/**@jsx React.DOM*/',
100 + 'var x = <div>',
101 + ' {this.props.children}',
102 + '</div>;'
103 + ].join('\n');
104 + result = [
105 + '/**@jsx React.DOM*/',
106 + 'var x = React.DOM.div(null, ',
107 + ' this.props.children',
108 + ');'
109 + ].join('\n');
110 + expect(transform(code).code).toEqual(result);
111 +
112 + // Try with a single interpolated child, wrapped in a composite.
113 + code = [
114 + '/**@jsx React.DOM*/',
115 + 'var x = <Composite>',
116 + ' {this.props.children}',
117 + '</Composite>;'
118 + ].join('\n');
119 + result = [
120 + '/**@jsx React.DOM*/',
121 + 'var x = Composite(null, ',
122 + ' this.props.children',
123 + ');'
124 + ].join('\n');
125 + expect(transform(code).code).toEqual(result);
126 +
127 + // Try with a single composite child, wrapped in a composite.
128 + code = [
129 + '/**@jsx React.DOM*/',
130 + 'var x = <Composite>',
131 + ' <Composite2 />',
132 + '</Composite>;'
133 + ].join('\n');
134 + result = [
135 + '/**@jsx React.DOM*/',
136 + 'var x = Composite(null, ',
137 + ' Composite2(null)',
138 + ');'
139 + ].join('\n');
140 + expect(transform(code).code).toEqual(result);
141 + });
142 +
143 + it('should insert commas after expressions before whitespace', function() {
144 + var code = [
145 + '/**@jsx React.DOM*/',
146 + 'var x =',
147 + ' <div',
148 + ' attr1={',
149 + ' "foo" + "bar"',
150 + ' }',
151 + ' attr2={',
152 + ' "foo" + "bar" +',
153 + ' ',
154 + ' "baz" + "bug"',
155 + ' }',
156 + ' attr3={',
157 + ' "foo" + "bar" +',
158 + ' "baz" + "bug"',
159 + ' // Extra line here.',
160 + ' }',
161 + ' attr4="baz">',
162 + ' </div>;'
163 + ].join('\n');
164 + var result = [
165 + '/**@jsx React.DOM*/',
166 + 'var x =',
167 + ' React.DOM.div({',
168 + ' attr1: ',
169 + ' "foo" + "bar", ',
170 + ' ',
171 + ' attr2: ',
172 + ' "foo" + "bar" +',
173 + ' ',
174 + ' "baz" + "bug", ',
175 + ' ',
176 + ' attr3: ',
177 + ' "foo" + "bar" +',
178 + ' "baz" + "bug", ',
179 + ' // Extra line here.',
180 + ' ',
181 + ' attr4: "baz"}',
182 + ' );'
183 + ].join('\n');
184 +
185 + expect(transform(code).code).toEqual(result);
186 + });
187 +
188 + it('should properly handle comments adjacent to children', function() {
189 + var code = [
190 + '/**',
191 + ' * @jsx React.DOM',
192 + ' */',
193 + 'var x = (',
194 + ' <div>',
195 + ' {/* A comment at the beginning */}',
196 + ' {/* A second comment at the beginning */}',
197 + ' <span>',
198 + ' {/* A nested comment */}',
199 + ' </span>',
200 + ' {/* A sandwiched comment */}',
201 + ' <br />',
202 + ' {/* A comment at the end */}',
203 + ' {/* A second comment at the end */}',
204 + ' </div>',
205 + ');'
206 + ].join('\n');
207 + var result = [
208 + '/**',
209 + ' * @jsx React.DOM',
210 + ' */',
211 + 'var x = (',
212 + ' React.DOM.div(null, ',
213 + ' /* A comment at the beginning */',
214 + ' /* A second comment at the beginning */',
215 + ' React.DOM.span(null',
216 + ' /* A nested comment */',
217 + ' ), ',
218 + ' /* A sandwiched comment */',
219 + ' React.DOM.br(null)',
220 + ' /* A comment at the end */',
221 + ' /* A second comment at the end */',
222 + ' )',
223 + ');'
224 + ].join('\n');
225 +
226 + expect(transform(code).code).toBe(result);
227 + });
228 +
229 + it('should properly handle comments between props', function() {
230 + var code = [
231 + '/**',
232 + ' * @jsx React.DOM',
233 + ' */',
234 + 'var x = (',
235 + ' <div',
236 + ' /* a multi-line',
237 + ' comment */',
238 + ' attr1="foo">',
239 + ' <span // a double-slash comment',
240 + ' attr2="bar"',
241 + ' />',
242 + ' </div>',
243 + ');'
244 + ].join('\n');
245 + var result = [
246 + '/**',
247 + ' * @jsx React.DOM',
248 + ' */',
249 + 'var x = (',
250 + ' React.DOM.div({',
251 + ' /* a multi-line',
252 + ' comment */',
253 + ' attr1: "foo"}, ',
254 + ' React.DOM.span({// a double-slash comment',
255 + ' attr2: "bar"}',
256 + ' )',
257 + ' )',
258 + ');'
259 + ].join('\n');
260 +
261 + expect(transform(code).code).toBe(result);
262 + });
263 +
264 + it('should not strip tags with a single child of &nbsp;', function() {
265 + var code = [
266 + '/**',
267 + ' * @jsx React.DOM',
268 + ' */',
269 + '<div>&nbsp;</div>;'
270 + ].join('\n');
271 + var result = [
272 + '/**',
273 + ' * @jsx React.DOM',
274 + ' */',
275 + 'React.DOM.div(null, "\u00A0");'
276 + ].join('\n');
277 +
278 + expect(transform(code).code).toBe(result);
279 + });
280 +
281 + it('should not strip &nbsp; even coupled with other whitespace', function() {
282 + var code = [
283 + '/**',
284 + ' * @jsx React.DOM',
285 + ' */',
286 + '<div>&nbsp; </div>;'
287 + ].join('\n');
288 + var result = [
289 + '/**',
290 + ' * @jsx React.DOM',
291 + ' */',
292 + 'React.DOM.div(null, "\u00A0 ");'
293 + ].join('\n');
294 +
295 + expect(transform(code).code).toBe(result);
296 + });
297 +
298 + it('should handle hasOwnProperty correctly', function() {
299 + var code = [
300 + '/**',
301 + ' * @jsx React.DOM',
302 + ' */',
303 + '<hasOwnProperty>testing</hasOwnProperty>;'
304 + ].join('\n');
305 + var result = [
306 + '/**',
307 + ' * @jsx React.DOM',
308 + ' */',
309 + 'hasOwnProperty(null, "testing");'
310 + ].join('\n');
311 +
312 + expect(transform(code).code).toBe(result);
313 + });
314 +
315 + it('should allow constructor as prop', function() {
316 + var code = [
317 + '/**',
318 + ' * @jsx React.DOM',
319 + ' */',
320 + '<Component constructor="foo" />;'
321 + ].join('\n');
322 + var result = [
323 + '/**',
324 + ' * @jsx React.DOM',
325 + ' */',
326 + 'Component({constructor: "foo"});'
327 + ].join('\n');
328 +
329 + expect(transform(code).code).toBe(result);
330 + });
331 +
332 + it('should allow JS namespacing', function() {
333 + var code = [
334 + '/**',
335 + ' * @jsx React.DOM',
336 + ' */',
337 + '<Namespace.Component />;'
338 + ].join('\n');
339 + var result = [
340 + '/**',
341 + ' * @jsx React.DOM',
342 + ' */',
343 + 'Namespace.Component(null);'
344 + ].join('\n');
345 +
346 + expect(transform(code).code).toBe(result);
347 + });
348 +
349 + it('should disallow XML namespacing', function() {
350 + var code = [
351 + '/**',
352 + ' * @jsx React.DOM',
353 + ' */',
354 + '<Namespace:Component />;'
355 + ].join('\n');
356 +
357 + expect(() => transform(code)).toThrow();
358 + });
359 +
360 +});
vendor/fbtransform/transforms/__tests__/reactDisplayName-test.js new
+150
@@ -0,0 +1,150 @@
1 +/**
2 + * Copyright 2013-2014 Facebook, Inc.
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + *
16 + * @emails javascript@lists.facebook.com
17 + */
18 +"use strict";
19 +
20 +require('mock-modules').autoMockOff();
21 +
22 +var transformAll = require('../../syntax.js').transformAll;
23 +
24 +function transform(source) {
25 + return transformAll(source, {}, ['allocate']);
26 +}
27 +
28 +describe('react displayName jsx', function() {
29 +
30 + it('should only inject displayName if missing', function() {
31 + var code = [
32 + '/** @jsx React.DOM */',
33 + '"use strict";',
34 + 'var Whateva = React.createClass({',
35 + ' displayName: \'Whateva\',',
36 + ' render: function() {',
37 + ' return <div className=\'whateva\'>...whateva.</div>;',
38 + ' }',
39 + '});'
40 + ].join('\n');
41 +
42 + var result = [
43 + '/** @jsx React.DOM */',
44 + '"use strict";',
45 + 'var Whateva = React.createClass({',
46 + ' displayName: \'Whateva\',',
47 + ' render: function() {',
48 + ' return React.DOM.div({className: "whateva"}, "...whateva.");',
49 + ' }',
50 + '});'
51 + ].join('\n');
52 +
53 + expect(transform(code).code).toEqual(result);
54 + });
55 +
56 + it('should inject displayName in simple assignment', () => {
57 + var code = [
58 + '/** @jsx React.DOM */',
59 + 'var Component = React.createClass({',
60 + ' render: function() {',
61 + ' return <div/>;',
62 + ' }',
63 + '});'
64 + ].join('\n');
65 +
66 + var result = [
67 + '/** @jsx React.DOM */',
68 + 'var Component = React.createClass({displayName: \'Component\',',
69 + ' render: function() {',
70 + ' return React.DOM.div(null);',
71 + ' }',
72 + '});'
73 + ].join('\n');
74 +
75 + expect(transform(code).code).toEqual(result);
76 + });
77 +
78 + it('should inject displayName in simple assignment without var', () => {
79 + var code = [
80 + '/** @jsx React.DOM */',
81 + 'var Component;',
82 + 'Component = React.createClass({',
83 + ' render: function() {',
84 + ' return <div/>;',
85 + ' }',
86 + '});'
87 + ].join('\n');
88 +
89 + var result = [
90 + '/** @jsx React.DOM */',
91 + 'var Component;',
92 + 'Component = React.createClass({displayName: \'Component\',',
93 + ' render: function() {',
94 + ' return React.DOM.div(null);',
95 + ' }',
96 + '});'
97 + ].join('\n');
98 +
99 + expect(transform(code).code).toEqual(result);
100 + });
101 +
102 + it('should inject displayName in property assignment', () => {
103 + var code = [
104 + '/** @jsx React.DOM */',
105 + 'exports.Component = React.createClass({',
106 + ' render: function() {',
107 + ' return <div/>;',
108 + ' }',
109 + '});'
110 + ].join('\n');
111 +
112 + var result = [
113 + '/** @jsx React.DOM */',
114 + 'exports.Component = React.createClass({displayName: \'Component\',',
115 + ' render: function() {',
116 + ' return React.DOM.div(null);',
117 + ' }',
118 + '});'
119 + ].join('\n');
120 +
121 + expect(transform(code).code).toEqual(result);
122 + });
123 +
124 + it('should inject displayName in object declaration', () => {
125 + var code = [
126 + '/** @jsx React.DOM */',
127 + 'exports = {',
128 + ' Component: React.createClass({',
129 + ' render: function() {',
130 + ' return <div/>;',
131 + ' }',
132 + ' })',
133 + '};'
134 + ].join('\n');
135 +
136 + var result = [
137 + '/** @jsx React.DOM */',
138 + 'exports = {',
139 + ' Component: React.createClass({displayName: \'Component\',',
140 + ' render: function() {',
141 + ' return React.DOM.div(null);',
142 + ' }',
143 + ' })',
144 + '};'
145 + ].join('\n');
146 +
147 + expect(transform(code).code).toEqual(result);
148 + });
149 +
150 +});