diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2019-05-07 21:16:47 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-07 21:16:47 +0900 |
commit | 05ef6a8ca35aaa801c11eb6b4896caa3690058af (patch) | |
tree | 2c7708ca91ac2b462cc86aa28612e3d3943496f3 /test/content/components/common/input.test.ts | |
parent | 457d954e08923b4accd28a919c72d0b61db1bb98 (diff) | |
parent | 27d0a7f37d24a0ad68a8ccb7dee18fc1d00eea58 (diff) |
Merge pull request #578 from ueokande/move-to-typescript
Move to TypeScript
Diffstat (limited to 'test/content/components/common/input.test.ts')
-rw-r--r-- | test/content/components/common/input.test.ts | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/test/content/components/common/input.test.ts b/test/content/components/common/input.test.ts new file mode 100644 index 0000000..f3a943c --- /dev/null +++ b/test/content/components/common/input.test.ts @@ -0,0 +1,72 @@ +import InputComponent from 'content/components/common/input'; + +describe('InputComponent', () => { + it('register callbacks', () => { + let component = new InputComponent(window.document); + let key = { key: 'a', ctrlKey: true, shiftKey: false, altKey: false, metaKey: false }; + component.onKey((key) => { + expect(key).to.deep.equal(key); + }); + component.onKeyDown(key); + }); + + it('invoke callback once', () => { + let component = new InputComponent(window.document); + let a = 0, b = 0; + component.onKey((key) => { + if (key.key == 'a') { + ++a; + } else { + key.key == 'b' + ++b; + } + }); + + let elem = document.body; + component.onKeyDown({ key: 'a', target: elem }); + component.onKeyDown({ key: 'b', target: elem }); + component.onKeyPress({ key: 'a', target: elem }); + component.onKeyUp({ key: 'a', target: elem }); + component.onKeyPress({ key: 'b', target: elem }); + component.onKeyUp({ key: 'b', target: elem }); + + expect(a).is.equals(1); + expect(b).is.equals(1); + }) + + 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 }); + }) +}); |