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

Warn if accessing .type on a factory

This was an important convenience as an upgrade path but shouldn't be necessary if you're using best-practice of calling createFactory in the consuming component.

Sebastian Markbage committed Feb 17, 2015 at 11:06 UTC 35874606750b398a7ca6c42b92b35a6cc27cc2b3
2 files changed +46
src/classic/element/ReactElementValidator.js
+27
@@ -387,6 +387,33 @@ var ReactElementValidator = {
387 );
388 // Legacy hook TODO: Warn if this is accessed
389 validatedFactory.type = type;
390 +
391 + if (__DEV__) {
392 + try {
393 + Object.defineProperty(
394 + validatedFactory,
395 + 'type',
396 + {
397 + enumerable: false,
398 + get: function() {
399 + warning(
400 + false,
401 + 'Factory.type is deprecated. Access the class directly ' +
402 + 'before passing it to createFactory.'
403 + );
404 + Object.defineProperty(this, 'type', {
405 + value: type
406 + });
407 + return type;
408 + }
409 + }
410 + );
411 + } catch (x) {
412 + // IE will fail on defineProperty (es5-shim/sham too)
413 + }
414 + }
415 +
416 +
417 return validatedFactory;
418 }
419
src/classic/element/__tests__/ReactElementValidator-test.js
+19
@@ -330,4 +330,23 @@ describe('ReactElementValidator', function() {
330 expect(console.warn.calls[0].args[0]).toContain('use of a keyed object');
331 });
332
333 + it('should warn when accessing .type on an element factory', function() {
334 + spyOn(console, 'warn');
335 + var TestComponent = React.createClass({
336 + render: function() {
337 + return <div />;
338 + }
339 + });
340 + var TestFactory = React.createFactory(TestComponent);
341 + expect(TestFactory.type).toBe(TestComponent);
342 + expect(console.warn.argsForCall.length).toBe(1);
343 + expect(console.warn.argsForCall[0][0]).toBe(
344 + 'Warning: Factory.type is deprecated. Access the class directly before ' +
345 + 'passing it to createFactory.'
346 + );
347 + // Warn once, not again
348 + expect(TestFactory.type).toBe(TestComponent);
349 + expect(console.warn.argsForCall.length).toBe(1);
350 + });
351 +
352 });