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

Disallow passing a DOM component to reactComponentExpect

We won't be able to support this after DOM-components-as-refs but we don't expect many people to be passing DOM components to this function anyway, and it should be fairly straightforward for people to clean up failing unit tests using this function. (This module also isn't public API and never has been.)

Ben Alpert committed Jun 16, 2015 at 17:51 UTC cf6b3ff0e23fd955ed313b7a50c7dd825b3f2bc5
5 files changed +43 -45
src/renderers/shared/reconciler/__tests__/ReactCompositeComponent-test.js
+3 -3
@@ -132,11 +132,11 @@ describe('ReactCompositeComponent', function() {
132 var instance = <MorphingComponent />;
133 instance = ReactTestUtils.renderIntoDocument(instance);
134
135 - reactComponentExpect(instance.refs.x).toBeDOMComponentWithTag('a');
135 + expect(React.findDOMNode(instance.refs.x).tagName).toBe('A');
136 instance._toggleActivatedState();
137 - reactComponentExpect(instance.refs.x).toBeDOMComponentWithTag('b');
137 + expect(React.findDOMNode(instance.refs.x).tagName).toBe('B');
138 instance._toggleActivatedState();
139 - reactComponentExpect(instance.refs.x).toBeDOMComponentWithTag('a');
139 + expect(React.findDOMNode(instance.refs.x).tagName).toBe('A');
140 });
141
142 it('should not cache old DOM nodes when switching constructors', function() {
src/renderers/shared/reconciler/__tests__/ReactIdentity-test.js
+3 -5
@@ -14,7 +14,6 @@
14 var React;
15 var ReactFragment;
16 var ReactTestUtils;
17 -var reactComponentExpect;
17 var ReactMount;
18
19 describe('ReactIdentity', function() {
@@ -24,7 +23,6 @@ describe('ReactIdentity', function() {
23 React = require('React');
24 ReactFragment = require('ReactFragment');
25 ReactTestUtils = require('ReactTestUtils');
27 - reactComponentExpect = require('reactComponentExpect');
26 ReactMount = require('ReactMount');
27 });
28
@@ -52,7 +50,7 @@ describe('ReactIdentity', function() {
50
51 instance = React.render(instance, document.createElement('div'));
52 var node = React.findDOMNode(instance);
55 - reactComponentExpect(instance).toBeDOMComponentWithChildCount(2);
53 + expect(node.childNodes.length).toBe(2);
54 checkId(node.childNodes[0], '.0.$first:0');
55 checkId(node.childNodes[1], '.0.$second:0');
56 });
@@ -68,7 +66,7 @@ describe('ReactIdentity', function() {
66
67 instance = React.render(instance, document.createElement('div'));
68 var node = React.findDOMNode(instance);
71 - reactComponentExpect(instance).toBeDOMComponentWithChildCount(4);
69 + expect(node.childNodes.length).toBe(4);
70 checkId(node.childNodes[0], '.0.$apple');
71 checkId(node.childNodes[1], '.0.$banana');
72 checkId(node.childNodes[2], '.0.$0');
@@ -92,7 +90,7 @@ describe('ReactIdentity', function() {
90
91 instance = React.render(instance, document.createElement('div'));
92 var node = React.findDOMNode(instance);
95 - reactComponentExpect(instance).toBeDOMComponentWithChildCount(3);
93 + expect(node.childNodes.length).toBe(3);
94
95 checkId(node.childNodes[0], '.0.$wrap1');
96 checkId(node.childNodes[0].firstChild, '.0.$wrap1.$squirrel');
src/renderers/shared/reconciler/__tests__/ReactMultiChildText-test.js
+7 -17
@@ -17,8 +17,6 @@ var React = require('React');
17 var ReactFragment = require('ReactFragment');
18 var ReactTestUtils = require('ReactTestUtils');
19
20 -var reactComponentExpect = require('reactComponentExpect');
21 -
20 var frag = ReactFragment.create;
21
22 // Helpers
@@ -42,9 +40,10 @@ var testAllPermutations = function(testCases) {
40 };
41
42 var expectChildren = function(d, children) {
43 + var outerNode = React.findDOMNode(d);
44 var textNode;
45 if (typeof children === 'string') {
47 - textNode = React.findDOMNode(d).firstChild;
46 + textNode = outerNode.firstChild;
47
48 if (children === '') {
49 expect(textNode != null).toBe(false);
@@ -54,32 +53,23 @@ var expectChildren = function(d, children) {
53 expect(textNode.data).toBe('' + children);
54 }
55 } else {
57 - expect(React.findDOMNode(d).childNodes.length).toBe(children.length);
56 + expect(outerNode.childNodes.length).toBe(children.length);
57
58 for (var i = 0; i < children.length; i++) {
59 var child = children[i];
60
61 if (typeof child === 'string') {
63 - reactComponentExpect(d)
64 - .expectRenderedChildAt(i)
65 - .toBeTextComponentWithValue(child);
66 -
67 - textNode = React.findDOMNode(d).childNodes[i].firstChild;
62 + textNode = outerNode.childNodes[i].firstChild;
63
64 if (child === '') {
70 - expect(textNode != null).toBe(false);
65 + expect(textNode).toBe(null);
66 } else {
72 - expect(textNode != null).toBe(true);
67 + expect(textNode).not.toBe(null);
68 expect(textNode.nodeType).toBe(3);
69 expect(textNode.data).toBe('' + child);
70 }
71 } else {
77 - var elementDOMNode = React.findDOMNode(reactComponentExpect(d)
78 - .expectRenderedChildAt(i)
79 - .toBeComponentOfType('div')
80 - .instance()
81 - );
82 -
72 + var elementDOMNode = outerNode.childNodes[i];
73 expect(elementDOMNode.tagName).toBe('DIV');
74 }
75 }
src/renderers/shared/reconciler/__tests__/refs-destruction-test.js
+25 -20
@@ -11,33 +11,38 @@
11
12 'use strict';
13
14 -var React = require('React');
15 -var reactComponentExpect = require('reactComponentExpect');
16 -
17 -var TestComponent = React.createClass({
18 - render: function() {
19 - return (
20 - <div>
21 - {this.props.destroy ? null :
22 - <div ref="theInnerDiv">
23 - Lets try to destroy this.
24 - </div>
25 - }
26 - </div>
27 - );
28 - },
29 -});
14 +var React;
15 +var ReactTestUtils;
16 +
17 +var TestComponent;
18
19 describe('refs-destruction', function() {
20 beforeEach(function() {
21 require('mock-modules').dumpCache();
22 +
23 + React = require('React');
24 + ReactTestUtils = require('ReactTestUtils');
25 +
26 + TestComponent = React.createClass({
27 + render: function() {
28 + return (
29 + <div>
30 + {this.props.destroy ? null :
31 + <div ref="theInnerDiv">
32 + Lets try to destroy this.
33 + </div>
34 + }
35 + </div>
36 + );
37 + },
38 + });
39 });
40
41 it('should remove refs when destroying the parent', function() {
42 var container = document.createElement('div');
43 var testInstance = React.render(<TestComponent />, container);
39 - reactComponentExpect(testInstance.refs.theInnerDiv)
40 - .toBeDOMComponentWithTag('div');
44 + expect(ReactTestUtils.isDOMComponent(testInstance.refs.theInnerDiv))
45 + .toBe(true);
46 expect(Object.keys(testInstance.refs || {}).length).toEqual(1);
47 React.unmountComponentAtNode(container);
48 expect(Object.keys(testInstance.refs || {}).length).toEqual(0);
@@ -46,8 +51,8 @@ describe('refs-destruction', function() {
51 it('should remove refs when destroying the child', function() {
52 var container = document.createElement('div');
53 var testInstance = React.render(<TestComponent />, container);
49 - reactComponentExpect(testInstance.refs.theInnerDiv)
50 - .toBeDOMComponentWithTag('div');
54 + expect(ReactTestUtils.isDOMComponent(testInstance.refs.theInnerDiv))
55 + .toBe(true);
56 expect(Object.keys(testInstance.refs || {}).length).toEqual(1);
57 React.render(<TestComponent destroy={true} />, container);
58 expect(Object.keys(testInstance.refs || {}).length).toEqual(0);
src/test/reactComponentExpect.js
+5
@@ -16,6 +16,7 @@ var ReactInstanceMap = require('ReactInstanceMap');
16 var ReactTestUtils = require('ReactTestUtils');
17
18 var assign = require('Object.assign');
19 +var invariant = require('invariant');
20
21 function reactComponentExpect(instance) {
22 if (instance instanceof reactComponentExpectInternal) {
@@ -28,6 +29,10 @@ function reactComponentExpect(instance) {
29
30 expect(instance).not.toBeNull();
31
32 + invariant(
33 + ReactTestUtils.isCompositeComponent(instance),
34 + 'reactComponentExpect(...): instance must be a composite component'
35 + );
36 var internalInstance = ReactInstanceMap.get(instance);
37
38 expect(typeof internalInstance).toBe('object');