aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShin'ya Ueoka <ueokande@i-beam.org>2017-08-15 20:47:52 +0900
committerShin'ya Ueoka <ueokande@i-beam.org>2017-08-15 20:47:52 +0900
commitb9fe3343fc46fb9898f6da9cff8bf0e4ab4e982c (patch)
treeaf4dbef491bc25185808234cd1107f2bb05a7ab5
parentae089cf5f16e5df40e4d14a9fe844ffc83bd6774 (diff)
add key-queue test
-rw-r--r--src/background/index.js2
-rw-r--r--src/background/key-queue.js12
-rw-r--r--test/background/key-queue.test.js47
3 files changed, 56 insertions, 5 deletions
diff --git a/src/background/index.js b/src/background/index.js
index 604ea92..d27c557 100644
--- a/src/background/index.js
+++ b/src/background/index.js
@@ -2,7 +2,7 @@ import * as actions from '../shared/actions';
import * as tabs from './tabs';
import KeyQueue from './key-queue';
-const queue = new KeyQueue();
+const queue = new KeyQueue(KeyQueue.DEFAULT_KEYMAP);
const keyDownHandle = (request) => {
return queue.push({
diff --git a/src/background/key-queue.js b/src/background/key-queue.js
index e21399e..666eec3 100644
--- a/src/background/key-queue.js
+++ b/src/background/key-queue.js
@@ -12,20 +12,20 @@ const DEFAULT_KEYMAP = [
export default class KeyQueue {
- constructor(keymap) {
+ constructor() {
this.data = [];
- this.keymap = keymap;
+ this.keymap = DEFAULT_KEYMAP;
}
push(key) {
this.data.push(key);
- let filtered = DEFAULT_KEYMAP.filter((map) => {
+ let filtered = this.keymap.filter((map) => {
return keys.hasPrefix(map.keys, this.data)
});
if (filtered.length == 0) {
this.data = [];
- return;
+ return null;
} else if (filtered.length == 1) {
let map = filtered[0];
if (map.keys.length == this.data.length) {
@@ -35,4 +35,8 @@ export default class KeyQueue {
}
return null;
}
+
+ queuedKeys() {
+ return this.data;
+ }
}
diff --git a/test/background/key-queue.test.js b/test/background/key-queue.test.js
new file mode 100644
index 0000000..7b0db9b
--- /dev/null
+++ b/test/background/key-queue.test.js
@@ -0,0 +1,47 @@
+import { expect } from "chai";
+import KeyQueue from '../../src/background/key-queue';
+import * as actions from '../../src/shared/actions';
+
+describe("keyQueue class", () => {
+ const KEYMAP = [
+ { keys: [{ code: KeyboardEvent.DOM_VK_G }, { code: KeyboardEvent.DOM_VK_G }],
+ action: [ actions.SCROLL_TOP ]},
+ { keys: [{ code: KeyboardEvent.DOM_VK_J }],
+ action: [ actions.SCROLL_DOWN ]},
+ ]
+
+ describe("#push", () => {
+ it("returns matched action", () => {
+ let queue = new KeyQueue(KEYMAP);
+ queue.push({ code: KeyboardEvent.DOM_VK_G });
+ let action = queue.push({ code: KeyboardEvent.DOM_VK_G });
+
+ expect(action).to.deep.equal([ actions.SCROLL_TOP ]);
+ });
+
+ it("returns null on no actions matched", () => {
+ let queue = new KeyQueue(KEYMAP);
+ queue.push({ code: KeyboardEvent.DOM_VK_G });
+ let action = queue.push({ code: KeyboardEvent.DOM_VK_X });
+
+ expect(action).to.be.null;
+ });
+ });
+
+ describe("#queuedKeys", () => {
+ it("queues keys on matched actions exist", () => {
+ let queue = new KeyQueue(KEYMAP);
+ queue.push({ code: KeyboardEvent.DOM_VK_G });
+
+ expect(queue.queuedKeys()).to.have.lengthOf(1);
+ });
+
+ it("flushs keys on no actions matched", () => {
+ let queue = new KeyQueue(KEYMAP);
+ queue.push({ code: KeyboardEvent.DOM_VK_G });
+ queue.push({ code: KeyboardEvent.DOM_VK_Z });
+
+ expect(queue.queuedKeys()).to.be.empty;
+ });
+ });
+});