add key-queue test

jh-changes
Shin'ya Ueoka 7 years ago
parent ae089cf5f1
commit b9fe3343fc
  1. 2
      src/background/index.js
  2. 12
      src/background/key-queue.js
  3. 47
      test/background/key-queue.test.js

@ -2,7 +2,7 @@ import * as actions from '../shared/actions';
import * as tabs from './tabs'; import * as tabs from './tabs';
import KeyQueue from './key-queue'; import KeyQueue from './key-queue';
const queue = new KeyQueue(); const queue = new KeyQueue(KeyQueue.DEFAULT_KEYMAP);
const keyDownHandle = (request) => { const keyDownHandle = (request) => {
return queue.push({ return queue.push({

@ -12,20 +12,20 @@ const DEFAULT_KEYMAP = [
export default class KeyQueue { export default class KeyQueue {
constructor(keymap) { constructor() {
this.data = []; this.data = [];
this.keymap = keymap; this.keymap = DEFAULT_KEYMAP;
} }
push(key) { push(key) {
this.data.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) return keys.hasPrefix(map.keys, this.data)
}); });
if (filtered.length == 0) { if (filtered.length == 0) {
this.data = []; this.data = [];
return; return null;
} else if (filtered.length == 1) { } else if (filtered.length == 1) {
let map = filtered[0]; let map = filtered[0];
if (map.keys.length == this.data.length) { if (map.keys.length == this.data.length) {
@ -35,4 +35,8 @@ export default class KeyQueue {
} }
return null; return null;
} }
queuedKeys() {
return this.data;
}
} }

@ -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;
});
});
});