@@ -2,6 +2,7 @@ import React from 'react';
22import { mount } from 'enzyme' ;
33
44import ContextMenu from '../src/ContextMenu' ;
5+ import MenuItem from '../src/MenuItem' ;
56import { showMenu , hideMenu } from '../src/actions' ;
67
78describe ( 'ContextMenu tests' , ( ) => {
@@ -194,4 +195,132 @@ 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+ } ) ;
286+
287+ test ( 'should preserve the selected item after an enter' , ( ) => {
288+ const data = { position : { x : 50 , y : 50 } , id : 'CORRECT_ID' } ;
289+ const onHide = jest . fn ( ) ;
290+ const component = mount (
291+ < ContextMenu id = { data . id } onHide = { onHide } >
292+ < MenuItem onClick = { jest . fn ( ) } preventClose > Item 1</ MenuItem >
293+ < MenuItem divider />
294+ < MenuItem onClick = { jest . fn ( ) } preventClose > Item 2</ MenuItem >
295+ </ ContextMenu >
296+ ) ;
297+ const upArrow = new window . KeyboardEvent ( 'keydown' , { keyCode : 38 } ) ;
298+ const enter = new window . KeyboardEvent ( 'keydown' , { keyCode : 13 } ) ;
299+
300+ showMenu ( data ) ;
301+ // Check that it's visible and there is no selected item at first.
302+ expect ( component . state ( ) ) . toEqual (
303+ Object . assign (
304+ { isVisible : true , forceSubMenuOpen : false , selectedItem : null } ,
305+ data . position
306+ )
307+ ) ;
308+
309+ // Select the second item up arrow.
310+ document . dispatchEvent ( upArrow ) ;
311+ expect ( component . state ( ) . selectedItem ) . toEqual ( {
312+ index : 1 ,
313+ type : MenuItem
314+ } ) ;
315+
316+ // Press enter to select it.
317+ document . dispatchEvent ( enter ) ;
318+ // The selected item should be preserved and not reset.
319+ expect ( component . state ( ) . selectedItem ) . toEqual ( {
320+ index : 1 ,
321+ type : MenuItem
322+ } ) ;
323+
324+ component . unmount ( ) ;
325+ } ) ;
197326} ) ;
0 commit comments