Skip to content

Commit 85028b7

Browse files
authored
test: port TableSpec, TabContentSpec and SplitButtonSpec to rtl (react-bootstrap#6179)
* port Tablespec * migrate tabcontentspec * port splitbuttonspec
1 parent 9345f1c commit 85028b7

File tree

6 files changed

+222
-160
lines changed

6 files changed

+222
-160
lines changed

test/SplitButtonSpec.js

Lines changed: 0 additions & 108 deletions
This file was deleted.

test/SplitButtonSpec.tsx

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
import { fireEvent, render } from '@testing-library/react';
2+
import sinon from 'sinon';
3+
import SplitButton from '../src/SplitButton';
4+
import DropdownItem from '../src/DropdownItem';
5+
6+
describe('<SplitButton>', () => {
7+
const simple = (
8+
<SplitButton data-testid="test-wrapper" title="Title" id="test-id">
9+
<DropdownItem>Item 1</DropdownItem>
10+
<DropdownItem>Item 2</DropdownItem>
11+
<DropdownItem>Item 3</DropdownItem>
12+
<DropdownItem>Item 4</DropdownItem>
13+
</SplitButton>
14+
);
15+
16+
it('should open the menu when dropdown button is clicked', () => {
17+
const { getByTestId } = render(simple);
18+
const splitButtonElem = getByTestId('test-wrapper');
19+
20+
splitButtonElem.classList.contains('show').should.be.false;
21+
fireEvent.click(splitButtonElem.children[1]);
22+
23+
splitButtonElem.classList.contains('show').should.be.true;
24+
});
25+
26+
it('should not open the menu when other button is clicked', () => {
27+
const { getByTestId } = render(simple);
28+
const splitButtonElem = getByTestId('test-wrapper');
29+
30+
splitButtonElem.classList.contains('show').should.be.false;
31+
fireEvent.click(splitButtonElem.children[0]);
32+
splitButtonElem.classList.contains('show').should.be.false;
33+
});
34+
35+
it('should invoke onClick when SplitButton.Button is clicked (prop)', (done) => {
36+
const { getByTestId } = render(
37+
<SplitButton
38+
data-testid="test-wrapper"
39+
title="Title"
40+
id="test-id"
41+
onClick={() => done()}
42+
>
43+
<DropdownItem>Item 1</DropdownItem>
44+
</SplitButton>,
45+
);
46+
const splitButtonElem = getByTestId('test-wrapper');
47+
48+
fireEvent.click(splitButtonElem.firstElementChild!);
49+
});
50+
51+
it('should not invoke onClick when SplitButton.Toggle is clicked (prop)', () => {
52+
const onClickSpy = sinon.spy();
53+
54+
const { getByTestId } = render(
55+
<SplitButton
56+
data-testid="test-wrapper"
57+
title="Title"
58+
id="test-id"
59+
onClick={onClickSpy}
60+
>
61+
<DropdownItem>Item 1</DropdownItem>
62+
</SplitButton>,
63+
);
64+
const splitButtonElem = getByTestId('test-wrapper');
65+
fireEvent.click(splitButtonElem.children[1]);
66+
67+
onClickSpy.should.not.have.been.called;
68+
});
69+
70+
it('Should pass disabled to both buttons', () => {
71+
const { getByTestId } = render(
72+
<SplitButton
73+
data-testid="test-wrapper"
74+
title="Title"
75+
id="test-id"
76+
disabled
77+
>
78+
<DropdownItem>Item 1</DropdownItem>
79+
</SplitButton>,
80+
);
81+
const splitButtonElem = getByTestId('test-wrapper');
82+
83+
splitButtonElem.getAttribute('disabled')!.should.exist;
84+
splitButtonElem.children[0].getAttribute('disabled')!.should.exist;
85+
splitButtonElem.children[1].getAttribute('disabled')!.should.exist;
86+
});
87+
88+
it('Should set target attribute on anchor', () => {
89+
const { getByTestId } = render(
90+
<SplitButton
91+
title="Title"
92+
id="test-id"
93+
data-testid="test-wrapper"
94+
href="/some/unique-thing/"
95+
target="_blank"
96+
>
97+
<DropdownItem eventKey="1">DropdownItem 1 content</DropdownItem>
98+
</SplitButton>,
99+
);
100+
const splitButtonElem = getByTestId('test-wrapper');
101+
splitButtonElem.firstElementChild!.tagName.toLowerCase().should.equal('a');
102+
splitButtonElem
103+
.firstElementChild!.getAttribute('href')!
104+
.should.equal('/some/unique-thing/');
105+
splitButtonElem
106+
.firstElementChild!.getAttribute('target')!
107+
.should.equal('_blank');
108+
});
109+
110+
it('should set accessible label on toggle', () => {
111+
const { getByText } = render(simple);
112+
const toggleLabelElem = getByText('Toggle dropdown');
113+
toggleLabelElem.classList.contains('visually-hidden').should.be.true;
114+
});
115+
116+
it('should set aria-label on toggle from toggleLabel', () => {
117+
const { getByText } = render(
118+
<SplitButton title="Title" id="test-id" toggleLabel="Label">
119+
<DropdownItem>Item 1</DropdownItem>
120+
</SplitButton>,
121+
);
122+
const labelElem = getByText('Label');
123+
labelElem.classList.contains('visually-hidden').should.be.true;
124+
});
125+
126+
it('should set type attribute from type', () => {
127+
const { getByTestId } = render(
128+
<SplitButton
129+
data-testid="test-wrapper"
130+
title="Title"
131+
id="test-id"
132+
type="submit"
133+
>
134+
<DropdownItem>Item 1</DropdownItem>
135+
</SplitButton>,
136+
);
137+
const splitButtonElem = getByTestId('test-wrapper');
138+
splitButtonElem
139+
.firstElementChild!.getAttribute('type')!
140+
.should.equal('submit');
141+
});
142+
});

test/TabContentSpec.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

test/TabContentSpec.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { render } from '@testing-library/react';
2+
3+
import TabContent from '../src/TabContent';
4+
5+
describe('<TabContent>', () => {
6+
it('Should have div as default component', () => {
7+
const { container } = render(<TabContent />);
8+
9+
container.tagName.toLowerCase().should.equal('div');
10+
});
11+
});

test/TableSpec.js

Lines changed: 0 additions & 43 deletions
This file was deleted.

test/TableSpec.tsx

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { render } from '@testing-library/react';
2+
3+
import Table from '../src/Table';
4+
5+
describe('Table', () => {
6+
it('Should be a table', () => {
7+
const { getByTestId } = render(<Table data-testid="test" />);
8+
const tableElem = getByTestId('test');
9+
10+
tableElem.classList.contains('table').should.be.true;
11+
tableElem.tagName.toLowerCase().should.equal('table');
12+
});
13+
14+
it('Should have correct class when striped', () => {
15+
const { getByTestId } = render(<Table data-testid="test" striped />);
16+
const tableElem = getByTestId('test');
17+
18+
tableElem.classList.contains('table-striped').should.be.true;
19+
});
20+
21+
it('Should have correct class when hover', () => {
22+
const { getByTestId } = render(<Table data-testid="test" hover />);
23+
const tableElem = getByTestId('test');
24+
25+
tableElem.classList.contains('table-hover').should.be.true;
26+
});
27+
28+
it('Should have correct class when bordered', () => {
29+
const { getByTestId } = render(<Table data-testid="test" bordered />);
30+
const tableElem = getByTestId('test');
31+
32+
tableElem.classList.contains('table-bordered').should.be.true;
33+
});
34+
35+
it('Should have correct class when borderless', () => {
36+
const { getByTestId } = render(<Table data-testid="test" borderless />);
37+
const tableElem = getByTestId('test');
38+
39+
tableElem.classList.contains('table-borderless').should.be.true;
40+
});
41+
42+
it('Should have correct class when small', () => {
43+
const { getByTestId } = render(<Table data-testid="test" size="sm" />);
44+
const tableElem = getByTestId('test');
45+
46+
tableElem.classList.contains('table-sm').should.be.true;
47+
});
48+
49+
it('Should have correct class when dark', () => {
50+
const { getByTestId } = render(<Table data-testid="test" variant="dark" />);
51+
const tableElem = getByTestId('test');
52+
53+
tableElem.classList.contains('table-dark').should.be.true;
54+
});
55+
56+
it('Should have responsive wrapper', () => {
57+
const { container } = render(<Table responsive />);
58+
const tableElem = container.firstElementChild!;
59+
60+
tableElem!.classList.contains('table-responsive').should.be.true;
61+
});
62+
63+
it('Should have responsive breakpoints', () => {
64+
const { container } = render(<Table responsive="sm" />);
65+
const tableElem = container.firstElementChild!;
66+
67+
tableElem!.classList.contains('table-responsive-sm').should.be.true;
68+
});
69+
});

0 commit comments

Comments
 (0)