1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | 'use strict';
|
12 |
|
13 | var _prodInvariant = require('./reactProdInvariant'),
|
14 | _assign = require('object-assign');
|
15 |
|
16 | var EventConstants = require('./EventConstants');
|
17 | var EventPluginHub = require('./EventPluginHub');
|
18 | var EventPluginRegistry = require('./EventPluginRegistry');
|
19 | var EventPropagators = require('./EventPropagators');
|
20 | var React = require('react/lib/React');
|
21 | var ReactDOM = require('./ReactDOM');
|
22 | var ReactDOMComponentTree = require('./ReactDOMComponentTree');
|
23 | var ReactBrowserEventEmitter = require('./ReactBrowserEventEmitter');
|
24 | var ReactInstanceMap = require('./ReactInstanceMap');
|
25 | var ReactUpdates = require('./ReactUpdates');
|
26 | var SyntheticEvent = require('./SyntheticEvent');
|
27 | var ReactShallowRenderer = require('./ReactShallowRenderer');
|
28 |
|
29 | var findDOMNode = require('./findDOMNode');
|
30 | var invariant = require('fbjs/lib/invariant');
|
31 | var warning = require('fbjs/lib/warning');
|
32 |
|
33 | var topLevelTypes = EventConstants.topLevelTypes;
|
34 |
|
35 | function Event(suffix) {}
|
36 |
|
37 |
|
38 |
|
39 |
|
40 | var hasWarnedAboutCreateRenderer = false;
|
41 | function createRendererWithWarning() {
|
42 | process.env.NODE_ENV !== 'production' ? warning(hasWarnedAboutCreateRenderer, 'Shallow renderer has been moved to react-test-renderer/shallow. ' + 'Update references to remove this warning.') : void 0;
|
43 | hasWarnedAboutCreateRenderer = true;
|
44 |
|
45 | return new ReactShallowRenderer();
|
46 | }
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | function findAllInRenderedTreeInternal(inst, test) {
|
53 | if (!inst || !inst.getPublicInstance) {
|
54 | return [];
|
55 | }
|
56 | var publicInst = inst.getPublicInstance();
|
57 | var ret = test(publicInst) ? [publicInst] : [];
|
58 | var currentElement = inst._currentElement;
|
59 | if (ReactTestUtils.isDOMComponent(publicInst)) {
|
60 | var renderedChildren = inst._renderedChildren;
|
61 | var key;
|
62 | for (key in renderedChildren) {
|
63 | if (!renderedChildren.hasOwnProperty(key)) {
|
64 | continue;
|
65 | }
|
66 | ret = ret.concat(findAllInRenderedTreeInternal(renderedChildren[key], test));
|
67 | }
|
68 | } else if (React.isValidElement(currentElement) && typeof currentElement.type === 'function') {
|
69 | ret = ret.concat(findAllInRenderedTreeInternal(inst._renderedComponent, test));
|
70 | }
|
71 | return ret;
|
72 | }
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 | var ReactTestUtils = {
|
84 | renderIntoDocument: function (element) {
|
85 | var div = document.createElement('div');
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 | return ReactDOM.render(element, div);
|
92 | },
|
93 |
|
94 | isElement: function (element) {
|
95 | return React.isValidElement(element);
|
96 | },
|
97 |
|
98 | isElementOfType: function (inst, convenienceConstructor) {
|
99 | return React.isValidElement(inst) && inst.type === convenienceConstructor;
|
100 | },
|
101 |
|
102 | isDOMComponent: function (inst) {
|
103 | return !!(inst && inst.nodeType === 1 && inst.tagName);
|
104 | },
|
105 |
|
106 | isDOMComponentElement: function (inst) {
|
107 | return !!(inst && React.isValidElement(inst) && !!inst.tagName);
|
108 | },
|
109 |
|
110 | isCompositeComponent: function (inst) {
|
111 | if (ReactTestUtils.isDOMComponent(inst)) {
|
112 |
|
113 |
|
114 | return false;
|
115 | }
|
116 | return inst != null && typeof inst.render === 'function' && typeof inst.setState === 'function';
|
117 | },
|
118 |
|
119 | isCompositeComponentWithType: function (inst, type) {
|
120 | if (!ReactTestUtils.isCompositeComponent(inst)) {
|
121 | return false;
|
122 | }
|
123 | var internalInstance = ReactInstanceMap.get(inst);
|
124 | var constructor = internalInstance._currentElement.type;
|
125 |
|
126 | return constructor === type;
|
127 | },
|
128 |
|
129 | isCompositeComponentElement: function (inst) {
|
130 | if (!React.isValidElement(inst)) {
|
131 | return false;
|
132 | }
|
133 |
|
134 |
|
135 | var prototype = inst.type.prototype;
|
136 | return typeof prototype.render === 'function' && typeof prototype.setState === 'function';
|
137 | },
|
138 |
|
139 | isCompositeComponentElementWithType: function (inst, type) {
|
140 | var internalInstance = ReactInstanceMap.get(inst);
|
141 | var constructor = internalInstance._currentElement.type;
|
142 |
|
143 | return !!(ReactTestUtils.isCompositeComponentElement(inst) && constructor === type);
|
144 | },
|
145 |
|
146 | getRenderedChildOfCompositeComponent: function (inst) {
|
147 | if (!ReactTestUtils.isCompositeComponent(inst)) {
|
148 | return null;
|
149 | }
|
150 | var internalInstance = ReactInstanceMap.get(inst);
|
151 | return internalInstance._renderedComponent.getPublicInstance();
|
152 | },
|
153 |
|
154 | findAllInRenderedTree: function (inst, test) {
|
155 | if (!inst) {
|
156 | return [];
|
157 | }
|
158 | !ReactTestUtils.isCompositeComponent(inst) ? process.env.NODE_ENV !== 'production' ? invariant(false, 'findAllInRenderedTree(...): instance must be a composite component') : _prodInvariant('10') : void 0;
|
159 | return findAllInRenderedTreeInternal(ReactInstanceMap.get(inst), test);
|
160 | },
|
161 |
|
162 | |
163 |
|
164 |
|
165 |
|
166 |
|
167 | scryRenderedDOMComponentsWithClass: function (root, classNames) {
|
168 | return ReactTestUtils.findAllInRenderedTree(root, function (inst) {
|
169 | if (ReactTestUtils.isDOMComponent(inst)) {
|
170 | var className = inst.className;
|
171 | if (typeof className !== 'string') {
|
172 |
|
173 | className = inst.getAttribute('class') || '';
|
174 | }
|
175 | var classList = className.split(/\s+/);
|
176 |
|
177 | if (!Array.isArray(classNames)) {
|
178 | !(classNames !== undefined) ? process.env.NODE_ENV !== 'production' ? invariant(false, 'TestUtils.scryRenderedDOMComponentsWithClass expects a className as a second argument.') : _prodInvariant('11') : void 0;
|
179 | classNames = classNames.split(/\s+/);
|
180 | }
|
181 | return classNames.every(function (name) {
|
182 | return classList.indexOf(name) !== -1;
|
183 | });
|
184 | }
|
185 | return false;
|
186 | });
|
187 | },
|
188 |
|
189 | |
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 | findRenderedDOMComponentWithClass: function (root, className) {
|
196 | var all = ReactTestUtils.scryRenderedDOMComponentsWithClass(root, className);
|
197 | if (all.length !== 1) {
|
198 | throw new Error('Did not find exactly one match (found: ' + all.length + ') ' + 'for class:' + className);
|
199 | }
|
200 | return all[0];
|
201 | },
|
202 |
|
203 | |
204 |
|
205 |
|
206 |
|
207 |
|
208 | scryRenderedDOMComponentsWithTag: function (root, tagName) {
|
209 | return ReactTestUtils.findAllInRenderedTree(root, function (inst) {
|
210 | return ReactTestUtils.isDOMComponent(inst) && inst.tagName.toUpperCase() === tagName.toUpperCase();
|
211 | });
|
212 | },
|
213 |
|
214 | |
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 | findRenderedDOMComponentWithTag: function (root, tagName) {
|
221 | var all = ReactTestUtils.scryRenderedDOMComponentsWithTag(root, tagName);
|
222 | if (all.length !== 1) {
|
223 | throw new Error('Did not find exactly one match (found: ' + all.length + ') ' + 'for tag:' + tagName);
|
224 | }
|
225 | return all[0];
|
226 | },
|
227 |
|
228 | |
229 |
|
230 |
|
231 |
|
232 | scryRenderedComponentsWithType: function (root, componentType) {
|
233 | return ReactTestUtils.findAllInRenderedTree(root, function (inst) {
|
234 | return ReactTestUtils.isCompositeComponentWithType(inst, componentType);
|
235 | });
|
236 | },
|
237 |
|
238 | |
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 | findRenderedComponentWithType: function (root, componentType) {
|
245 | var all = ReactTestUtils.scryRenderedComponentsWithType(root, componentType);
|
246 | if (all.length !== 1) {
|
247 | throw new Error('Did not find exactly one match (found: ' + all.length + ') ' + 'for componentType:' + componentType);
|
248 | }
|
249 | return all[0];
|
250 | },
|
251 |
|
252 | |
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 | mockComponent: function (module, mockTagName) {
|
266 | mockTagName = mockTagName || module.mockTagName || 'div';
|
267 |
|
268 | module.prototype.render.mockImplementation(function () {
|
269 | return React.createElement(mockTagName, null, this.props.children);
|
270 | });
|
271 |
|
272 | return this;
|
273 | },
|
274 |
|
275 | |
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 | simulateNativeEventOnNode: function (topLevelType, node, fakeNativeEvent) {
|
283 | fakeNativeEvent.target = node;
|
284 | fakeNativeEvent.simulated = true;
|
285 | ReactBrowserEventEmitter.ReactEventListener.dispatchEvent(topLevelType, fakeNativeEvent);
|
286 | },
|
287 |
|
288 | |
289 |
|
290 |
|
291 |
|
292 |
|
293 |
|
294 |
|
295 | simulateNativeEventOnDOMComponent: function (topLevelType, comp, fakeNativeEvent) {
|
296 | ReactTestUtils.simulateNativeEventOnNode(topLevelType, findDOMNode(comp), fakeNativeEvent);
|
297 | },
|
298 |
|
299 | nativeTouchData: function (x, y) {
|
300 | return {
|
301 | touches: [{ pageX: x, pageY: y }]
|
302 | };
|
303 | },
|
304 |
|
305 | createRenderer: createRendererWithWarning,
|
306 |
|
307 | Simulate: null,
|
308 | SimulateNative: {}
|
309 | };
|
310 |
|
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 |
|
318 |
|
319 | function makeSimulator(eventType) {
|
320 | return function (domComponentOrNode, eventData) {
|
321 | var node;
|
322 | !!React.isValidElement(domComponentOrNode) ? process.env.NODE_ENV !== 'production' ? invariant(false, 'TestUtils.Simulate expects a component instance and not a ReactElement.TestUtils.Simulate will not work if you are using shallow rendering.') : _prodInvariant('14') : void 0;
|
323 | if (ReactTestUtils.isDOMComponent(domComponentOrNode)) {
|
324 | node = findDOMNode(domComponentOrNode);
|
325 | } else if (domComponentOrNode.tagName) {
|
326 | node = domComponentOrNode;
|
327 | }
|
328 |
|
329 | var dispatchConfig = EventPluginRegistry.eventNameDispatchConfigs[eventType];
|
330 |
|
331 | var fakeNativeEvent = new Event();
|
332 | fakeNativeEvent.target = node;
|
333 | fakeNativeEvent.type = eventType.toLowerCase();
|
334 |
|
335 |
|
336 |
|
337 | var event = new SyntheticEvent(dispatchConfig, ReactDOMComponentTree.getInstanceFromNode(node), fakeNativeEvent, node);
|
338 |
|
339 |
|
340 | event.persist();
|
341 | _assign(event, eventData);
|
342 |
|
343 | if (dispatchConfig.phasedRegistrationNames) {
|
344 | EventPropagators.accumulateTwoPhaseDispatches(event);
|
345 | } else {
|
346 | EventPropagators.accumulateDirectDispatches(event);
|
347 | }
|
348 |
|
349 | ReactUpdates.batchedUpdates(function () {
|
350 | EventPluginHub.enqueueEvents(event);
|
351 | EventPluginHub.processEventQueue(true);
|
352 | });
|
353 | };
|
354 | }
|
355 |
|
356 | function buildSimulators() {
|
357 | ReactTestUtils.Simulate = {};
|
358 |
|
359 | var eventType;
|
360 | for (eventType in EventPluginRegistry.eventNameDispatchConfigs) {
|
361 | |
362 |
|
363 |
|
364 |
|
365 | ReactTestUtils.Simulate[eventType] = makeSimulator(eventType);
|
366 | }
|
367 | }
|
368 |
|
369 |
|
370 | var oldInjectEventPluginOrder = EventPluginHub.injection.injectEventPluginOrder;
|
371 | EventPluginHub.injection.injectEventPluginOrder = function () {
|
372 | oldInjectEventPluginOrder.apply(this, arguments);
|
373 | buildSimulators();
|
374 | };
|
375 | var oldInjectEventPlugins = EventPluginHub.injection.injectEventPluginsByName;
|
376 | EventPluginHub.injection.injectEventPluginsByName = function () {
|
377 | oldInjectEventPlugins.apply(this, arguments);
|
378 | buildSimulators();
|
379 | };
|
380 |
|
381 | buildSimulators();
|
382 |
|
383 |
|
384 |
|
385 |
|
386 |
|
387 |
|
388 |
|
389 |
|
390 |
|
391 |
|
392 |
|
393 |
|
394 |
|
395 |
|
396 |
|
397 |
|
398 |
|
399 | function makeNativeSimulator(eventType) {
|
400 | return function (domComponentOrNode, nativeEventData) {
|
401 | var fakeNativeEvent = new Event(eventType);
|
402 | _assign(fakeNativeEvent, nativeEventData);
|
403 | if (ReactTestUtils.isDOMComponent(domComponentOrNode)) {
|
404 | ReactTestUtils.simulateNativeEventOnDOMComponent(eventType, domComponentOrNode, fakeNativeEvent);
|
405 | } else if (domComponentOrNode.tagName) {
|
406 |
|
407 | ReactTestUtils.simulateNativeEventOnNode(eventType, domComponentOrNode, fakeNativeEvent);
|
408 | }
|
409 | };
|
410 | }
|
411 |
|
412 | Object.keys(topLevelTypes).forEach(function (eventType) {
|
413 |
|
414 | var convenienceName = eventType.indexOf('top') === 0 ? eventType.charAt(3).toLowerCase() + eventType.substr(4) : eventType;
|
415 | |
416 |
|
417 |
|
418 |
|
419 | ReactTestUtils.SimulateNative[convenienceName] = makeNativeSimulator(eventType);
|
420 | });
|
421 |
|
422 | module.exports = ReactTestUtils; |
\ | No newline at end of file |