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

Move tests concerned with cloning into ReactElementClone-test

Dan Abramov committed May 26, 2016 at 00:27 UTC d9ae319821b566ec1de2a21079babe95faaafa3e
2 files changed +116 -106
src/isomorphic/classic/element/__tests__/ReactElement-test.js
+4 -106
@@ -170,7 +170,7 @@ describe('ReactElement', function() {
170 expect(element.props).toEqual(expectation);
171 });
172
173 - it('extracts null key and ref values when creating an element', function() {
173 + it('extracts null key and ref', function() {
174 var element = React.createFactory(ComponentClass)({
175 key: null,
176 ref: null,
@@ -184,7 +184,7 @@ describe('ReactElement', function() {
184 expect(element.props).toEqual(expectation);
185 });
186
187 - it('ignores undefined key and ref when creating an element', function() {
187 + it('ignores undefined key and ref', function() {
188 var props = {
189 foo: '56',
190 key: undefined,
@@ -199,7 +199,7 @@ describe('ReactElement', function() {
199 expect(element.props).toEqual(expectation);
200 });
201
202 - it('ignores key and ref getters when creating an element', function() {
202 + it('ignores key and ref getters', function() {
203 var props = {
204 foo: '56',
205 };
@@ -222,74 +222,6 @@ describe('ReactElement', function() {
222 expect(element.props).toEqual(expectation);
223 });
224
225 - it('ignores key and ref getters when cloning an element', function() {
226 - var element = React.createFactory(ComponentClass)({
227 - key: '12',
228 - ref: '34',
229 - foo: '56',
230 - });
231 - var props = {
232 - foo: 'ef',
233 - };
234 - Object.defineProperty(props, 'key', {
235 - get: function() {
236 - return 'ab';
237 - },
238 - });
239 - Object.defineProperty(props, 'ref', {
240 - get: function() {
241 - return 'cd';
242 - },
243 - });
244 - var clone = React.cloneElement(element, props);
245 - expect(clone.type).toBe(ComponentClass);
246 - expect(clone.key).toBe('12');
247 - expect(clone.ref).toBe('34');
248 - var expectation = {foo: 'ef'};
249 - Object.freeze(expectation);
250 - expect(clone.props).toEqual(expectation);
251 - });
252 -
253 - it('ignores undefined key and ref values when cloning an element', function() {
254 - var element = React.createFactory(ComponentClass)({
255 - key: '12',
256 - ref: '34',
257 - foo: '56',
258 - });
259 - var props = {
260 - key: undefined,
261 - ref: undefined,
262 - foo: 'ef',
263 - };
264 - var clone = React.cloneElement(element, props);
265 - expect(clone.type).toBe(ComponentClass);
266 - expect(clone.key).toBe('12');
267 - expect(clone.ref).toBe('34');
268 - var expectation = {foo: 'ef'};
269 - Object.freeze(expectation);
270 - expect(clone.props).toEqual(expectation);
271 - });
272 -
273 - it('extracts null key and ref values when cloning an element', function() {
274 - var element = React.createFactory(ComponentClass)({
275 - key: '12',
276 - ref: '34',
277 - foo: '56',
278 - });
279 - var props = {
280 - key: null,
281 - ref: null,
282 - foo: 'ef',
283 - };
284 - var clone = React.cloneElement(element, props);
285 - expect(clone.type).toBe(ComponentClass);
286 - expect(clone.key).toBe('null');
287 - expect(clone.ref).toBe(null);
288 - var expectation = {foo: 'ef'};
289 - Object.freeze(expectation);
290 - expect(clone.props).toEqual(expectation);
291 - });
292 -
225 it('coerces the key to a string', function() {
226 var element = React.createFactory(ComponentClass)({
227 key: 12,
@@ -349,18 +281,6 @@ describe('ReactElement', function() {
281 expect(console.error.calls.count()).toBe(0);
282 });
283
352 - it('overrides children if undefined is provided as an argument', function() {
353 - var element = React.createElement(ComponentClass, {
354 - children: 'text',
355 - }, undefined);
356 - expect(element.props.children).toBe(undefined);
357 -
358 - var element2 = React.cloneElement(React.createElement(ComponentClass, {
359 - children: 'text',
360 - }), {}, undefined);
361 - expect(element2.props.children).toBe(undefined);
362 - });
363 -
284 it('merges rest arguments onto the children prop in an array', function() {
285 spyOn(console, 'error');
286 var a = 1;
@@ -490,29 +410,6 @@ describe('ReactElement', function() {
410 expect(inst2.props.prop).toBe(null);
411 });
412
493 - it('should normalize props with default values in cloning', function() {
494 - var Component = React.createClass({
495 - getDefaultProps: function() {
496 - return {prop: 'testKey'};
497 - },
498 - render: function() {
499 - return <span />;
500 - },
501 - });
502 -
503 - var instance = React.createElement(Component);
504 - var clonedInstance = React.cloneElement(instance, {prop: undefined});
505 - expect(clonedInstance.props.prop).toBe('testKey');
506 - var clonedInstance2 = React.cloneElement(instance, {prop: null});
507 - expect(clonedInstance2.props.prop).toBe(null);
508 -
509 - var instance2 = React.createElement(Component, {prop: 'newTestKey'});
510 - var cloneInstance3 = React.cloneElement(instance2, {prop: undefined});
511 - expect(cloneInstance3.props.prop).toBe('testKey');
512 - var cloneInstance4 = React.cloneElement(instance2, {});
513 - expect(cloneInstance4.props.prop).toBe('newTestKey');
514 - });
515 -
413 it('throws when changing a prop (in dev) after element creation', function() {
414 var Outer = React.createClass({
415 render: function() {
@@ -703,4 +600,5 @@ describe('comparing jsx vs .createFactory() vs .createElement()', function() {
600 expect(Child.mock.calls[0][0]).toEqual({ foo: 'foo value', children: 'children value' });
601 });
602 });
603 +
604 });
src/isomorphic/classic/element/__tests__/ReactElementClone-test.js
+112
@@ -16,11 +16,20 @@ var ReactDOM;
16 var ReactTestUtils;
17
18 describe('ReactElementClone', function() {
19 + var ComponentClass;
20
21 beforeEach(function() {
22 React = require('React');
23 ReactDOM = require('ReactDOM');
24 ReactTestUtils = require('ReactTestUtils');
25 +
26 + // NOTE: We're explicitly not using JSX here. This is intended to test
27 + // classic JS without JSX.
28 + ComponentClass = React.createClass({
29 + render: function() {
30 + return React.createElement('div');
31 + },
32 + });
33 });
34
35 it('should clone a DOM component with new props', function() {
@@ -155,6 +164,18 @@ describe('ReactElementClone', function() {
164 ]);
165 });
166
167 + it('should override children if undefined is provided as an argument', function() {
168 + var element = React.createElement(ComponentClass, {
169 + children: 'text',
170 + }, undefined);
171 + expect(element.props.children).toBe(undefined);
172 +
173 + var element2 = React.cloneElement(React.createElement(ComponentClass, {
174 + children: 'text',
175 + }), {}, undefined);
176 + expect(element2.props.children).toBe(undefined);
177 + });
178 +
179 it('should support keys and refs', function() {
180 var Parent = React.createClass({
181 render: function() {
@@ -208,6 +229,29 @@ describe('ReactElementClone', function() {
229 );
230 });
231
232 + it('should normalize props with default values', function() {
233 + var Component = React.createClass({
234 + getDefaultProps: function() {
235 + return {prop: 'testKey'};
236 + },
237 + render: function() {
238 + return <span />;
239 + },
240 + });
241 +
242 + var instance = React.createElement(Component);
243 + var clonedInstance = React.cloneElement(instance, {prop: undefined});
244 + expect(clonedInstance.props.prop).toBe('testKey');
245 + var clonedInstance2 = React.cloneElement(instance, {prop: null});
246 + expect(clonedInstance2.props.prop).toBe(null);
247 +
248 + var instance2 = React.createElement(Component, {prop: 'newTestKey'});
249 + var cloneInstance3 = React.cloneElement(instance2, {prop: undefined});
250 + expect(cloneInstance3.props.prop).toBe('testKey');
251 + var cloneInstance4 = React.cloneElement(instance2, {});
252 + expect(cloneInstance4.props.prop).toBe('newTestKey');
253 + });
254 +
255 it('warns for keys for arrays of elements in rest args', function() {
256 spyOn(console, 'error');
257
@@ -278,4 +322,72 @@ describe('ReactElementClone', function() {
322 );
323 });
324
325 + it('should ignore key and ref getters', function() {
326 + var element = React.createFactory(ComponentClass)({
327 + key: '12',
328 + ref: '34',
329 + foo: '56',
330 + });
331 + var props = {
332 + foo: 'ef',
333 + };
334 + Object.defineProperty(props, 'key', {
335 + get: function() {
336 + return 'ab';
337 + },
338 + });
339 + Object.defineProperty(props, 'ref', {
340 + get: function() {
341 + return 'cd';
342 + },
343 + });
344 + var clone = React.cloneElement(element, props);
345 + expect(clone.type).toBe(ComponentClass);
346 + expect(clone.key).toBe('12');
347 + expect(clone.ref).toBe('34');
348 + var expectation = {foo: 'ef'};
349 + Object.freeze(expectation);
350 + expect(clone.props).toEqual(expectation);
351 + });
352 +
353 + it('should ignore undefined key and ref', function() {
354 + var element = React.createFactory(ComponentClass)({
355 + key: '12',
356 + ref: '34',
357 + foo: '56',
358 + });
359 + var props = {
360 + key: undefined,
361 + ref: undefined,
362 + foo: 'ef',
363 + };
364 + var clone = React.cloneElement(element, props);
365 + expect(clone.type).toBe(ComponentClass);
366 + expect(clone.key).toBe('12');
367 + expect(clone.ref).toBe('34');
368 + var expectation = {foo: 'ef'};
369 + Object.freeze(expectation);
370 + expect(clone.props).toEqual(expectation);
371 + });
372 +
373 + it('should extract null key and ref', function() {
374 + var element = React.createFactory(ComponentClass)({
375 + key: '12',
376 + ref: '34',
377 + foo: '56',
378 + });
379 + var props = {
380 + key: null,
381 + ref: null,
382 + foo: 'ef',
383 + };
384 + var clone = React.cloneElement(element, props);
385 + expect(clone.type).toBe(ComponentClass);
386 + expect(clone.key).toBe('null');
387 + expect(clone.ref).toBe(null);
388 + var expectation = {foo: 'ef'};
389 + Object.freeze(expectation);
390 + expect(clone.props).toEqual(expectation);
391 + });
392 +
393 });