Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81152 views
1
/**
2
* Copyright 2013-2014, 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
* @nolint
11
*/
12
13
"use strict";
14
15
var ReactTestUtils = require('ReactTestUtils');
16
17
var assign = require('Object.assign');
18
19
function reactComponentExpect(instance) {
20
if (instance instanceof reactComponentExpect) {
21
return instance;
22
}
23
24
if (!(this instanceof reactComponentExpect)) {
25
return new reactComponentExpect(instance);
26
}
27
28
this._instance = instance;
29
expect(typeof instance).toBe('object');
30
expect(typeof instance.constructor).toBe('function');
31
expect(ReactTestUtils.isElement(instance)).toBe(false);
32
}
33
34
assign(reactComponentExpect.prototype, {
35
// Getters -------------------------------------------------------------------
36
37
/**
38
* @instance: Retrieves the backing instance.
39
*/
40
instance: function() {
41
return this._instance;
42
},
43
44
/**
45
* There are two types of components in the world.
46
* - A component created via React.createClass() - Has a single child
47
* subComponent - the return value from the .render() function. This
48
* function @subComponent expects that this._instance is component created
49
* with React.createClass().
50
* - A primitive DOM component - which has many renderedChildren, each of
51
* which may have a name that is unique with respect to its siblings. This
52
* method will fail if this._instance is a primitive component.
53
*
54
* TL;DR: An instance may have a subComponent (this._renderedComponent) or
55
* renderedChildren, but never both. Neither will actually show up until you
56
* render the component (simply instantiating is not enough).
57
*/
58
expectRenderedChild: function() {
59
this.toBeCompositeComponent();
60
return new reactComponentExpect(this.instance()._renderedComponent);
61
},
62
63
/**
64
* The nth child of a DOMish component instance that is not falsy.
65
*/
66
expectRenderedChildAt: function(childIndex) {
67
// Currently only dom components have arrays of children, but that will
68
// change soon.
69
this.toBeDOMComponent();
70
var renderedChildren = this.instance()._renderedChildren || {};
71
for (var name in renderedChildren) {
72
if (!renderedChildren.hasOwnProperty(name)) {
73
continue;
74
}
75
if (renderedChildren[name]) {
76
if (renderedChildren[name]._mountIndex === childIndex) {
77
return new reactComponentExpect(renderedChildren[name]);
78
}
79
}
80
}
81
throw new Error('Child:' + childIndex + ' is not found');
82
},
83
84
toBeDOMComponentWithChildCount: function(n) {
85
this.toBeDOMComponent();
86
expect(this.instance()._renderedChildren).toBeTruthy();
87
var len = Object.keys(this.instance()._renderedChildren).length;
88
expect(len).toBe(n);
89
return this;
90
},
91
92
toBeDOMComponentWithNoChildren: function() {
93
this.toBeDOMComponent();
94
expect(this.instance()._renderedChildren).toBeFalsy();
95
return this;
96
},
97
98
// Matchers ------------------------------------------------------------------
99
100
toBeComponentOfType: function(convenienceConstructor) {
101
var type = typeof convenienceConstructor === 'string' ?
102
convenienceConstructor :
103
convenienceConstructor.type;
104
expect(
105
this.instance()._currentElement.type === type
106
).toBe(true);
107
return this;
108
},
109
110
/**
111
* A component that is created with React.createClass. Just duck typing
112
* here.
113
*/
114
toBeCompositeComponent: function() {
115
expect(
116
typeof this.instance().render === 'function' &&
117
typeof this.instance().setState === 'function'
118
).toBe(true);
119
return this;
120
},
121
122
toBeCompositeComponentWithType: function(convenienceConstructor) {
123
this.toBeCompositeComponent();
124
expect(
125
this.instance()._currentElement.type === convenienceConstructor.type
126
).toBe(true);
127
return this;
128
},
129
130
toBeTextComponent: function() {
131
expect(ReactTestUtils.isTextComponent(this.instance())).toBe(true);
132
return this;
133
},
134
135
toBePresent: function() {
136
expect(this.instance()).toBeTruthy();
137
return this;
138
},
139
140
/**
141
* A terminal type of component representing some virtual dom node. Just duck
142
* typing here.
143
*/
144
toBeDOMComponent: function() {
145
expect(ReactTestUtils.isDOMComponent(this.instance())).toBe(true);
146
return this;
147
},
148
149
/**
150
* @deprecated
151
* @see toBeComponentOfType
152
*/
153
toBeDOMComponentWithTag: function(tag) {
154
this.toBeDOMComponent();
155
expect(this.instance().tagName).toBe(tag.toUpperCase());
156
return this;
157
},
158
159
/**
160
* Check that internal state values are equal to a state of expected values.
161
*/
162
scalarStateEqual: function(stateNameToExpectedValue) {
163
expect(this.instance()).toBeTruthy();
164
for (var stateName in stateNameToExpectedValue) {
165
if (!stateNameToExpectedValue.hasOwnProperty(stateName)) {
166
continue;
167
}
168
expect(this.instance().state[stateName])
169
.toEqual(stateNameToExpectedValue[stateName]);
170
}
171
return this;
172
},
173
174
/**
175
* Check a set of props are equal to a set of expected values - only works
176
* with scalars.
177
*/
178
scalarPropsEqual: function(propNameToExpectedValue) {
179
expect(this.instance()).toBeTruthy();
180
for (var propName in propNameToExpectedValue) {
181
if (!propNameToExpectedValue.hasOwnProperty(propName)) {
182
continue;
183
}
184
expect(this.instance().props[propName])
185
.toEqual(propNameToExpectedValue[propName]);
186
}
187
return this;
188
},
189
190
/**
191
* Check a set of props are equal to a set of expected values - only works
192
* with scalars.
193
*/
194
scalarContextEqual: function(contextNameToExpectedValue) {
195
expect(this.instance()).toBeTruthy();
196
for (var contextName in contextNameToExpectedValue) {
197
if (!contextNameToExpectedValue.hasOwnProperty(contextName)) {
198
continue;
199
}
200
expect(this.instance().context[contextName])
201
.toEqual(contextNameToExpectedValue[contextName]);
202
}
203
return this;
204
}
205
});
206
207
module.exports = reactComponentExpect;
208
209