forked from enzymejs/enzyme
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathShallowWrapper-spec.jsx
4695 lines (4221 loc) · 143 KB
/
ShallowWrapper-spec.jsx
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 PropTypes from 'prop-types';
import { expect } from 'chai';
import sinon from 'sinon';
import { shallow, render, ShallowWrapper, mount } from 'enzyme';
import { ITERATOR_SYMBOL, withSetStateAllowed, sym } from 'enzyme/build/Utils';
import './_helpers/setupAdapters';
import { createClass, createContext } from './_helpers/react-compat';
import { describeIf, itIf, itWithData, generateEmptyRenderData } from './_helpers';
import { REACT013, REACT014, REACT15, REACT150_4, REACT16, REACT163, is } from './_helpers/version';
// The shallow renderer in react 16 does not yet support batched updates. When it does,
// we should be able to go un-skip all of the tests that are skipped with this flag.
const BATCHING = !REACT16;
// some React versions pass undefined as an argument of setState callback.
const CALLING_SETSTATE_CALLBACK_WITH_UNDEFINED = REACT15 && !REACT150_4;
const getElementPropSelector = prop => x => x.props[prop];
const getWrapperPropSelector = prop => x => x.prop(prop);
describe('shallow', () => {
describe('top level wrapper', () => {
it('does what i expect', () => {
class Box extends React.Component {
render() {
return <div className="box">{this.props.children}</div>;
}
}
class Foo extends React.Component {
render() {
return (
<Box bam>
<div className="div" />
</Box>
);
}
}
const wrapper = shallow(<Foo bar />);
expect(wrapper.type()).to.equal(Box);
expect(wrapper.props().bam).to.equal(true);
expect(wrapper.instance()).to.be.instanceOf(Foo);
expect(wrapper.children().at(0).type()).to.equal('div');
expect(wrapper.find(Box).children().props().className).to.equal('div');
expect(wrapper.find(Box).children().at(0).props().className).to.equal('div');
expect(wrapper.find(Box).children().props().className).to.equal('div');
expect(wrapper.children().type()).to.equal('div');
expect(wrapper.children().props().bam).to.equal(undefined);
});
});
describe('context', () => {
it('can pass in context', () => {
const SimpleComponent = createClass({
contextTypes: {
name: PropTypes.string,
},
render() {
return <div>{this.context.name}</div>;
},
});
const context = { name: 'foo' };
const wrapper = shallow(<SimpleComponent />, { context });
expect(wrapper.text()).to.equal('foo');
});
it('should not throw if context is passed in but contextTypes is missing', () => {
const SimpleComponent = createClass({
render() {
return <div>{this.context.name}</div>;
},
});
const context = { name: 'foo' };
expect(() => shallow(<SimpleComponent />, { context })).to.not.throw();
});
it('is introspectable through context API', () => {
const SimpleComponent = createClass({
contextTypes: {
name: PropTypes.string,
},
render() {
return <div>{this.context.name}</div>;
},
});
const context = { name: 'foo' };
const wrapper = shallow(<SimpleComponent />, { context });
expect(wrapper.context().name).to.equal(context.name);
expect(wrapper.context('name')).to.equal(context.name);
});
itIf(REACT163, 'should find elements through Context elements', () => {
const { Provider, Consumer } = createContext('');
class Consumes extends React.Component {
render() {
return (
<span>
<Consumer>{value => <span>{value}</span>}</Consumer>
</span>
);
}
}
class Provides extends React.Component {
render() {
return (
<Provider value="foo"><div><Consumes /></div></Provider>
);
}
}
expect(shallow(<Consumes />).find('span')).to.have.length(1);
expect(shallow(<Provides />).find(Consumes)).to.have.length(1);
});
describeIf(!REACT013, 'stateless function components', () => {
it('can pass in context', () => {
const SimpleComponent = (props, context) => (
<div>{context.name}</div>
);
SimpleComponent.contextTypes = { name: PropTypes.string };
const context = { name: 'foo' };
const wrapper = shallow(<SimpleComponent />, { context });
expect(wrapper.text()).to.equal('foo');
});
it('should not throw if context is passed in but contextTypes is missing', () => {
const SimpleComponent = (props, context) => (
<div>{context.name}</div>
);
const context = { name: 'foo' };
expect(() => shallow(<SimpleComponent />, { context })).not.to.throw();
});
itIf(!REACT16, 'is introspectable through context API', () => {
const SimpleComponent = (props, context) => (
<div>{context.name}</div>
);
SimpleComponent.contextTypes = { name: PropTypes.string };
const wrapper = shallow(<SimpleComponent />, { context });
expect(wrapper.context().name).to.equal(context.name);
expect(wrapper.context('name')).to.equal(context.name);
});
itIf(REACT16, 'is not introspectable through context API', () => {
const SimpleComponent = (props, context) => (
<div>{context.name}</div>
);
SimpleComponent.contextTypes = { name: PropTypes.string };
const wrapper = shallow(<SimpleComponent />, { context });
expect(() => wrapper.context()).to.throw(
Error,
'ShallowWrapper::context() can only be called on wrapped nodes that have a non-null instance',
);
expect(() => wrapper.context('name')).to.throw(
Error,
'ShallowWrapper::context() can only be called on wrapped nodes that have a non-null instance',
);
});
});
});
describeIf(!REACT013, 'stateless function components', () => {
it('works with stateless components', () => {
const Foo = ({ foo }) => (
<div>
<div className="bar">bar</div>
<div className="qoo">{foo}</div>
</div>
);
const wrapper = shallow(<Foo foo="qux" />);
expect(wrapper.type()).to.equal('div');
expect(wrapper.find('.bar')).to.have.length(1);
expect(wrapper.find('.qoo').text()).to.equal('qux');
});
});
describe('.instance()', () => {
it('should return the component instance', () => {
class Foo extends React.Component {
render() { return <div />; }
}
const wrapper = shallow(<Foo />);
expect(wrapper.instance()).to.be.instanceof(Foo);
expect(wrapper.instance().render).to.equal(Foo.prototype.render);
});
it('should throw if called on something other than the root node', () => {
class Foo extends React.Component {
render() { return <div><a /></div>; }
}
const wrapper = shallow(<Foo />);
const div = wrapper.find('div');
expect(() => div.instance()).to.throw(
Error,
'ShallowWrapper::instance() can only be called on the root',
);
});
});
describe('.contains(node)', () => {
it('should allow matches on the root node', () => {
const a = <div className="foo" />;
const b = <div className="foo" />;
const c = <div className="bar" />;
expect(shallow(a).contains(b)).to.equal(true);
expect(shallow(a).contains(c)).to.equal(false);
});
it('should allow matches on a nested node', () => {
const wrapper = shallow((
<div>
<div className="foo" />
</div>
));
const b = <div className="foo" />;
expect(wrapper.contains(b)).to.equal(true);
});
it('should match composite components', () => {
class Foo extends React.Component {
render() { return <div />; }
}
const wrapper = shallow((
<div>
<Foo />
</div>
));
const b = <Foo />;
expect(wrapper.contains(b)).to.equal(true);
});
it('should work with strings', () => {
const wrapper = shallow(<div>foo</div>);
expect(wrapper.contains('foo')).to.equal(true);
expect(wrapper.contains('bar')).to.equal(false);
});
it('should work with numbers', () => {
const wrapper = shallow(<div>{1}</div>);
expect(wrapper.contains(1)).to.equal(true);
expect(wrapper.contains(2)).to.equal(false);
expect(wrapper.contains('1')).to.equal(false);
});
it('should work with nested strings & numbers', () => {
const wrapper = shallow((
<div>
<div>
<div>{5}</div>
</div>
<div>foo</div>
</div>
));
expect(wrapper.contains('foo')).to.equal(true);
expect(wrapper.contains(<div>foo</div>)).to.equal(true);
expect(wrapper.contains(5)).to.equal(true);
expect(wrapper.contains(<div>{5}</div>)).to.equal(true);
});
it('should do something with arrays of nodes', () => {
const wrapper = shallow((
<div>
<span>Hello</span>
<div>Goodbye</div>
<span>More</span>
</div>
));
const fails = [
<span>wrong</span>,
<div>Goodbye</div>,
];
const passes1 = [
<span>Hello</span>,
<div>Goodbye</div>,
];
const passes2 = [
<div>Goodbye</div>,
<span>More</span>,
];
expect(wrapper.contains(fails)).to.equal(false);
expect(wrapper.contains(passes1)).to.equal(true);
expect(wrapper.contains(passes2)).to.equal(true);
});
it('should throw on invalid argument', () => {
const wrapper = shallow(<div />);
expect(() => wrapper.contains({})).to.throw(
Error,
'ShallowWrapper::contains() can only be called with ReactElement (or array of them), string or number as argument.', // eslint-disable-line max-len
);
expect(() => wrapper.contains(() => ({}))).to.throw(
Error,
'ShallowWrapper::contains() can only be called with ReactElement (or array of them), string or number as argument.', // eslint-disable-line max-len
);
});
describeIf(!REACT013, 'stateless function components', () => {
it('should match composite components', () => {
function Foo() {
return <div />;
}
const wrapper = shallow((
<div>
<Foo />
</div>
));
const b = <Foo />;
expect(wrapper.contains(b)).to.equal(true);
});
it('should match composite components if rendered by function', () => {
function Foo() {
return <div />;
}
const renderStatelessComponent = () => <Foo />;
const wrapper = shallow((
<div>
{renderStatelessComponent()}
</div>
));
const b = <Foo />;
expect(wrapper.contains(b)).to.equal(true);
});
});
});
describe('.equals(node)', () => {
it('should allow matches on the root node', () => {
const a = <div className="foo" />;
const b = <div className="foo" />;
const c = <div className="bar" />;
expect(shallow(a).equals(b)).to.equal(true);
expect(shallow(a).equals(c)).to.equal(false);
});
it('should NOT allow matches on a nested node', () => {
const wrapper = shallow((
<div>
<div className="foo" />
</div>
));
const b = <div className="foo" />;
expect(wrapper.equals(b)).to.equal(false);
});
it('should match composite components', () => {
class Foo extends React.Component {
render() { return <div />; }
}
const wrapper = shallow((
<div>
<Foo />
</div>
));
const b = <div><Foo /></div>;
expect(wrapper.equals(b)).to.equal(true);
});
it('should not expand `node` content', () => {
class Bar extends React.Component {
render() { return <div />; }
}
class Foo extends React.Component {
render() { return <Bar />; }
}
expect(shallow(<Foo />).equals(<Bar />)).to.equal(true);
expect(shallow(<Foo />).equals(<Foo />)).to.equal(false);
});
describeIf(!REACT013, 'stateless components', () => {
it('should match composite stateless components', () => {
const Foo = () => (
<div />
);
const wrapper = shallow((
<div>
<Foo />
</div>
));
const b = <div><Foo /></div>;
expect(wrapper.equals(b)).to.equal(true);
});
it('should not expand `node` content', () => {
const Bar = () => (
<div />
);
const Foo = () => (
<Bar />
);
expect(shallow(<Foo />).equals(<Bar />)).to.equal(true);
expect(shallow(<Foo />).equals(<Foo />)).to.equal(false);
});
});
});
describe('.hostNodes()', () => {
it('should strip out any non-hostNode', () => {
class Foo extends React.Component {
render() {
return <div {...this.props} />;
}
}
const wrapper = shallow((
<div>
<Foo className="foo" />
<div className="foo" />
</div>
));
const foos = wrapper.find('.foo');
expect(foos).to.have.lengthOf(2);
const hostNodes = foos.hostNodes();
expect(hostNodes).to.have.lengthOf(1);
expect(hostNodes.is('div')).to.equal(true);
expect(hostNodes.hasClass('foo')).to.equal(true);
});
});
describe('.find(selector)', () => {
it('should be able to match the root DOM element', () => {
const wrapper = shallow(<div id="ttt" className="ttt">hello</div>);
expect(wrapper.find('#ttt')).to.have.length(1);
expect(wrapper.find('.ttt')).to.have.length(1);
});
it('should find an element based on a class name', () => {
const wrapper = shallow((
<div>
<input className="foo" />
</div>
));
expect(wrapper.find('.foo').type()).to.equal('input');
});
it('should find an element that has dot in attribute', () => {
const wrapper = shallow((
<div>
<div data-baz="foo.bar" />
</div>
));
const elements = wrapper.find('[data-baz="foo.bar"]');
expect(elements.length).to.equal(1);
});
it('should find an element that with class and attribute', () => {
const wrapper = shallow((
<div>
<div data-baz="bar" className="classBar" />
</div>
));
const elements = wrapper.find('.classBar[data-baz="bar"]');
expect(elements.length).to.equal(1);
});
it('should find an element that with multiple classes and one attribute', () => {
const wrapper = shallow((
<div>
<div data-baz="bar" className="classBar classFoo" />
</div>
));
const elements = wrapper.find('.classBar.classFoo[data-baz="bar"]');
expect(elements.length).to.equal(1);
});
it('should find an element that with class and class with hyphen', () => {
const wrapper = shallow((
<div>
<div data-baz="bar" className="classBar class-Foo" />
</div>
));
const elements = wrapper.find('.classBar.class-Foo');
expect(elements.length).to.equal(1);
});
it('should find an element based on a tag name and class name', () => {
const wrapper = shallow((
<div>
<input className="foo" />
</div>
));
expect(wrapper.find('input.foo').length).to.equal(1);
});
it('should find an element based on a tag name and id', () => {
const wrapper = shallow((
<div>
<input id="foo" />
</div>
));
expect(wrapper.find('input#foo').length).to.equal(1);
});
it('should find an element based on a tag name, id, and class name', () => {
const wrapper = shallow((
<div>
<input id="foo" className="bar" />
</div>
));
expect(wrapper.find('input#foo.bar').length).to.equal(1);
});
it('should find an element based on a tag name', () => {
const wrapper = shallow((
<div>
<input className="foo" />
<button className="bar">Button</button>
<textarea className="magic" />
<select className="reality" />
</div>
));
expect(wrapper.find('input').props().className).to.equal('foo');
expect(wrapper.find('button').props().className).to.equal('bar');
expect(wrapper.find('textarea').props().className).to.equal('magic');
expect(wrapper.find('select').props().className).to.equal('reality');
});
it('should find a component based on a constructor', () => {
class Foo extends React.Component {
render() { return <div />; }
}
const wrapper = shallow((
<div>
<Foo className="foo" />
</div>
));
expect(wrapper.find(Foo).type()).to.equal(Foo);
});
it('should find a component based on a display name', () => {
class Foo extends React.Component {
render() { return <div />; }
}
const wrapper = shallow((
<div>
<Foo className="foo" />
</div>
));
expect(wrapper.find('Foo').type()).to.equal(Foo);
});
it('should find multiple elements based on a class name', () => {
const wrapper = shallow((
<div>
<input className="foo" />
<button className="foo" />
</div>
));
expect(wrapper.find('.foo').length).to.equal(2);
});
it('should find multiple elements based on a tag name', () => {
const wrapper = shallow((
<div>
<input className="foo" />
<input />
<button />
</div>
));
expect(wrapper.find('input').length).to.equal(2);
expect(wrapper.find('button').length).to.equal(1);
});
it('should work on non-single nodes', () => {
const wrapper = shallow((
<div className="a">
<div className="b">
<div className="c">Text</div>
<div className="c">Text</div>
<div className="c">Text</div>
</div>
<div className="b">
<div className="c">Text</div>
<div className="c">Text</div>
<div className="c">Text</div>
</div>
</div>
));
expect(wrapper.find('.a').length).to.equal(1);
expect(wrapper.find('.b').length).to.equal(2);
expect(wrapper.find('.b').find('.c').length).to.equal(6);
});
it('should find component based on a react prop', () => {
const wrapper = shallow((
<div>
<span title="foo" />
</div>
));
expect(wrapper.find('[title="foo"]')).to.have.length(1);
expect(wrapper.find('[title]')).to.have.length(1);
});
it('works with an adjacent sibling selector', () => {
const a = 'some';
const b = 'text';
const wrapper = shallow((
<div>
<div className="row">
{a}
{b}
</div>
<div className="row">
{a}
{b}
</div>
</div>
));
expect(wrapper.find('.row')).to.have.lengthOf(2);
expect(wrapper.find('.row + .row')).to.have.lengthOf(1);
});
it('should throw for non-numeric attribute values without quotes', () => {
const wrapper = shallow((
<div>
<input type="text" />
<input type="hidden" />
<input type="text" />
</div>
));
expect(() => wrapper.find('[type=text]')).to.throw(
Error,
'Failed to parse selector: [type=text]',
);
expect(() => wrapper.find('[type=hidden]')).to.throw(
Error,
'Failed to parse selector: [type=hidden]',
);
expect(() => wrapper.find('[type="text"]')).to.not.throw(
Error,
'Failed to parse selector: [type="text"]',
);
});
it('should error sensibly if any of the search props are undefined', () => {
const wrapper = shallow((
<div>
<input type={undefined} />
</div>
));
expect(() => wrapper.find({ type: undefined })).to.throw(
TypeError,
'Enzyme::Props can’t have `undefined` values. Try using ‘findWhere()’ instead.',
);
});
it('should compound tag and prop selector', () => {
const wrapper = shallow((
<div>
<span preserveAspectRatio="xMaxYMax" />
</div>
));
expect(wrapper.find('span[preserveAspectRatio="xMaxYMax"]')).to.have.length(1);
expect(wrapper.find('span[preserveAspectRatio]')).to.have.length(1);
});
it('should support data prop selectors', () => {
const wrapper = shallow((
<div>
<span data-foo="bar" />
</div>
));
expect(wrapper.find('[data-foo="bar"]')).to.have.length(1);
expect(wrapper.find('[data-foo]')).to.have.length(1);
});
it('should find components with multiple matching react props', () => {
function noop() {}
const wrapper = shallow((
<div>
<span htmlFor="foo" onChange={noop} preserveAspectRatio="xMaxYMax" />
</div>
));
expect(wrapper.find('span[htmlFor="foo"][onChange]')).to.have.length(1);
expect(wrapper.find('span[htmlFor="foo"][preserveAspectRatio="xMaxYMax"]')).to.have.length(1);
expect(wrapper.find('[htmlFor][preserveAspectRatio]')).to.have.length(1);
});
it('should support boolean and numeric values for matching props', () => {
const wrapper = shallow((
<div>
<span value={1} />
<a value={false} />
</div>
));
expect(wrapper.find('span[value=1]')).to.have.length(1);
expect(wrapper.find('span[value=2]')).to.have.length(0);
expect(wrapper.find('a[value=false]')).to.have.length(1);
expect(wrapper.find('a[value=true]')).to.have.length(0);
});
it('should not find key or ref via property selector', () => {
const arrayOfComponents = [<div key="1" />, <div key="2" />];
const wrapper = shallow((
<div>
<div ref="foo" />
{arrayOfComponents}
</div>
));
expect(wrapper.find('div[ref="foo"]')).to.have.length(0);
expect(wrapper.find('div[key="1"]')).to.have.length(0);
expect(wrapper.find('[ref]')).to.have.length(0);
expect(wrapper.find('[key]')).to.have.length(0);
});
it('should find multiple elements based on a constructor', () => {
const wrapper = shallow((
<div>
<input className="foo" />
<input />
<button />
</div>
));
expect(wrapper.find('input').length).to.equal(2);
expect(wrapper.find('button').length).to.equal(1);
});
it('should support object property selectors', () => {
const wrapper = shallow((
<div>
<input data-test="ref" className="foo" type="text" />
<input data-test="ref" type="text" />
<button data-test="ref" prop={undefined} />
<span data-test="ref" prop={null} />
<div data-test="ref" prop={123} />
<input data-test="ref" prop={false} />
<a data-test="ref" prop />
</div>
));
expect(wrapper.find({ a: 1 })).to.have.length(0);
expect(wrapper.find({ 'data-test': 'ref' })).to.have.length(7);
expect(wrapper.find({ className: 'foo' })).to.have.length(1);
expect(wrapper.find({ prop: null })).to.have.length(1);
expect(wrapper.find({ prop: 123 })).to.have.length(1);
expect(wrapper.find({ prop: false })).to.have.length(1);
expect(wrapper.find({ prop: true })).to.have.length(1);
});
it('should support complex and nested object property selectors', () => {
const testFunction = () => ({});
const wrapper = shallow((
<div>
<span more={[{ id: 1 }]} data-test="ref" prop onChange={testFunction} />
<a more={[{ id: 1 }]} data-test="ref" />
<div more={{ item: { id: 1 } }} data-test="ref" />
<input style={{ height: 20 }} data-test="ref" />
</div>
));
expect(wrapper.find({ 'data-test': 'ref' })).to.have.length(4);
expect(wrapper.find({ more: { a: 1 } })).to.have.length(0);
expect(wrapper.find({ more: [{ id: 1 }] })).to.have.length(2);
expect(wrapper.find({ more: { item: { id: 1 } } })).to.have.length(1);
expect(wrapper.find({ style: { height: 20 } })).to.have.length(1);
expect(wrapper.find({
more: [{ id: 1 }],
'data-test': 'ref',
prop: true,
onChange: testFunction,
})).to.have.length(1);
});
it('should throw when given empty object, null, or an array', () => {
const wrapper = shallow((
<div>
<input className="foo" type="text" />
</div>
));
expect(() => wrapper.find({})).to.throw(
TypeError,
'Enzyme::Selector does not support an array, null, or empty object as a selector',
);
expect(() => wrapper.find([])).to.throw(
TypeError,
'Enzyme::Selector does not support an array, null, or empty object as a selector',
);
expect(() => wrapper.find(null)).to.throw(
TypeError,
'Enzyme::Selector does not support an array, null, or empty object as a selector',
);
});
describeIf(!REACT013, 'stateless function components', () => {
it('should find a component based on a constructor', () => {
const Foo = () => (
<div />
);
const wrapper = shallow((
<div>
<Foo className="foo" />
</div>
));
expect(wrapper.find(Foo).type()).to.equal(Foo);
});
it('should find a component based on a display name', () => {
const Foo = () => (
<div />
);
const wrapper = shallow((
<div>
<Foo className="foo" />
</div>
));
expect(wrapper.find('Foo').type()).to.equal(Foo);
});
});
describe('works with attribute selectors containing #', () => {
let wrapper;
beforeEach(() => {
wrapper = shallow((
<div>
<a id="test" href="/page">Hello</a>
<a href="/page#anchor">World</a>
</div>
));
});
it('works with an ID', () => {
expect(wrapper.find('a#test')).to.have.lengthOf(1);
});
it('works with a normal attribute', () => {
expect(wrapper.find('a[href="/page"]')).to.have.lengthOf(1);
});
it('works with an attribute with a #', () => {
expect(wrapper.find('a[href="/page#anchor"]')).to.have.lengthOf(1);
});
});
});
describe('.findWhere(predicate)', () => {
it('should return all elements for a truthy test', () => {
const wrapper = shallow((
<div>
<input className="foo" />
<input />
</div>
));
expect(wrapper.findWhere(() => true).length).to.equal(3);
});
it('should return no elements for a falsy test', () => {
const wrapper = shallow((
<div>
<input className="foo" />
<input />
</div>
));
expect(wrapper.findWhere(() => false).length).to.equal(0);
});
it('should call the predicate with the wrapped node as the first argument', () => {
const wrapper = shallow((
<div>
<div className="foo bar" />
<div className="foo baz" />
<div className="foo bux" />
</div>
));
const stub = sinon.stub();
stub.returns(true);
const spy = sinon.spy(stub);
wrapper.findWhere(spy);
expect(spy.callCount).to.equal(4);
expect(spy.args[0][0]).to.be.instanceOf(ShallowWrapper);
expect(spy.args[1][0]).to.be.instanceOf(ShallowWrapper);
expect(spy.args[2][0]).to.be.instanceOf(ShallowWrapper);
expect(spy.args[3][0]).to.be.instanceOf(ShallowWrapper);
expect(spy.args[1][0].hasClass('bar')).to.equal(true);
expect(spy.args[2][0].hasClass('baz')).to.equal(true);
expect(spy.args[3][0].hasClass('bux')).to.equal(true);
});
describeIf(!REACT013, 'stateless function components', () => {
it('finds nodes', () => {
const SFC = function SFC({ selector }) {
return (
<div>
<span data-foo={selector} />
<i data-foo={selector} />
</div>
);
};
const selector = 'blah';
const wrapper = shallow(<SFC selector={selector} />);
const foundSpan = wrapper.findWhere(n => (
n.type() === 'span' && n.props()['data-foo'] === selector
));
expect(foundSpan.type()).to.equal('span');
const foundNotSpan = wrapper.findWhere(n => (
n.type() !== 'span' && n.props()['data-foo'] === selector
));
expect(foundNotSpan.type()).to.equal('i');
});
it('finds nodes when conditionally rendered', () => {
const SFC = function SFC({ selector }) {
return (
<div>
<span data-foo={selector} />
{selector === 'baz' ? <i data-foo={selector} /> : null}
</div>
);
};
const selector = 'blah';
const wrapper = shallow(<SFC selector={selector} />);
const foundSpan = wrapper.findWhere(n => (
n.type() === 'span' && n.props()['data-foo'] === selector
));
expect(foundSpan.type()).to.equal('span');
const foundNotSpan = wrapper.findWhere(n => (
n.type() !== 'span' && n.props()['data-foo'] === selector
));
expect(foundNotSpan).to.have.length(0);
});
});
it('should not pass in null or false nodes', () => {
const wrapper = shallow((
<div>
<div className="foo bar" />
{null}
{false}
</div>
));
const stub = sinon.stub();
stub.returns(true);
const spy = sinon.spy(stub);
wrapper.findWhere(spy);
expect(spy.callCount).to.equal(2);
});
});
describe('.setProps(newProps)', () => {
it('should set props for a component multiple times', () => {
class Foo extends React.Component {
render() {
return (
<div className={this.props.id}>
{this.props.id}
</div>
);
}
}
const wrapper = shallow(<Foo id="foo" />);
expect(wrapper.find('.foo').length).to.equal(1);
wrapper.setProps({ id: 'bar', foo: 'bla' });