-
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
ShallowWrapper.js
1101 lines (1024 loc) · 31.5 KB
/
ShallowWrapper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import React from 'react';
import flatten from 'lodash/flatten';
import unique from 'lodash/uniq';
import compact from 'lodash/compact';
import cheerio from 'cheerio';
import ComplexSelector from './ComplexSelector';
import {
nodeEqual,
nodeMatches,
containsChildrenSubArray,
propFromEvent,
withSetStateAllowed,
propsOfNode,
typeOfNode,
isReactElementAlike,
displayNameOfNode,
isFunctionalComponent,
isCustomComponentElement,
ITERATOR_SYMBOL,
} from './Utils';
import {
debugNodes,
} from './Debug';
import {
getTextFromNode,
hasClassName,
childrenOfNode,
parentsOfNode,
treeFilter,
buildPredicate,
} from './ShallowTraversal';
import {
createShallowRenderer,
renderToStaticMarkup,
batchedUpdates,
isDOMComponentElement,
} from './react-compat';
import { REACT155 } from './version';
/**
* Finds all nodes in the current wrapper nodes' render trees that match the provided predicate
* function.
*
* @param {ShallowWrapper} wrapper
* @param {Function} predicate
* @param {Function} filter
* @returns {ShallowWrapper}
*/
function findWhereUnwrapped(wrapper, predicate, filter = treeFilter) {
return wrapper.flatMap(n => filter(n.getNode(), predicate));
}
/**
* Returns a new wrapper instance with only the nodes of the current wrapper instance that match
* the provided predicate function.
*
* @param {ShallowWrapper} wrapper
* @param {Function} predicate
* @returns {ShallowWrapper}
*/
function filterWhereUnwrapped(wrapper, predicate) {
return wrapper.wrap(compact(wrapper.getNodes().filter(predicate)));
}
/**
* Ensure options passed to ShallowWrapper are valid. Throws otherwise.
* @param {Object} options
*/
function validateOptions(options) {
const { lifecycleExperimental, disableLifecycleMethods } = options;
if (
typeof lifecycleExperimental !== 'undefined' &&
typeof lifecycleExperimental !== 'boolean'
) {
throw new Error(
'lifecycleExperimental must be either true or false if provided',
);
}
if (
typeof disableLifecycleMethods !== 'undefined' &&
typeof disableLifecycleMethods !== 'boolean'
) {
throw new Error(
'disableLifecycleMethods must be either true or false if provided',
);
}
if (
lifecycleExperimental != null &&
disableLifecycleMethods != null &&
lifecycleExperimental === disableLifecycleMethods
) {
throw new Error(
'lifecycleExperimental and disableLifecycleMethods cannot be set to the same value',
);
}
}
function performBatchedUpdates(wrapper, fn) {
const renderer = wrapper.root.renderer;
if (REACT155 && renderer.unstable_batchedUpdates) {
// React 15.5+ exposes batching on shallow renderer itself
return renderer.unstable_batchedUpdates(fn);
}
// React <15.5: Fallback to ReactDOM
return batchedUpdates(fn);
}
/**
* @class ShallowWrapper
*/
class ShallowWrapper {
constructor(nodes, root, options = {}) {
validateOptions(options);
if (!root) {
this.root = this;
this.unrendered = nodes;
this.renderer = createShallowRenderer();
withSetStateAllowed(() => {
performBatchedUpdates(this, () => {
this.renderer.render(nodes, options.context);
const instance = this.instance();
if (
options.lifecycleExperimental &&
instance &&
typeof instance.componentDidMount === 'function'
) {
instance.componentDidMount();
}
});
});
this.node = this.renderer.getRenderOutput();
this.nodes = [this.node];
this.length = 1;
} else {
this.root = root;
this.unrendered = null;
this.renderer = null;
if (!Array.isArray(nodes)) {
this.node = nodes;
this.nodes = [nodes];
} else {
this.node = nodes[0];
this.nodes = nodes;
}
this.length = this.nodes.length;
}
this.options = options;
this.complexSelector = new ComplexSelector(buildPredicate, findWhereUnwrapped, childrenOfNode);
}
/**
* Returns the wrapped ReactElement.
*
* @return {ReactElement}
*/
getNode() {
if (this.length !== 1) {
throw new Error(
'ShallowWrapper::getNode() can only be called when wrapping one node',
);
}
return this.root === this ? this.renderer.getRenderOutput() : this.node;
}
/**
* Returns the wrapped ReactElements.
*
* @return {Array<ReactElement>}
*/
getNodes() {
return this.root === this ? [this.renderer.getRenderOutput()] : this.nodes;
}
/**
* Gets the instance of the component being rendered as the root node passed into `shallow()`.
*
* NOTE: can only be called on a wrapper instance that is also the root instance.
*
* Example:
* ```
* const wrapper = shallow(<MyComponent />);
* const inst = wrapper.instance();
* expect(inst).to.be.instanceOf(MyComponent);
* ```
* @returns {ReactComponent}
*/
instance() {
if (this.root !== this) {
throw new Error('ShallowWrapper::instance() can only be called on the root');
}
return this.renderer._instance ? this.renderer._instance._instance : null;
}
/**
* Forces a re-render. Useful to run before checking the render output if something external
* may be updating the state of the component somewhere.
*
* NOTE: can only be called on a wrapper instance that is also the root instance.
*
* @returns {ShallowWrapper}
*/
update() {
if (this.root !== this) {
throw new Error('ShallowWrapper::update() can only be called on the root');
}
this.single('update', () => {
this.node = this.renderer.getRenderOutput();
this.nodes = [this.node];
});
return this;
}
/**
* A method is for re-render with new props and context.
* This calls componentDidUpdate method if lifecycleExperimental is enabled.
*
* NOTE: can only be called on a wrapper instance that is also the root instance.
*
* @param {Object} props
* @param {Object} context
* @returns {ShallowWrapper}
*/
rerender(props, context) {
this.single('rerender', () => {
withSetStateAllowed(() => {
const instance = this.instance();
const state = instance.state;
const prevProps = instance.props;
const prevContext = instance.context;
const nextProps = props || prevProps;
const nextContext = context || prevContext;
performBatchedUpdates(this, () => {
let shouldRender = true;
// dirty hack:
// make sure that componentWillReceiveProps is called before shouldComponentUpdate
let originalComponentWillReceiveProps;
if (
this.options.lifecycleExperimental &&
instance &&
typeof instance.componentWillReceiveProps === 'function'
) {
instance.componentWillReceiveProps(nextProps, nextContext);
originalComponentWillReceiveProps = instance.componentWillReceiveProps;
instance.componentWillReceiveProps = () => {};
}
// dirty hack: avoid calling shouldComponentUpdate twice
let originalShouldComponentUpdate;
if (
this.options.lifecycleExperimental &&
instance &&
typeof instance.shouldComponentUpdate === 'function'
) {
shouldRender = instance.shouldComponentUpdate(nextProps, state, nextContext);
originalShouldComponentUpdate = instance.shouldComponentUpdate;
}
if (shouldRender) {
if (props) this.unrendered = React.cloneElement(this.unrendered, props);
if (originalShouldComponentUpdate) {
instance.shouldComponentUpdate = () => true;
}
this.renderer.render(this.unrendered, nextContext);
if (originalShouldComponentUpdate) {
instance.shouldComponentUpdate = originalShouldComponentUpdate;
}
if (
this.options.lifecycleExperimental &&
instance &&
typeof instance.componentDidUpdate === 'function'
) {
instance.componentDidUpdate(prevProps, state, prevContext);
}
this.update();
// If it doesn't need to rerender, update only its props.
} else if (props) {
instance.props = props;
}
if (originalComponentWillReceiveProps) {
instance.componentWillReceiveProps = originalComponentWillReceiveProps;
}
});
});
});
return this;
}
/**
* A method that sets the props of the root component, and re-renders. Useful for when you are
* wanting to test how the component behaves over time with changing props. Calling this, for
* instance, will call the `componentWillReceiveProps` lifecycle method.
*
* Similar to `setState`, this method accepts a props object and will merge it in with the already
* existing props.
*
* NOTE: can only be called on a wrapper instance that is also the root instance.
*
* @param {Object} props object
* @returns {ShallowWrapper}
*/
setProps(props) {
if (this.root !== this) {
throw new Error('ShallowWrapper::setProps() can only be called on the root');
}
return this.rerender(props);
}
/**
* A method to invoke `setState` on the root component instance similar to how you might in the
* definition of the component, and re-renders. This method is useful for testing your component
* in hard to achieve states, however should be used sparingly. If possible, you should utilize
* your component's external API in order to get it into whatever state you want to test, in order
* to be as accurate of a test as possible. This is not always practical, however.
*
* NOTE: can only be called on a wrapper instance that is also the root instance.
*
* @param {Object} state to merge
* @param {Function} cb - callback function
* @returns {ShallowWrapper}
*/
setState(state, callback = undefined) {
if (this.root !== this) {
throw new Error('ShallowWrapper::setState() can only be called on the root');
}
if (isFunctionalComponent(this.instance())) {
throw new Error('ShallowWrapper::setState() can only be called on class components');
}
this.single('setState', () => {
withSetStateAllowed(() => {
this.instance().setState(state, callback);
this.update();
});
});
return this;
}
/**
* A method that sets the context of the root component, and re-renders. Useful for when you are
* wanting to test how the component behaves over time with changing contexts.
*
* NOTE: can only be called on a wrapper instance that is also the root instance.
*
* @param {Object} context object
* @returns {ShallowWrapper}
*/
setContext(context) {
if (this.root !== this) {
throw new Error('ShallowWrapper::setContext() can only be called on the root');
}
if (!this.options.context) {
throw new Error(
'ShallowWrapper::setContext() can only be called on a wrapper that was originally passed ' +
'a context option',
);
}
return this.rerender(null, context);
}
/**
* Whether or not a given react element exists in the shallow render tree.
*
* Example:
* ```
* const wrapper = shallow(<MyComponent />);
* expect(wrapper.contains(<div className="foo bar" />)).to.equal(true);
* ```
*
* @param {ReactElement|Array<ReactElement>} nodeOrNodes
* @returns {Boolean}
*/
contains(nodeOrNodes) {
if (!isReactElementAlike(nodeOrNodes)) {
throw new Error(
'ShallowWrapper::contains() can only be called with ReactElement (or array of them), ' +
'string or number as argument.',
);
}
const predicate = Array.isArray(nodeOrNodes)
? other => containsChildrenSubArray(nodeEqual, other, nodeOrNodes)
: other => nodeEqual(nodeOrNodes, other);
return findWhereUnwrapped(this, predicate).length > 0;
}
/**
* Whether or not a given react element exists in the shallow render tree.
* Match is based on the expected element and not on wrappers element.
* It will determine if one of the wrappers element "looks like" the expected
* element by checking if all props of the expected element are present
* on the wrappers element and equals to each other.
*
* Example:
* ```
* // MyComponent outputs <div><div class="foo">Hello</div></div>
* const wrapper = shallow(<MyComponent />);
* expect(wrapper.containsMatchingElement(<div>Hello</div>)).to.equal(true);
* ```
*
* @param {ReactElement} node
* @returns {Boolean}
*/
containsMatchingElement(node) {
const predicate = other => nodeMatches(node, other, (a, b) => a <= b);
return findWhereUnwrapped(this, predicate).length > 0;
}
/**
* Whether or not all the given react elements exists in the shallow render tree.
* Match is based on the expected element and not on wrappers element.
* It will determine if one of the wrappers element "looks like" the expected
* element by checking if all props of the expected element are present
* on the wrappers element and equals to each other.
*
* Example:
* ```
* const wrapper = shallow(<MyComponent />);
* expect(wrapper.containsAllMatchingElements([
* <div>Hello</div>,
* <div>Goodbye</div>,
* ])).to.equal(true);
* ```
*
* @param {Array<ReactElement>} nodes
* @returns {Boolean}
*/
containsAllMatchingElements(nodes) {
const invertedEquals = (n1, n2) => nodeMatches(n2, n1, (a, b) => a <= b);
const predicate = other => containsChildrenSubArray(invertedEquals, other, nodes);
return findWhereUnwrapped(this, predicate).length > 0;
}
/**
* Whether or not one of the given react elements exists in the shallow render tree.
* Match is based on the expected element and not on wrappers element.
* It will determine if one of the wrappers element "looks like" the expected
* element by checking if all props of the expected element are present
* on the wrappers element and equals to each other.
*
* Example:
* ```
* const wrapper = shallow(<MyComponent />);
* expect(wrapper.containsAnyMatchingElements([
* <div>Hello</div>,
* <div>Goodbye</div>,
* ])).to.equal(true);
* ```
*
* @param {Array<ReactElement>} nodes
* @returns {Boolean}
*/
containsAnyMatchingElements(nodes) {
return Array.isArray(nodes) && nodes.some(node => this.containsMatchingElement(node));
}
/**
* Whether or not a given react element exists in the shallow render tree.
*
* Example:
* ```
* const wrapper = shallow(<MyComponent />);
* expect(wrapper.contains(<div className="foo bar" />)).to.equal(true);
* ```
*
* @param {ReactElement} node
* @returns {Boolean}
*/
equals(node) {
return this.single('equals', () => nodeEqual(this.getNode(), node));
}
/**
* Whether or not a given react element matches the shallow render tree.
* Match is based on the expected element and not on wrapper root node.
* It will determine if the wrapper root node "looks like" the expected
* element by checking if all props of the expected element are present
* on the wrapper root node and equals to each other.
*
* Example:
* ```
* // MyComponent outputs <div class="foo">Hello</div>
* const wrapper = shallow(<MyComponent />);
* expect(wrapper.matchesElement(<div>Hello</div>)).to.equal(true);
* ```
*
* @param {ReactElement} node
* @returns {Boolean}
*/
matchesElement(node) {
return this.single('matchesElement', () => nodeMatches(node, this.getNode(), (a, b) => a <= b));
}
/**
* Finds every node in the render tree of the current wrapper that matches the provided selector.
*
* @param {String|Function} selector
* @returns {ShallowWrapper}
*/
find(selector) {
return this.complexSelector.find(selector, this);
}
/**
* Returns whether or not current node matches a provided selector.
*
* NOTE: can only be called on a wrapper of a single node.
*
* @param {String|Function} selector
* @returns {boolean}
*/
is(selector) {
const predicate = buildPredicate(selector);
return this.single('is', n => predicate(n));
}
/**
* Returns true if the component rendered nothing, i.e., null or false.
*
* @returns {boolean}
*/
isEmptyRender() {
return this.type() === null;
}
/**
* Returns a new wrapper instance with only the nodes of the current wrapper instance that match
* the provided predicate function. The predicate should receive a wrapped node as its first
* argument.
*
* @param {Function} predicate
* @returns {ShallowWrapper}
*/
filterWhere(predicate) {
return filterWhereUnwrapped(this, n => predicate(this.wrap(n)));
}
/**
* Returns a new wrapper instance with only the nodes of the current wrapper instance that match
* the provided selector.
*
* @param {String|Function} selector
* @returns {ShallowWrapper}
*/
filter(selector) {
const predicate = buildPredicate(selector);
return filterWhereUnwrapped(this, predicate);
}
/**
* Returns a new wrapper instance with only the nodes of the current wrapper that did not match
* the provided selector. Essentially the inverse of `filter`.
*
* @param {String|Function} selector
* @returns {ShallowWrapper}
*/
not(selector) {
const predicate = buildPredicate(selector);
return filterWhereUnwrapped(this, n => !predicate(n));
}
/**
* Returns a string of the rendered text of the current render tree. This function should be
* looked at with skepticism if being used to test what the actual HTML output of the component
* will be. If that is what you would like to test, use enzyme's `render` function instead.
*
* NOTE: can only be called on a wrapper of a single node.
*
* @returns {String}
*/
text() {
return this.single('text', getTextFromNode);
}
/**
* Returns the HTML of the node.
*
* NOTE: can only be called on a wrapper of a single node.
*
* @returns {String}
*/
html() {
return this.single('html', n => (this.type() === null ? null : renderToStaticMarkup(n)));
}
/**
* Returns the current node rendered to HTML and wrapped in a CheerioWrapper.
*
* NOTE: can only be called on a wrapper of a single node.
*
* @returns {CheerioWrapper}
*/
render() {
return this.type() === null ? cheerio() : cheerio.load(this.html()).root();
}
/**
* A method that unmounts the component. This can be used to simulate a component going through
* and unmount/mount lifecycle.
* @returns {ShallowWrapper}
*/
unmount() {
this.renderer.unmount();
return this;
}
/**
* Used to simulate events. Pass an eventname and (optionally) event arguments. This method of
* testing events should be met with some skepticism.
*
* @param {String} event
* @param {Array} args
* @returns {ShallowWrapper}
*/
simulate(event, ...args) {
const handler = this.prop(propFromEvent(event));
if (handler) {
withSetStateAllowed(() => {
// TODO(lmr): create/use synthetic events
// TODO(lmr): emulate React's event propagation
performBatchedUpdates(this, () => {
handler(...args);
});
this.root.update();
});
}
return this;
}
/**
* Returns the props hash for the current node of the wrapper.
*
* NOTE: can only be called on a wrapper of a single node.
*
* @returns {Object}
*/
props() {
return this.single('props', propsOfNode);
}
/**
* Returns the state hash for the root node of the wrapper. Optionally pass in a prop name and it
* will return just that value.
*
* NOTE: can only be called on a wrapper of a single node.
*
* @param {String} name (optional)
* @returns {*}
*/
state(name) {
if (this.root !== this) {
throw new Error('ShallowWrapper::state() can only be called on the root');
}
if (isFunctionalComponent(this.instance())) {
throw new Error('ShallowWrapper::state() can only be called on class components');
}
const _state = this.single('state', () => this.instance().state);
if (name !== undefined) {
return _state[name];
}
return _state;
}
/**
* Returns the context hash for the root node of the wrapper.
* Optionally pass in a prop name and it will return just that value.
*
* NOTE: can only be called on a wrapper of a single node.
*
* @param {String} name (optional)
* @returns {*}
*/
context(name) {
if (this.root !== this) {
throw new Error('ShallowWrapper::context() can only be called on the root');
}
if (!this.options.context) {
throw new Error(
'ShallowWrapper::context() can only be called on a wrapper that was originally passed ' +
'a context option',
);
}
const _context = this.single('context', () => this.instance().context);
if (name) {
return _context[name];
}
return _context;
}
/**
* Returns a new wrapper with all of the children of the current wrapper.
*
* @param {String|Function} [selector]
* @returns {ShallowWrapper}
*/
children(selector) {
const allChildren = this.flatMap(n => childrenOfNode(n.getNode()));
return selector ? allChildren.filter(selector) : allChildren;
}
/**
* Returns a new wrapper with a specific child
*
* @param {Number} [index]
* @returns {ShallowWrapper}
*/
childAt(index) {
return this.single('childAt', () => this.children().at(index));
}
/**
* Returns a wrapper around all of the parents/ancestors of the wrapper. Does not include the node
* in the current wrapper.
*
* NOTE: can only be called on a wrapper of a single node.
*
* @param {String|Function} [selector]
* @returns {ShallowWrapper}
*/
parents(selector) {
const allParents = this.wrap(
this.single('parents', n => parentsOfNode(n, this.root.getNode())),
);
return selector ? allParents.filter(selector) : allParents;
}
/**
* Returns a wrapper around the immediate parent of the current node.
*
* @returns {ShallowWrapper}
*/
parent() {
return this.flatMap(n => [n.parents().get(0)]);
}
/**
*
* @param {String|Function} selector
* @returns {ShallowWrapper}
*/
closest(selector) {
return this.is(selector) ? this : this.parents().filter(selector).first();
}
/**
* Shallow renders the current node and returns a shallow wrapper around it.
*
* NOTE: can only be called on wrapper of a single node.
*
* @param {Object} options
* @returns {ShallowWrapper}
*/
shallow(options) {
return this.single('shallow', n => this.wrap(n, null, options));
}
/**
* Returns the value of prop with the given name of the current node.
*
* @param propName
* @returns {*}
*/
prop(propName) {
return this.props()[propName];
}
/**
* Returns the key assigned to the current node.
*
* @returns {String}
*/
key() {
return this.single('key', n => n.key);
}
/**
* Returns the type of the current node of this wrapper. If it's a composite component, this will
* be the component constructor. If it's a native DOM node, it will be a string.
*
* @returns {String|Function}
*/
type() {
return this.single('type', typeOfNode);
}
/**
* Returns the name of the current node of this wrapper.
*
* In order of precedence => type.displayName -> type.name -> type.
*
* @returns {String}
*/
name() {
return this.single('name', displayNameOfNode);
}
/**
* Returns whether or not the current node has the given class name or not.
*
* NOTE: can only be called on a wrapper of a single node.
*
* @param className
* @returns {Boolean}
*/
hasClass(className) {
if (className && className.indexOf('.') !== -1) {
// eslint-disable-next-line no-console
console.warn(
'It looks like you\'re calling `ShallowWrapper::hasClass()` with a CSS selector. ' +
'hasClass() expects a class name, not a CSS selector.',
);
}
return this.single('hasClass', n => hasClassName(n, className));
}
/**
* Iterates through each node of the current wrapper and executes the provided function with a
* wrapper around the corresponding node passed in as the first argument.
*
* @param {Function} fn
* @returns {ShallowWrapper}
*/
forEach(fn) {
this.getNodes().forEach((n, i) => fn.call(this, this.wrap(n), i));
return this;
}
/**
* Maps the current array of nodes to another array. Each node is passed in as a `ShallowWrapper`
* to the map function.
*
* @param {Function} fn
* @returns {Array}
*/
map(fn) {
return this.getNodes().map((n, i) => fn.call(this, this.wrap(n), i));
}
/**
* Reduces the current array of nodes to a value. Each node is passed in as a `ShallowWrapper`
* to the reducer function.
*
* @param {Function} fn - the reducer function
* @param {*} initialValue - the initial value
* @returns {*}
*/
reduce(fn, initialValue) {
return this.getNodes().reduce(
(accum, n, i) => fn.call(this, accum, this.wrap(n), i),
initialValue,
);
}
/**
* Reduces the current array of nodes to another array, from right to left. Each node is passed
* in as a `ShallowWrapper` to the reducer function.
*
* @param {Function} fn - the reducer function
* @param {*} initialValue - the initial value
* @returns {*}
*/
reduceRight(fn, initialValue) {
return this.getNodes().reduceRight(
(accum, n, i) => fn.call(this, accum, this.wrap(n), i),
initialValue,
);
}
/**
* Returns a new wrapper with a subset of the nodes of the original wrapper, according to the
* rules of `Array#slice`.
*
* @param {Number} begin
* @param {Number} end
* @returns {ShallowWrapper}
*/
slice(begin, end) {
return this.wrap(this.getNodes().slice(begin, end));
}
/**
* Returns whether or not any of the nodes in the wrapper match the provided selector.
*
* @param {Function|String} selector
* @returns {Boolean}
*/
some(selector) {
if (this.root === this) {
throw new Error('ShallowWrapper::some() can not be called on the root');
}
const predicate = buildPredicate(selector);
return this.getNodes().some(predicate);
}
/**
* Returns whether or not any of the nodes in the wrapper pass the provided predicate function.
*
* @param {Function} predicate
* @returns {Boolean}
*/
someWhere(predicate) {
return this.getNodes().some((n, i) => predicate.call(this, this.wrap(n), i));
}
/**
* Returns whether or not all of the nodes in the wrapper match the provided selector.
*
* @param {Function|String} selector
* @returns {Boolean}
*/
every(selector) {
const predicate = buildPredicate(selector);
return this.getNodes().every(predicate);
}
/**
* Returns whether or not any of the nodes in the wrapper pass the provided predicate function.
*
* @param {Function} predicate
* @returns {Boolean}
*/
everyWhere(predicate) {
return this.getNodes().every((n, i) => predicate.call(this, this.wrap(n), i));
}
/**
* Utility method used to create new wrappers with a mapping function that returns an array of
* nodes in response to a single node wrapper. The returned wrapper is a single wrapper around
* all of the mapped nodes flattened (and de-duplicated).
*
* @param {Function} fn
* @returns {ShallowWrapper}
*/
flatMap(fn) {
const nodes = this.getNodes().map((n, i) => fn.call(this, this.wrap(n), i));
const flattened = flatten(nodes, true);
const uniques = unique(flattened);
const compacted = compact(uniques);
return this.wrap(compacted);
}
/**
* Finds all nodes in the current wrapper nodes' render trees that match the provided predicate
* function. The predicate function will receive the nodes inside a ShallowWrapper as its
* first argument.
*
* @param {Function} predicate
* @returns {ShallowWrapper}
*/
findWhere(predicate) {
return findWhereUnwrapped(this, n => predicate(this.wrap(n)));
}
/**
* Returns the node at a given index of the current wrapper.
*
* @param index
* @returns {ReactElement}
*/
get(index) {
return this.getNodes()[index];
}
/**
* Returns a wrapper around the node at a given index of the current wrapper.
*
* @param index
* @returns {ShallowWrapper}
*/
at(index) {
return this.wrap(this.getNodes()[index]);
}
/**
* Returns a wrapper around the first node of the current wrapper.
*
* @returns {ShallowWrapper}
*/
first() {
return this.at(0);
}
/**
* Returns a wrapper around the last node of the current wrapper.
*
* @returns {ShallowWrapper}
*/
last() {
return this.at(this.length - 1);
}
/**
* Delegates to exists()
*
* @returns {boolean}
*/