UNPKG

60.2 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
8
9var _object = require('object.assign');
10
11var _object2 = _interopRequireDefault(_object);
12
13var _arrayPrototype = require('array.prototype.flat');
14
15var _arrayPrototype2 = _interopRequireDefault(_arrayPrototype);
16
17var _lodash = require('lodash.isequal');
18
19var _lodash2 = _interopRequireDefault(_lodash);
20
21var _cheerio = require('cheerio');
22
23var _cheerio2 = _interopRequireDefault(_cheerio);
24
25var _Utils = require('./Utils');
26
27var _getAdapter = require('./getAdapter');
28
29var _getAdapter2 = _interopRequireDefault(_getAdapter);
30
31var _Debug = require('./Debug');
32
33var _RSTTraversal = require('./RSTTraversal');
34
35var _selectors = require('./selectors');
36
37function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
38
39function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
40
41function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
42
43function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
44
45var NODE = (0, _Utils.sym)('__node__');
46var NODES = (0, _Utils.sym)('__nodes__');
47var RENDERER = (0, _Utils.sym)('__renderer__');
48var UNRENDERED = (0, _Utils.sym)('__unrendered__');
49var ROOT = (0, _Utils.sym)('__root__');
50var OPTIONS = (0, _Utils.sym)('__options__');
51var SET_STATE = (0, _Utils.sym)('__setState__');
52/**
53 * Finds all nodes in the current wrapper nodes' render trees that match the provided predicate
54 * function.
55 *
56 * @param {ShallowWrapper} wrapper
57 * @param {Function} predicate
58 * @param {Function} filter
59 * @returns {ShallowWrapper}
60 */
61function findWhereUnwrapped(wrapper, predicate) {
62 var filter = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : _RSTTraversal.treeFilter;
63
64 return wrapper.flatMap(function (n) {
65 return filter(n.getNodeInternal(), predicate);
66 });
67}
68
69/**
70 * Returns a new wrapper instance with only the nodes of the current wrapper instance that match
71 * the provided predicate function.
72 *
73 * @param {ShallowWrapper} wrapper
74 * @param {Function} predicate
75 * @returns {ShallowWrapper}
76 */
77function filterWhereUnwrapped(wrapper, predicate) {
78 return wrapper.wrap(wrapper.getNodesInternal().filter(predicate).filter(Boolean));
79}
80
81/**
82 * Ensure options passed to ShallowWrapper are valid. Throws otherwise.
83 * @param {Object} options
84 */
85function validateOptions(options) {
86 var lifecycleExperimental = options.lifecycleExperimental,
87 disableLifecycleMethods = options.disableLifecycleMethods,
88 enableComponentDidUpdateOnSetState = options.enableComponentDidUpdateOnSetState,
89 supportPrevContextArgumentOfComponentDidUpdate = options.supportPrevContextArgumentOfComponentDidUpdate,
90 lifecycles = options.lifecycles;
91
92 if (typeof lifecycleExperimental !== 'undefined' && typeof lifecycleExperimental !== 'boolean') {
93 throw new Error('lifecycleExperimental must be either true or false if provided');
94 }
95
96 if (typeof disableLifecycleMethods !== 'undefined' && typeof disableLifecycleMethods !== 'boolean') {
97 throw new Error('disableLifecycleMethods must be either true or false if provided');
98 }
99
100 if (lifecycleExperimental != null && disableLifecycleMethods != null && lifecycleExperimental === disableLifecycleMethods) {
101 throw new Error('lifecycleExperimental and disableLifecycleMethods cannot be set to the same value');
102 }
103
104 if (typeof enableComponentDidUpdateOnSetState !== 'undefined' && lifecycles.componentDidUpdate && lifecycles.componentDidUpdate.onSetState !== enableComponentDidUpdateOnSetState) {
105 throw new TypeError('the legacy enableComponentDidUpdateOnSetState option should be matched by `lifecycles: { componentDidUpdate: { onSetState: true } }`, for compatibility');
106 }
107
108 if (typeof supportPrevContextArgumentOfComponentDidUpdate !== 'undefined' && lifecycles.componentDidUpdate && lifecycles.componentDidUpdate.prevContext !== supportPrevContextArgumentOfComponentDidUpdate) {
109 throw new TypeError('the legacy supportPrevContextArgumentOfComponentDidUpdate option should be matched by `lifecycles: { componentDidUpdate: { prevContext: true } }`, for compatibility');
110 }
111}
112
113function getAdapterLifecycles(_ref) {
114 var options = _ref.options;
115 var _options$lifecycles = options.lifecycles,
116 lifecycles = _options$lifecycles === undefined ? {} : _options$lifecycles,
117 enableComponentDidUpdateOnSetState = options.enableComponentDidUpdateOnSetState,
118 supportPrevContextArgumentOfComponentDidUpdate = options.supportPrevContextArgumentOfComponentDidUpdate;
119
120
121 var hasLegacySetStateArg = typeof enableComponentDidUpdateOnSetState !== 'undefined';
122 var hasLegacyPrevContextArg = typeof supportPrevContextArgumentOfComponentDidUpdate !== 'undefined';
123 var componentDidUpdate = hasLegacySetStateArg || hasLegacyPrevContextArg ? (0, _object2['default'])({}, hasLegacySetStateArg && {
124 onSetState: !!enableComponentDidUpdateOnSetState
125 }, hasLegacyPrevContextArg && {
126 prevContext: !!supportPrevContextArgumentOfComponentDidUpdate
127 }) : null;
128
129 return (0, _object2['default'])({}, lifecycles, {
130 setState: (0, _object2['default'])({}, lifecycles.setState)
131 }, componentDidUpdate && { componentDidUpdate: componentDidUpdate });
132}
133
134function getRootNode(node) {
135 if (node.nodeType === 'host') {
136 return node;
137 }
138 return node.rendered;
139}
140
141function getRootNodeInternal(wrapper) {
142 return wrapper[ROOT][NODE];
143}
144
145function nodeParents(wrapper, node) {
146 return (0, _RSTTraversal.parentsOfNode)(node, getRootNodeInternal(wrapper));
147}
148
149function privateSetNodes(wrapper, nodes) {
150 if (!Array.isArray(nodes)) {
151 (0, _Utils.privateSet)(wrapper, NODE, nodes);
152 (0, _Utils.privateSet)(wrapper, NODES, [nodes]);
153 } else {
154 (0, _Utils.privateSet)(wrapper, NODE, nodes[0]);
155 (0, _Utils.privateSet)(wrapper, NODES, nodes);
156 }
157 (0, _Utils.privateSet)(wrapper, 'length', wrapper[NODES].length);
158}
159
160function pureComponentShouldComponentUpdate(prevProps, props, prevState, state) {
161 return !(0, _lodash2['default'])(prevProps, props) || !(0, _lodash2['default'])(prevState, state);
162}
163
164function isPureComponent(instance) {
165 return instance && instance.isPureReactComponent;
166}
167
168/**
169 * @class ShallowWrapper
170 */
171
172var ShallowWrapper = function () {
173 function ShallowWrapper(nodes, root) {
174 var _this = this;
175
176 var passedOptions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
177
178 _classCallCheck(this, ShallowWrapper);
179
180 validateOptions(passedOptions);
181
182 var options = (0, _Utils.makeOptions)(passedOptions);
183 var adapter = (0, _getAdapter2['default'])(options);
184 var lifecycles = getAdapterLifecycles(adapter);
185
186 // mounting a ShallowRender component
187 if (!root) {
188 if (!adapter.isValidElement(nodes)) {
189 throw new TypeError('ShallowWrapper can only wrap valid elements');
190 }
191
192 (0, _Utils.privateSet)(this, ROOT, this);
193 (0, _Utils.privateSet)(this, UNRENDERED, nodes);
194 var renderer = adapter.createRenderer((0, _object2['default'])({ mode: 'shallow' }, options));
195 (0, _Utils.privateSet)(this, RENDERER, renderer);
196 this[RENDERER].render(nodes, options.context);
197 var renderedNode = this[RENDERER].getNode();
198 privateSetNodes(this, getRootNode(renderedNode));
199 (0, _Utils.privateSet)(this, OPTIONS, options);
200
201 var instance = renderedNode.instance;
202
203 if (instance && !options.disableLifecycleMethods) {
204 // Ensure to call componentDidUpdate when instance.setState is called
205 if (lifecycles.componentDidUpdate.onSetState && !instance[SET_STATE]) {
206 (0, _Utils.privateSet)(instance, SET_STATE, instance.setState);
207 instance.setState = function (updater) {
208 var callback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined;
209 return _this.setState.apply(_this, _toConsumableArray(callback == null ? [updater] : [updater, callback]));
210 };
211 }
212
213 if (typeof instance.componentDidMount === 'function') {
214 this[RENDERER].batchedUpdates(function () {
215 instance.componentDidMount();
216 });
217 }
218 }
219 // creating a child component through enzyme's ShallowWrapper APIs.
220 } else {
221 (0, _Utils.privateSet)(this, ROOT, root);
222 (0, _Utils.privateSet)(this, UNRENDERED, null);
223 (0, _Utils.privateSet)(this, RENDERER, root[RENDERER]);
224 privateSetNodes(this, nodes);
225 (0, _Utils.privateSet)(this, OPTIONS, root[OPTIONS]);
226 }
227 }
228
229 /**
230 * Returns the root wrapper
231 *
232 * @return {ShallowWrapper}
233 */
234
235
236 _createClass(ShallowWrapper, [{
237 key: 'root',
238 value: function () {
239 function root() {
240 return this[ROOT];
241 }
242
243 return root;
244 }()
245
246 /**
247 * Returns the wrapped component.
248 *
249 * @return {ReactComponent}
250 */
251
252 }, {
253 key: 'getNodeInternal',
254 value: function () {
255 function getNodeInternal() {
256 if (this.length !== 1) {
257 throw new Error('ShallowWrapper::getNode() can only be called when wrapping one node');
258 }
259 if (this[ROOT] === this) {
260 this.update();
261 }
262 return this[NODE];
263 }
264
265 return getNodeInternal;
266 }()
267
268 /**
269 * Returns the the wrapped components.
270 *
271 * @return {Array<ReactComponent>}
272 */
273
274 }, {
275 key: 'getNodesInternal',
276 value: function () {
277 function getNodesInternal() {
278 if (this[ROOT] === this && this.length === 1) {
279 this.update();
280 }
281 return this[NODES];
282 }
283
284 return getNodesInternal;
285 }()
286
287 /**
288 * Returns the wrapped ReactElement.
289 *
290 * @return {ReactElement}
291 */
292
293 }, {
294 key: 'getElement',
295 value: function () {
296 function getElement() {
297 if (this.length !== 1) {
298 throw new Error('ShallowWrapper::getElement() can only be called when wrapping one node');
299 }
300 return (0, _getAdapter2['default'])(this[OPTIONS]).nodeToElement(this.getNodeInternal());
301 }
302
303 return getElement;
304 }()
305
306 /**
307 * Returns the wrapped ReactElements.
308 *
309 * @return {Array<ReactElement>}
310 */
311
312 }, {
313 key: 'getElements',
314 value: function () {
315 function getElements() {
316 return this.getNodesInternal().map((0, _getAdapter2['default'])(this[OPTIONS]).nodeToElement);
317 }
318
319 return getElements;
320 }()
321
322 // eslint-disable-next-line class-methods-use-this
323
324 }, {
325 key: 'getNode',
326 value: function () {
327 function getNode() {
328 throw new Error('ShallowWrapper::getNode() is no longer supported. Use ShallowWrapper::getElement() instead');
329 }
330
331 return getNode;
332 }()
333
334 // eslint-disable-next-line class-methods-use-this
335
336 }, {
337 key: 'getNodes',
338 value: function () {
339 function getNodes() {
340 throw new Error('ShallowWrapper::getNodes() is no longer supported. Use ShallowWrapper::getElements() instead');
341 }
342
343 return getNodes;
344 }()
345
346 /**
347 * Gets the instance of the component being rendered as the root node passed into `shallow()`.
348 *
349 * NOTE: can only be called on a wrapper instance that is also the root instance.
350 *
351 * Example:
352 * ```
353 * const wrapper = shallow(<MyComponent />);
354 * const inst = wrapper.instance();
355 * expect(inst).to.be.instanceOf(MyComponent);
356 * ```
357 * @returns {ReactComponent}
358 */
359
360 }, {
361 key: 'instance',
362 value: function () {
363 function instance() {
364 if (this[ROOT] !== this) {
365 throw new Error('ShallowWrapper::instance() can only be called on the root');
366 }
367 return this[RENDERER].getNode().instance;
368 }
369
370 return instance;
371 }()
372
373 /**
374 * Forces a re-render. Useful to run before checking the render output if something external
375 * may be updating the state of the component somewhere.
376 *
377 * NOTE: can only be called on a wrapper instance that is also the root instance.
378 *
379 * @returns {ShallowWrapper}
380 */
381
382 }, {
383 key: 'update',
384 value: function () {
385 function update() {
386 if (this[ROOT] !== this) {
387 throw new Error('ShallowWrapper::update() can only be called on the root');
388 }
389 if (this.length !== 1) {
390 throw new Error('ShallowWrapper::update() can only be called when wrapping one node');
391 }
392 privateSetNodes(this, getRootNode(this[RENDERER].getNode()));
393 return this;
394 }
395
396 return update;
397 }()
398
399 /**
400 * A method that unmounts the component. This can be used to simulate a component going through
401 * and unmount/mount lifecycle.
402 * @returns {ShallowWrapper}
403 */
404
405 }, {
406 key: 'unmount',
407 value: function () {
408 function unmount() {
409 this[RENDERER].unmount();
410 return this;
411 }
412
413 return unmount;
414 }()
415
416 /**
417 * A method is for re-render with new props and context.
418 * This calls componentDidUpdate method if disableLifecycleMethods is not enabled.
419 *
420 * NOTE: can only be called on a wrapper instance that is also the root instance.
421 *
422 * @param {Object} props
423 * @param {Object} context
424 * @returns {ShallowWrapper}
425 */
426
427 }, {
428 key: 'rerender',
429 value: function () {
430 function rerender(props, context) {
431 var _this2 = this;
432
433 var adapter = (0, _getAdapter2['default'])(this[OPTIONS]);
434 this.single('rerender', function () {
435 (0, _Utils.withSetStateAllowed)(function () {
436 // NOTE(lmr): In react 16, instances will be null for SFCs, but
437 // rerendering with props/context is still a valid thing to do. In
438 // this case, state will be undefined, but props/context will exist.
439 var instance = _this2.instance() || {};
440 var state = instance.state;
441
442 var prevProps = instance.props || _this2[UNRENDERED].props;
443 var prevContext = instance.context || _this2[OPTIONS].context;
444 var nextContext = context || prevContext;
445 if (context) {
446 _this2[OPTIONS] = (0, _object2['default'])({}, _this2[OPTIONS], { context: nextContext });
447 }
448 _this2[RENDERER].batchedUpdates(function () {
449 // When shouldComponentUpdate returns false we shouldn't call componentDidUpdate.
450 // so we spy shouldComponentUpdate to get the result.
451 var shouldRender = true;
452 var spy = void 0;
453 if (!_this2[OPTIONS].disableLifecycleMethods && instance && typeof instance.shouldComponentUpdate === 'function') {
454 spy = (0, _Utils.spyMethod)(instance, 'shouldComponentUpdate');
455 } else if (isPureComponent(instance)) {
456 shouldRender = pureComponentShouldComponentUpdate(prevProps, props, state, instance.state);
457 }
458 if (props) _this2[UNRENDERED] = (0, _Utils.cloneElement)(adapter, _this2[UNRENDERED], props);
459 _this2[RENDERER].render(_this2[UNRENDERED], nextContext);
460 if (spy) {
461 shouldRender = spy.getLastReturnValue();
462 spy.restore();
463 }
464 if (shouldRender && !_this2[OPTIONS].disableLifecycleMethods && instance) {
465 var lifecycles = getAdapterLifecycles(adapter);
466
467 if (lifecycles.getSnapshotBeforeUpdate) {
468 var snapshot = void 0;
469 if (typeof instance.getSnapshotBeforeUpdate === 'function') {
470 snapshot = instance.getSnapshotBeforeUpdate(prevProps, state);
471 }
472 if (lifecycles.componentDidUpdate && typeof instance.componentDidUpdate === 'function') {
473 instance.componentDidUpdate(prevProps, state, snapshot);
474 }
475 } else if (lifecycles.componentDidUpdate && typeof instance.componentDidUpdate === 'function') {
476 if (lifecycles.componentDidUpdate.prevContext) {
477 instance.componentDidUpdate(prevProps, state, prevContext);
478 } else {
479 instance.componentDidUpdate(prevProps, state);
480 }
481 }
482 // If it doesn't need to rerender, update only its props.
483 } else if (!(0, _lodash2['default'])(props, instance.props)) {
484 instance.props = (Object.freeze || Object)((0, _object2['default'])({}, instance.props, props));
485 }
486 _this2.update();
487 });
488 });
489 });
490 return this;
491 }
492
493 return rerender;
494 }()
495
496 /**
497 * A method that sets the props of the root component, and re-renders. Useful for when you are
498 * wanting to test how the component behaves over time with changing props. Calling this, for
499 * instance, will call the `componentWillReceiveProps` lifecycle method.
500 *
501 * Similar to `setState`, this method accepts a props object and will merge it in with the already
502 * existing props.
503 *
504 * NOTE: can only be called on a wrapper instance that is also the root instance.
505 *
506 * @param {Object} props object
507 * @param {Function} cb - callback function
508 * @returns {ShallowWrapper}
509 */
510
511 }, {
512 key: 'setProps',
513 value: function () {
514 function setProps(props) {
515 var callback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined;
516
517 if (this[ROOT] !== this) {
518 throw new Error('ShallowWrapper::setProps() can only be called on the root');
519 }
520 if (arguments.length > 1 && typeof callback !== 'function') {
521 throw new TypeError('ReactWrapper::setProps() expects a function as its second argument');
522 }
523 this.rerender(props);
524 if (callback) {
525 callback();
526 }
527 return this;
528 }
529
530 return setProps;
531 }()
532
533 /**
534 * A method to invoke `setState` on the root component instance similar to how you might in the
535 * definition of the component, and re-renders. This method is useful for testing your component
536 * in hard to achieve states, however should be used sparingly. If possible, you should utilize
537 * your component's external API in order to get it into whatever state you want to test, in order
538 * to be as accurate of a test as possible. This is not always practical, however.
539 *
540 * NOTE: can only be called on a wrapper instance that is also the root instance.
541 *
542 * @param {Object} state to merge
543 * @param {Function} cb - callback function
544 * @returns {ShallowWrapper}
545 */
546
547 }, {
548 key: 'setState',
549 value: function () {
550 function setState(state) {
551 var _this3 = this;
552
553 var callback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined;
554
555 if (this[ROOT] !== this) {
556 throw new Error('ShallowWrapper::setState() can only be called on the root');
557 }
558 if (this.instance() === null || this[RENDERER].getNode().nodeType !== 'class') {
559 throw new Error('ShallowWrapper::setState() can only be called on class components');
560 }
561 if (arguments.length > 1 && typeof callback !== 'function') {
562 throw new TypeError('ReactWrapper::setState() expects a function as its second argument');
563 }
564
565 this.single('setState', function () {
566 (0, _Utils.withSetStateAllowed)(function () {
567 var adapter = (0, _getAdapter2['default'])(_this3[OPTIONS]);
568
569 var lifecycles = getAdapterLifecycles(adapter);
570
571 var instance = _this3.instance();
572 var prevProps = instance.props;
573 var prevState = instance.state;
574 var prevContext = instance.context;
575
576 var statePayload = typeof state === 'function' ? state.call(instance, prevState, prevProps) : state;
577
578 // returning null or undefined prevents the update in React 16+
579 // https://github.com/facebook/react/pull/12756
580 var maybeHasUpdate = !lifecycles.setState.skipsComponentDidUpdateOnNullish || statePayload != null;
581
582 // When shouldComponentUpdate returns false we shouldn't call componentDidUpdate.
583 // so we spy shouldComponentUpdate to get the result.
584 var spy = void 0;
585 var shouldRender = true;
586 if (!_this3[OPTIONS].disableLifecycleMethods && lifecycles.componentDidUpdate && lifecycles.componentDidUpdate.onSetState && instance && typeof instance.shouldComponentUpdate === 'function') {
587 spy = (0, _Utils.spyMethod)(instance, 'shouldComponentUpdate');
588 } else if (isPureComponent(instance)) {
589 shouldRender = pureComponentShouldComponentUpdate(prevProps, instance.props, prevState, statePayload);
590 }
591 // We don't pass the setState callback here
592 // to guarantee to call the callback after finishing the render
593 if (instance[SET_STATE]) {
594 instance[SET_STATE](statePayload);
595 } else {
596 instance.setState(statePayload);
597 }
598 if (spy) {
599 shouldRender = spy.getLastReturnValue();
600 spy.restore();
601 }
602 if (maybeHasUpdate && shouldRender && !_this3[OPTIONS].disableLifecycleMethods && lifecycles.componentDidUpdate && lifecycles.componentDidUpdate.onSetState && instance) {
603 if (lifecycles.getSnapshotBeforeUpdate && typeof instance.getSnapshotBeforeUpdate === 'function') {
604 var snapshot = instance.getSnapshotBeforeUpdate(prevProps, prevState);
605 if (typeof instance.componentDidUpdate === 'function') {
606 instance.componentDidUpdate(prevProps, prevState, snapshot);
607 }
608 } else if (typeof instance.componentDidUpdate === 'function') {
609 if (lifecycles.componentDidUpdate.prevContext) {
610 instance.componentDidUpdate(prevProps, prevState, prevContext);
611 } else {
612 instance.componentDidUpdate(prevProps, prevState);
613 }
614 }
615 }
616 _this3.update();
617 // call the setState callback
618 if (callback) {
619 if (adapter.invokeSetStateCallback) {
620 adapter.invokeSetStateCallback(instance, callback);
621 } else {
622 callback.call(instance);
623 }
624 }
625 });
626 });
627 return this;
628 }
629
630 return setState;
631 }()
632
633 /**
634 * A method that sets the context of the root component, and re-renders. Useful for when you are
635 * wanting to test how the component behaves over time with changing contexts.
636 *
637 * NOTE: can only be called on a wrapper instance that is also the root instance.
638 *
639 * @param {Object} context object
640 * @returns {ShallowWrapper}
641 */
642
643 }, {
644 key: 'setContext',
645 value: function () {
646 function setContext(context) {
647 if (this[ROOT] !== this) {
648 throw new Error('ShallowWrapper::setContext() can only be called on the root');
649 }
650 if (!this[OPTIONS].context) {
651 throw new Error('ShallowWrapper::setContext() can only be called on a wrapper that was originally passed a context option');
652 }
653 return this.rerender(null, context);
654 }
655
656 return setContext;
657 }()
658
659 /**
660 * Whether or not a given react element exists in the shallow render tree.
661 *
662 * Example:
663 * ```
664 * const wrapper = shallow(<MyComponent />);
665 * expect(wrapper.contains(<div className="foo bar" />)).to.equal(true);
666 * ```
667 *
668 * @param {ReactElement|Array<ReactElement>} nodeOrNodes
669 * @returns {Boolean}
670 */
671
672 }, {
673 key: 'contains',
674 value: function () {
675 function contains(nodeOrNodes) {
676 var adapter = (0, _getAdapter2['default'])(this[OPTIONS]);
677 if (!(0, _Utils.isReactElementAlike)(nodeOrNodes, adapter)) {
678 throw new Error('ShallowWrapper::contains() can only be called with ReactElement (or array of them), string or number as argument.');
679 }
680 var predicate = Array.isArray(nodeOrNodes) ? function (other) {
681 return (0, _Utils.containsChildrenSubArray)(_Utils.nodeEqual, other, nodeOrNodes.map(function (node) {
682 return adapter.elementToNode(node);
683 }));
684 } : function (other) {
685 return (0, _Utils.nodeEqual)(adapter.elementToNode(nodeOrNodes), other);
686 };
687
688 return findWhereUnwrapped(this, predicate).length > 0;
689 }
690
691 return contains;
692 }()
693
694 /**
695 * Whether or not a given react element exists in the shallow render tree.
696 * Match is based on the expected element and not on wrappers element.
697 * It will determine if one of the wrappers element "looks like" the expected
698 * element by checking if all props of the expected element are present
699 * on the wrappers element and equals to each other.
700 *
701 * Example:
702 * ```
703 * // MyComponent outputs <div><div class="foo">Hello</div></div>
704 * const wrapper = shallow(<MyComponent />);
705 * expect(wrapper.containsMatchingElement(<div>Hello</div>)).to.equal(true);
706 * ```
707 *
708 * @param {ReactElement} node
709 * @returns {Boolean}
710 */
711
712 }, {
713 key: 'containsMatchingElement',
714 value: function () {
715 function containsMatchingElement(node) {
716 var adapter = (0, _getAdapter2['default'])(this[OPTIONS]);
717 var rstNode = adapter.elementToNode(node);
718 var predicate = function () {
719 function predicate(other) {
720 return (0, _Utils.nodeMatches)(rstNode, other, function (a, b) {
721 return a <= b;
722 });
723 }
724
725 return predicate;
726 }();
727 return findWhereUnwrapped(this, predicate).length > 0;
728 }
729
730 return containsMatchingElement;
731 }()
732
733 /**
734 * Whether or not all the given react elements exists in the shallow render tree.
735 * Match is based on the expected element and not on wrappers element.
736 * It will determine if one of the wrappers element "looks like" the expected
737 * element by checking if all props of the expected element are present
738 * on the wrappers element and equals to each other.
739 *
740 * Example:
741 * ```
742 * const wrapper = shallow(<MyComponent />);
743 * expect(wrapper.containsAllMatchingElements([
744 * <div>Hello</div>,
745 * <div>Goodbye</div>,
746 * ])).to.equal(true);
747 * ```
748 *
749 * @param {Array<ReactElement>} nodes
750 * @returns {Boolean}
751 */
752
753 }, {
754 key: 'containsAllMatchingElements',
755 value: function () {
756 function containsAllMatchingElements(nodes) {
757 var _this4 = this;
758
759 if (!Array.isArray(nodes)) {
760 throw new TypeError('nodes should be an Array');
761 }
762
763 return nodes.every(function (node) {
764 return _this4.containsMatchingElement(node);
765 });
766 }
767
768 return containsAllMatchingElements;
769 }()
770
771 /**
772 * Whether or not one of the given react elements exists in the shallow render tree.
773 * Match is based on the expected element and not on wrappers element.
774 * It will determine if one of the wrappers element "looks like" the expected
775 * element by checking if all props of the expected element are present
776 * on the wrappers element and equals to each other.
777 *
778 * Example:
779 * ```
780 * const wrapper = shallow(<MyComponent />);
781 * expect(wrapper.containsAnyMatchingElements([
782 * <div>Hello</div>,
783 * <div>Goodbye</div>,
784 * ])).to.equal(true);
785 * ```
786 *
787 * @param {Array<ReactElement>} nodes
788 * @returns {Boolean}
789 */
790
791 }, {
792 key: 'containsAnyMatchingElements',
793 value: function () {
794 function containsAnyMatchingElements(nodes) {
795 var _this5 = this;
796
797 return Array.isArray(nodes) && nodes.some(function (node) {
798 return _this5.containsMatchingElement(node);
799 });
800 }
801
802 return containsAnyMatchingElements;
803 }()
804
805 /**
806 * Whether or not a given react element exists in the render tree.
807 *
808 * Example:
809 * ```
810 * const wrapper = shallow(<MyComponent />);
811 * expect(wrapper.contains(<div className="foo bar" />)).to.equal(true);
812 * ```
813 *
814 * @param {ReactElement} node
815 * @returns {Boolean}
816 */
817
818 }, {
819 key: 'equals',
820 value: function () {
821 function equals(node) {
822 var _this6 = this;
823
824 return this.single('equals', function () {
825 return (0, _Utils.nodeEqual)(_this6.getNodeInternal(), node);
826 });
827 }
828
829 return equals;
830 }()
831
832 /**
833 * Whether or not a given react element matches the render tree.
834 * Match is based on the expected element and not on wrapper root node.
835 * It will determine if the wrapper root node "looks like" the expected
836 * element by checking if all props of the expected element are present
837 * on the wrapper root node and equals to each other.
838 *
839 * Example:
840 * ```
841 * // MyComponent outputs <div class="foo">Hello</div>
842 * const wrapper = shallow(<MyComponent />);
843 * expect(wrapper.matchesElement(<div>Hello</div>)).to.equal(true);
844 * ```
845 *
846 * @param {ReactElement} node
847 * @returns {Boolean}
848 */
849
850 }, {
851 key: 'matchesElement',
852 value: function () {
853 function matchesElement(node) {
854 var _this7 = this;
855
856 return this.single('matchesElement', function () {
857 var adapter = (0, _getAdapter2['default'])(_this7[OPTIONS]);
858 var rstNode = adapter.elementToNode(node);
859 return (0, _Utils.nodeMatches)(rstNode, _this7.getNodeInternal(), function (a, b) {
860 return a <= b;
861 });
862 });
863 }
864
865 return matchesElement;
866 }()
867
868 /**
869 * Finds every node in the render tree of the current wrapper that matches the provided selector.
870 *
871 * @param {String|Function} selector
872 * @returns {ShallowWrapper}
873 */
874
875 }, {
876 key: 'find',
877 value: function () {
878 function find(selector) {
879 return this.wrap((0, _selectors.reduceTreesBySelector)(selector, this.getNodesInternal()));
880 }
881
882 return find;
883 }()
884
885 /**
886 * Returns whether or not current node matches a provided selector.
887 *
888 * NOTE: can only be called on a wrapper of a single node.
889 *
890 * @param {String|Function} selector
891 * @returns {boolean}
892 */
893
894 }, {
895 key: 'is',
896 value: function () {
897 function is(selector) {
898 var predicate = (0, _selectors.buildPredicate)(selector);
899 return this.single('is', function (n) {
900 return predicate(n);
901 });
902 }
903
904 return is;
905 }()
906
907 /**
908 * Returns true if the component rendered nothing, i.e., null or false.
909 *
910 * @returns {boolean}
911 */
912
913 }, {
914 key: 'isEmptyRender',
915 value: function () {
916 function isEmptyRender() {
917 return this.type() === null;
918 }
919
920 return isEmptyRender;
921 }()
922
923 /**
924 * Returns a new wrapper instance with only the nodes of the current wrapper instance that match
925 * the provided predicate function. The predicate should receive a wrapped node as its first
926 * argument.
927 *
928 * @param {Function} predicate
929 * @returns {ShallowWrapper}
930 */
931
932 }, {
933 key: 'filterWhere',
934 value: function () {
935 function filterWhere(predicate) {
936 var _this8 = this;
937
938 return filterWhereUnwrapped(this, function (n) {
939 return predicate(_this8.wrap(n));
940 });
941 }
942
943 return filterWhere;
944 }()
945
946 /**
947 * Returns a new wrapper instance with only the nodes of the current wrapper instance that match
948 * the provided selector.
949 *
950 * @param {String|Function} selector
951 * @returns {ShallowWrapper}
952 */
953
954 }, {
955 key: 'filter',
956 value: function () {
957 function filter(selector) {
958 var predicate = (0, _selectors.buildPredicate)(selector);
959 return filterWhereUnwrapped(this, predicate);
960 }
961
962 return filter;
963 }()
964
965 /**
966 * Returns a new wrapper instance with only the nodes of the current wrapper that did not match
967 * the provided selector. Essentially the inverse of `filter`.
968 *
969 * @param {String|Function} selector
970 * @returns {ShallowWrapper}
971 */
972
973 }, {
974 key: 'not',
975 value: function () {
976 function not(selector) {
977 var predicate = (0, _selectors.buildPredicate)(selector);
978 return filterWhereUnwrapped(this, function (n) {
979 return !predicate(n);
980 });
981 }
982
983 return not;
984 }()
985
986 /**
987 * Returns a string of the rendered text of the current render tree. This function should be
988 * looked at with skepticism if being used to test what the actual HTML output of the component
989 * will be. If that is what you would like to test, use enzyme's `render` function instead.
990 *
991 * NOTE: can only be called on a wrapper of a single node.
992 *
993 * @returns {String}
994 */
995
996 }, {
997 key: 'text',
998 value: function () {
999 function text() {
1000 return this.single('text', _RSTTraversal.getTextFromNode);
1001 }
1002
1003 return text;
1004 }()
1005
1006 /**
1007 * Returns the HTML of the node.
1008 *
1009 * NOTE: can only be called on a wrapper of a single node.
1010 *
1011 * @returns {String}
1012 */
1013
1014 }, {
1015 key: 'html',
1016 value: function () {
1017 function html() {
1018 var _this9 = this;
1019
1020 return this.single('html', function (n) {
1021 if (_this9.type() === null) return null;
1022 var adapter = (0, _getAdapter2['default'])(_this9[OPTIONS]);
1023 var renderer = adapter.createRenderer((0, _object2['default'])({}, _this9[OPTIONS], { mode: 'string' }));
1024 return renderer.render(adapter.nodeToElement(n));
1025 });
1026 }
1027
1028 return html;
1029 }()
1030
1031 /**
1032 * Returns the current node rendered to HTML and wrapped in a CheerioWrapper.
1033 *
1034 * NOTE: can only be called on a wrapper of a single node.
1035 *
1036 * @returns {CheerioWrapper}
1037 */
1038
1039 }, {
1040 key: 'render',
1041 value: function () {
1042 function render() {
1043 return this.type() === null ? (0, _cheerio2['default'])() : _cheerio2['default'].load('')(this.html());
1044 }
1045
1046 return render;
1047 }()
1048
1049 /**
1050 * Used to simulate events. Pass an eventname and (optionally) event arguments. This method of
1051 * testing events should be met with some skepticism.
1052 *
1053 * @param {String} event
1054 * @param {Array} args
1055 * @returns {ShallowWrapper}
1056 */
1057
1058 }, {
1059 key: 'simulate',
1060 value: function () {
1061 function simulate(event) {
1062 var _this10 = this;
1063
1064 for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
1065 args[_key - 1] = arguments[_key];
1066 }
1067
1068 return this.single('simulate', function (n) {
1069 var _RENDERER;
1070
1071 (_RENDERER = _this10[RENDERER]).simulateEvent.apply(_RENDERER, [n, event].concat(args));
1072 _this10[ROOT].update();
1073 return _this10;
1074 });
1075 }
1076
1077 return simulate;
1078 }()
1079
1080 /**
1081 * Used to simulate throwing a rendering error. Pass an error to throw.
1082 *
1083 * @param {String} error
1084 * @returns {ShallowWrapper}
1085 */
1086
1087 }, {
1088 key: 'simulateError',
1089 value: function () {
1090 function simulateError(error) {
1091 var _this11 = this;
1092
1093 // in shallow, the "root" is the "rendered" thing.
1094
1095 return this.single('simulateError', function (thisNode) {
1096 if (thisNode.nodeType === 'host') {
1097 throw new TypeError('ShallowWrapper::simulateError() can only be called on custom components');
1098 }
1099
1100 var renderer = _this11[RENDERER];
1101 if (typeof renderer.simulateError !== 'function') {
1102 throw new TypeError('your adapter does not support `simulateError`. Try upgrading it!');
1103 }
1104
1105 var rootNode = getRootNodeInternal(_this11);
1106 var nodeHierarchy = [thisNode].concat(nodeParents(_this11, thisNode));
1107 renderer.simulateError(nodeHierarchy, rootNode, error);
1108
1109 return _this11;
1110 });
1111 }
1112
1113 return simulateError;
1114 }()
1115
1116 /**
1117 * Returns the props hash for the current node of the wrapper.
1118 *
1119 * NOTE: can only be called on a wrapper of a single node.
1120 *
1121 * @returns {Object}
1122 */
1123
1124 }, {
1125 key: 'props',
1126 value: function () {
1127 function props() {
1128 return this.single('props', _RSTTraversal.propsOfNode);
1129 }
1130
1131 return props;
1132 }()
1133
1134 /**
1135 * Returns the state hash for the root node of the wrapper. Optionally pass in a prop name and it
1136 * will return just that value.
1137 *
1138 * NOTE: can only be called on a wrapper of a single node.
1139 *
1140 * @param {String} name (optional)
1141 * @returns {*}
1142 */
1143
1144 }, {
1145 key: 'state',
1146 value: function () {
1147 function state(name) {
1148 var _this12 = this;
1149
1150 if (this[ROOT] !== this) {
1151 throw new Error('ShallowWrapper::state() can only be called on the root');
1152 }
1153 if (this.instance() === null || this[RENDERER].getNode().nodeType !== 'class') {
1154 throw new Error('ShallowWrapper::state() can only be called on class components');
1155 }
1156 var _state = this.single('state', function () {
1157 return _this12.instance().state;
1158 });
1159 if (typeof name !== 'undefined') {
1160 return _state[name];
1161 }
1162 return _state;
1163 }
1164
1165 return state;
1166 }()
1167
1168 /**
1169 * Returns the context hash for the root node of the wrapper.
1170 * Optionally pass in a prop name and it will return just that value.
1171 *
1172 * NOTE: can only be called on a wrapper of a single node.
1173 *
1174 * @param {String} name (optional)
1175 * @returns {*}
1176 */
1177
1178 }, {
1179 key: 'context',
1180 value: function () {
1181 function context(name) {
1182 var _this13 = this;
1183
1184 if (this[ROOT] !== this) {
1185 throw new Error('ShallowWrapper::context() can only be called on the root');
1186 }
1187 if (!this[OPTIONS].context) {
1188 throw new Error('ShallowWrapper::context() can only be called on a wrapper that was originally passed a context option');
1189 }
1190 if (this.instance() === null) {
1191 throw new Error('ShallowWrapper::context() can only be called on wrapped nodes that have a non-null instance');
1192 }
1193 var _context = this.single('context', function () {
1194 return _this13.instance().context;
1195 });
1196 if (name) {
1197 return _context[name];
1198 }
1199 return _context;
1200 }
1201
1202 return context;
1203 }()
1204
1205 /**
1206 * Returns a new wrapper with all of the children of the current wrapper.
1207 *
1208 * @param {String|Function} [selector]
1209 * @returns {ShallowWrapper}
1210 */
1211
1212 }, {
1213 key: 'children',
1214 value: function () {
1215 function children(selector) {
1216 var allChildren = this.flatMap(function (n) {
1217 return (0, _RSTTraversal.childrenOfNode)(n.getNodeInternal());
1218 });
1219 return selector ? allChildren.filter(selector) : allChildren;
1220 }
1221
1222 return children;
1223 }()
1224
1225 /**
1226 * Returns a new wrapper with a specific child
1227 *
1228 * @param {Number} [index]
1229 * @returns {ShallowWrapper}
1230 */
1231
1232 }, {
1233 key: 'childAt',
1234 value: function () {
1235 function childAt(index) {
1236 var _this14 = this;
1237
1238 return this.single('childAt', function () {
1239 return _this14.children().at(index);
1240 });
1241 }
1242
1243 return childAt;
1244 }()
1245
1246 /**
1247 * Returns a wrapper around all of the parents/ancestors of the wrapper. Does not include the node
1248 * in the current wrapper.
1249 *
1250 * NOTE: can only be called on a wrapper of a single node.
1251 *
1252 * @param {String|Function} [selector]
1253 * @returns {ShallowWrapper}
1254 */
1255
1256 }, {
1257 key: 'parents',
1258 value: function () {
1259 function parents(selector) {
1260 var _this15 = this;
1261
1262 var allParents = this.wrap(this.single('parents', function (n) {
1263 return nodeParents(_this15, n);
1264 }));
1265 return selector ? allParents.filter(selector) : allParents;
1266 }
1267
1268 return parents;
1269 }()
1270
1271 /**
1272 * Returns a wrapper around the immediate parent of the current node.
1273 *
1274 * @returns {ShallowWrapper}
1275 */
1276
1277 }, {
1278 key: 'parent',
1279 value: function () {
1280 function parent() {
1281 return this.flatMap(function (n) {
1282 return [n.parents().get(0)];
1283 });
1284 }
1285
1286 return parent;
1287 }()
1288
1289 /**
1290 *
1291 * @param {String|Function} selector
1292 * @returns {ShallowWrapper}
1293 */
1294
1295 }, {
1296 key: 'closest',
1297 value: function () {
1298 function closest(selector) {
1299 if (this.is(selector)) {
1300 return this;
1301 }
1302 var matchingAncestors = this.parents().filter(selector);
1303 return matchingAncestors.length > 0 ? matchingAncestors.first() : this.findWhere(function () {
1304 return false;
1305 });
1306 }
1307
1308 return closest;
1309 }()
1310
1311 /**
1312 * Shallow renders the current node and returns a shallow wrapper around it.
1313 *
1314 * NOTE: can only be called on wrapper of a single node.
1315 *
1316 * @param {Object} options
1317 * @returns {ShallowWrapper}
1318 */
1319
1320 }, {
1321 key: 'shallow',
1322 value: function () {
1323 function shallow(options) {
1324 var _this16 = this;
1325
1326 return this.single('shallow', function (n) {
1327 return _this16.wrap((0, _getAdapter2['default'])(_this16[OPTIONS]).nodeToElement(n), null, options);
1328 });
1329 }
1330
1331 return shallow;
1332 }()
1333
1334 /**
1335 * Returns the value of prop with the given name of the current node.
1336 *
1337 * @param propName
1338 * @returns {*}
1339 */
1340
1341 }, {
1342 key: 'prop',
1343 value: function () {
1344 function prop(propName) {
1345 return this.props()[propName];
1346 }
1347
1348 return prop;
1349 }()
1350
1351 /**
1352 * Returns the key assigned to the current node.
1353 *
1354 * @returns {String}
1355 */
1356
1357 }, {
1358 key: 'key',
1359 value: function () {
1360 function key() {
1361 return this.single('key', function (n) {
1362 return n.key === undefined ? null : n.key;
1363 });
1364 }
1365
1366 return key;
1367 }()
1368
1369 /**
1370 * Returns the type of the current node of this wrapper. If it's a composite component, this will
1371 * be the component constructor. If it's a native DOM node, it will be a string of the tag name.
1372 * If it's null, it will be null.
1373 *
1374 * @returns {String|Function|null}
1375 */
1376
1377 }, {
1378 key: 'type',
1379 value: function () {
1380 function type() {
1381 return this.single('type', function (n) {
1382 return (0, _Utils.typeOfNode)(n);
1383 });
1384 }
1385
1386 return type;
1387 }()
1388
1389 /**
1390 * Returns the name of the current node of this wrapper.
1391 *
1392 * In order of precedence => type.displayName -> type.name -> type.
1393 *
1394 * @returns {String}
1395 */
1396
1397 }, {
1398 key: 'name',
1399 value: function () {
1400 function name() {
1401 var adapter = (0, _getAdapter2['default'])(this[OPTIONS]);
1402 return this.single('name', function (n) {
1403 return adapter.displayNameOfNode ? adapter.displayNameOfNode(n) : (0, _Utils.displayNameOfNode)(n);
1404 });
1405 }
1406
1407 return name;
1408 }()
1409
1410 /**
1411 * Returns whether or not the current node has the given class name or not.
1412 *
1413 * NOTE: can only be called on a wrapper of a single node.
1414 *
1415 * @param className
1416 * @returns {Boolean}
1417 */
1418
1419 }, {
1420 key: 'hasClass',
1421 value: function () {
1422 function hasClass(className) {
1423 if (className && className.indexOf('.') !== -1) {
1424 // eslint-disable-next-line no-console
1425 console.warn('It looks like you\'re calling `ShallowWrapper::hasClass()` with a CSS selector. hasClass() expects a class name, not a CSS selector.');
1426 }
1427 return this.single('hasClass', function (n) {
1428 return (0, _RSTTraversal.hasClassName)(n, className);
1429 });
1430 }
1431
1432 return hasClass;
1433 }()
1434
1435 /**
1436 * Iterates through each node of the current wrapper and executes the provided function with a
1437 * wrapper around the corresponding node passed in as the first argument.
1438 *
1439 * @param {Function} fn
1440 * @returns {ShallowWrapper}
1441 */
1442
1443 }, {
1444 key: 'forEach',
1445 value: function () {
1446 function forEach(fn) {
1447 var _this17 = this;
1448
1449 this.getNodesInternal().forEach(function (n, i) {
1450 return fn.call(_this17, _this17.wrap(n), i);
1451 });
1452 return this;
1453 }
1454
1455 return forEach;
1456 }()
1457
1458 /**
1459 * Maps the current array of nodes to another array. Each node is passed in as a `ShallowWrapper`
1460 * to the map function.
1461 *
1462 * @param {Function} fn
1463 * @returns {Array}
1464 */
1465
1466 }, {
1467 key: 'map',
1468 value: function () {
1469 function map(fn) {
1470 var _this18 = this;
1471
1472 return this.getNodesInternal().map(function (n, i) {
1473 return fn.call(_this18, _this18.wrap(n), i);
1474 });
1475 }
1476
1477 return map;
1478 }()
1479
1480 /**
1481 * Reduces the current array of nodes to a value. Each node is passed in as a `ShallowWrapper`
1482 * to the reducer function.
1483 *
1484 * @param {Function} fn - the reducer function
1485 * @param {*} initialValue - the initial value
1486 * @returns {*}
1487 */
1488
1489 }, {
1490 key: 'reduce',
1491 value: function () {
1492 function reduce(fn) {
1493 var _this19 = this;
1494
1495 var initialValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined;
1496
1497 if (arguments.length > 1) {
1498 return this.getNodesInternal().reduce(function (accum, n, i) {
1499 return fn.call(_this19, accum, _this19.wrap(n), i);
1500 }, initialValue);
1501 }
1502 return this.getNodesInternal().reduce(function (accum, n, i) {
1503 return fn.call(_this19, i === 1 ? _this19.wrap(accum) : accum, _this19.wrap(n), i);
1504 });
1505 }
1506
1507 return reduce;
1508 }()
1509
1510 /**
1511 * Reduces the current array of nodes to another array, from right to left. Each node is passed
1512 * in as a `ShallowWrapper` to the reducer function.
1513 *
1514 * @param {Function} fn - the reducer function
1515 * @param {*} initialValue - the initial value
1516 * @returns {*}
1517 */
1518
1519 }, {
1520 key: 'reduceRight',
1521 value: function () {
1522 function reduceRight(fn) {
1523 var _this20 = this;
1524
1525 var initialValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined;
1526
1527 if (arguments.length > 1) {
1528 return this.getNodesInternal().reduceRight(function (accum, n, i) {
1529 return fn.call(_this20, accum, _this20.wrap(n), i);
1530 }, initialValue);
1531 }
1532 return this.getNodesInternal().reduceRight(function (accum, n, i) {
1533 return fn.call(_this20, i === 1 ? _this20.wrap(accum) : accum, _this20.wrap(n), i);
1534 });
1535 }
1536
1537 return reduceRight;
1538 }()
1539
1540 /**
1541 * Returns a new wrapper with a subset of the nodes of the original wrapper, according to the
1542 * rules of `Array#slice`.
1543 *
1544 * @param {Number} begin
1545 * @param {Number} end
1546 * @returns {ShallowWrapper}
1547 */
1548
1549 }, {
1550 key: 'slice',
1551 value: function () {
1552 function slice(begin, end) {
1553 return this.wrap(this.getNodesInternal().slice(begin, end));
1554 }
1555
1556 return slice;
1557 }()
1558
1559 /**
1560 * Returns whether or not any of the nodes in the wrapper match the provided selector.
1561 *
1562 * @param {Function|String} selector
1563 * @returns {Boolean}
1564 */
1565
1566 }, {
1567 key: 'some',
1568 value: function () {
1569 function some(selector) {
1570 if (this[ROOT] === this) {
1571 throw new Error('ShallowWrapper::some() can not be called on the root');
1572 }
1573 var predicate = (0, _selectors.buildPredicate)(selector);
1574 return this.getNodesInternal().some(predicate);
1575 }
1576
1577 return some;
1578 }()
1579
1580 /**
1581 * Returns whether or not any of the nodes in the wrapper pass the provided predicate function.
1582 *
1583 * @param {Function} predicate
1584 * @returns {Boolean}
1585 */
1586
1587 }, {
1588 key: 'someWhere',
1589 value: function () {
1590 function someWhere(predicate) {
1591 var _this21 = this;
1592
1593 return this.getNodesInternal().some(function (n, i) {
1594 return predicate.call(_this21, _this21.wrap(n), i);
1595 });
1596 }
1597
1598 return someWhere;
1599 }()
1600
1601 /**
1602 * Returns whether or not all of the nodes in the wrapper match the provided selector.
1603 *
1604 * @param {Function|String} selector
1605 * @returns {Boolean}
1606 */
1607
1608 }, {
1609 key: 'every',
1610 value: function () {
1611 function every(selector) {
1612 var predicate = (0, _selectors.buildPredicate)(selector);
1613 return this.getNodesInternal().every(predicate);
1614 }
1615
1616 return every;
1617 }()
1618
1619 /**
1620 * Returns whether or not any of the nodes in the wrapper pass the provided predicate function.
1621 *
1622 * @param {Function} predicate
1623 * @returns {Boolean}
1624 */
1625
1626 }, {
1627 key: 'everyWhere',
1628 value: function () {
1629 function everyWhere(predicate) {
1630 var _this22 = this;
1631
1632 return this.getNodesInternal().every(function (n, i) {
1633 return predicate.call(_this22, _this22.wrap(n), i);
1634 });
1635 }
1636
1637 return everyWhere;
1638 }()
1639
1640 /**
1641 * Utility method used to create new wrappers with a mapping function that returns an array of
1642 * nodes in response to a single node wrapper. The returned wrapper is a single wrapper around
1643 * all of the mapped nodes flattened (and de-duplicated).
1644 *
1645 * @param {Function} fn
1646 * @returns {ShallowWrapper}
1647 */
1648
1649 }, {
1650 key: 'flatMap',
1651 value: function () {
1652 function flatMap(fn) {
1653 var _this23 = this;
1654
1655 var nodes = this.getNodesInternal().map(function (n, i) {
1656 return fn.call(_this23, _this23.wrap(n), i);
1657 });
1658 var flattened = (0, _arrayPrototype2['default'])(nodes, 1);
1659 return this.wrap(flattened.filter(Boolean));
1660 }
1661
1662 return flatMap;
1663 }()
1664
1665 /**
1666 * Finds all nodes in the current wrapper nodes' render trees that match the provided predicate
1667 * function. The predicate function will receive the nodes inside a ShallowWrapper as its
1668 * first argument.
1669 *
1670 * @param {Function} predicate
1671 * @returns {ShallowWrapper}
1672 */
1673
1674 }, {
1675 key: 'findWhere',
1676 value: function () {
1677 function findWhere(predicate) {
1678 var _this24 = this;
1679
1680 return findWhereUnwrapped(this, function (n) {
1681 return predicate(_this24.wrap(n));
1682 });
1683 }
1684
1685 return findWhere;
1686 }()
1687
1688 /**
1689 * Returns the node at a given index of the current wrapper.
1690 *
1691 * @param index
1692 * @returns {ReactElement}
1693 */
1694
1695 }, {
1696 key: 'get',
1697 value: function () {
1698 function get(index) {
1699 return this.getElements()[index];
1700 }
1701
1702 return get;
1703 }()
1704
1705 /**
1706 * Returns a wrapper around the node at a given index of the current wrapper.
1707 *
1708 * @param index
1709 * @returns {ShallowWrapper}
1710 */
1711
1712 }, {
1713 key: 'at',
1714 value: function () {
1715 function at(index) {
1716 var nodes = this.getNodesInternal();
1717 if (index < nodes.length) {
1718 return this.wrap(nodes[index]);
1719 }
1720 return this.wrap([]);
1721 }
1722
1723 return at;
1724 }()
1725
1726 /**
1727 * Returns a wrapper around the first node of the current wrapper.
1728 *
1729 * @returns {ShallowWrapper}
1730 */
1731
1732 }, {
1733 key: 'first',
1734 value: function () {
1735 function first() {
1736 return this.at(0);
1737 }
1738
1739 return first;
1740 }()
1741
1742 /**
1743 * Returns a wrapper around the last node of the current wrapper.
1744 *
1745 * @returns {ShallowWrapper}
1746 */
1747
1748 }, {
1749 key: 'last',
1750 value: function () {
1751 function last() {
1752 return this.at(this.length - 1);
1753 }
1754
1755 return last;
1756 }()
1757
1758 /**
1759 * Delegates to exists()
1760 *
1761 * @returns {boolean}
1762 */
1763
1764 }, {
1765 key: 'isEmpty',
1766 value: function () {
1767 function isEmpty() {
1768 // eslint-disable-next-line no-console
1769 console.warn('Enzyme::Deprecated method isEmpty() called, use exists() instead.');
1770 return !this.exists();
1771 }
1772
1773 return isEmpty;
1774 }()
1775
1776 /**
1777 * Returns true if the current wrapper has nodes. False otherwise.
1778 * If called with a selector it returns `.find(selector).exists()` instead.
1779 *
1780 * @param {String|Function} selector (optional)
1781 * @returns {boolean}
1782 */
1783
1784 }, {
1785 key: 'exists',
1786 value: function () {
1787 function exists() {
1788 var selector = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
1789
1790 if (arguments.length > 0 && typeof selector !== 'string') {
1791 throw new TypeError('`selector` argument must be a string, if present.');
1792 }
1793 return typeof selector === 'string' ? this.find(selector).exists() : this.length > 0;
1794 }
1795
1796 return exists;
1797 }()
1798
1799 /**
1800 * Utility method that throws an error if the current instance has a length other than one.
1801 * This is primarily used to enforce that certain methods are only run on a wrapper when it is
1802 * wrapping a single node.
1803 *
1804 * @param fn
1805 * @returns {*}
1806 */
1807
1808 }, {
1809 key: 'single',
1810 value: function () {
1811 function single(name, fn) {
1812 var fnName = typeof name === 'string' ? name : 'unknown';
1813 var callback = typeof fn === 'function' ? fn : name;
1814 if (this.length !== 1) {
1815 throw new Error('Method \u201C' + fnName + '\u201D is only meant to be run on a single node. ' + String(this.length) + ' found instead.');
1816 }
1817 return callback.call(this, this.getNodeInternal());
1818 }
1819
1820 return single;
1821 }()
1822
1823 /**
1824 * Helpful utility method to create a new wrapper with the same root as the current wrapper, with
1825 * any nodes passed in as the first parameter automatically wrapped.
1826 *
1827 * @param node
1828 * @returns {ShallowWrapper}
1829 */
1830
1831 }, {
1832 key: 'wrap',
1833 value: function () {
1834 function wrap(node) {
1835 var root = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this[ROOT];
1836
1837 if (node instanceof ShallowWrapper) {
1838 return node;
1839 }
1840
1841 for (var _len2 = arguments.length, args = Array(_len2 > 2 ? _len2 - 2 : 0), _key2 = 2; _key2 < _len2; _key2++) {
1842 args[_key2 - 2] = arguments[_key2];
1843 }
1844
1845 return new (Function.prototype.bind.apply(ShallowWrapper, [null].concat([node, root], args)))();
1846 }
1847
1848 return wrap;
1849 }()
1850
1851 /**
1852 * Returns an HTML-like string of the shallow render for debugging purposes.
1853 *
1854 * @param {Object} [options] - Property bag of additional options.
1855 * @param {boolean} [options.ignoreProps] - if true, props are omitted from the string.
1856 * @param {boolean} [options.verbose] - if true, arrays and objects to be verbosely printed.
1857 * @returns {String}
1858 */
1859
1860 }, {
1861 key: 'debug',
1862 value: function () {
1863 function debug() {
1864 var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
1865
1866 return (0, _Debug.debugNodes)(this.getNodesInternal(), options);
1867 }
1868
1869 return debug;
1870 }()
1871
1872 /**
1873 * Invokes intercepter and returns itself. intercepter is called with itself.
1874 * This is helpful when debugging nodes in method chains.
1875 * @param fn
1876 * @returns {ShallowWrapper}
1877 */
1878
1879 }, {
1880 key: 'tap',
1881 value: function () {
1882 function tap(intercepter) {
1883 intercepter(this);
1884 return this;
1885 }
1886
1887 return tap;
1888 }()
1889
1890 /**
1891 * Primarily useful for HOCs (higher-order components), this method may only be
1892 * run on a single, non-DOM node, and will return the node, shallow-rendered.
1893 *
1894 * @param {Object} options
1895 * @returns {ShallowWrapper}
1896 */
1897
1898 }, {
1899 key: 'dive',
1900 value: function () {
1901 function dive() {
1902 var _this25 = this;
1903
1904 var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
1905
1906 var adapter = (0, _getAdapter2['default'])(this[OPTIONS]);
1907 var name = 'dive';
1908 return this.single(name, function (n) {
1909 if (n && n.nodeType === 'host') {
1910 throw new TypeError('ShallowWrapper::' + name + '() can not be called on Host Components');
1911 }
1912 var el = (0, _getAdapter2['default'])(_this25[OPTIONS]).nodeToElement(n);
1913 if (!(0, _Utils.isCustomComponentElement)(el, adapter)) {
1914 throw new TypeError('ShallowWrapper::' + name + '() can only be called on components');
1915 }
1916 return _this25.wrap(el, null, (0, _object2['default'])({}, _this25[OPTIONS], options));
1917 });
1918 }
1919
1920 return dive;
1921 }()
1922
1923 /**
1924 * Strips out all the not host-nodes from the list of nodes
1925 *
1926 * This method is useful if you want to check for the presence of host nodes
1927 * (actually rendered HTML elements) ignoring the React nodes.
1928 */
1929
1930 }, {
1931 key: 'hostNodes',
1932 value: function () {
1933 function hostNodes() {
1934 return this.filterWhere(function (n) {
1935 return typeof n.type() === 'string';
1936 });
1937 }
1938
1939 return hostNodes;
1940 }()
1941 }]);
1942
1943 return ShallowWrapper;
1944}();
1945
1946if (_Utils.ITERATOR_SYMBOL) {
1947 Object.defineProperty(ShallowWrapper.prototype, _Utils.ITERATOR_SYMBOL, {
1948 configurable: true,
1949 value: function () {
1950 function iterator() {
1951 var _ref2;
1952
1953 var iter = this.getNodesInternal()[_Utils.ITERATOR_SYMBOL]();
1954 var adapter = (0, _getAdapter2['default'])(this[OPTIONS]);
1955 return _ref2 = {}, _defineProperty(_ref2, _Utils.ITERATOR_SYMBOL, function () {
1956 return this;
1957 }), _defineProperty(_ref2, 'next', function () {
1958 function next() {
1959 var next = iter.next();
1960 if (next.done) {
1961 return { done: true };
1962 }
1963 return {
1964 done: false,
1965 value: adapter.nodeToElement(next.value)
1966 };
1967 }
1968
1969 return next;
1970 }()), _ref2;
1971 }
1972
1973 return iterator;
1974 }()
1975 });
1976}
1977
1978function privateWarning(prop, extraMessage) {
1979 Object.defineProperty(ShallowWrapper.prototype, prop, {
1980 get: function () {
1981 function get() {
1982 throw new Error('\n Attempted to access ShallowWrapper::' + String(prop) + ', which was previously a private property on\n Enzyme ShallowWrapper instances, but is no longer and should not be relied upon.\n ' + String(extraMessage) + '\n ');
1983 }
1984
1985 return get;
1986 }(),
1987
1988 enumerable: false,
1989 configurable: false
1990 });
1991}
1992
1993privateWarning('node', 'Consider using the getElement() method instead.');
1994privateWarning('nodes', 'Consider using the getElements() method instead.');
1995privateWarning('renderer', '');
1996privateWarning('options', '');
1997privateWarning('complexSelector', '');
1998
1999exports['default'] = ShallowWrapper;
\No newline at end of file