forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreactComponentExpect.js
211 lines (187 loc) · 6.23 KB
/
reactComponentExpect.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/**
* Copyright 2013-2014 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @providesModule reactComponentExpect
* @nolint
*/
"use strict";
var ReactTestUtils = require('ReactTestUtils');
var mergeInto = require('mergeInto');
function reactComponentExpect(instance) {
if (instance instanceof reactComponentExpect) {
return instance;
}
if (!(this instanceof reactComponentExpect)) {
return new reactComponentExpect(instance);
}
this._instance = instance;
expect(typeof instance).toBe('object');
expect(typeof instance.constructor).toBe('function');
expect(ReactTestUtils.isDescriptor(instance)).toBe(false);
}
mergeInto(reactComponentExpect.prototype, {
// Getters -------------------------------------------------------------------
/**
* @instance: Retrieves the backing instance.
*/
instance: function() {
return this._instance;
},
/**
* There are two types of components in the world.
* - A component created via React.createClass() - Has a single child
* subComponent - the return value from the .render() function. This
* function @subComponent expects that this._instance is component created
* with React.createClass().
* - A primitive DOM component - which has many renderedChildren, each of
* which may have a name that is unique with respect to its siblings. This
* method will fail if this._instance is a primitive component.
*
* TL;DR: An instance may have a subComponent (this._renderedComponent) or
* renderedChildren, but never both. Neither will actually show up until you
* render the component (simply instantiating is not enough).
*/
expectRenderedChild: function() {
this.toBeCompositeComponent();
return new reactComponentExpect(this.instance()._renderedComponent);
},
/**
* The nth child of a DOMish component instance that is not falsy.
*/
expectRenderedChildAt: function(childIndex) {
// Currently only dom components have arrays of children, but that will
// change soon.
this.toBeDOMComponent();
var renderedChildren = this.instance()._renderedChildren || {};
for (var name in renderedChildren) {
if (!renderedChildren.hasOwnProperty(name)) {
continue;
}
if (renderedChildren[name]) {
if (renderedChildren[name]._mountIndex === childIndex) {
return new reactComponentExpect(renderedChildren[name]);
}
}
}
throw new Error('Child:' + childIndex + ' is not found');
},
toBeDOMComponentWithChildCount: function(n) {
this.toBeDOMComponent();
expect(this.instance()._renderedChildren).toBeTruthy();
var len = Object.keys(this.instance()._renderedChildren).length;
expect(len).toBe(n);
return this;
},
toBeDOMComponentWithNoChildren: function() {
this.toBeDOMComponent();
expect(this.instance()._renderedChildren).toBeFalsy();
return this;
},
// Matchers ------------------------------------------------------------------
toBeComponentOfType: function(convenienceConstructor) {
expect(
this.instance().constructor === convenienceConstructor.type
).toBe(true);
return this;
},
/**
* A component that is created with React.createClass. Just duck typing
* here.
*/
toBeCompositeComponent: function() {
expect(
typeof this.instance().render === 'function' &&
typeof this.instance().setState === 'function'
).toBe(true);
return this;
},
toBeCompositeComponentWithType: function(convenienceConstructor) {
this.toBeCompositeComponent();
expect(
this.instance().constructor === convenienceConstructor.type
).toBe(true);
return this;
},
toBeTextComponent: function() {
expect(ReactTestUtils.isTextComponent(this.instance())).toBe(true);
return this;
},
toBePresent: function() {
expect(this.instance()).toBeTruthy();
return this;
},
/**
* A terminal type of component representing some virtual dom node. Just duck
* typing here.
*/
toBeDOMComponent: function() {
expect(ReactTestUtils.isDOMComponent(this.instance())).toBe(true);
return this;
},
/**
* @deprecated
* @see toBeComponentOfType
*/
toBeDOMComponentWithTag: function(tag) {
this.toBeDOMComponent();
expect(this.instance().tagName).toBe(tag.toUpperCase());
return this;
},
/**
* Check that internal state values are equal to a state of expected values.
*/
scalarStateEqual: function(stateNameToExpectedValue) {
expect(this.instance()).toBeTruthy();
for (var stateName in stateNameToExpectedValue) {
if (!stateNameToExpectedValue.hasOwnProperty(stateName)) {
continue;
}
expect(this.instance().state[stateName])
.toEqual(stateNameToExpectedValue[stateName]);
}
return this;
},
/**
* Check a set of props are equal to a set of expected values - only works
* with scalars.
*/
scalarPropsEqual: function(propNameToExpectedValue) {
expect(this.instance()).toBeTruthy();
for (var propName in propNameToExpectedValue) {
if (!propNameToExpectedValue.hasOwnProperty(propName)) {
continue;
}
expect(this.instance().props[propName])
.toEqual(propNameToExpectedValue[propName]);
}
return this;
},
/**
* Check a set of props are equal to a set of expected values - only works
* with scalars.
*/
scalarContextEqual: function(contextNameToExpectedValue) {
expect(this.instance()).toBeTruthy();
for (var contextName in contextNameToExpectedValue) {
if (!contextNameToExpectedValue.hasOwnProperty(contextName)) {
continue;
}
expect(this.instance().context[contextName])
.toEqual(contextNameToExpectedValue[contextName]);
}
return this;
}
});
module.exports = reactComponentExpect;