1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | 'use strict';
|
10 |
|
11 | var _prodInvariant = require('./reactProdInvariant'),
|
12 | _assign = require('object-assign');
|
13 |
|
14 | var ReactInstanceMap = require('./ReactInstanceMap');
|
15 | var ReactTestUtils = require('./ReactTestUtils');
|
16 |
|
17 | var invariant = require('fbjs/lib/invariant');
|
18 |
|
19 | function reactComponentExpect(instance) {
|
20 | if (instance instanceof reactComponentExpectInternal) {
|
21 | return instance;
|
22 | }
|
23 |
|
24 | if (!(this instanceof reactComponentExpect)) {
|
25 | return new reactComponentExpect(instance);
|
26 | }
|
27 |
|
28 | expect(instance).not.toBeNull();
|
29 | expect(instance).not.toBeUndefined();
|
30 |
|
31 | !ReactTestUtils.isCompositeComponent(instance) ? process.env.NODE_ENV !== 'production' ? invariant(false, 'reactComponentExpect(...): instance must be a composite component') : _prodInvariant('15') : void 0;
|
32 | var internalInstance = ReactInstanceMap.get(instance);
|
33 |
|
34 | expect(typeof internalInstance).toBe('object');
|
35 | expect(typeof internalInstance.constructor).toBe('function');
|
36 | expect(ReactTestUtils.isElement(internalInstance)).toBe(false);
|
37 |
|
38 | return new reactComponentExpectInternal(internalInstance);
|
39 | }
|
40 |
|
41 | function reactComponentExpectInternal(internalInstance) {
|
42 | this._instance = internalInstance;
|
43 | }
|
44 |
|
45 | _assign(reactComponentExpectInternal.prototype, {
|
46 |
|
47 |
|
48 | |
49 |
|
50 |
|
51 | instance: function () {
|
52 | return this._instance.getPublicInstance();
|
53 | },
|
54 |
|
55 | |
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | expectRenderedChild: function () {
|
70 | this.toBeCompositeComponent();
|
71 | var child = this._instance._renderedComponent;
|
72 |
|
73 | return new reactComponentExpectInternal(child);
|
74 | },
|
75 |
|
76 | |
77 |
|
78 |
|
79 | expectRenderedChildAt: function (childIndex) {
|
80 |
|
81 |
|
82 | this.toBeDOMComponent();
|
83 | var renderedChildren = this._instance._renderedChildren || {};
|
84 | for (var name in renderedChildren) {
|
85 | if (!renderedChildren.hasOwnProperty(name)) {
|
86 | continue;
|
87 | }
|
88 | if (renderedChildren[name]) {
|
89 | if (renderedChildren[name]._mountIndex === childIndex) {
|
90 | return new reactComponentExpectInternal(renderedChildren[name]);
|
91 | }
|
92 | }
|
93 | }
|
94 | throw new Error('Child:' + childIndex + ' is not found');
|
95 | },
|
96 |
|
97 | toBeDOMComponentWithChildCount: function (count) {
|
98 | this.toBeDOMComponent();
|
99 | var renderedChildren = this._instance._renderedChildren;
|
100 | expect(renderedChildren).toBeTruthy();
|
101 | expect(Object.keys(renderedChildren).length).toBe(count);
|
102 | return this;
|
103 | },
|
104 |
|
105 | toBeDOMComponentWithNoChildren: function () {
|
106 | this.toBeDOMComponent();
|
107 | expect(this._instance._renderedChildren).toBeFalsy();
|
108 | return this;
|
109 | },
|
110 |
|
111 |
|
112 |
|
113 | toBeComponentOfType: function (constructor) {
|
114 | expect(this._instance._currentElement.type === constructor).toBe(true);
|
115 | return this;
|
116 | },
|
117 |
|
118 | /**
|
119 | * A component that is created with React.createClass. Just duck typing
|
120 | * here.
|
121 | */
|
122 | toBeCompositeComponent: function () {
|
123 | expect(typeof this.instance() === 'object' && typeof this.instance().render === 'function').toBe(true);
|
124 | return this;
|
125 | },
|
126 |
|
127 | toBeCompositeComponentWithType: function (constructor) {
|
128 | this.toBeCompositeComponent();
|
129 | expect(this._instance._currentElement.type === constructor).toBe(true);
|
130 | return this;
|
131 | },
|
132 |
|
133 | toBeTextComponentWithValue: function (val) {
|
134 | var elementType = typeof this._instance._currentElement;
|
135 | expect(elementType === 'string' || elementType === 'number').toBe(true);
|
136 | expect(this._instance._stringText).toBe(val);
|
137 | return this;
|
138 | },
|
139 |
|
140 | toBeEmptyComponent: function () {
|
141 | var element = this._instance._currentElement;
|
142 | return element === null || element === false;
|
143 | },
|
144 |
|
145 | toBePresent: function () {
|
146 | expect(this.instance()).toBeTruthy();
|
147 | return this;
|
148 | },
|
149 |
|
150 | |
151 |
|
152 |
|
153 |
|
154 | toBeDOMComponent: function () {
|
155 | expect(ReactTestUtils.isDOMComponent(this.instance())).toBe(true);
|
156 | return this;
|
157 | },
|
158 |
|
159 | |
160 |
|
161 |
|
162 |
|
163 | toBeDOMComponentWithTag: function (tag) {
|
164 | this.toBeDOMComponent();
|
165 | expect(this.instance().tagName).toBe(tag.toUpperCase());
|
166 | return this;
|
167 | },
|
168 |
|
169 | |
170 |
|
171 |
|
172 | scalarStateEqual: function (stateNameToExpectedValue) {
|
173 | expect(this.instance()).toBeTruthy();
|
174 | for (var stateName in stateNameToExpectedValue) {
|
175 | if (!stateNameToExpectedValue.hasOwnProperty(stateName)) {
|
176 | continue;
|
177 | }
|
178 | expect(this.instance().state[stateName]).toEqual(stateNameToExpectedValue[stateName]);
|
179 | }
|
180 | return this;
|
181 | },
|
182 |
|
183 | |
184 |
|
185 |
|
186 |
|
187 | scalarPropsEqual: function (propNameToExpectedValue) {
|
188 | expect(this.instance()).toBeTruthy();
|
189 | for (var propName in propNameToExpectedValue) {
|
190 | if (!propNameToExpectedValue.hasOwnProperty(propName)) {
|
191 | continue;
|
192 | }
|
193 | expect(this.instance().props[propName]).toEqual(propNameToExpectedValue[propName]);
|
194 | }
|
195 | return this;
|
196 | },
|
197 |
|
198 | |
199 |
|
200 |
|
201 |
|
202 | scalarContextEqual: function (contextNameToExpectedValue) {
|
203 | expect(this.instance()).toBeTruthy();
|
204 | for (var contextName in contextNameToExpectedValue) {
|
205 | if (!contextNameToExpectedValue.hasOwnProperty(contextName)) {
|
206 | continue;
|
207 | }
|
208 | expect(this.instance().context[contextName]).toEqual(contextNameToExpectedValue[contextName]);
|
209 | }
|
210 | return this;
|
211 | }
|
212 | });
|
213 |
|
214 | module.exports = reactComponentExpect; |
\ | No newline at end of file |