Remove reactComponentExpect (#9856)
Legacy.
Ben Alpert committed
Jun 5, 2017 at 16:42 UTC
9d07ea71050ab65cc03d00d6659a4eff76b6ef7a
4 files changed
-471
scripts/fiber/tests-passing.txt
-7
@@ -2004,10 +2004,3 @@ src/shared/utils/__tests__/PooledClass-test.js
2004
2005
src/shared/utils/__tests__/reactProdInvariant-test.js
2006
* should throw with the correct number of `%s`s in the URL
2007
-
2008
-src/test/__tests__/reactComponentExpect-test.js
2009
-* should match composite components
2010
-* should match empty DOM components
2011
-* should match non-empty DOM components
2012
-* should match DOM component children
2013
-* should detect text components
scripts/rollup/packaging.js
-1
@@ -19,7 +19,6 @@ const RN_PROD = Bundles.bundleTypes.RN_PROD;
19
const facebookWWW = 'facebook-www';
20
// these files need to be copied to the facebook-www build
21
const facebookWWWSrcDependencies = [
22
- 'src/test/reactComponentExpect.js',
22
'src/renderers/dom/shared/eventPlugins/TapEventPlugin.js',
23
];
24
src/test/__tests__/reactComponentExpect-test.js
deleted
-168
@@ -1,168 +0,0 @@
1
-/**
2
- * Copyright 2013-present, 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
-'use strict';
13
-
14
-var React;
15
-var ReactTestUtils;
16
-var reactComponentExpect;
17
-
18
-describe('reactComponentExpect', () => {
19
- beforeEach(() => {
20
- React = require('react');
21
- ReactTestUtils = require('ReactTestUtils');
22
- reactComponentExpect = require('reactComponentExpect');
23
- });
24
-
25
- it('should match composite components', () => {
26
- class SomeComponent extends React.Component {
27
- state = {y: 2};
28
- render() {
29
- return <div className="Hello" />;
30
- }
31
- }
32
-
33
- var component = ReactTestUtils.renderIntoDocument(<SomeComponent x={1} />);
34
- reactComponentExpect(component)
35
- .toBePresent()
36
- .toBeCompositeComponent()
37
- .toBeComponentOfType(SomeComponent)
38
- .toBeCompositeComponentWithType(SomeComponent)
39
- .scalarPropsEqual({x: 1})
40
- .scalarStateEqual({y: 2});
41
- });
42
-
43
- it('should match empty DOM components', () => {
44
- class SomeComponent extends React.Component {
45
- render() {
46
- return <div className="Hello" />;
47
- }
48
- }
49
-
50
- var component = ReactTestUtils.renderIntoDocument(<SomeComponent />);
51
- reactComponentExpect(component)
52
- .expectRenderedChild()
53
- .toBePresent()
54
- .toBeDOMComponent()
55
- .toBeDOMComponentWithNoChildren()
56
- .toBeComponentOfType('div');
57
- });
58
-
59
- it('should match non-empty DOM components', () => {
60
- class SomeComponent extends React.Component {
61
- render() {
62
- return (
63
- <div className="Hello">
64
- <p>1</p>
65
- <p>2</p>
66
- </div>
67
- );
68
- }
69
- }
70
-
71
- var component = ReactTestUtils.renderIntoDocument(<SomeComponent />);
72
- reactComponentExpect(component)
73
- .expectRenderedChild()
74
- .toBePresent()
75
- .toBeDOMComponent()
76
- .toBeDOMComponentWithChildCount(2)
77
- .toBeComponentOfType('div');
78
- });
79
-
80
- it('should match DOM component children', () => {
81
- class Inner extends React.Component {
82
- render() {
83
- return <section />;
84
- }
85
- }
86
-
87
- class Noop extends React.Component {
88
- render() {
89
- return null;
90
- }
91
- }
92
-
93
- class SomeComponent extends React.Component {
94
- render() {
95
- return (
96
- <div className="Hello">
97
- <p>1</p>
98
- <Inner foo="bar" />
99
- <span>{'Two'}{3}</span>
100
- <Noop />
101
- </div>
102
- );
103
- }
104
- }
105
-
106
- var component = ReactTestUtils.renderIntoDocument(<SomeComponent />);
107
- reactComponentExpect(component)
108
- .expectRenderedChild()
109
- .expectRenderedChildAt(0)
110
- .toBePresent()
111
- .toBeDOMComponent()
112
- .toBeDOMComponentWithNoChildren()
113
- .toBeComponentOfType('p');
114
-
115
- reactComponentExpect(component)
116
- .expectRenderedChild()
117
- .expectRenderedChildAt(1)
118
- .toBePresent()
119
- .toBeCompositeComponentWithType(Inner)
120
- .scalarPropsEqual({foo: 'bar'})
121
- .expectRenderedChild()
122
- .toBeComponentOfType('section')
123
- .toBeDOMComponentWithNoChildren();
124
-
125
- reactComponentExpect(component)
126
- .expectRenderedChild()
127
- .expectRenderedChildAt(2)
128
- .toBePresent()
129
- .toBeDOMComponent()
130
- .toBeComponentOfType('span')
131
- .toBeDOMComponentWithChildCount(2)
132
- .expectRenderedChildAt(0)
133
- .toBeTextComponentWithValue('Two');
134
-
135
- reactComponentExpect(component)
136
- .expectRenderedChild()
137
- .expectRenderedChildAt(2)
138
- .expectRenderedChildAt(1)
139
- .toBeTextComponentWithValue('3');
140
-
141
- reactComponentExpect(component)
142
- .expectRenderedChild()
143
- .expectRenderedChildAt(3)
144
- .toBePresent()
145
- .toBeCompositeComponentWithType(Noop)
146
- .expectRenderedChild()
147
- .toBeEmptyComponent();
148
- });
149
-
150
- it('should detect text components', () => {
151
- class SomeComponent extends React.Component {
152
- render() {
153
- return (
154
- <div>
155
- <div>This is a div</div>
156
- {'This is text'}
157
- </div>
158
- );
159
- }
160
- }
161
-
162
- var component = ReactTestUtils.renderIntoDocument(<SomeComponent />);
163
- reactComponentExpect(component)
164
- .expectRenderedChild()
165
- .expectRenderedChildAt(1)
166
- .toBeTextComponentWithValue('This is text');
167
- });
168
-});
src/test/reactComponentExpect.js
deleted
-295
@@ -1,295 +0,0 @@
1
-/**
2
- * Copyright 2013-present, 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
- * @providesModule reactComponentExpect
10
- */
11
-
12
-'use strict';
13
-
14
-var ReactInstanceMap = require('ReactInstanceMap');
15
-var ReactTestUtils = require('ReactTestUtils');
16
-
17
-var HostText = 6; // ReactTypeOfWork
18
-
19
-var invariant = require('fbjs/lib/invariant');
20
-
21
-// Fiber doesn't actually have an instance for empty components
22
-// but we'll pretend it does while we keep compatibility with Stack.
23
-var fiberNullInstance = {
24
- type: null,
25
- child: null,
26
- sibling: null,
27
- tag: 99,
28
-};
29
-
30
-function reactComponentExpect(instance) {
31
- if (instance instanceof reactComponentExpectInternal) {
32
- return instance;
33
- }
34
-
35
- if (!(this instanceof reactComponentExpect)) {
36
- return new reactComponentExpect(instance);
37
- }
38
-
39
- expect(instance).not.toBeNull();
40
- expect(instance).not.toBeUndefined();
41
-
42
- invariant(
43
- ReactTestUtils.isCompositeComponent(instance),
44
- 'reactComponentExpect(...): instance must be a composite component',
45
- );
46
- var internalInstance = ReactInstanceMap.get(instance);
47
-
48
- expect(typeof internalInstance).toBe('object');
49
- expect(typeof internalInstance.constructor).toBe('function');
50
- expect(ReactTestUtils.isElement(internalInstance)).toBe(false);
51
-
52
- return new reactComponentExpectInternal(internalInstance);
53
-}
54
-
55
-function reactComponentExpectInternal(internalInstance) {
56
- this._instance = internalInstance;
57
-}
58
-
59
-Object.assign(reactComponentExpectInternal.prototype, {
60
- // Getters -------------------------------------------------------------------
61
-
62
- /**
63
- * @instance: Retrieves the backing instance.
64
- */
65
- instance: function() {
66
- if (typeof this._instance.tag === 'number') {
67
- // Fiber reconciler
68
- return this._instance.stateNode;
69
- } else {
70
- // Stack reconciler
71
- return this._instance.getPublicInstance();
72
- }
73
- },
74
-
75
- /**
76
- * There are two types of components in the world.
77
- * - A component created via React.createClass() - Has a single child
78
- * subComponent - the return value from the .render() function. This
79
- * function @subComponent expects that this._instance is component created
80
- * with React.createClass().
81
- * - A primitive DOM component - which has many renderedChildren, each of
82
- * which may have a name that is unique with respect to its siblings. This
83
- * method will fail if this._instance is a primitive component.
84
- *
85
- * TL;DR: An instance may have a subComponent (this._renderedComponent) or
86
- * renderedChildren, but never both. Neither will actually show up until you
87
- * render the component (simply instantiating is not enough).
88
- */
89
- expectRenderedChild: function() {
90
- this.toBeCompositeComponent();
91
- var child = null;
92
- if (typeof this._instance.tag === 'number') {
93
- // Fiber reconciler
94
- child = this._instance.child || fiberNullInstance;
95
- } else {
96
- // Stack reconciler
97
- child = this._instance._renderedComponent;
98
- }
99
- // TODO: Hide ReactEmptyComponent instances here?
100
- return new reactComponentExpectInternal(child);
101
- },
102
-
103
- /**
104
- * The nth child of a DOMish component instance that is not falsy.
105
- */
106
- expectRenderedChildAt: function(childIndex) {
107
- // Currently only dom components have arrays of children, but that will
108
- // change soon.
109
- this.toBeDOMComponent();
110
-
111
- if (typeof this._instance.tag === 'number') {
112
- // Fiber reconciler
113
- var child = this._instance.child;
114
- var i = 0;
115
- while (child) {
116
- if (i === childIndex) {
117
- return new reactComponentExpectInternal(child);
118
- }
119
- child = child.sibling;
120
- i++;
121
- }
122
- } else {
123
- // Stack reconciler
124
- var renderedChildren = this._instance._renderedChildren || {};
125
- for (var name in renderedChildren) {
126
- if (!renderedChildren.hasOwnProperty(name)) {
127
- continue;
128
- }
129
- if (renderedChildren[name]) {
130
- if (renderedChildren[name]._mountIndex === childIndex) {
131
- return new reactComponentExpectInternal(renderedChildren[name]);
132
- }
133
- }
134
- }
135
- }
136
- throw new Error('Child:' + childIndex + ' is not found');
137
- },
138
-
139
- toBeDOMComponentWithChildCount: function(count) {
140
- this.toBeDOMComponent();
141
- if (typeof this._instance.tag === 'number') {
142
- // Fiber reconciler
143
- var child = this._instance.child;
144
- var i = 0;
145
- while (child) {
146
- child = child.sibling;
147
- i++;
148
- }
149
- expect(i).toBe(count);
150
- } else {
151
- // Stack reconciler
152
- var renderedChildren = this._instance._renderedChildren;
153
- if (count > 0) {
154
- expect(renderedChildren).toBeTruthy();
155
- expect(Object.keys(renderedChildren).length).toBe(count);
156
- } else if (renderedChildren) {
157
- expect(Object.keys(renderedChildren).length).toBe(0);
158
- }
159
- }
160
- return this;
161
- },
162
-
163
- toBeDOMComponentWithNoChildren: function() {
164
- this.toBeDOMComponentWithChildCount(0);
165
- return this;
166
- },
167
-
168
- // Matchers ------------------------------------------------------------------
169
-
170
- toBeComponentOfType: function(constructor) {
171
- if (typeof this._instance.tag === 'number') {
172
- // Fiber reconciler
173
- expect(this._instance.type === constructor).toBe(true);
174
- } else {
175
- // Stack reconciler
176
- expect(this._instance._currentElement.type === constructor).toBe(true);
177
- }
178
- return this;
179
- },
180
-
181
- /**
182
- * A component that is created with React.createClass. Just duck typing
183
- * here.
184
- */
185
- toBeCompositeComponent: function() {
186
- // TODO: this code predates functional components
187
- // and doesn't work with them.
188
- expect(
189
- typeof this.instance() === 'object' &&
190
- typeof this.instance().render === 'function',
191
- ).toBe(true);
192
- return this;
193
- },
194
-
195
- toBeCompositeComponentWithType: function(constructor) {
196
- this.toBeCompositeComponent();
197
- this.toBeComponentOfType(constructor);
198
- return this;
199
- },
200
-
201
- toBeTextComponentWithValue: function(val) {
202
- if (typeof this._instance.tag === 'number') {
203
- // Fiber reconciler
204
- expect(this._instance.tag === HostText).toBe(true);
205
- var actualVal = this._instance.memoizedProps;
206
- expect(
207
- typeof actualVal === 'string' || typeof actualVal === 'number',
208
- ).toBe(true);
209
- expect('' + actualVal).toBe(val);
210
- } else {
211
- // Fiber reconciler
212
- var elementType = typeof this._instance._currentElement;
213
- expect(elementType === 'string' || elementType === 'number').toBe(true);
214
- expect(this._instance._stringText).toBe(val);
215
- }
216
- return this;
217
- },
218
-
219
- toBeEmptyComponent: function() {
220
- if (typeof this._instance.tag === 'number') {
221
- // Fiber reconciler
222
- expect(this._instance).toBe(fiberNullInstance);
223
- } else {
224
- // Stack reconciler
225
- var element = this._instance._currentElement;
226
- expect(element === null || element === false).toBe(true);
227
- }
228
- },
229
-
230
- toBePresent: function() {
231
- expect(this.instance()).toBeTruthy();
232
- return this;
233
- },
234
-
235
- /**
236
- * A terminal type of component representing some virtual dom node. Just duck
237
- * typing here.
238
- */
239
- toBeDOMComponent: function() {
240
- expect(ReactTestUtils.isDOMComponent(this.instance())).toBe(true);
241
- return this;
242
- },
243
-
244
- /**
245
- * Check that internal state values are equal to a state of expected values.
246
- */
247
- scalarStateEqual: function(stateNameToExpectedValue) {
248
- expect(this.instance()).toBeTruthy();
249
- for (var stateName in stateNameToExpectedValue) {
250
- if (!stateNameToExpectedValue.hasOwnProperty(stateName)) {
251
- continue;
252
- }
253
- expect(this.instance().state[stateName]).toEqual(
254
- stateNameToExpectedValue[stateName],
255
- );
256
- }
257
- return this;
258
- },
259
-
260
- /**
261
- * Check a set of props are equal to a set of expected values - only works
262
- * with scalars.
263
- */
264
- scalarPropsEqual: function(propNameToExpectedValue) {
265
- expect(this.instance()).toBeTruthy();
266
- for (var propName in propNameToExpectedValue) {
267
- if (!propNameToExpectedValue.hasOwnProperty(propName)) {
268
- continue;
269
- }
270
- expect(this.instance().props[propName]).toEqual(
271
- propNameToExpectedValue[propName],
272
- );
273
- }
274
- return this;
275
- },
276
-
277
- /**
278
- * Check a set of props are equal to a set of expected values - only works
279
- * with scalars.
280
- */
281
- scalarContextEqual: function(contextNameToExpectedValue) {
282
- expect(this.instance()).toBeTruthy();
283
- for (var contextName in contextNameToExpectedValue) {
284
- if (!contextNameToExpectedValue.hasOwnProperty(contextName)) {
285
- continue;
286
- }
287
- expect(this.instance().context[contextName]).toEqual(
288
- contextNameToExpectedValue[contextName],
289
- );
290
- }
291
- return this;
292
- },
293
-});
294
-
295
-module.exports = reactComponentExpect;