-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
馃専 Fix #156: Add a customKeyCodes configuration option
- Loading branch information
Showing
6 changed files
with
156 additions
and
1 deletion.
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
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,55 @@ | ||
import React from 'react'; | ||
import {mount} from 'enzyme'; | ||
import {expect} from 'chai'; | ||
import sinon from 'sinon'; | ||
import simulant from 'simulant'; | ||
|
||
import { configure, GlobalHotKeys } from '../../src/'; | ||
|
||
describe('GlobalHotKeys: Using custom key codes:', function () { | ||
beforeEach(function () { | ||
this.reactDiv = document.createElement('div'); | ||
document.body.appendChild(this.reactDiv); | ||
}); | ||
|
||
afterEach(function() { | ||
document.body.removeChild(this.reactDiv); | ||
}); | ||
|
||
describe('when a custom key code has been added and a keyMap has been defined in terms of the custom key', () => { | ||
beforeEach(function () { | ||
this.customKeyName = 'BackTV'; | ||
|
||
configure({ customKeyCodes: { 10009: this.customKeyName }}); | ||
|
||
const keyMap = { | ||
ACTION: this.customKeyName | ||
}; | ||
|
||
this.handler = sinon.spy(); | ||
|
||
const handlers = { | ||
ACTION: this.handler | ||
}; | ||
|
||
this.wrapper = mount( | ||
<GlobalHotKeys keyMap={keyMap} handlers={handlers}> | ||
<div className="childElement" /> | ||
</GlobalHotKeys>, | ||
{ attachTo: this.reactDiv } | ||
); | ||
}); | ||
|
||
afterEach(function() { | ||
configure({ customKeyCodes: {}}); | ||
}); | ||
|
||
it('then calls the corresponding handler when the custom key occurs', function() { | ||
expect(this.handler).to.not.have.been.called; | ||
|
||
simulant.fire(this.reactDiv, 'keydown', { keyCode: 10009 }); | ||
|
||
expect(this.handler).to.have.been.calledOnce; | ||
}); | ||
}); | ||
}); |
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,45 @@ | ||
import React from 'react'; | ||
import {mount} from 'enzyme'; | ||
import {expect} from 'chai'; | ||
import sinon from 'sinon'; | ||
|
||
import {HotKeys, configure} from '../../src/'; | ||
import FocusableElement from '../support/FocusableElement'; | ||
|
||
describe('Using custom key codes:', function () { | ||
describe('when a custom key code has been added and a keyMap has been defined in terms of the custom key', () => { | ||
beforeEach(function () { | ||
this.customKeyName = 'BackTV'; | ||
this.customKeyCode = 10009; | ||
|
||
configure({ customKeyCodes: { [this.customKeyCode]: this.customKeyName }}); | ||
|
||
const keyMap = { | ||
ACTION: this.customKeyName | ||
}; | ||
|
||
this.handler = sinon.spy(); | ||
|
||
this.wrapper = mount( | ||
<HotKeys keyMap={keyMap} handlers={{ACTION: this.handler}}> | ||
<div className="childElement" /> | ||
</HotKeys> | ||
); | ||
|
||
this.targetElement = new FocusableElement(this.wrapper, '.childElement'); | ||
this.targetElement.focus(); | ||
}); | ||
|
||
afterEach(function() { | ||
configure({ customKeyCodes: {}}); | ||
}); | ||
|
||
it('then calls the corresponding handler when the custom key occurs', function() { | ||
expect(this.handler).to.not.have.been.called; | ||
|
||
this.targetElement.keyDown(undefined, { keyCode: this.customKeyCode }); | ||
|
||
expect(this.handler).to.have.been.calledOnce; | ||
}); | ||
}); | ||
}); |