-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDropdownMenuSpec.js
242 lines (201 loc) · 7.25 KB
/
DropdownMenuSpec.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
import keycode from 'keycode';
import React from 'react';
import ReactDOM from 'react-dom';
import ReactTestUtils from 'react-dom/test-utils';
import { shallow } from 'enzyme';
import DropdownMenu from '../src/DropdownMenu';
import MenuItem from '../src/MenuItem';
import { getOne } from './helpers';
describe('<Dropdown.Menu>', () => {
const simpleMenu = (
<DropdownMenu>
<MenuItem eventKey="1">Item 1</MenuItem>
<MenuItem eventKey="2">Item 2</MenuItem>
<MenuItem eventKey="3">Item 3</MenuItem>
<MenuItem eventKey="4">Item 4</MenuItem>
</DropdownMenu>
);
it('renders ul with dropdown-menu class', () => {
const instance = ReactTestUtils.renderIntoDocument(simpleMenu);
const node = ReactDOM.findDOMNode(instance);
node.tagName.should.equal('UL');
node.className.should.match(/\bdropdown-menu\b/);
});
it('has role="menu"', () => {
const instance = ReactTestUtils.renderIntoDocument(simpleMenu);
const node = ReactDOM.findDOMNode(instance);
node.getAttribute('role').should.equal('menu');
});
it('has aria-labelledby=<id>', () => {
const instance1 = ReactTestUtils.renderIntoDocument(
<DropdownMenu labelledBy="herpa" />
);
const instance2 = ReactTestUtils.renderIntoDocument(
<DropdownMenu labelledBy="derpa" />
);
const node1 = ReactDOM.findDOMNode(instance1);
const node2 = ReactDOM.findDOMNode(instance2);
node1.getAttribute('aria-labelledby').should.equal('herpa');
node2.getAttribute('aria-labelledby').should.equal('derpa');
});
it('forwards onSelect handler to MenuItems', done => {
const selectedEvents = [];
const onSelect = eventKey => {
selectedEvents.push(eventKey);
if (selectedEvents.length === 4) {
selectedEvents.should.eql(['1', '2', '3', '4']);
done();
}
};
const instance = ReactTestUtils.renderIntoDocument(
<DropdownMenu onSelect={onSelect}>
<MenuItem eventKey="1">Item 1</MenuItem>
<MenuItem eventKey="2">Item 2</MenuItem>
<MenuItem eventKey="3">Item 3</MenuItem>
<MenuItem eventKey="4">Item 4</MenuItem>
</DropdownMenu>
);
const menuItems = ReactTestUtils.scryRenderedDOMComponentsWithTag(
instance,
'A'
);
menuItems.forEach(item => {
ReactTestUtils.Simulate.click(item);
});
});
it('does not pass onSelect to DOM node', () => {
shallow(<DropdownMenu onSelect={() => {}} />)
.find('ul')
.props()
.should.not.have.property('onSelect');
});
it('applies pull right', () => {
const instance = ReactTestUtils.renderIntoDocument(
<DropdownMenu pullRight>
<MenuItem>Item</MenuItem>
</DropdownMenu>
);
const node = ReactDOM.findDOMNode(instance);
node.className.should.match(/\bdropdown-menu-right\b/);
});
it('handles empty children', () => {
ReactTestUtils.renderIntoDocument(
<DropdownMenu pullRight>
<MenuItem>Item</MenuItem>
{false && <MenuItem>Item 2</MenuItem>}
</DropdownMenu>
);
});
describe('focusable state', () => {
let focusableContainer;
beforeEach(() => {
focusableContainer = document.createElement('div');
document.body.appendChild(focusableContainer);
});
afterEach(() => {
ReactDOM.unmountComponentAtNode(focusableContainer);
document.body.removeChild(focusableContainer);
});
it('clicking anything outside the menu will request close', () => {
const requestClose = sinon.stub();
const instance = ReactDOM.render(
<div>
<button>Something to click</button>
<DropdownMenu onClose={requestClose} open>
<MenuItem>Item</MenuItem>
</DropdownMenu>
</div>,
focusableContainer
);
const button = getOne(instance.getElementsByTagName('button'));
button.click();
requestClose.should.have.been.calledOnce;
requestClose.getCall(0).args.length.should.equal(2);
});
describe('Keyboard Navigation', () => {
it('sets focus on next menu item when the key "down" is pressed', () => {
const instance = ReactDOM.render(simpleMenu, focusableContainer);
const items = ReactTestUtils.scryRenderedDOMComponentsWithTag(
instance,
'A'
);
items.length.should.equal(4);
items[0].focus();
for (let i = 1; i < items.length; i++) {
ReactTestUtils.Simulate.keyDown(document.activeElement, {
keyCode: keycode('down')
});
document.activeElement.should.equal(items[i]);
}
});
it('with last item is focused when the key "down" is pressed first item gains focus', () => {
const instance = ReactDOM.render(simpleMenu, focusableContainer);
const items = ReactTestUtils.scryRenderedDOMComponentsWithTag(
instance,
'A'
);
items.length.should.equal(4);
items[3].focus();
ReactTestUtils.Simulate.keyDown(document.activeElement, {
keyCode: keycode('down')
});
document.activeElement.should.equal(items[0]);
});
it('sets focus on previous menu item when the key "up" is pressed', () => {
const instance = ReactDOM.render(simpleMenu, focusableContainer);
const items = ReactTestUtils.scryRenderedDOMComponentsWithTag(
instance,
'A'
);
items.length.should.equal(4);
items[3].focus();
for (let i = 2; i >= 0; i--) {
ReactTestUtils.Simulate.keyDown(document.activeElement, {
keyCode: keycode('up')
});
document.activeElement.should.equal(items[i]);
}
});
it('with first item focused when the key "up" is pressed last item gains focus', () => {
const instance = ReactDOM.render(simpleMenu, focusableContainer);
const items = ReactTestUtils.scryRenderedDOMComponentsWithTag(
instance,
'A'
);
items.length.should.equal(4);
items[0].focus();
ReactTestUtils.Simulate.keyDown(document.activeElement, {
keyCode: keycode('up')
});
document.activeElement.should.equal(items[3]);
});
['esc', 'tab'].forEach(key => {
it(`when the key "${key}" is pressed the requestClose prop is invoked with the originating event`, () => {
const requestClose = sinon.spy();
const instance = ReactDOM.render(
<DropdownMenu onClose={requestClose}>
<MenuItem>Item</MenuItem>
</DropdownMenu>,
focusableContainer
);
const item = ReactTestUtils.findRenderedDOMComponentWithTag(
instance,
'A'
);
ReactTestUtils.Simulate.keyDown(item, { keyCode: keycode(key) });
requestClose.should.have.been.calledOnce;
requestClose.getCall(0).args[0].keyCode.should.equal(keycode(key));
});
});
});
});
it('Should pass props to dropdown', () => {
let instance = ReactTestUtils.renderIntoDocument(
<DropdownMenu className="new-fancy-class">
<MenuItem eventKey="1">MenuItem 1 content</MenuItem>
</DropdownMenu>
);
let node = ReactDOM.findDOMNode(instance);
assert.ok(node.className.match(/\bnew-fancy-class\b/));
});
});