forked from react-bootstrap/react-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNavSpec.js
292 lines (242 loc) · 7.79 KB
/
NavSpec.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
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
import { mount } from 'enzyme';
import React from 'react';
import Card from '../src/Card';
import Nav from '../src/Nav';
import Navbar from '../src/Navbar';
import NavDropdown from '../src/NavDropdown';
import { shouldWarn } from './helpers';
describe('<Nav>', () => {
let mountPoint;
beforeEach(() => {
mountPoint = document.createElement('div');
document.body.appendChild(mountPoint);
});
afterEach(() => {
document.body.removeChild(mountPoint);
});
it('should have div as default component', () => {
mount(<Nav />).assertSingle('div');
});
it('should set the correct item active', () => {
const wrapper = mount(
<Nav variant="pills" defaultActiveKey={1}>
<Nav.Link eventKey={1}>Pill 1 content</Nav.Link>
<Nav.Link eventKey={2}>Pill 2 content</Nav.Link>
</Nav>,
);
const items = wrapper.find('a.nav-link');
assert.ok(items.at(0).is('.active'));
assert.notOk(items.at(1).is('.active'));
});
it('should add variant class', () => {
mount(
<Nav variant="tabs">
<Nav.Link eventKey={1}>Pill 1 content</Nav.Link>
<Nav.Link eventKey={2}>Pill 2 content</Nav.Link>
</Nav>,
).assertSingle('div.nav.nav-tabs');
});
it('should add justified class', () => {
mount(
<Nav justify>
<Nav.Link eventKey={1}>Pill 1 content</Nav.Link>
<Nav.Link eventKey={2}>Pill 2 content</Nav.Link>
</Nav>,
).assertSingle('div.nav.nav-justified');
});
it('should add fill class', () => {
mount(
<Nav fill>
<Nav.Link eventKey={1}>Pill 1 content</Nav.Link>
<Nav.Link eventKey={2}>Pill 2 content</Nav.Link>
</Nav>,
).assertSingle('div.nav.nav-fill');
});
it('should be navbar aware', () => {
mount(
<Navbar>
<Nav>
<Nav.Link eventKey={1}>Pill 1 content</Nav.Link>
<Nav.Link eventKey={2}>Pill 2 content</Nav.Link>
</Nav>
</Navbar>,
).assertSingle('div.navbar-nav');
});
it('should be card aware', () => {
mount(
<Card>
<Nav variant="pills">
<Nav.Link eventKey={1}>Pill 1 content</Nav.Link>
<Nav.Link eventKey={2}>Pill 2 content</Nav.Link>
</Nav>
</Card>,
).assertSingle('div.card-header-pills');
});
it('should call onSelect when a Nav.Link is selected', (done) => {
function handleSelect(key) {
assert.equal(key, '2');
done();
}
mount(
<Nav onSelect={handleSelect}>
<Nav.Link eventKey={1}>Tab 1 content</Nav.Link>
<Nav.Link eventKey={2}>
<span>Tab 2 content</span>
</Nav.Link>
</Nav>,
)
.find('a')
.last()
.simulate('click');
});
it('should call onSelect when a NavDropdown.Item is selected', () => {
const onSelectSpy = sinon.spy();
mount(
<Nav onSelect={onSelectSpy}>
<NavDropdown title="Dropdown" id="nav-dropdown-test" renderMenuOnMount>
<NavDropdown.Item eventKey={1}>Dropdown item</NavDropdown.Item>
</NavDropdown>
</Nav>,
)
.find('DropdownItem')
.simulate('click');
onSelectSpy.should.have.been.calledOnce;
});
it('should set the correct item active by href', () => {
mount(
<Nav defaultActiveKey="#item1">
<Nav.Link href="#item1" className="test-selected">
Pill 1 content
</Nav.Link>
<Nav.Link href="#item2">Pill 2 content</Nav.Link>
</Nav>,
).assertSingle('a.test-selected.active');
});
it('should warn when attempting to use a justify navbar nav', () => {
shouldWarn('justify navbar `Nav`s are not supported');
mount(<Nav navbar justify />);
});
describe('keyboard navigation', () => {
let wrapper;
let selectSpy;
let keyDownSpy;
beforeEach(() => {
selectSpy = sinon.spy((activeKey) => {
wrapper.setProps({ activeKey });
});
keyDownSpy = sinon.spy();
wrapper = mount(
<Nav
activeKey={1}
onSelect={selectSpy}
onKeyDown={keyDownSpy}
role="tablist"
>
<Nav.Link eventKey={1}>Nav.Link 1 content</Nav.Link>
<Nav.Link eventKey={2} disabled>
Nav.Link 2 content
</Nav.Link>
<Nav.Link eventKey={3}>Nav.Link 3 content</Nav.Link>
<Nav.Link eventKey={4} disabled>
Nav.Link 4 content
</Nav.Link>
<Nav.Link eventKey={5}>Nav.Link 5 content</Nav.Link>
</Nav>,
{ attachTo: mountPoint },
);
});
afterEach(() => wrapper.unmount());
it('should not allow focusing on disabled tabs', () => {
const links = wrapper.find('a').map((n) => n.getDOMNode());
expect(links[0].getAttribute('tabindex')).to.not.equal('-1');
expect(links[1].getAttribute('tabindex')).to.equal('-1');
expect(links[2].getAttribute('tabindex')).to.not.equal('-1');
expect(links[3].getAttribute('tabindex')).to.equal('-1');
expect(links[4].getAttribute('tabindex')).to.not.equal('-1');
});
it('should focus the next tab on arrow key', () => {
const anchors = wrapper.find('a');
anchors
.at(0)
.tap((a) => a.getDOMNode().focus())
.simulate('keydown', {
key: 'ArrowRight',
});
expect(wrapper.prop('activeKey')).to.equal('3');
expect(document.activeElement).to.equal(anchors.at(2).getDOMNode());
});
it('should focus the previous tab on arrow key', () => {
wrapper.setProps({ activeKey: 5 });
const anchors = wrapper.find('a');
anchors
.at(4)
.tap((a) => a.getDOMNode().focus())
.simulate('keydown', {
key: 'ArrowLeft',
});
expect(wrapper.prop('activeKey')).to.equal('3');
expect(document.activeElement).to.equal(anchors.at(2).getDOMNode());
});
it('should wrap to the next tab on arrow key', () => {
wrapper.setProps({ activeKey: 5 });
const anchors = wrapper.find('a');
anchors
.at(4)
.tap((a) => a.getDOMNode().focus())
.simulate('keydown', {
key: 'ArrowDown',
});
expect(wrapper.prop('activeKey')).to.equal('1');
expect(document.activeElement).to.equal(anchors.at(0).getDOMNode());
});
it('should wrap to the previous tab on arrow key', () => {
const anchors = wrapper.find('a');
anchors
.at(0)
.tap((a) => a.getDOMNode().focus())
.simulate('keydown', {
key: 'ArrowUp',
});
expect(wrapper.prop('activeKey')).to.equal('5');
expect(document.activeElement).to.equal(anchors.at(4).getDOMNode());
});
it('should forward to a onKeyDown listener', () => {
const anchors = wrapper.find('a');
anchors
.at(0)
.tap((a) => a.getDOMNode().focus())
.simulate('keydown', {
key: 'ArrowUp',
});
sinon.assert.calledOnce(keyDownSpy);
});
['a', 'b', 'left', 'up'].forEach(({ key }) => {
it(`should ignore non-arrow keys: case ${key}`, () => {
const anchors = wrapper.find('a');
anchors
.at(0)
.tap((a) => a.getDOMNode().focus())
.simulate('keydown', {
key,
});
expect(document.activeElement).to.equal(anchors.at(0).getDOMNode());
sinon.assert.calledOnce(keyDownSpy);
});
});
});
describe('Web Accessibility', () => {
it('should have tablist and tab roles', () => {
const wrapper = mount(
<Nav>
<Nav.Link key={1}>Tab 1 content</Nav.Link>
<Nav.Link key={2}>Tab 2 content</Nav.Link>
</Nav>,
);
wrapper.assertNone('div.nav[role="tablist"]');
wrapper.assertNone('a[role="tab"]');
wrapper.setProps({ role: 'tablist' });
wrapper.assertSingle('div.nav[role="tablist"]');
wrapper.find('a[role="tab"]').length.should.equal(2);
});
});
});