diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2017-10-21 11:06:58 +0900 |
---|---|---|
committer | Shin'ya Ueoka <ueokande@i-beam.org> | 2017-10-21 12:47:44 +0900 |
commit | 214a5103f3e2914028206a13ba115c69a7ee07f1 (patch) | |
tree | f396f94064ae1626ae134e1932316fa11641c7c0 /test | |
parent | c2a663a64d23816821d333c0d1c7c7c968f494b0 (diff) |
emit mapped keys from input-component
Diffstat (limited to 'test')
-rw-r--r-- | test/content/actions/input.test.js | 8 | ||||
-rw-r--r-- | test/content/components/common/input.test.js | 78 |
2 files changed, 79 insertions, 7 deletions
diff --git a/test/content/actions/input.test.js b/test/content/actions/input.test.js index 6031829..30705d2 100644 --- a/test/content/actions/input.test.js +++ b/test/content/actions/input.test.js @@ -5,16 +5,10 @@ import * as inputActions from 'content/actions/input'; describe("input actions", () => { describe("keyPress", () => { it('create INPUT_KEY_PRESS action', () => { - let action = inputActions.keyPress('a', false); + let action = inputActions.keyPress('a'); expect(action.type).to.equal(actions.INPUT_KEY_PRESS); expect(action.key).to.equal('a'); }); - - it('create INPUT_KEY_PRESS action from key with ctrl', () => { - let action = inputActions.keyPress('b', true); - expect(action.type).to.equal(actions.INPUT_KEY_PRESS); - expect(action.key).to.equal('<C-B>'); - }); }); describe("clearKeys", () => { diff --git a/test/content/components/common/input.test.js b/test/content/components/common/input.test.js new file mode 100644 index 0000000..d4f554a --- /dev/null +++ b/test/content/components/common/input.test.js @@ -0,0 +1,78 @@ +import InputComponent from 'content/components/common/input'; +import { expect } from "chai"; + +describe('InputComponent', () => { + it('register callbacks', () => { + let component = new InputComponent(window.document); + component.onKey((key) => { + expect(key).is.equals('a'); + }); + component.onKeyDown({ key: 'a' }); + }); + + it('invoke callback once', () => { + let component = new InputComponent(window.document); + let a = 0, b = 0; + component.onKey((key) => { + if (key == 'a') { + ++a; + } else { + key == 'b' + ++b; + } + }); + component.onKeyDown({ key: 'a' }); + component.onKeyDown({ key: 'b' }); + component.onKeyPress({ key: 'a' }); + component.onKeyUp({ key: 'a' }); + component.onKeyPress({ key: 'b' }); + component.onKeyUp({ key: 'b' }); + + expect(a).is.equals(1); + expect(b).is.equals(1); + }) + + it('add prefix when ctrl pressed', () => { + let component = new InputComponent(window.document); + component.onKey((key) => { + expect(key).is.equals('<C-A>'); + }); + component.onKeyDown({ key: 'a', ctrlKey: true }); + }) + + it('does not invoke only meta keys', () => { + let component = new InputComponent(window.document); + component.onKey((key) => { + expect.fail(); + }); + component.onKeyDown({ key: 'Shift' }); + component.onKeyDown({ key: 'Control' }); + component.onKeyDown({ key: 'Alt' }); + component.onKeyDown({ key: 'OS' }); + }) + + it('ignores events from input elements', () => { + ['input', 'textarea', 'select'].forEach((name) => { + let target = window.document.createElement(name); + let component = new InputComponent(target); + component.onKey((key) => { + expect.fail(); + }); + component.onKeyDown({ key: 'x', target }); + }); + }); + + it('ignores events from contenteditable elements', () => { + let target = window.document.createElement('div'); + let component = new InputComponent(target); + component.onKey((key) => { + expect.fail(); + }); + + target.setAttribute('contenteditable', ''); + component.onKeyDown({ key: 'x', target }); + + target.setAttribute('contenteditable', 'true'); + component.onKeyDown({ key: 'x', target }); + }) +}); |