aboutsummaryrefslogtreecommitdiff
path: root/test/content/components/common/input.test.ts
diff options
context:
space:
mode:
authorShin'ya Ueoka <ueokande@i-beam.org>2019-04-30 14:00:07 +0900
committerShin'ya Ueoka <ueokande@i-beam.org>2019-05-02 11:14:19 +0900
commitc60d0e7392fc708e961614d6b756a045de74f458 (patch)
tree0b9a5fce1879e38a92d5dbb2915779aee0ad22d6 /test/content/components/common/input.test.ts
parent257162e5b6b4993e1dff0d705ffa6f0d809033eb (diff)
Rename .js/.jsx to .ts/.tsx
Diffstat (limited to 'test/content/components/common/input.test.ts')
-rw-r--r--test/content/components/common/input.test.ts70
1 files changed, 70 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..2ba5507
--- /dev/null
+++ b/test/content/components/common/input.test.ts
@@ -0,0 +1,70 @@
+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;
+ }
+ });
+ 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('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 });
+ })
+});