@@ -36,18 +36,21 @@ const createKeyboardEvent = (type, data) => {
3636 } ) ;
3737} ;
3838
39+ function init ( ) {
40+ ReactFeatureFlags = require ( 'shared/ReactFeatureFlags' ) ;
41+ ReactFeatureFlags . enableEventAPI = true ;
42+ React = require ( 'react' ) ;
43+ ReactDOM = require ( 'react-dom' ) ;
44+ Press = require ( 'react-events/press' ) ;
45+ Scheduler = require ( 'scheduler' ) ;
46+ }
47+
3948describe ( 'Event responder: Press' , ( ) => {
4049 let container ;
4150
4251 beforeEach ( ( ) => {
4352 jest . resetModules ( ) ;
44- ReactFeatureFlags = require ( 'shared/ReactFeatureFlags' ) ;
45- ReactFeatureFlags . enableEventAPI = true ;
46- React = require ( 'react' ) ;
47- ReactDOM = require ( 'react-dom' ) ;
48- Press = require ( 'react-events/press' ) ;
49- Scheduler = require ( 'scheduler' ) ;
50-
53+ init ( ) ;
5154 container = document . createElement ( 'div' ) ;
5255 document . body . appendChild ( container ) ;
5356 } ) ;
@@ -2579,4 +2582,100 @@ describe('Event responder: Press', () => {
25792582 Scheduler . flushAll ( ) ;
25802583 document . body . removeChild ( newContainer ) ;
25812584 } ) ;
2585+
2586+ describe ( 'onContextMenu' , ( ) => {
2587+ it ( 'is called after a right mouse click' , ( ) => {
2588+ const onContextMenu = jest . fn ( ) ;
2589+ const ref = React . createRef ( ) ;
2590+ const element = (
2591+ < Press onContextMenu = { onContextMenu } >
2592+ < div ref = { ref } />
2593+ </ Press >
2594+ ) ;
2595+ ReactDOM . render ( element , container ) ;
2596+
2597+ ref . current . dispatchEvent (
2598+ createEvent ( 'pointerdown' , { pointerType : 'mouse' , button : 2 } ) ,
2599+ ) ;
2600+ ref . current . dispatchEvent ( createEvent ( 'contextmenu' ) ) ;
2601+ expect ( onContextMenu ) . toHaveBeenCalledTimes ( 1 ) ;
2602+ expect ( onContextMenu ) . toHaveBeenCalledWith (
2603+ expect . objectContaining ( { pointerType : 'mouse' , type : 'contextmenu' } ) ,
2604+ ) ;
2605+ } ) ;
2606+
2607+ it ( 'is called after a left mouse click + ctrl key on Mac' , ( ) => {
2608+ jest . resetModules ( ) ;
2609+ const platformGetter = jest . spyOn ( global . navigator , 'platform' , 'get' ) ;
2610+ platformGetter . mockReturnValue ( 'MacIntel' ) ;
2611+ init ( ) ;
2612+
2613+ const onContextMenu = jest . fn ( ) ;
2614+ const ref = React . createRef ( ) ;
2615+ const element = (
2616+ < Press onContextMenu = { onContextMenu } >
2617+ < div ref = { ref } />
2618+ </ Press >
2619+ ) ;
2620+ ReactDOM . render ( element , container ) ;
2621+
2622+ ref . current . dispatchEvent (
2623+ createEvent ( 'pointerdown' , {
2624+ pointerType : 'mouse' ,
2625+ button : 0 ,
2626+ ctrlKey : true ,
2627+ } ) ,
2628+ ) ;
2629+ ref . current . dispatchEvent ( createEvent ( 'contextmenu' ) ) ;
2630+ expect ( onContextMenu ) . toHaveBeenCalledTimes ( 1 ) ;
2631+ expect ( onContextMenu ) . toHaveBeenCalledWith (
2632+ expect . objectContaining ( { pointerType : 'mouse' , type : 'contextmenu' } ) ,
2633+ ) ;
2634+ platformGetter . mockClear ( ) ;
2635+ } ) ;
2636+
2637+ it ( 'is not called after a left mouse click + ctrl key on Windows' , ( ) => {
2638+ jest . resetModules ( ) ;
2639+ const platformGetter = jest . spyOn ( global . navigator , 'platform' , 'get' ) ;
2640+ platformGetter . mockReturnValue ( 'Win32' ) ;
2641+ init ( ) ;
2642+
2643+ const onContextMenu = jest . fn ( ) ;
2644+ const ref = React . createRef ( ) ;
2645+ const element = (
2646+ < Press onContextMenu = { onContextMenu } >
2647+ < div ref = { ref } />
2648+ </ Press >
2649+ ) ;
2650+ ReactDOM . render ( element , container ) ;
2651+
2652+ ref . current . dispatchEvent (
2653+ createEvent ( 'pointerdown' , {
2654+ pointerType : 'mouse' ,
2655+ button : 0 ,
2656+ ctrlKey : true ,
2657+ } ) ,
2658+ ) ;
2659+ ref . current . dispatchEvent ( createEvent ( 'contextmenu' ) ) ;
2660+ expect ( onContextMenu ) . toHaveBeenCalledTimes ( 0 ) ;
2661+ platformGetter . mockClear ( ) ;
2662+ } ) ;
2663+
2664+ it ( 'is not called after a right mouse click occurs during an active press' , ( ) => {
2665+ const onContextMenu = jest . fn ( ) ;
2666+ const ref = React . createRef ( ) ;
2667+ const element = (
2668+ < Press onContextMenu = { onContextMenu } >
2669+ < div ref = { ref } />
2670+ </ Press >
2671+ ) ;
2672+ ReactDOM . render ( element , container ) ;
2673+
2674+ ref . current . dispatchEvent (
2675+ createEvent ( 'pointerdown' , { pointerType : 'mouse' , button : 0 } ) ,
2676+ ) ;
2677+ ref . current . dispatchEvent ( createEvent ( 'contextmenu' ) ) ;
2678+ expect ( onContextMenu ) . toHaveBeenCalledTimes ( 0 ) ;
2679+ } ) ;
2680+ } ) ;
25822681} ) ;
0 commit comments