aboutsummaryrefslogtreecommitdiff
path: root/test/shared/settings/Key.test.ts
blob: 8ad9265d82bbb987ef1262b7d4f1ee90d5fe8337 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import Key from "../../../src/shared/settings/Key";

describe("Key", () => {
  describe("fromMapKey", () => {
    it("return for X", () => {
      const key = Key.fromMapKey("x");
      expect(key.key).toEqual("x");
      expect(key.shift).toBeFalsy;
      expect(key.ctrl).toBeFalsy;
      expect(key.alt).toBeFalsy;
      expect(key.meta).toBeFalsy;
    });

    it("return for Shift+X", () => {
      const key = Key.fromMapKey("X");
      expect(key.key).toEqual("X");
      expect(key.shift).toBeTruthy;
      expect(key.ctrl).toBeFalsy;
      expect(key.alt).toBeFalsy;
      expect(key.meta).toBeFalsy;
    });

    it("return for Ctrl+X", () => {
      const key = Key.fromMapKey("<C-X>");
      expect(key.key).toEqual("x");
      expect(key.shift).toBeFalsy;
      expect(key.ctrl).toBeTruthy;
      expect(key.alt).toBeFalsy;
      expect(key.meta).toBeFalsy;
    });

    it("returns for Ctrl+Meta+X", () => {
      const key = Key.fromMapKey("<C-M-X>");
      expect(key.key).toEqual("x");
      expect(key.shift).toBeFalsy;
      expect(key.ctrl).toBeTruthy;
      expect(key.alt).toBeFalsy;
      expect(key.meta).toBeTruthy;
    });

    it("returns for Ctrl+Shift+x", () => {
      const key = Key.fromMapKey("<C-S-x>");
      expect(key.key).toEqual("X");
      expect(key.shift).toBeTruthy;
      expect(key.ctrl).toBeTruthy;
      expect(key.alt).toBeFalsy;
      expect(key.meta).toBeFalsy;
    });

    it("returns for Shift+Esc", () => {
      const key = Key.fromMapKey("<S-Esc>");
      expect(key.key).toEqual("Esc");
      expect(key.shift).toBeTruthy;
      expect(key.ctrl).toBeFalsy;
      expect(key.alt).toBeFalsy;
      expect(key.meta).toBeFalsy;
    });

    it("returns for Ctrl+Esc", () => {
      const key = Key.fromMapKey("<C-Esc>");
      expect(key.key).toEqual("Esc");
      expect(key.shift).toBeFalsy;
      expect(key.ctrl).toBeTruthy;
      expect(key.alt).toBeFalsy;
      expect(key.meta).toBeFalsy;
    });

    it("returns for Ctrl+Esc", () => {
      const key = Key.fromMapKey("<C-Space>");
      expect(key.key).toEqual("Space");
      expect(key.shift).toBeFalsy;
      expect(key.ctrl).toBeTruthy;
      expect(key.alt).toBeFalsy;
      expect(key.meta).toBeFalsy;
    });
  });

  describe("idDigit", () => {
    it("returns true if the key is a digit", () => {
      expect(new Key({ key: "0" }).isDigit()).toBeTruthy;
      expect(new Key({ key: "9" }).isDigit()).toBeTruthy;
      expect(new Key({ key: "9", alt: true }).isDigit()).toBeFalsy;

      expect(new Key({ key: "a" }).isDigit()).toBeFalsy;
      expect(new Key({ key: "0" }).isDigit()).toBeFalsy;
    });
  });

  describe("equals", () => {
    it("returns true if the keys are equivalent", () => {
      expect(
        new Key({
          key: "x",
          shift: false,
          ctrl: true,
          alt: false,
          meta: false,
        }).equals(
          new Key({
            key: "x",
            shift: false,
            ctrl: true,
            alt: false,
            meta: false,
          })
        )
      ).toBeTruthy;

      expect(
        new Key({
          key: "x",
          shift: false,
          ctrl: false,
          alt: false,
          meta: false,
        }).equals(
          new Key({
            key: "X",
            shift: true,
            ctrl: false,
            alt: false,
            meta: false,
          })
        )
      ).toBeFalsy;
    });
  });
});