Skip to content

Commit b13681e

Browse files
committed
Add some tests for down and up arrows
1 parent e549774 commit b13681e

1 file changed

Lines changed: 89 additions & 0 deletions

File tree

tests/ContextMenu.test.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from 'react';
22
import { mount } from 'enzyme';
33

44
import ContextMenu from '../src/ContextMenu';
5+
import MenuItem from '../src/MenuItem';
56
import { showMenu, hideMenu } from '../src/actions';
67

78
describe('ContextMenu tests', () => {
@@ -194,4 +195,92 @@ describe('ContextMenu tests', () => {
194195
expect(onMouseLeave).toHaveBeenCalled();
195196
component.unmount();
196197
});
198+
199+
test('should select the proper menu items with down arrow', () => {
200+
const data = { position: { x: 50, y: 50 }, id: 'CORRECT_ID' };
201+
const onHide = jest.fn();
202+
const component = mount(
203+
<ContextMenu id={data.id} onHide={onHide}>
204+
<MenuItem onClick={jest.fn()}>Item 1</MenuItem>
205+
<MenuItem onClick={jest.fn()}>Item 2</MenuItem>
206+
</ContextMenu>
207+
);
208+
const downArrow = new window.KeyboardEvent('keydown', { keyCode: 40 });
209+
210+
showMenu(data);
211+
// Check that it's visible and there is no selected item at first.
212+
expect(component.state()).toEqual(
213+
Object.assign(
214+
{ isVisible: true, forceSubMenuOpen: false, selectedItem: null },
215+
data.position
216+
)
217+
);
218+
219+
// Select the first item with down arrow.
220+
document.dispatchEvent(downArrow);
221+
// Index 0 with MenuItem type should be selected.
222+
expect(component.state().selectedItem).toEqual({
223+
index: 0,
224+
type: MenuItem
225+
});
226+
227+
// Select the second item with down arrow.
228+
document.dispatchEvent(downArrow);
229+
// Index 1 with MenuItem type should be selected.
230+
expect(component.state().selectedItem).toEqual({
231+
index: 1,
232+
type: MenuItem
233+
});
234+
235+
// Select the next item. But since this was the last item, it should loop
236+
// back to the first again.
237+
document.dispatchEvent(downArrow);
238+
// Index 0 with MenuItem type should be selected.
239+
expect(component.state().selectedItem).toEqual({
240+
index: 0,
241+
type: MenuItem
242+
});
243+
244+
component.unmount();
245+
});
246+
247+
test('should select the proper menu items with up arrow', () => {
248+
const data = { position: { x: 50, y: 50 }, id: 'CORRECT_ID' };
249+
const onHide = jest.fn();
250+
const component = mount(
251+
<ContextMenu id={data.id} onHide={onHide}>
252+
<MenuItem onClick={jest.fn()}>Item 1</MenuItem>
253+
<MenuItem onClick={jest.fn()}>Item 2</MenuItem>
254+
</ContextMenu>
255+
);
256+
const upArrow = new window.KeyboardEvent('keydown', { keyCode: 38 });
257+
258+
showMenu(data);
259+
// Check that it's visible and there is no selected item at first.
260+
expect(component.state()).toEqual(
261+
Object.assign(
262+
{ isVisible: true, forceSubMenuOpen: false, selectedItem: null },
263+
data.position
264+
)
265+
);
266+
267+
// Select the previous item. But since there was nothing selected, it
268+
// should loop back down to the last item.
269+
document.dispatchEvent(upArrow);
270+
// Index 1 with MenuItem type should be selected.
271+
expect(component.state().selectedItem).toEqual({
272+
index: 1,
273+
type: MenuItem
274+
});
275+
276+
// Select the first item with up arrow.
277+
document.dispatchEvent(upArrow);
278+
// Index 0 with MenuItem type should be selected.
279+
expect(component.state().selectedItem).toEqual({
280+
index: 0,
281+
type: MenuItem
282+
});
283+
284+
component.unmount();
285+
});
197286
});

0 commit comments

Comments
 (0)