Skip to content

Commit 5c4a557

Browse files
authored
tests: added more tests (react-bootstrap#5193)
* test: Added more tests * Switch to fake timers * Clean up callback tests
1 parent 46c6698 commit 5c4a557

File tree

8 files changed

+150
-1
lines changed

8 files changed

+150
-1
lines changed

test/CarouselSpec.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,14 @@ describe('<Carousel>', () => {
244244
);
245245
});
246246

247+
it('should render correctly when fade is set', () => {
248+
mount(
249+
<Carousel defaultActiveIndex={1} fade>
250+
{items}
251+
</Carousel>,
252+
).assertSingle('.carousel-fade');
253+
});
254+
247255
describe('automatic traversal', () => {
248256
let clock;
249257

@@ -544,4 +552,78 @@ describe('<Carousel>', () => {
544552
expect(carouselItems.at(1).is('.active')).to.be.true;
545553
});
546554
});
555+
556+
describe('callback tests', () => {
557+
let clock;
558+
559+
beforeEach(() => {
560+
clock = sinon.useFakeTimers();
561+
});
562+
563+
afterEach(() => {
564+
clock.restore();
565+
});
566+
567+
it('should call onSlide when slide animation is disabled', () => {
568+
const onSlideSpy = sinon.spy();
569+
const wrapper = mount(
570+
<Carousel slide={false} onSelect={() => {}} onSlide={onSlideSpy}>
571+
{items}
572+
</Carousel>,
573+
);
574+
575+
wrapper.find('a.carousel-control-next').simulate('click');
576+
clock.tick(150);
577+
onSlideSpy.should.have.been.calledOnce;
578+
579+
wrapper.find('a.carousel-control-prev').simulate('click');
580+
clock.tick(150);
581+
onSlideSpy.should.have.been.calledTwice;
582+
});
583+
584+
it('should call onSlid when slide animation is disabled', () => {
585+
const onSlidSpy = sinon.spy();
586+
const wrapper = mount(
587+
<Carousel slide={false} onSelect={() => {}} onSlid={onSlidSpy}>
588+
{items}
589+
</Carousel>,
590+
);
591+
592+
wrapper.find('a.carousel-control-next').simulate('click');
593+
clock.tick(150);
594+
onSlidSpy.should.have.been.calledOnce;
595+
596+
wrapper.find('a.carousel-control-prev').simulate('click');
597+
clock.tick(150);
598+
onSlidSpy.should.have.been.calledTwice;
599+
});
600+
601+
it('should transition/call onSelect once if previous arrow double clicked', () => {
602+
const onSelectSpy = sinon.spy();
603+
const wrapper = mount(
604+
<Carousel onSelect={onSelectSpy}>{items}</Carousel>,
605+
);
606+
607+
const prev = wrapper.find('a.carousel-control-prev');
608+
prev.simulate('click');
609+
prev.simulate('click');
610+
611+
clock.tick(1000);
612+
onSelectSpy.should.have.been.calledOnce;
613+
});
614+
615+
it('should transition/call onSelect once if next arrow double clicked', () => {
616+
const onSelectSpy = sinon.spy();
617+
const wrapper = mount(
618+
<Carousel onSelect={onSelectSpy}>{items}</Carousel>,
619+
);
620+
621+
const next = wrapper.find('a.carousel-control-next');
622+
next.simulate('click');
623+
next.simulate('click');
624+
625+
clock.tick(1000);
626+
onSelectSpy.should.have.been.calledOnce;
627+
});
628+
});
547629
});

test/DropdownItemSpec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,17 @@ describe('<Dropdown.Item>', () => {
118118
.simulate('click');
119119
});
120120

121+
it('should not call onSelect for disabled item that is not a SafeAnchor', () => {
122+
const onSelectSpy = sinon.spy();
123+
mount(
124+
<Dropdown.Item as="div" onSelect={onSelectSpy} disabled>
125+
Text
126+
</Dropdown.Item>,
127+
).simulate('click');
128+
129+
onSelectSpy.should.not.have.been.called;
130+
});
131+
121132
it('should pass through props', () => {
122133
let node = mount(
123134
<Dropdown.Item

test/DropdownSpec.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,22 @@ describe('<Dropdown>', () => {
3636
node.className.should.match(/\bdropup\b/);
3737
});
3838

39+
it('renders div with dropright class', () => {
40+
mount(
41+
<Dropdown title="Dropup" drop="right">
42+
{dropdownChildren}
43+
</Dropdown>,
44+
).assertSingle('.dropright');
45+
});
46+
47+
it('renders div with dropleft class', () => {
48+
mount(
49+
<Dropdown title="Dropup" drop="left">
50+
{dropdownChildren}
51+
</Dropdown>,
52+
).assertSingle('.dropleft');
53+
});
54+
3955
it('renders toggle with Dropdown.Toggle', () => {
4056
const buttonNode = mount(simpleDropdown)
4157
.find('DropdownToggle')

test/InputGroupSpec.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ describe('<InputGroup>', () => {
99
expect(wrapper.find('div').length).to.equal(1);
1010
});
1111

12+
it('Should render size correctly', () => {
13+
mount(<InputGroup size="sm" />).assertSingle('.input-group-sm');
14+
});
15+
1216
describe('<Checkbox>', () => {
1317
it('Should forward props to underlying input element', () => {
1418
const name = 'foobar';

test/NavbarSpec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,4 +228,16 @@ describe('<Navbar>', () => {
228228
const wrapper = mount(<Navbar />);
229229
expect(wrapper.find('nav').length).to.equal(1);
230230
});
231+
232+
it('Should render correctly when expand is a string', () => {
233+
mount(<Navbar expand="sm" />).assertSingle('.navbar-expand-sm');
234+
});
235+
236+
it('Should render correctly when bg is set', () => {
237+
mount(<Navbar bg="light" />).assertSingle('.navbar.bg-light');
238+
});
239+
240+
it('Should render correctly when sticky is set', () => {
241+
mount(<Navbar sticky="top" />).assertSingle('.navbar.sticky-top');
242+
});
231243
});

test/PageItemSpec.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import { mount } from 'enzyme';
33

4-
import { First } from '../src/PageItem';
4+
import PageItem, { First } from '../src/PageItem';
55

66
describe('<PageItem>', () => {
77
describe('<First>', () => {
@@ -18,5 +18,13 @@ describe('<PageItem>', () => {
1818
);
1919
expect(inner.text()).to.equal(innerHTML);
2020
});
21+
22+
it('should render a span if active is true', () => {
23+
mount(<PageItem active />).assertSingle('span.page-link');
24+
});
25+
26+
it('should render a span if disabled is true', () => {
27+
mount(<PageItem disabled />).assertSingle('span.page-link');
28+
});
2129
});
2230
});

test/PaginationSpec.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,10 @@ describe('<Pagination>', () => {
77
it('should have class', () => {
88
mount(<Pagination>Item content</Pagination>).assertSingle('.pagination');
99
});
10+
11+
it('should render correctly when size is set', () => {
12+
mount(<Pagination size="sm">Item content</Pagination>).assertSingle(
13+
'.pagination.pagination-sm',
14+
);
15+
});
1016
});

test/RowSpec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ describe('Row', () => {
1212
mount(<Row xs={4} md={8} />).assertSingle('.row-cols-md-8.row-cols-4');
1313
});
1414

15+
it('Should allow sizes as objects', () => {
16+
mount(<Row xs={{ cols: 4 }} md={{ cols: 8 }} />).assertSingle(
17+
'.row-cols-md-8.row-cols-4',
18+
);
19+
});
20+
1521
it('uses "div" by default', () => {
1622
mount(
1723
<Row className="custom-class">
@@ -23,4 +29,8 @@ describe('Row', () => {
2329
it('should allow custom elements instead of "div"', () => {
2430
mount(<Row as="section" />).assertSingle('section.row');
2531
});
32+
33+
it('Should render no-gutters correctly', () => {
34+
mount(<Row noGutters />).assertSingle('.row.no-gutters');
35+
});
2636
});

0 commit comments

Comments
 (0)