1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | var _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 |
|
9 | var _object = require('object.assign');
|
10 |
|
11 | var _object2 = _interopRequireDefault(_object);
|
12 |
|
13 | var _arrayPrototype = require('array.prototype.flat');
|
14 |
|
15 | var _arrayPrototype2 = _interopRequireDefault(_arrayPrototype);
|
16 |
|
17 | var _lodash = require('lodash.isequal');
|
18 |
|
19 | var _lodash2 = _interopRequireDefault(_lodash);
|
20 |
|
21 | var _cheerio = require('cheerio');
|
22 |
|
23 | var _cheerio2 = _interopRequireDefault(_cheerio);
|
24 |
|
25 | var _Utils = require('./Utils');
|
26 |
|
27 | var _getAdapter = require('./getAdapter');
|
28 |
|
29 | var _getAdapter2 = _interopRequireDefault(_getAdapter);
|
30 |
|
31 | var _Debug = require('./Debug');
|
32 |
|
33 | var _RSTTraversal = require('./RSTTraversal');
|
34 |
|
35 | var _selectors = require('./selectors');
|
36 |
|
37 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
38 |
|
39 | function _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 |
|
41 | function _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 |
|
43 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
44 |
|
45 | var NODE = (0, _Utils.sym)('__node__');
|
46 | var NODES = (0, _Utils.sym)('__nodes__');
|
47 | var RENDERER = (0, _Utils.sym)('__renderer__');
|
48 | var UNRENDERED = (0, _Utils.sym)('__unrendered__');
|
49 | var ROOT = (0, _Utils.sym)('__root__');
|
50 | var OPTIONS = (0, _Utils.sym)('__options__');
|
51 | var SET_STATE = (0, _Utils.sym)('__setState__');
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | function 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 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 | function filterWhereUnwrapped(wrapper, predicate) {
|
78 | return wrapper.wrap(wrapper.getNodesInternal().filter(predicate).filter(Boolean));
|
79 | }
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 | function 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 |
|
113 | function 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 |
|
134 | function getRootNode(node) {
|
135 | if (node.nodeType === 'host') {
|
136 | return node;
|
137 | }
|
138 | return node.rendered;
|
139 | }
|
140 |
|
141 | function getRootNodeInternal(wrapper) {
|
142 | return wrapper[ROOT][NODE];
|
143 | }
|
144 |
|
145 | function nodeParents(wrapper, node) {
|
146 | return (0, _RSTTraversal.parentsOfNode)(node, getRootNodeInternal(wrapper));
|
147 | }
|
148 |
|
149 | function 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 |
|
160 | function pureComponentShouldComponentUpdate(prevProps, props, prevState, state) {
|
161 | return !(0, _lodash2['default'])(prevProps, props) || !(0, _lodash2['default'])(prevState, state);
|
162 | }
|
163 |
|
164 | function isPureComponent(instance) {
|
165 | return instance && instance.isPureReactComponent;
|
166 | }
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 | var 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 |
|
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 |
|
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 |
|
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 |
|
231 |
|
232 |
|
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 |
|
248 |
|
249 |
|
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 |
|
270 |
|
271 |
|
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 |
|
289 |
|
290 |
|
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 |
|
308 |
|
309 |
|
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 |
|
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 |
|
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 |
|
348 |
|
349 |
|
350 |
|
351 |
|
352 |
|
353 |
|
354 |
|
355 |
|
356 |
|
357 |
|
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 |
|
375 |
|
376 |
|
377 |
|
378 |
|
379 |
|
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 |
|
401 |
|
402 |
|
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 |
|
418 |
|
419 |
|
420 |
|
421 |
|
422 |
|
423 |
|
424 |
|
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 |
|
437 |
|
438 |
|
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 |
|
450 |
|
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 |
|
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 |
|
498 |
|
499 |
|
500 |
|
501 |
|
502 |
|
503 |
|
504 |
|
505 |
|
506 |
|
507 |
|
508 |
|
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 |
|
535 |
|
536 |
|
537 |
|
538 |
|
539 |
|
540 |
|
541 |
|
542 |
|
543 |
|
544 |
|
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 |
|
579 |
|
580 | var maybeHasUpdate = !lifecycles.setState.skipsComponentDidUpdateOnNullish || statePayload != null;
|
581 |
|
582 |
|
583 |
|
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 |
|
592 |
|
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 |
|
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 |
|
635 |
|
636 |
|
637 |
|
638 |
|
639 |
|
640 |
|
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 |
|
661 |
|
662 |
|
663 |
|
664 |
|
665 |
|
666 |
|
667 |
|
668 |
|
669 |
|
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 |
|
696 |
|
697 |
|
698 |
|
699 |
|
700 |
|
701 |
|
702 |
|
703 |
|
704 |
|
705 |
|
706 |
|
707 |
|
708 |
|
709 |
|
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 |
|
735 |
|
736 |
|
737 |
|
738 |
|
739 |
|
740 |
|
741 |
|
742 |
|
743 |
|
744 |
|
745 |
|
746 |
|
747 |
|
748 |
|
749 |
|
750 |
|
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 |
|
773 |
|
774 |
|
775 |
|
776 |
|
777 |
|
778 |
|
779 |
|
780 |
|
781 |
|
782 |
|
783 |
|
784 |
|
785 |
|
786 |
|
787 |
|
788 |
|
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 |
|
807 |
|
808 |
|
809 |
|
810 |
|
811 |
|
812 |
|
813 |
|
814 |
|
815 |
|
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 |
|
834 |
|
835 |
|
836 |
|
837 |
|
838 |
|
839 |
|
840 |
|
841 |
|
842 |
|
843 |
|
844 |
|
845 |
|
846 |
|
847 |
|
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 |
|
870 |
|
871 |
|
872 |
|
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 |
|
887 |
|
888 |
|
889 |
|
890 |
|
891 |
|
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 |
|
909 |
|
910 |
|
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 |
|
925 |
|
926 |
|
927 |
|
928 |
|
929 |
|
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 |
|
948 |
|
949 |
|
950 |
|
951 |
|
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 |
|
967 |
|
968 |
|
969 |
|
970 |
|
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 |
|
988 |
|
989 |
|
990 |
|
991 |
|
992 |
|
993 |
|
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 |
|
1008 |
|
1009 |
|
1010 |
|
1011 |
|
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 |
|
1033 |
|
1034 |
|
1035 |
|
1036 |
|
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 |
|
1051 |
|
1052 |
|
1053 |
|
1054 |
|
1055 |
|
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 |
|
1082 |
|
1083 |
|
1084 |
|
1085 |
|
1086 |
|
1087 | }, {
|
1088 | key: 'simulateError',
|
1089 | value: function () {
|
1090 | function simulateError(error) {
|
1091 | var _this11 = this;
|
1092 |
|
1093 |
|
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 |
|
1118 |
|
1119 |
|
1120 |
|
1121 |
|
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 |
|
1136 |
|
1137 |
|
1138 |
|
1139 |
|
1140 |
|
1141 |
|
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 |
|
1170 |
|
1171 |
|
1172 |
|
1173 |
|
1174 |
|
1175 |
|
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 |
|
1207 |
|
1208 |
|
1209 |
|
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 |
|
1227 |
|
1228 |
|
1229 |
|
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 |
|
1248 |
|
1249 |
|
1250 |
|
1251 |
|
1252 |
|
1253 |
|
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 |
|
1273 |
|
1274 |
|
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 |
|
1292 |
|
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 |
|
1313 |
|
1314 |
|
1315 |
|
1316 |
|
1317 |
|
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 |
|
1336 |
|
1337 |
|
1338 |
|
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 |
|
1353 |
|
1354 |
|
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 |
|
1371 |
|
1372 |
|
1373 |
|
1374 |
|
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 |
|
1391 |
|
1392 |
|
1393 |
|
1394 |
|
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 |
|
1412 |
|
1413 |
|
1414 |
|
1415 |
|
1416 |
|
1417 |
|
1418 |
|
1419 | }, {
|
1420 | key: 'hasClass',
|
1421 | value: function () {
|
1422 | function hasClass(className) {
|
1423 | if (className && className.indexOf('.') !== -1) {
|
1424 |
|
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 |
|
1437 |
|
1438 |
|
1439 |
|
1440 |
|
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 |
|
1460 |
|
1461 |
|
1462 |
|
1463 |
|
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 |
|
1482 |
|
1483 |
|
1484 |
|
1485 |
|
1486 |
|
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 |
|
1512 |
|
1513 |
|
1514 |
|
1515 |
|
1516 |
|
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 |
|
1542 |
|
1543 |
|
1544 |
|
1545 |
|
1546 |
|
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 |
|
1561 |
|
1562 |
|
1563 |
|
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 |
|
1582 |
|
1583 |
|
1584 |
|
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 |
|
1603 |
|
1604 |
|
1605 |
|
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 |
|
1621 |
|
1622 |
|
1623 |
|
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 |
|
1642 |
|
1643 |
|
1644 |
|
1645 |
|
1646 |
|
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 |
|
1667 |
|
1668 |
|
1669 |
|
1670 |
|
1671 |
|
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 |
|
1690 |
|
1691 |
|
1692 |
|
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 |
|
1728 |
|
1729 |
|
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 |
|
1744 |
|
1745 |
|
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 |
|
1760 |
|
1761 |
|
1762 |
|
1763 |
|
1764 | }, {
|
1765 | key: 'isEmpty',
|
1766 | value: function () {
|
1767 | function isEmpty() {
|
1768 |
|
1769 | console.warn('Enzyme::Deprecated method isEmpty() called, use exists() instead.');
|
1770 | return !this.exists();
|
1771 | }
|
1772 |
|
1773 | return isEmpty;
|
1774 | }()
|
1775 |
|
1776 | |
1777 |
|
1778 |
|
1779 |
|
1780 |
|
1781 |
|
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 |
|
1801 |
|
1802 |
|
1803 |
|
1804 |
|
1805 |
|
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 |
|
1825 |
|
1826 |
|
1827 |
|
1828 |
|
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 |
|
1853 |
|
1854 |
|
1855 |
|
1856 |
|
1857 |
|
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 |
|
1874 |
|
1875 |
|
1876 |
|
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 |
|
1892 |
|
1893 |
|
1894 |
|
1895 |
|
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 |
|
1925 |
|
1926 |
|
1927 |
|
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 |
|
1946 | if (_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 |
|
1978 | function 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 |
|
1993 | privateWarning('node', 'Consider using the getElement() method instead.');
|
1994 | privateWarning('nodes', 'Consider using the getElements() method instead.');
|
1995 | privateWarning('renderer', '');
|
1996 | privateWarning('options', '');
|
1997 | privateWarning('complexSelector', '');
|
1998 |
|
1999 | exports['default'] = ShallowWrapper; |
\ | No newline at end of file |