forked from react-bootstrap/react-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDropdownSpec.js
465 lines (361 loc) · 15 KB
/
DropdownSpec.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
import keycode from 'keycode';
import React from 'react';
import ReactTestUtils from 'react/lib/ReactTestUtils';
import ReactDOM from 'react-dom';
import Dropdown from '../src/Dropdown';
import DropdownMenu from '../src/DropdownMenu';
import Grid from '../src/Grid';
import MenuItem from '../src/MenuItem';
import { shouldWarn } from './helpers';
class CustomMenu extends React.Component {
render() {
return (
<div className='custom-menu'>
{this.props.children}
</div>
);
}
}
describe('Dropdown', () => {
let BaseDropdown = Dropdown.ControlledComponent;
const dropdownChildren = [
<Dropdown.Toggle>
Child Title
</Dropdown.Toggle>,
<Dropdown.Menu>
<MenuItem>Item 1</MenuItem>
<MenuItem>Item 2</MenuItem>
<MenuItem>Item 3</MenuItem>
<MenuItem>Item 4</MenuItem>
</Dropdown.Menu>
];
const simpleDropdown = (
<Dropdown id='test-id'>
{dropdownChildren}
</Dropdown>
);
it('renders div with dropdown class', () => {
const instance = ReactTestUtils.renderIntoDocument(simpleDropdown);
const node = ReactDOM.findDOMNode(instance);
node.tagName.should.equal('DIV');
node.className.should.match(/\bdropdown\b/);
node.className.should.not.match(/\bdropup\b/);
});
it('renders div with dropup class', () => {
const instance = ReactTestUtils.renderIntoDocument(
<Dropdown title='Dropup' dropup id='test-id'>
{dropdownChildren}
</Dropdown>
);
const node = ReactDOM.findDOMNode(instance);
node.tagName.should.equal('DIV');
node.className.should.not.match(/\bdropdown\b/);
node.className.should.match(/\bdropup\b/);
});
it('renders toggle with Dropdown.Toggle', () => {
const instance = ReactTestUtils.renderIntoDocument(
simpleDropdown
);
const buttonNode = ReactTestUtils.findRenderedDOMComponentWithTag(instance, 'BUTTON');
buttonNode.innerText.should.match(/Child Title/);
buttonNode.tagName.should.equal('BUTTON');
buttonNode.className.should.match(/\bbtn[ $]/);
buttonNode.className.should.match(/\bbtn-default\b/);
buttonNode.className.should.match(/\bdropdown-toggle\b/);
buttonNode.getAttribute('type').should.equal('button');
buttonNode.getAttribute('aria-expanded').should.equal('false');
buttonNode.getAttribute('id').should.be.ok;
});
it('renders dropdown toggle button caret', () => {
const instance = ReactTestUtils.renderIntoDocument(simpleDropdown);
const caretNode = ReactTestUtils.findRenderedDOMComponentWithClass(instance, 'caret');
caretNode.tagName.should.equal('SPAN');
});
it('does not render toggle button caret', () => {
const instance = ReactTestUtils.renderIntoDocument(
<Dropdown.Toggle noCaret>
Child Text
</Dropdown.Toggle>
);
const caretNode = ReactTestUtils.scryRenderedDOMComponentsWithClass(instance, 'caret');
caretNode.length.should.equal(0);
});
it('renders custom menu', () => {
const instance = ReactTestUtils.renderIntoDocument(
<Dropdown title='Single child' id='test-id'>
<Dropdown.Toggle>Child Text</Dropdown.Toggle>
<CustomMenu bsRole='menu'>
<MenuItem>Item 1</MenuItem>
</CustomMenu>
</Dropdown>
);
ReactTestUtils.scryRenderedComponentsWithType(instance, DropdownMenu).length.should.equal(0);
ReactTestUtils.scryRenderedComponentsWithType(instance, CustomMenu).length.should.equal(1);
});
it('prop validation with multiple menus', () => {
const props = {
title: 'herpa derpa',
children: [(
<Dropdown.Toggle>Child Text</Dropdown.Toggle>
), (
<Dropdown.Menu>
<MenuItem>Item 1</MenuItem>
</Dropdown.Menu>
), (
<Dropdown.Menu>
<MenuItem>Item 1</MenuItem>
</Dropdown.Menu>
)]
};
let err = BaseDropdown.propTypes.children(props, 'children', 'DropdownButton');
err.message.should.match(/Duplicate children.*bsRole: menu/);
});
it('only renders one menu', () => {
const instance = ReactTestUtils.renderIntoDocument(
<Dropdown title='Single child' id='test-id'>
<Dropdown.Toggle>Child Text</Dropdown.Toggle>
<CustomMenu bsRole='menu'>
<MenuItem>Item 1</MenuItem>
</CustomMenu>
<DropdownMenu>
<MenuItem>Item 1</MenuItem>
</DropdownMenu>
</Dropdown>
);
ReactTestUtils.scryRenderedComponentsWithType(instance, DropdownMenu).length.should.equal(0);
ReactTestUtils.scryRenderedComponentsWithType(instance, CustomMenu).length.should.equal(1);
shouldWarn(/Duplicate children.*bsRole: menu/);
});
it('forwards pullRight to menu', () => {
const instance = ReactTestUtils.renderIntoDocument(
<Dropdown pullRight id='test-id'>
{dropdownChildren}
</Dropdown>
);
const menu = ReactTestUtils.findRenderedComponentWithType(instance, DropdownMenu);
menu.props.pullRight.should.be.true;
});
// NOTE: The onClick event handler is invoked for both the Enter and Space
// keys as well since the component is a button. I cannot figure out how to
// get ReactTestUtils to simulate such though.
it('toggles open/closed when clicked', () => {
const instance = ReactTestUtils.renderIntoDocument(simpleDropdown);
const node = ReactDOM.findDOMNode(instance);
const buttonNode = ReactTestUtils.findRenderedDOMComponentWithTag(instance, 'BUTTON');
node.className.should.not.match(/\bopen\b/);
buttonNode.getAttribute('aria-expanded').should.equal('false');
ReactTestUtils.Simulate.click(buttonNode);
node.className.should.match(/\bopen\b/);
buttonNode.getAttribute('aria-expanded').should.equal('true');
ReactTestUtils.Simulate.click(buttonNode);
node.className.should.not.match(/\bopen\b/);
buttonNode.getAttribute('aria-expanded').should.equal('false');
});
it('opens if dropdown contains no focusable menu item', () => {
const instance = ReactTestUtils.renderIntoDocument(
<Dropdown title='custom child' id='dropdown'>
<Dropdown.Toggle>Toggle</Dropdown.Toggle>
<Dropdown.Menu>
<li>Some custom nonfocusable content</li>
</Dropdown.Menu>
</Dropdown>
);
const node = ReactDOM.findDOMNode(instance);
const buttonNode = ReactTestUtils.findRenderedDOMComponentWithTag(instance, 'BUTTON');
ReactTestUtils.Simulate.click(buttonNode);
node.className.should.match(/\bopen\b/);
});
it('when focused and closed toggles open when the key "down" is pressed', () => {
const instance = ReactTestUtils.renderIntoDocument(simpleDropdown);
const node = ReactDOM.findDOMNode(instance);
const buttonNode = ReactTestUtils.findRenderedDOMComponentWithTag(instance, 'BUTTON');
ReactTestUtils.Simulate.keyDown(buttonNode, { keyCode: keycode('down') });
node.className.should.match(/\bopen\b/);
buttonNode.getAttribute('aria-expanded').should.equal('true');
});
it('button has aria-haspopup attribute (As per W3C WAI-ARIA Spec)', () => {
const instance = ReactTestUtils.renderIntoDocument(simpleDropdown);
const buttonNode = ReactTestUtils.findRenderedDOMComponentWithTag(instance, 'BUTTON');
buttonNode.getAttribute('aria-haspopup').should.equal('true');
});
it('closes when child MenuItem is selected', () => {
const instance = ReactTestUtils.renderIntoDocument(
simpleDropdown
);
const node = ReactDOM.findDOMNode(instance);
const buttonNode = ReactTestUtils.findRenderedDOMComponentWithTag(instance, 'BUTTON');
ReactTestUtils.Simulate.click(buttonNode);
node.className.should.match(/\bopen\b/);
const menuItem = ReactTestUtils.scryRenderedDOMComponentsWithTag(instance, 'A')[0];
ReactTestUtils.Simulate.click(menuItem);
node.className.should.not.match(/\bopen\b/);
});
it('does not close when onToggle is controlled', () => {
const handleSelect = () => {};
const instance = ReactTestUtils.renderIntoDocument(
<Dropdown open={true} onToggle={handleSelect} id='test-id'>
{dropdownChildren}
</Dropdown>
);
const node = ReactDOM.findDOMNode(instance);
const buttonNode = ReactTestUtils.findRenderedDOMComponentWithTag(instance, 'BUTTON');
const menuItem = ReactTestUtils.scryRenderedDOMComponentsWithTag(instance, 'A')[0];
ReactTestUtils.Simulate.click(buttonNode);
node.className.should.match(/\bopen\b/);
ReactTestUtils.Simulate.click(menuItem);
node.className.should.match(/\bopen\b/);
});
it('is open with explicit prop', () => {
class OpenProp extends React.Component {
constructor(props) {
super(props);
this.state = {
open: false
};
}
render() {
return (
<div>
<button className='outer-button'
onClick={() => this.setState({open: !this.state.open})}>
Outer button
</button>
<Dropdown
open={this.state.open}
onToggle={() => {}}
title='Prop open control'
id='test-id'
>
{dropdownChildren}
</Dropdown>
</div>
);
}
}
const instance = ReactTestUtils.renderIntoDocument(<OpenProp />);
const outerToggle = ReactTestUtils.findRenderedDOMComponentWithClass(instance, 'outer-button');
const dropdownNode = ReactTestUtils.findRenderedDOMComponentWithClass(instance, 'dropdown');
dropdownNode.className.should.not.match(/\bopen\b/);
ReactTestUtils.Simulate.click(outerToggle);
dropdownNode.className.should.match(/\bopen\b/);
ReactTestUtils.Simulate.click(outerToggle);
dropdownNode.className.should.not.match(/\bopen\b/);
});
it('has aria-labelledby same id as toggle button', () => {
const instance = ReactTestUtils.renderIntoDocument(simpleDropdown);
const node = ReactDOM.findDOMNode(instance);
const buttonNode = ReactTestUtils.findRenderedDOMComponentWithTag(instance, 'BUTTON');
const menuNode = node.children[1];
buttonNode.getAttribute('id').should.equal(menuNode.getAttribute('aria-labelledby'));
});
describe('PropType validation', () => {
describe('children', () => {
it('menu is exclusive', () => {
const props = {
children: [
<Dropdown.Toggle/>,
<Dropdown.Menu/>,
<Dropdown.Menu/>
]
};
BaseDropdown.propTypes.children(props, 'children', 'Dropdown')
.message.should.match(/Duplicate children.*bsRole: menu/);
});
it('menu is required', () => {
const props = {
children: [
<Dropdown.Toggle/>
]
};
BaseDropdown.propTypes.children(props, 'children', 'Dropdown')
.message.should.match(/Missing a required child.*bsRole: menu/);
});
it('toggles are not exclusive', () => {
const props = {
children: [
<Dropdown.Toggle/>,
<Dropdown.Toggle/>,
<Dropdown.Menu/>
]
};
expect(BaseDropdown.propTypes.children(props, 'children', 'Dropdown'))
.to.not.exist;
});
it('toggle is required', () => {
const props = {
children: [
<Dropdown.Menu/>
]
};
BaseDropdown.propTypes.children(props, 'children', 'Dropdown')
.message.should.match(/Missing a required child.*bsRole: toggle/);
});
});
});
describe('focusable state', () => {
let focusableContainer;
beforeEach(() => {
focusableContainer = document.createElement('div');
document.body.appendChild(focusableContainer);
});
afterEach(() => {
ReactDOM.unmountComponentAtNode(focusableContainer);
document.body.removeChild(focusableContainer);
});
it('when focused and closed sets focus on first menu item when the key "down" is pressed', () => {
const instance = ReactDOM.render(simpleDropdown, focusableContainer);
const buttonNode = ReactTestUtils.findRenderedDOMComponentWithTag(instance, 'BUTTON');
buttonNode.focus();
ReactTestUtils.Simulate.keyDown(buttonNode, { keyCode: keycode('down') });
const firstMenuItemAnchor = ReactTestUtils.scryRenderedDOMComponentsWithTag(instance, 'A')[0];
document.activeElement.should.equal(firstMenuItemAnchor);
});
it('when focused and open does not toggle closed when the key "down" is pressed', () => {
const instance = ReactTestUtils.renderIntoDocument(simpleDropdown);
const node = ReactDOM.findDOMNode(instance);
const buttonNode = ReactTestUtils.findRenderedDOMComponentWithTag(instance, 'BUTTON');
ReactTestUtils.Simulate.click(buttonNode);
ReactTestUtils.Simulate.keyDown(buttonNode, { keyCode: keycode('down') });
node.className.should.match(/\bopen\b/);
buttonNode.getAttribute('aria-expanded').should.equal('true');
});
// This test is more complicated then it appears to need. This is
// because there was an intermittent failure of the test when not structured this way
// The failure occured when all tests in the suite were run together, but not a subset of the tests.
//
// I am fairly confident that the failure is due to a test specific conflict and not an actual bug.
it('when open and the key "esc" is pressed the menu is closed and focus is returned to the button', () => {
const instance = ReactDOM.render(
<Dropdown defaultOpen role="menuitem" id="test-id">
{dropdownChildren}
</Dropdown>
, focusableContainer);
const buttonNode = ReactTestUtils.findRenderedDOMComponentWithTag(instance, 'BUTTON');
const firstMenuItemAnchor = ReactTestUtils.scryRenderedDOMComponentsWithTag(instance, 'A')[0];
document.activeElement.should.equal(firstMenuItemAnchor);
ReactTestUtils.Simulate.keyDown(firstMenuItemAnchor, { type: 'keydown', keyCode: keycode('esc') });
document.activeElement.should.equal(buttonNode);
});
it('when open and the key "tab" is pressed the menu is closed and focus is progress to the next focusable element', done => {
const instance = ReactDOM.render(
<Grid>
{simpleDropdown}
<input type='text' id='next-focusable' />
</Grid>, focusableContainer);
// Need to use Grid instead of div above to make instance a composite
// element, to make this call legal.
const node = ReactTestUtils.findRenderedComponentWithType(instance, Dropdown);
const buttonNode = ReactTestUtils.findRenderedDOMComponentWithTag(node, 'BUTTON');
ReactTestUtils.Simulate.click(buttonNode);
buttonNode.getAttribute('aria-expanded').should.equal('true');
ReactTestUtils.Simulate.keyDown(buttonNode, { key: keycode('tab'), keyCode: keycode('tab') });
setTimeout(() => {
buttonNode.getAttribute('aria-expanded').should.equal('false');
done();
});
// simulating a tab event doesn't actually shift focus.
// at least that seems to be the case according to SO.
// hence no assert on the input having focus.
});
});
});