From 43d94721d5c5bc4ca30bc8e667f1d805a6d93613 Mon Sep 17 00:00:00 2001 From: Ashwin P Chandran Date: Sun, 10 Mar 2019 03:13:02 -0400 Subject: [PATCH] Added testcases for isKeyboardEvent in keycodes module (#14073) * Added testcases for isKeyboardEvent in keycodes module * Fixed assertion checks and updated attachEventListeners function * Added testcases for isKeyboardEvent in keycodes module * Fixed assertion checks and updated attachEventListeners function --- packages/keycodes/src/test/index.js | 244 ++++++++++++++++++++++++++++ 1 file changed, 244 insertions(+) diff --git a/packages/keycodes/src/test/index.js b/packages/keycodes/src/test/index.js index d795560fcb3a7..2d7d83249ec57 100644 --- a/packages/keycodes/src/test/index.js +++ b/packages/keycodes/src/test/index.js @@ -6,6 +6,7 @@ import { displayShortcut, rawShortcut, shortcutAriaLabel, + isKeyboardEvent, } from '../'; const isAppleOSFalse = () => false; @@ -230,3 +231,246 @@ describe( 'rawShortcut', () => { } ); } ); } ); + +describe( 'isKeyboardEvent', () => { + afterEach( () => { + while ( document.body.firstChild ) { + document.body.removeChild( document.body.firstChild ); + } + } ); + + function keyPress( target, modifiers = {} ) { + [ 'keydown', 'keypress', 'keyup' ].forEach( ( eventName ) => { + const event = new window.Event( eventName, { bubbles: true } ); + Object.assign( event, modifiers ); + target.dispatchEvent( event ); + } ); + } + + function attachEventListeners( eventHandler ) { + const attachNode = document.createElement( 'div' ); + document.body.appendChild( attachNode ); + + [ 'keydown', 'keypress', 'keyup' ].forEach( ( eventName ) => { + attachNode.addEventListener( eventName, eventHandler ); + } ); + + return attachNode; + } + + describe( 'primary', () => { + it( 'should identify modifier key when Ctrl is pressed', () => { + expect.assertions( 3 ); + const attachNode = attachEventListeners( ( event ) => { + expect( isKeyboardEvent.primary( event, undefined, isAppleOSFalse ) ).toBe( true ); + } ); + + keyPress( attachNode, { + ctrlKey: true, + key: 'Ctrl', + } ); + } ); + + it( 'should identify modifier key when ⌘ is pressed', () => { + expect.assertions( 3 ); + const attachNode = attachEventListeners( ( event ) => { + expect( isKeyboardEvent.primary( event, undefined, isAppleOSTrue ) ).toBe( true ); + } ); + + keyPress( attachNode, { + metaKey: true, + key: 'Meta', + } ); + } ); + + it( 'should identify modifier key when Ctrl + M is pressed', () => { + expect.assertions( 3 ); + const attachNode = attachEventListeners( ( event ) => { + expect( isKeyboardEvent.primary( event, 'm', isAppleOSFalse ) ).toBe( true ); + } ); + + keyPress( attachNode, { + ctrlKey: true, + key: 'm', + } ); + } ); + + it( 'should identify modifier key when ⌘M is pressed', () => { + expect.assertions( 3 ); + const attachNode = attachEventListeners( ( event ) => { + expect( isKeyboardEvent.primary( event, 'm', isAppleOSTrue ) ).toBe( true ); + } ); + + keyPress( attachNode, { + metaKey: true, + key: 'm', + } ); + } ); + } ); + + describe( 'primaryShift', () => { + it( 'should identify modifier key when Shift + Ctrl is pressed', () => { + expect.assertions( 3 ); + const attachNode = attachEventListeners( ( event ) => { + expect( isKeyboardEvent.primary( event, undefined, isAppleOSFalse ) ).toBe( true ); + } ); + + keyPress( attachNode, { + ctrlKey: true, + shiftKey: true, + key: 'Ctrl', + } ); + } ); + + it( 'should identify modifier key when ⇧⌘ is pressed', () => { + expect.assertions( 3 ); + const attachNode = attachEventListeners( ( event ) => { + expect( isKeyboardEvent.primary( event, undefined, isAppleOSTrue ) ).toBe( true ); + } ); + + keyPress( attachNode, { + metaKey: true, + shiftKey: true, + key: 'Meta', + } ); + } ); + + it( 'should identify modifier key when Shift + Ctrl + M is pressed', () => { + expect.assertions( 3 ); + const attachNode = attachEventListeners( ( event ) => { + expect( isKeyboardEvent.primary( event, 'm', isAppleOSFalse ) ).toBe( true ); + } ); + + keyPress( attachNode, { + ctrlKey: true, + shiftKey: true, + key: 'm', + } ); + } ); + + it( 'should identify modifier key when ⇧⌘M is pressed', () => { + expect.assertions( 3 ); + const attachNode = attachEventListeners( ( event ) => { + expect( isKeyboardEvent.primary( event, 'm', isAppleOSTrue ) ).toBe( true ); + } ); + + keyPress( attachNode, { + metaKey: true, + shiftKey: true, + key: 'm', + } ); + } ); + } ); + + describe( 'secondary', () => { + it( 'should identify modifier key when Shift + Alt + Ctrl is pressed', () => { + expect.assertions( 3 ); + const attachNode = attachEventListeners( ( event ) => { + expect( isKeyboardEvent.primary( event, undefined, isAppleOSFalse ) ).toBe( true ); + } ); + + keyPress( attachNode, { + ctrlKey: true, + shiftKey: true, + altKey: true, + key: 'Ctrl', + } ); + } ); + + it( 'should identify modifier key when ⇧⌥⌘ is pressed', () => { + expect.assertions( 3 ); + const attachNode = attachEventListeners( ( event ) => { + expect( isKeyboardEvent.primary( event, undefined, isAppleOSTrue ) ).toBe( true ); + } ); + + keyPress( attachNode, { + metaKey: true, + shiftKey: true, + altKey: true, + key: 'Meta', + } ); + } ); + + it( 'should identify modifier key when Shift + Ctrl + ALt + M is pressed', () => { + expect.assertions( 3 ); + const attachNode = attachEventListeners( ( event ) => { + expect( isKeyboardEvent.primary( event, 'm', isAppleOSFalse ) ).toBe( true ); + } ); + + keyPress( attachNode, { + ctrlKey: true, + shiftKey: true, + altKey: true, + key: 'm', + } ); + } ); + + it( 'should identify modifier key when ⇧⌥⌘M is pressed', () => { + expect.assertions( 3 ); + const attachNode = attachEventListeners( ( event ) => { + expect( isKeyboardEvent.primary( event, 'm', isAppleOSTrue ) ).toBe( true ); + } ); + + keyPress( attachNode, { + metaKey: true, + shiftKey: true, + altKey: true, + key: 'm', + } ); + } ); + } ); + + describe( 'access', () => { + it( 'should identify modifier key when Alt + Ctrl is pressed', () => { + expect.assertions( 3 ); + const attachNode = attachEventListeners( ( event ) => { + expect( isKeyboardEvent.primary( event, undefined, isAppleOSFalse ) ).toBe( true ); + } ); + + keyPress( attachNode, { + ctrlKey: true, + altKey: true, + key: 'Ctrl', + } ); + } ); + + it( 'should identify modifier key when ⌥⌘ is pressed', () => { + expect.assertions( 3 ); + const attachNode = attachEventListeners( ( event ) => { + expect( isKeyboardEvent.primary( event, undefined, isAppleOSTrue ) ).toBe( true ); + } ); + + keyPress( attachNode, { + metaKey: true, + altKey: true, + key: 'Meta', + } ); + } ); + + it( 'should identify modifier key when Ctrl + ALt + M is pressed', () => { + expect.assertions( 3 ); + const attachNode = attachEventListeners( ( event ) => { + expect( isKeyboardEvent.primary( event, 'm', isAppleOSFalse ) ).toBe( true ); + } ); + + keyPress( attachNode, { + ctrlKey: true, + altKey: true, + key: 'm', + } ); + } ); + + it( 'should identify modifier key when ⌥⌘M is pressed', () => { + expect.assertions( 3 ); + const attachNode = attachEventListeners( ( event ) => { + expect( isKeyboardEvent.primary( event, 'm', isAppleOSTrue ) ).toBe( true ); + } ); + + keyPress( attachNode, { + metaKey: true, + altKey: true, + key: 'm', + } ); + } ); + } ); +} );