Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added testcases for isKeyboardEvent in keycodes module #14073

Merged
merged 5 commits into from
Mar 10, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering if removing DOM element will also remove all attached listeners. It looks like the answer is yes:

https://stackoverflow.com/questions/12528049/if-a-dom-element-is-removed-are-its-listeners-also-removed-from-memory

I thought I will share it for others looking at this PR who weren't sure.

}
} );

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',
} );
} );
} );
} );