-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNavSpec.js
313 lines (258 loc) · 9.32 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import keycode from 'keycode';
import React from 'react';
import ReactTestUtils from 'react-dom/test-utils';
import { mount } from 'enzyme';
import Nav from '../src/Nav';
import NavItem from '../src/NavItem';
import { shouldWarn } from './helpers';
describe('<Nav>', () => {
let mountPoint;
beforeEach(() => {
mountPoint = document.createElement('div');
document.body.appendChild(mountPoint);
});
afterEach(() => {
document.body.removeChild(mountPoint);
});
it('Should set the correct item active', () => {
const instance = ReactTestUtils.renderIntoDocument(
<Nav bsStyle="pills" activeKey={1}>
<NavItem eventKey={1}>Pill 1 content</NavItem>
<NavItem eventKey={2}>Pill 2 content</NavItem>
</Nav>
);
const items = ReactTestUtils.scryRenderedComponentsWithType(
instance,
NavItem
);
assert.ok(items[0].props.active);
assert.notOk(items[1].props.active);
});
it('Should adds style class', () => {
const instance = ReactTestUtils.renderIntoDocument(
<Nav bsStyle="tabs" activeKey={1}>
<NavItem eventKey={1}>Tab 1 content</NavItem>
<NavItem eventKey={2}>Tab 2 content</NavItem>
</Nav>
);
assert.ok(
ReactTestUtils.findRenderedDOMComponentWithClass(instance, 'nav')
);
assert.ok(
ReactTestUtils.findRenderedDOMComponentWithClass(instance, 'nav-tabs')
);
});
it('Should adds stacked variation class', () => {
const instance = ReactTestUtils.renderIntoDocument(
<Nav bsStyle="tabs" stacked activeKey={1}>
<NavItem eventKey={1}>Tab 1 content</NavItem>
<NavItem eventKey={2}>Tab 2 content</NavItem>
</Nav>
);
assert.ok(
ReactTestUtils.findRenderedDOMComponentWithClass(instance, 'nav-stacked')
);
});
it('Should adds variation class', () => {
const instance = ReactTestUtils.renderIntoDocument(
<Nav bsStyle="tabs" justified activeKey={1}>
<NavItem eventKey={1}>Tab 1 content</NavItem>
<NavItem eventKey={2}>Tab 2 content</NavItem>
</Nav>
);
assert.ok(
ReactTestUtils.findRenderedDOMComponentWithClass(
instance,
'nav-justified'
)
);
});
it('Should add pull-right class', () => {
const instance = ReactTestUtils.renderIntoDocument(
<Nav bsStyle="tabs" pullRight activeKey={1}>
<NavItem eventKey={1}>Tab 1 content</NavItem>
<NavItem eventKey={2}>Tab 2 content</NavItem>
</Nav>
);
assert.ok(
ReactTestUtils.findRenderedDOMComponentWithClass(instance, 'pull-right')
);
});
it('Should add navbar-right class', () => {
const instance = ReactTestUtils.renderIntoDocument(
<Nav bsStyle="tabs" navbar pullRight activeKey={1}>
<NavItem key={1}>Tab 1 content</NavItem>
<NavItem key={2}>Tab 2 content</NavItem>
</Nav>
);
assert.ok(
ReactTestUtils.findRenderedDOMComponentWithClass(instance, 'navbar-right')
);
});
it('Should call on select when item is selected', done => {
function handleSelect(key) {
assert.equal(key, '2');
done();
}
const instance = ReactTestUtils.renderIntoDocument(
<Nav bsStyle="tabs" activeKey={1} onSelect={handleSelect}>
<NavItem eventKey={1}>Tab 1 content</NavItem>
<NavItem eventKey={2}>
<span>Tab 2 content</span>
</NavItem>
</Nav>
);
const items = ReactTestUtils.scryRenderedDOMComponentsWithTag(
instance,
'A'
);
ReactTestUtils.Simulate.click(items[1]);
});
it('Should set the correct item active by href', () => {
const instance = ReactTestUtils.renderIntoDocument(
<Nav bsStyle="pills" activeHref="#item2">
<NavItem eventKey={1} href="#item1">
Pill 1 content
</NavItem>
<NavItem eventKey={2} href="#item2">
Pill 2 content
</NavItem>
</Nav>
);
const items = ReactTestUtils.scryRenderedComponentsWithType(
instance,
NavItem
);
assert.ok(items[1].props.active);
assert.notOk(items[0].props.active);
});
it('Should warn when attempting to use a justified navbar nav', () => {
shouldWarn('justified navbar `Nav`s are not supported');
ReactTestUtils.renderIntoDocument(<Nav navbar justified />);
});
describe('keyboard navigation', () => {
let instance;
let selectSpy;
beforeEach(() => {
instance = mount(
<Nav activeKey={1} onSelect={selectSpy} role="tablist">
<NavItem eventKey={1}>NavItem 1 content</NavItem>
<NavItem eventKey={2} disabled>
NavItem 2 content
</NavItem>
<NavItem eventKey={3}>NavItem 3 content</NavItem>
<NavItem eventKey={4} disabled>
NavItem 4 content
</NavItem>
<NavItem eventKey={5}>NavItem 5 content</NavItem>
{false && <NavItem eventKey={6}>NavItem 6 content</NavItem>}
</Nav>,
{ attachTo: mountPoint }
);
selectSpy = sinon.spy(activeKey => instance.setProps({ activeKey }));
});
afterEach(() => instance.unmount());
it('only the active tab should be focusable', () => {
const links = instance.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.equal('-1');
expect(links[3].getAttribute('tabindex')).to.equal('-1');
expect(links[4].getAttribute('tabindex')).to.equal('-1');
});
it('should focus the next tab on arrow key', () => {
const anchors = instance.find('a').map(n => n.getDOMNode());
anchors[0].focus();
ReactTestUtils.Simulate.keyDown(anchors[0], {
keyCode: keycode('right')
});
expect(instance.prop('activeKey')).to.equal(3);
expect(document.activeElement).to.equal(anchors[2]);
});
it('should focus the previous tab on arrow key', () => {
instance.setProps({ activeKey: 5 });
const anchors = instance.find('a').map(n => n.getDOMNode());
anchors[4].focus();
ReactTestUtils.Simulate.keyDown(anchors[4], { keyCode: keycode('left') });
expect(instance.props().activeKey).to.equal(3);
expect(document.activeElement).to.equal(anchors[2]);
});
it('should wrap to the next tab on arrow key', () => {
instance.setProps({ activeKey: 5 });
const anchors = instance.find('a').map(n => n.getDOMNode());
anchors[4].focus();
ReactTestUtils.Simulate.keyDown(anchors[4], { keyCode: keycode('down') });
expect(instance.props().activeKey).to.equal(1);
expect(document.activeElement).to.equal(anchors[0]);
});
it('should wrap to the previous tab on arrow key', () => {
const anchors = instance.find('a').map(n => n.getDOMNode());
anchors[0].focus();
ReactTestUtils.Simulate.keyDown(anchors[0], { keyCode: keycode('up') });
expect(instance.props().activeKey).to.equal(5);
expect(document.activeElement).to.equal(anchors[4]);
});
});
describe('event keys', () => {
it('should accept any number as an event key', () => {
let instance;
let selectSpy = sinon.spy(activeKey => instance.setProps({ activeKey }));
instance = mount(
<Nav activeKey={-100} onSelect={selectSpy} role="tablist">
<NavItem eventKey={-100}>NavItem 1 content</NavItem>
<NavItem eventKey={0}>NavItem 2 content</NavItem>
<NavItem eventKey={1}>NavItem 3 content</NavItem>
</Nav>,
{ attachTo: mountPoint }
);
const anchors = instance.find('a').map(n => n.getDOMNode());
anchors[0].focus();
ReactTestUtils.Simulate.keyDown(anchors[0], {
keyCode: keycode('right')
});
expect(instance.props().activeKey).to.equal(0);
expect(document.activeElement).to.equal(anchors[1]);
instance.unmount();
});
it('should accept any string as an event key', () => {
let instance;
let selectSpy = sinon.spy(activeKey => instance.setProps({ activeKey }));
instance = mount(
<Nav activeKey="" onSelect={selectSpy} role="tablist">
<NavItem eventKey="a">NavItem 1 content</NavItem>
<NavItem eventKey="b">NavItem 2 content</NavItem>
<NavItem eventKey="">NavItem 3 content</NavItem>
</Nav>,
{ attachTo: mountPoint }
);
const anchors = instance.find('a').map(n => n.getDOMNode());
anchors[2].focus();
ReactTestUtils.Simulate.keyDown(anchors[2], {
keyCode: keycode('right')
});
expect(instance.props().activeKey).to.equal('a');
expect(document.activeElement).to.equal(anchors[0]);
instance.unmount();
});
});
describe('Web Accessibility', () => {
it('Should have tablist and tab roles', () => {
const instance = ReactTestUtils.renderIntoDocument(
<Nav role="tablist" bsStyle="tabs" activeKey={1}>
<NavItem key={1}>Tab 1 content</NavItem>
<NavItem key={2}>Tab 2 content</NavItem>
</Nav>
);
const ul = ReactTestUtils.scryRenderedDOMComponentsWithTag(
instance,
'ul'
)[0];
const navItem = ReactTestUtils.scryRenderedDOMComponentsWithTag(
instance,
'a'
)[0];
assert.equal(ul.getAttribute('role'), 'tablist');
assert.equal(navItem.getAttribute('role'), 'tab');
});
});
});