aboutsummaryrefslogtreecommitdiff
path: root/test/background/key-queue.test.js
blob: ac43228a785987c14dabc26d5d341703435bd3a2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { expect } from "chai";
import KeyQueue from '../../src/background/key-queue';

describe("keyQueue class", () => {
  const KEYMAP = {
    'g<C-X>GG': [],
    'gg': [ 'scroll.top' ],
  };

  const g = 'g'.charCodeAt(0);
  const G = 'G'.charCodeAt(0);
  const x = 'x'.charCodeAt(0);

  describe("#push", () => {
    it("returns matched action", () => {
      let queue = new KeyQueue(KEYMAP);
      queue.push({ code: g });
      let action = queue.push({ code: g });

      expect(action).to.deep.equal([ 'scroll.top' ]);
    });

    it("returns null on no actions matched", () => {
      let queue = new KeyQueue(KEYMAP);
      queue.push({ code: g });
      let action = queue.push({ code: G });

      expect(action).to.be.null;
      expect(queue.asKeymapChars()).to.be.empty;
    });
  });

  describe('#asKeymapChars', () => {
    let queue = new KeyQueue(KEYMAP);
    queue.push({ code: g });
    queue.push({ code: x, ctrl: true });
    queue.push({ code: G });

    expect(queue.asKeymapChars()).to.equal('g<C-X>G');
  });

  describe('#asCaretChars', () => {
    let queue = new KeyQueue(KEYMAP);
    queue.push({ code: g });
    queue.push({ code: x, ctrl: true });
    queue.push({ code: G });

    expect(queue.asCaretChars()).to.equal('g^XG');
  });
});