-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Components: Enhance KeyboardShortcuts to optionally bind globally
To capture events within inputs
- Loading branch information
Showing
3 changed files
with
83 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { mount } from 'enzyme'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import KeyboardShortcuts from '../'; | ||
|
||
describe( 'KeyboardShortcuts', () => { | ||
afterEach( () => { | ||
while ( document.body.firstChild ) { | ||
document.body.removeChild( document.body.firstChild ); | ||
} | ||
} ); | ||
|
||
function keyPress( which, target ) { | ||
[ 'keydown', 'keypress', 'keyup' ].forEach( ( eventName ) => { | ||
const event = new window.Event( eventName, { bubbles: true } ); | ||
event.keyCode = which; | ||
event.which = which; | ||
target.dispatchEvent( event ); | ||
} ); | ||
} | ||
|
||
it( 'should capture key events', () => { | ||
const spy = jest.fn(); | ||
mount( | ||
<KeyboardShortcuts | ||
shortcuts={ { | ||
d: spy, | ||
} } /> | ||
); | ||
|
||
keyPress( 68, document ); | ||
|
||
expect( spy ).toHaveBeenCalled(); | ||
} ); | ||
|
||
it( 'should capture key events globally', () => { | ||
const spy = jest.fn(); | ||
const attachNode = document.createElement( 'div' ); | ||
document.body.appendChild( attachNode ); | ||
|
||
const wrapper = mount( | ||
<div> | ||
<KeyboardShortcuts | ||
shortcuts={ { | ||
d: [ spy, true ], | ||
} } /> | ||
<textarea></textarea> | ||
</div>, | ||
{ attachTo: attachNode } | ||
); | ||
|
||
keyPress( 68, wrapper.find( 'textarea' ).getDOMNode() ); | ||
|
||
expect( spy ).toHaveBeenCalled(); | ||
} ); | ||
} ); |