-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
AccordionSpec.js
237 lines (203 loc) · 7.42 KB
/
AccordionSpec.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
import { mount } from 'enzyme';
import { fireEvent, render } from '@testing-library/react';
import Accordion from '../src/Accordion';
import AccordionCollapse from '../src/AccordionCollapse';
import Dropdown from '../src/Dropdown';
import ListGroup from '../src/ListGroup';
import Nav from '../src/Nav';
describe('<Accordion>', () => {
it('should output a div', () => {
mount(<Accordion />).assertSingle('div');
});
it('should render flush prop', () => {
mount(<Accordion flush />).assertSingle('.accordion.accordion-flush');
});
it('should output a h1', () => {
const wrapper = mount(
<Accordion>
<Accordion.Button>Hi</Accordion.Button>
<AccordionCollapse as="h1" eventKey="0">
<span>hidden Data</span>
</AccordionCollapse>
</Accordion>,
);
wrapper.find('AccordionCollapse').assertSingle('h1');
});
it('should only have second item collapsed', () => {
const wrapper = mount(
<Accordion defaultActiveKey="0">
<Accordion.Item eventKey="0">
<Accordion.Header />
<Accordion.Body>body text</Accordion.Body>
</Accordion.Item>
<Accordion.Item eventKey="1">
<Accordion.Header />
<Accordion.Body>body text</Accordion.Body>
</Accordion.Item>
</Accordion>,
);
const collapses = wrapper.find('AccordionCollapse');
collapses.at(0).getDOMNode().className.should.include('show');
collapses.at(1).getDOMNode().className.should.include('collapse');
});
it('should expand next item and collapse current item on click', () => {
const onClickSpy = sinon.spy();
const wrapper = mount(
<Accordion>
<Accordion.Item eventKey="0">
<Accordion.Header onClick={onClickSpy} />
<Accordion.Body>body text</Accordion.Body>
</Accordion.Item>
<Accordion.Item eventKey="1">
<Accordion.Header onClick={onClickSpy} />
<Accordion.Body>body text</Accordion.Body>
</Accordion.Item>
</Accordion>,
);
wrapper.find('AccordionHeader').at(1).find('button').simulate('click');
onClickSpy.should.be.calledOnce;
const collapses = wrapper.find('AccordionCollapse');
collapses.at(0).getDOMNode().className.should.include('collapse');
// Enzyme doesn't really provide support for async utilities
// on components, but in an ideal setup we should be testing for
// this className to be `show` after the collapsing animation is done
// (which is possible in `@testing-library` via `waitForElement`).
// https://testing-library.com/docs/dom-testing-library/api-async#waitforelement
collapses.at(1).getDOMNode().className.should.include('collapsing');
});
it('should collapse current item on click', () => {
const onClickSpy = sinon.spy();
const wrapper = mount(
<Accordion defaultActiveKey="0">
<Accordion.Item eventKey="0">
<Accordion.Header onClick={onClickSpy} />
<Accordion.Body>body text</Accordion.Body>
</Accordion.Item>
<Accordion.Item eventKey="1">
<Accordion.Header onClick={onClickSpy} />
<Accordion.Body>body text</Accordion.Body>
</Accordion.Item>
</Accordion>,
);
wrapper.find('AccordionHeader').at(0).find('button').simulate('click');
onClickSpy.should.be.calledOnce;
const collapses = wrapper.find('AccordionCollapse');
collapses.at(0).getDOMNode().className.should.include('collapse');
collapses.at(1).getDOMNode().className.should.include('collapse');
// Enzyme doesn't really provide support for async utilities
// on components, but in an ideal setup we should be testing for
// this className to be `show` after the collapsing animation is done
// (which is possible in `@testing-library` via `waitForElement`).
// https://testing-library.com/docs/dom-testing-library/api-async#waitforelement
collapses.at(0).getDOMNode().className.should.include('collapsing');
});
// https://github.com/react-bootstrap/react-bootstrap/issues/4176
it('Should not close accordion when child dropdown clicked', () => {
const wrapper = mount(
<Accordion defaultActiveKey="0">
<Accordion.Item eventKey="0">
<Accordion.Header />
<Accordion.Body>
<Dropdown show>
<Dropdown.Toggle id="dropdown-test">
Dropdown Button
</Dropdown.Toggle>
<Dropdown.Menu>
<Dropdown.Item href="#">Action</Dropdown.Item>
</Dropdown.Menu>
</Dropdown>
</Accordion.Body>
</Accordion.Item>
</Accordion>,
);
wrapper.find('a.dropdown-item').simulate('click');
wrapper
.find('AccordionCollapse')
.at(0)
.getDOMNode()
.className.should.include('show');
});
it('Should not close accordion when child ListGroup clicked', () => {
const wrapper = mount(
<Accordion defaultActiveKey="0">
<Accordion.Item eventKey="0">
<Accordion.Header />
<Accordion.Body>
<ListGroup defaultActiveKey="#link1">
<ListGroup.Item action href="#link1">
Link 1
</ListGroup.Item>
</ListGroup>
</Accordion.Body>
</Accordion.Item>
</Accordion>,
);
wrapper.find('ListGroupItem').simulate('click');
wrapper
.find('AccordionCollapse')
.at(0)
.getDOMNode()
.className.should.include('show');
});
it('Should not close accordion when child Nav clicked', () => {
const wrapper = mount(
<Accordion defaultActiveKey="0">
<Accordion.Item eventKey="0">
<Accordion.Header />
<Accordion.Body>
<Nav activeKey="/home">
<Nav.Item>
<Nav.Link href="#">Active</Nav.Link>
</Nav.Item>
</Nav>
</Accordion.Body>
</Accordion.Item>
</Accordion>,
);
wrapper.find('NavLink').simulate('click');
wrapper
.find('AccordionCollapse')
.at(0)
.getDOMNode()
.className.should.include('show');
});
it('should allow multiple items to stay open', () => {
const onSelectSpy = sinon.spy();
const { getByText } = render(
<Accordion onSelect={onSelectSpy} alwaysOpen>
<Accordion.Item eventKey="0">
<Accordion.Header>header0</Accordion.Header>
<Accordion.Body>body</Accordion.Body>
</Accordion.Item>
<Accordion.Item eventKey="1">
<Accordion.Header>header1</Accordion.Header>
<Accordion.Body>body</Accordion.Body>
</Accordion.Item>
</Accordion>,
);
fireEvent.click(getByText('header0'));
fireEvent.click(getByText('header1'));
onSelectSpy.should.be.calledWith(['0', '1']);
});
it('should remove only one of the active indices', () => {
const onSelectSpy = sinon.spy();
const { getByText } = render(
<Accordion
onSelect={onSelectSpy}
defaultActiveKey={['0', '1']}
alwaysOpen
>
<Accordion.Item eventKey="0">
<Accordion.Header>header0</Accordion.Header>
<Accordion.Body>body</Accordion.Body>
</Accordion.Item>
<Accordion.Item eventKey="1">
<Accordion.Header>header1</Accordion.Header>
<Accordion.Body>body</Accordion.Body>
</Accordion.Item>
</Accordion>,
);
fireEvent.click(getByText('header1'));
onSelectSpy.should.be.calledWith(['0']);
});
});