/
ReactWrapper-spec.jsx
8023 lines (6992 loc) · 239 KB
/
ReactWrapper-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
/* globals document */
import React from 'react';
import PropTypes from 'prop-types';
import { expect } from 'chai';
import sinon from 'sinon';
import wrap from 'mocha-wrap';
import isEqual from 'lodash.isequal';
import {
mount,
render,
ReactWrapper,
} from 'enzyme';
import {
ITERATOR_SYMBOL,
withSetStateAllowed,
sym,
} from 'enzyme/build/Utils';
import getAdapter from 'enzyme/build/getAdapter';
import { fakeDynamicImport } from 'enzyme-adapter-utils/build/Utils';
import {
Portal,
} from 'react-is';
import './_helpers/setupAdapters';
import {
createClass,
createContext,
createPortal,
createRef,
lazy,
Fragment,
forwardRef,
PureComponent,
Suspense,
} from './_helpers/react-compat';
import {
describeWithDOM,
describeIf,
itIf,
itWithData,
generateEmptyRenderData,
} from './_helpers';
import {
REACT16,
is,
} from './_helpers/version';
import realArrowFunction from './_helpers/realArrowFunction';
import sloppyReturnThis from './_helpers/untranspiledSloppyReturnThis';
const getElementPropSelector = prop => x => x.props[prop];
const getWrapperPropSelector = prop => x => x.prop(prop);
// some React versions pass undefined as an argument of setState callback.
const CALLING_SETSTATE_CALLBACK_WITH_UNDEFINED = is('^15.5');
describeWithDOM('mount', () => {
describe('top level wrapper', () => {
it('does what i expect', () => {
class Box extends React.Component {
render() {
const { children } = this.props;
return <div className="box">{children}</div>;
}
}
class Foo extends React.Component {
render() {
return (
<Box bam>
<div className="div" />
</Box>
);
}
}
const wrapper = mount(<Foo bar />);
expect(wrapper.type()).to.equal(Foo);
expect(wrapper.props()).to.deep.equal({ bar: true });
expect(wrapper.instance()).to.be.instanceOf(Foo);
expect(wrapper.children().at(0).type()).to.equal(Box);
expect(wrapper.find(Box).children().props().className).to.equal('box');
expect(wrapper.find(Box).instance()).to.be.instanceOf(Box);
expect(wrapper.find(Box).children().at(0).props().className).to.equal('box');
expect(wrapper.find(Box).children().props().className).to.equal('box');
expect(wrapper.children().type()).to.equal(Box);
expect(wrapper.children().instance()).to.be.instanceOf(Box);
expect(wrapper.children().props().bam).to.equal(true);
});
it('calls ref', () => {
const spy = sinon.spy();
mount(<div ref={spy} />);
expect(spy).to.have.property('callCount', 1);
});
describe('wrapping invalid elements', () => {
it('throws with combined dangerouslySetInnerHTML and children on host nodes', () => {
/* eslint react/no-danger-with-children: 0 */
expect(() => mount((
<div dangerouslySetInnerHTML={{ __html: '{}' }}>child</div>
))).to.throw(Error, 'Can only set one of `children` or `props.dangerouslySetInnerHTML`.');
});
itIf(is('>= 16'), 'throws when mounting Portals', () => {
const portal = createPortal(
<div />,
{ nodeType: 1 },
);
expect(() => mount(portal)).to.throw(
Error,
'ReactWrapper can only wrap valid elements',
);
});
it('throws when mounting plain text', () => {
expect(() => mount('Foo')).to.throw(
Error,
'ReactWrapper can only wrap valid elements',
);
});
it('throws when mounting multiple elements', () => {
expect(() => mount([<div />])).to.throw(
TypeError,
'ReactWrapper can only wrap valid elements',
);
});
});
it('mounts built in components', () => {
expect(() => mount(<div />)).not.to.throw();
});
it('mounts composite components', () => {
class Foo extends React.Component {
render() {
return <div />;
}
}
expect(() => mount(<Foo />)).not.to.throw();
});
it('starts out with undefined state', () => {
class Foo extends React.Component {
render() {
return (
<div>
{typeof this.state}
{JSON.stringify(this.state)}
</div>
);
}
}
const wrapper = mount(<Foo />);
expect(wrapper.state()).to.equal(null);
expect(wrapper.debug()).to.equal(`
<Foo>
<div>
object
null
</div>
</Foo>
`.trim());
expect(() => wrapper.state('key')).to.throw('ReactWrapper::state("key") requires that `state` not be `null` or `undefined`');
});
describeIf(is('>= 16.3'), 'uses the isValidElementType from the Adapter to validate the prop type of Component', () => {
const Foo = () => null;
const Bar = () => null;
wrap()
.withConsoleThrows()
.withOverride(() => getAdapter(), 'isValidElementType', () => val => val === Foo)
.it('with isValidElementType defined on the Adapter', () => {
expect(() => {
mount(<Bar />);
}).to.throw('Warning: Failed prop type: Component must be a valid element type!\n in WrapperComponent');
});
});
});
describe('context', () => {
it('can pass in context', () => {
const SimpleComponent = createClass({
contextTypes: {
name: PropTypes.string,
},
render() {
const { name } = this.context;
return <div>{name}</div>;
},
});
const context = { name: 'foo' };
const wrapper = mount(<SimpleComponent />, { context });
expect(wrapper.text()).to.equal('foo');
});
it('can pass context to the child of mounted component', () => {
const SimpleComponent = createClass({
contextTypes: {
name: PropTypes.string,
},
render() {
const { name } = this.context;
return <div>{name}</div>;
},
});
const ComplexComponent = createClass({
render() {
return <div><SimpleComponent /></div>;
},
});
const childContextTypes = {
name: PropTypes.string.isRequired,
};
const context = { name: 'foo' };
const wrapper = mount(<ComplexComponent />, { context, childContextTypes });
expect(wrapper.find(SimpleComponent)).to.have.lengthOf(1);
});
describe('does not attempt to mutate Component.childContextTypes', () => {
const SimpleComponent = createClass({
displayName: 'Simple',
render() {
return <div />;
},
});
class ClassComponent extends React.Component {
render() {
const { a } = this.context;
return (
<div>
{a}
<SimpleComponent />
</div>
);
}
}
ClassComponent.contextTypes = Object.freeze({ a: PropTypes.string });
const CreateClassComponent = createClass({
contextTypes: ClassComponent.contextTypes,
render: ClassComponent.prototype.render,
});
it('works without options', () => {
expect(() => mount(<ClassComponent />)).not.to.throw();
expect(() => mount(<CreateClassComponent />)).not.to.throw();
});
it('works with a childContextTypes option', () => {
const options = {
childContextTypes: { b: PropTypes.string },
context: { a: 'hello', b: 'world' },
};
expect(() => mount(<ClassComponent />, options)).not.to.throw();
expect(() => mount(<CreateClassComponent />, options)).not.to.throw();
});
});
it('does not throw if context is passed in but contextTypes is missing', () => {
const SimpleComponent = createClass({
render() {
const { name } = this.context;
return <div>{name}</div>;
},
});
const context = { name: 'foo' };
expect(() => mount(<SimpleComponent />, { context })).not.to.throw();
});
it('is introspectable through context API', () => {
const SimpleComponent = createClass({
contextTypes: {
name: PropTypes.string,
},
render() {
const { name } = this.context;
return <div>{name}</div>;
},
});
const context = { name: 'foo' };
const wrapper = mount(<SimpleComponent />, { context });
expect(wrapper.context().name).to.equal(context.name);
expect(wrapper.context('name')).to.equal(context.name);
});
itIf(is('>= 16.3'), 'finds elements through Context elements', () => {
const { Provider, Consumer } = createContext('');
class Foo extends React.Component {
render() {
return (
<Consumer>{value => <span>{value}</span>}</Consumer>
);
}
}
const wrapper = mount(<Provider value="foo"><div><Foo /></div></Provider>);
expect(wrapper.find('span').text()).to.equal('foo');
});
describeIf(is('>= 16.3'), 'forwarded ref Components', () => {
wrap().withConsoleThrows().it('mounts without complaint', () => {
const SomeComponent = forwardRef((props, ref) => (
<div {...props} ref={ref} />
));
expect(() => mount(<SomeComponent />)).not.to.throw();
});
it('finds elements through forwardedRef elements', () => {
const testRef = () => {};
const SomeComponent = forwardRef((props, ref) => (
<div ref={ref}>
<span className="child1" />
<span className="child2" />
</div>
));
const wrapper = mount(<div><SomeComponent ref={testRef} /></div>);
expect(wrapper.find('.child2')).to.have.lengthOf(1);
});
it('finds forwardRef element', () => {
const SomeComponent = forwardRef((props, ref) => (
<div ref={ref}>
<span className="child1" />
</div>
));
const Parent = () => <span><SomeComponent foo="hello" /></span>;
const wrapper = mount(<Parent foo="hello" />);
const results = wrapper.find(SomeComponent);
expect(results).to.have.lengthOf(1);
expect(results.props()).to.eql({ foo: 'hello' });
});
});
describeIf(is('> 0.13'), 'stateless function components (SFCs)', () => {
it('can pass in context', () => {
const SimpleComponent = (props, context) => (
<div>{context.name}</div>
);
SimpleComponent.contextTypes = { name: PropTypes.string };
const context = { name: 'foo' };
const wrapper = mount(<SimpleComponent />, { context });
expect(wrapper.text()).to.equal('foo');
});
it('can pass context to the child of mounted component', () => {
const SimpleComponent = (props, context) => (
<div>{context.name}</div>
);
SimpleComponent.contextTypes = { name: PropTypes.string };
const ComplexComponent = () => (
<div><SimpleComponent /></div>
);
const childContextTypes = {
name: PropTypes.string.isRequired,
};
const context = { name: 'foo' };
const wrapper = mount(<ComplexComponent />, { context, childContextTypes });
expect(wrapper.find(SimpleComponent)).to.have.lengthOf(1);
});
it('does not throw if context is passed in but contextTypes is missing', () => {
const SimpleComponent = (props, context) => (
<div>{context.name}</div>
);
const context = { name: 'foo' };
expect(() => mount(<SimpleComponent />, { context })).to.not.throw();
});
itIf(is('< 16'), 'is introspectable through context API', () => {
const SimpleComponent = (props, context) => (
<div>{context.name}</div>
);
SimpleComponent.contextTypes = { name: PropTypes.string };
const context = { name: 'foo' };
const wrapper = mount(<SimpleComponent />, { context });
expect(wrapper.context().name).to.equal(context.name);
expect(wrapper.context('name')).to.equal(context.name);
});
itIf(is('< 16'), 'works with SFCs', () => {
const Foo = ({ foo }) => (
<div>
<div className="bar">bar</div>
<div className="qoo">{foo}</div>
</div>
);
Foo.contextTypes = {
_: PropTypes.string,
};
const wrapper = mount(<Foo foo="qux" />, {
context: {
_: 'foo',
},
});
expect(wrapper.context('_')).to.equal('foo');
});
});
});
describeIf(is('> 0.13'), 'stateless function components (SFCs)', () => {
it('works with SFCs', () => {
const Foo = ({ foo }) => (
<div>
<div className="bar">bar</div>
<div className="qoo">{foo}</div>
</div>
);
const wrapper = mount(<Foo foo="qux" />);
expect(wrapper.type()).to.equal(Foo);
expect(wrapper.find('.bar')).to.have.lengthOf(1);
expect(wrapper.find('.qoo').text()).to.equal('qux');
});
it('supports findDOMNode with SFCs', () => {
const Foo = ({ foo }) => (
<div>{foo}</div>
);
const wrapper = mount(<Foo foo="qux" />);
expect(wrapper.text()).to.equal('qux');
});
it('works with nested stateless', () => {
const TestItem = () => (
<div className="item">1</div>
);
const Test = () => (
<div className="box">
<TestItem test="123" />
<TestItem />
<TestItem />
</div>
);
const wrapper = mount(<Test />);
const children = wrapper.find('.box').children();
expect(children).to.have.lengthOf(3);
expect(children.at(0).props().test).to.equal('123');
expect(wrapper.find(TestItem)).to.have.lengthOf(3);
expect(wrapper.find(TestItem).first().props().test).to.equal('123');
});
});
describeIf(is('>= 16'), 'portals', () => {
it('shows portals in mount debug tree', () => {
const containerDiv = global.document.createElement('div');
const Foo = () => (
<div>
{createPortal(
<div className="in-portal">InPortal</div>,
containerDiv,
)}
</div>
);
const wrapper = mount(<Foo />);
expect(wrapper.debug()).to.equal(`<Foo>
<div>
<Portal containerInfo={{...}}>
<div className="in-portal">
InPortal
</div>
</Portal>
</div>
</Foo>`);
});
it('shows portal container in debug tree', () => {
const containerDiv = global.document.createElement('div');
containerDiv.setAttribute('data-foo', 'bar');
const Foo = () => (
<div className="foo">
{createPortal(
<div className="in-portal">InPortal</div>,
containerDiv,
)}
</div>
);
const wrapper = mount(<Foo />);
expect(wrapper.debug({ verbose: true })).to.equal(`<Foo>
<div className="foo">
<Portal containerInfo={<div data-foo="bar">...</div>}>
<div className="in-portal">
InPortal
</div>
</Portal>
</div>
</Foo>`);
});
it('shows nested portal children in debug tree', () => {
const Bar = () => null;
const containerDiv = global.document.createElement('div');
const Foo = () => (
<div className="foo">
{createPortal(
<div className="in-portal">
<div className="nested-in-portal">
<Bar />
</div>
</div>,
containerDiv,
)}
</div>
);
const wrapper = mount(<Foo />);
expect(wrapper.debug()).to.equal(`<Foo>
<div className="foo">
<Portal containerInfo={{...}}>
<div className="in-portal">
<div className="nested-in-portal">
<Bar />
</div>
</div>
</Portal>
</div>
</Foo>`);
});
it('has top level portals in debug tree', () => {
const containerDiv = global.document.createElement('div');
const Foo = () => createPortal(
<div className="in-portal">InPortal</div>,
containerDiv,
);
const wrapper = mount(<Foo />);
expect(wrapper.debug()).to.equal(`<Foo>
<Portal containerInfo={{...}}>
<div className="in-portal">
InPortal
</div>
</Portal>
</Foo>`);
});
});
describeIf(is('>= 16.6'), 'Suspense & lazy', () => {
class DynamicComponent extends React.Component {
render() {
return (
<div>Dynamic Component</div>
);
}
}
class Fallback extends React.Component {
render() {
return (
<div>Fallback</div>
);
}
}
it('finds Suspense and its children when no lazy component', () => {
class Component extends React.Component {
render() {
return (
<div>test</div>
);
}
}
const SuspenseComponent = () => (
<Suspense fallback={Fallback}>
<Component />
</Suspense>
);
const wrapper = mount(<SuspenseComponent />);
expect(wrapper.is(SuspenseComponent)).to.equal(true);
expect(wrapper.find(Component)).to.have.lengthOf(1);
expect(wrapper.find(Fallback)).to.have.lengthOf(0);
});
it('can mount Suspense directly', () => {
const wrapper = mount(<Suspense fallback={Fallback} />);
expect(wrapper.is(Suspense)).to.equal(true);
});
it('finds fallback when given lazy component in initial mount', () => {
const LazyComponent = lazy(() => fakeDynamicImport(DynamicComponent));
const SuspenseComponent = () => (
<Suspense fallback={<Fallback />}>
<LazyComponent />
</Suspense>
);
const wrapper = mount(<SuspenseComponent />);
expect(wrapper.is(SuspenseComponent)).to.equal(true);
expect(wrapper.find(LazyComponent)).to.have.lengthOf(0);
expect(wrapper.find(Fallback)).to.have.lengthOf(1);
});
});
describe('.contains(node)', () => {
it('allows matches on the root node', () => {
const a = <div className="foo" />;
const b = <div className="foo" />;
const c = <div className="bar" />;
expect(mount(a).contains(b)).to.equal(true);
expect(mount(a).contains(c)).to.equal(false);
});
it('allows matches on a nested node', () => {
const wrapper = mount((
<div>
<div className="foo" />
</div>
));
const b = <div className="foo" />;
expect(wrapper.contains(b)).to.equal(true);
});
it('matches composite components', () => {
class Foo extends React.Component {
render() { return <div />; }
}
const wrapper = mount((
<div>
<Foo />
</div>
));
const b = <Foo />;
expect(wrapper.contains(b)).to.equal(true);
});
it('does something with arrays of nodes', () => {
const wrapper = mount((
<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);
});
describeIf(is('> 0.13'), 'stateless function components (SFCs)', () => {
it('matches composite components', () => {
function Foo() {
return <div />;
}
const wrapper = mount((
<div>
<Foo />
</div>
));
const b = <Foo />;
expect(wrapper.contains(b)).to.equal(true);
});
it('matches composite components if rendered by function', () => {
function Foo() {
return <div />;
}
const renderStatelessComponent = () => <Foo />;
const wrapper = mount((
<div>
{renderStatelessComponent()}
</div>
));
const b = <Foo />;
expect(wrapper.contains(b)).to.equal(true);
});
});
});
describe('.equals(node)', () => {
it('allows matches on the root node', () => {
const a = <div className="foo" />;
const b = <div className="foo" />;
const c = <div className="bar" />;
expect(mount(a).equals(b)).to.equal(true);
expect(mount(a).equals(c)).to.equal(false);
});
it('does NOT allow matches on a nested node', () => {
const wrapper = mount((
<div>
<div className="foo" />
</div>
));
const b = <div className="foo" />;
expect(wrapper.equals(b)).to.equal(false);
});
it('matches composite components', () => {
class Foo extends React.Component {
render() { return <div />; }
}
const wrapper = mount((
<div>
<Foo />
</div>
));
const b = <div><Foo /></div>;
expect(wrapper.equals(b)).to.equal(true);
});
it('does not expand `node` content', () => {
class Bar extends React.Component {
render() { return <div />; }
}
class Foo extends React.Component {
render() { return <Bar />; }
}
const wrapper = mount(<Foo />).children();
expect(wrapper.equals(<Bar />)).to.equal(true);
expect(wrapper.equals(<Foo />)).to.equal(false);
});
describeIf(is('> 0.13'), 'stateless function components (SFCs)', () => {
it('matches composite SFCs', () => {
const Foo = () => (
<div />
);
const wrapper = mount((
<div>
<Foo />
</div>
));
const b = <div><Foo /></div>;
expect(wrapper.equals(b)).to.equal(true);
});
it('does not expand `node` content', () => {
const Bar = () => (
<div />
);
const Foo = () => (
<Bar />
);
const wrapper = mount(<Foo />).children();
expect(wrapper.equals(<Bar />)).to.equal(true);
expect(wrapper.equals(<Foo />)).to.equal(false);
});
});
it('flattens arrays of children to compare', () => {
class TwoChildren extends React.Component {
render() {
return (
<div className="parent-component-class">
<div key="a" className="asd" />
<div key="b" className="fgh" />
</div>
);
}
}
class TwoChildrenOneArrayed extends React.Component {
render() {
return (
<div className="parent-component-class">
<div key="a" className="asd" />
{[<div key="b" className="fgh" />]}
</div>
);
}
}
const twoChildren = mount(<TwoChildren />).children();
const twoChildrenOneArrayed = mount(<TwoChildrenOneArrayed />).children();
expect(twoChildren.equals(twoChildrenOneArrayed.getElement())).to.equal(true);
expect(twoChildrenOneArrayed.equals(twoChildren.getElement())).to.equal(true);
});
});
describe('.hostNodes()', () => {
it('strips out any non-hostNode', () => {
class Foo extends React.Component {
render() {
return <div {...this.props} />;
}
}
const wrapper = mount((
<div>
<Foo className="foo" />
<span className="foo" />
</div>
));
const foos = wrapper.find('.foo');
expect(foos).to.have.lengthOf(3);
const hostNodes = foos.hostNodes();
expect(hostNodes).to.have.lengthOf(2);
expect(hostNodes.filter('.foo')).to.have.lengthOf(2);
expect(hostNodes.filter('div')).to.have.lengthOf(1);
expect(hostNodes.filter('span')).to.have.lengthOf(1);
});
it('does NOT allow matches on a nested node', () => {
const wrapper = mount((
<div>
<div className="foo" />
</div>
));
const b = <div className="foo" />;
expect(wrapper.equals(b)).to.equal(false);
});
it('matches composite components', () => {
class Foo extends React.Component {
render() { return <div />; }
}
const wrapper = mount((
<div>
<Foo />
</div>
));
const b = <div><Foo /></div>;
expect(wrapper.equals(b)).to.equal(true);
});
it.skip('does not expand `node` content', () => {
class Bar extends React.Component {
render() { return <div />; }
}
class Foo extends React.Component {
render() { return <Bar />; }
}
expect(mount(<Foo />).equals(<Bar />)).to.equal(true);
expect(mount(<Foo />).equals(<Foo />)).to.equal(false);
});
describeIf(is('> 0.13'), 'stateless function components (SFCs)', () => {
it('matches composite SFCs', () => {
const Foo = () => (
<div />
);
const wrapper = mount((
<div>
<Foo />
</div>
));
const b = <div><Foo /></div>;
expect(wrapper.equals(b)).to.equal(true);
});
it.skip('does not expand `node` content', () => {
const Bar = () => (
<div />
);
const Foo = () => (
<Bar />
);
expect(mount(<Foo />).equals(<Bar />)).to.equal(true);
expect(mount(<Foo />).equals(<Foo />)).to.equal(false);
});
});
it.skip('flattens arrays of children to compare', () => {
class TwoChildren extends React.Component {
render() {
return (
<div className="parent-component-class">
<div key="a" className="asd" />
<div key="b" className="fgh" />
</div>
);
}
}
class TwoChildrenOneArrayed extends React.Component {
render() {
return (
<div className="parent-component-class">
<div key="a" className="asd" />
{[<div key="b" className="fgh" />]}
</div>
);
}
}
const twoChildren = mount(<TwoChildren />);
const twoChildrenOneArrayed = mount(<TwoChildrenOneArrayed />);
expect(twoChildren.equals(twoChildrenOneArrayed.getElement())).to.equal(true);
expect(twoChildrenOneArrayed.equals(twoChildren.getElement())).to.equal(true);
});
});
describe('.find(selector)', () => {
it('finds an element based on a class name', () => {
const wrapper = mount((
<div>
<input className="foo" />
</div>
));
expect(wrapper.find('.foo').type()).to.equal('input');
});
it('finds an SVG element based on a class name', () => {
const wrapper = mount((
<div>
<svg className="foo" />
</div>
));
expect(wrapper.find('.foo').type()).to.equal('svg');
});
it('finds an element based on a tag name', () => {
const wrapper = mount((
<div>
<input className="foo" />
</div>
));
expect(wrapper.find('input').props().className).to.equal('foo');
});
it('finds an element based on a tag name and class name', () => {
const wrapper = mount((
<div>
<input className="foo" />
<div className="foo" />
</div>
));
expect(wrapper.find('input.foo')).to.have.lengthOf(1);
});
it('works on non-single nodes', () => {
const wrapper = mount((
<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')).to.have.lengthOf(1);
expect(wrapper.find('.b')).to.have.lengthOf(2);
expect(wrapper.find('.b').find('.c')).to.have.lengthOf(6);
});
it('finds an element based on a tag name and id', () => {
const wrapper = mount((
<div>