-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
/
Menu.spec.js
203 lines (178 loc) · 6.42 KB
/
Menu.spec.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
// @flow
import React from 'react';
import keycode from 'keycode';
import { assert } from 'chai';
import { ReactWrapper } from 'enzyme';
import TestUtils from 'react-dom/test-utils';
import { createMount } from 'src/test-utils';
import Popover from 'src/Popover';
import SimpleMenu from './fixtures/menus/SimpleMenu';
function simulateEvent(node, event, mock) {
const eventFn = TestUtils.Simulate[event];
if (!eventFn) {
throw new TypeError(`simulateEvent: event '${event}' does not exist`);
}
eventFn(node, mock);
}
describe('<Menu> integration', () => {
let mount;
before(() => {
mount = createMount();
});
after(() => {
mount.cleanUp();
});
describe('mounted open', () => {
let wrapper;
let portalLayer;
before(() => {
wrapper = mount(<SimpleMenu transitionDuration={0} />);
});
it('should not be open', () => {
const popover = wrapper.find(Popover);
assert.strictEqual(popover.props().open, false, 'should have passed open=false to Popover');
const menuEl = document.getElementById('simple-menu');
assert.strictEqual(menuEl, null, 'should not render the menu to the DOM');
});
it('should focus the first item as nothing has been selected', () => {
wrapper.setState({ open: true });
portalLayer = wrapper
.find('Portal')
.instance()
.getMountNode();
assert.strictEqual(
document.activeElement,
portalLayer.querySelectorAll('li')[0],
'should be the first menu item',
);
});
it('should change focus to the 2nd item when down arrow is pressed', () => {
simulateEvent(portalLayer.querySelector('ul'), 'keyDown', { which: keycode('down') });
assert.strictEqual(
document.activeElement,
portalLayer.querySelectorAll('li')[1],
'should be the 2nd menu item',
);
});
it('should change focus to the 3rd item when down arrow is pressed', () => {
simulateEvent(portalLayer.querySelector('ul'), 'keyDown', { which: keycode('down') });
assert.strictEqual(
document.activeElement,
portalLayer.querySelectorAll('li')[2],
'should be the 3rd menu item',
);
});
it('should keep focus on the 3rd item (last item) when down arrow is pressed', () => {
simulateEvent(portalLayer.querySelector('ul'), 'keyDown', { which: keycode('down') });
assert.strictEqual(
document.activeElement,
portalLayer.querySelectorAll('li')[2],
'should be the 3rd menu item',
);
});
it('should keep focus on the last item when a key with no associated action is pressed', () => {
simulateEvent(portalLayer.querySelector('ul'), 'keyDown', { which: keycode('right') });
assert.strictEqual(
document.activeElement,
portalLayer.querySelectorAll('li')[2],
'should be the 3rd menu item',
);
});
it('should change focus to the 2nd item when up arrow is pressed', () => {
simulateEvent(portalLayer.querySelector('ul'), 'keyDown', { which: keycode('up') });
assert.strictEqual(
document.activeElement,
portalLayer.querySelectorAll('li')[1],
'should be the 2nd menu item',
);
});
it('should select the 2nd item and close the menu', () => {
portalLayer.querySelectorAll('li')[1].click();
assert.strictEqual(wrapper.state().selectedIndex, 1, 'should be index 1');
assert.strictEqual(wrapper.state().open, false, 'should have closed');
});
});
describe('opening with a selected item', () => {
let wrapper;
before(() => {
wrapper = mount(<SimpleMenu transitionDuration={0} />);
wrapper.setState({ selectedIndex: 2 });
});
it('should not be open', () => {
const popover = wrapper.find(Popover);
assert.strictEqual(popover.props().open, false, 'should have passed open=false to Popover');
const menuEl = document.getElementById('simple-menu');
assert.strictEqual(menuEl, null, 'should not render the menu to the DOM');
});
it('should focus the selected item', () => {
wrapper.setState({ open: true });
const portalLayer = wrapper
.find('Portal')
.instance()
.getMountNode();
assert.strictEqual(
document.activeElement,
portalLayer.querySelectorAll('li')[2],
'should be the 3rd menu item',
);
});
it('should select the 2nd item and close the menu', () => {
const portalLayer = wrapper
.find('Portal')
.instance()
.getMountNode();
const item = portalLayer.querySelector('ul').children[1];
item.click();
assert.strictEqual(wrapper.state().selectedIndex, 1, 'should be index 1');
assert.strictEqual(wrapper.state().open, false, 'should have closed');
});
it('should focus the selected item', () => {
wrapper.setState({ open: true });
const portalLayer = wrapper
.find('Portal')
.instance()
.getMountNode();
assert.strictEqual(
document.activeElement,
portalLayer.querySelectorAll('li')[1],
'should be the 2nd menu item',
);
});
});
describe('closing', () => {
let wrapper;
let list;
let backdrop;
beforeEach(() => {
wrapper = mount(<SimpleMenu transitionDuration={0} />);
wrapper.setState({ open: true });
const portal = wrapper.find('Portal').props().children;
const portalWrapper = new ReactWrapper(portal);
list = portalWrapper.find('List');
backdrop = portalWrapper.find('Backdrop');
assert.strictEqual(backdrop.length, 1, 'find a backdrop');
});
it('should close the menu with tab', done => {
wrapper.setProps({
onExited() {
assert.strictEqual(document.getElementById('[data-mui-test="Menu"]'), null);
done();
},
});
assert.strictEqual(wrapper.state().open, true, 'should start open');
list.simulate('keyDown', { which: keycode('tab') });
assert.strictEqual(wrapper.state().open, false, 'should be closed');
});
it('should close the menu using the backdrop', done => {
wrapper.setProps({
onExited() {
assert.strictEqual(document.getElementById('[data-mui-test="Menu"]'), null);
done();
},
});
assert.strictEqual(wrapper.state().open, true, 'should start open');
backdrop.simulate('click');
assert.strictEqual(wrapper.state().open, false, 'should be closed');
});
});
});