Skip to content

Commit

Permalink
Added testcases for isKeyboardEvent in keycodes module (#14073)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ashwin-pc authored and youknowriad committed Mar 20, 2019
1 parent 0772b73 commit 43d9472
Showing 1 changed file with 244 additions and 0 deletions.
244 changes: 244 additions & 0 deletions packages/keycodes/src/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
displayShortcut,
rawShortcut,
shortcutAriaLabel,
isKeyboardEvent,
} from '../';

const isAppleOSFalse = () => false;
Expand Down Expand Up @@ -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',
} );
} );
} );
} );

0 comments on commit 43d9472

Please sign in to comment.