aboutsummaryrefslogtreecommitdiff
path: root/test/content/usecases/KeymapUseCase.test.ts
blob: 9fced9c1c2d89a3ca9857afd494f61e01bcd30d2 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import "reflect-metadata";
import KeymapUseCase from '../../../src/content/usecases/KeymapUseCase';
import {expect} from 'chai';
import SettingRepository from "../../../src/content/repositories/SettingRepository";
import Settings from "../../../src/shared/settings/Settings";
import AddonEnabledRepository from "../../../src/content/repositories/AddonEnabledRepository";
import {KeymapRepositoryImpl} from "../../../src/content/repositories/KeymapRepository";
import Key from "../../../src/shared/settings/Key";
import AddressRepository from "../../../src/content/repositories/AddressRepository";

class MockSettingRepository implements SettingRepository {
  constructor(
    private readonly settings: Settings,
  ) {
  }

  get(): Settings {
    return this.settings;
  }

  set(_setting: Settings): void {
    throw new Error('TODO');
  }
}

class MockAddonEnabledRepository implements AddonEnabledRepository {
  constructor(
    private readonly enabled: boolean,
  ) {
  }

  get(): boolean {
    return this.enabled;
  }

  set(_on: boolean): void {
    throw new Error('TODO');
  }
}

class MockAddressRepository implements AddressRepository {
  constructor(
    private url: URL,
  ) {
  }

  getCurrentURL(): URL {
    return this.url;
  }
}


describe('KeymapUseCase', () => {
  context('with no-digis keymaps', () => {
    let settings = Settings.fromJSON({
      keymaps: {
        k: {type: 'scroll.vertically', count: -1},
        j: {type: 'scroll.vertically', count: 1},
        gg: {type: 'scroll.top'},
      },
    });

    let sut: KeymapUseCase;

    before(() => {
      sut = new KeymapUseCase(
          new KeymapRepositoryImpl(),
          new MockSettingRepository(settings),
          new MockAddonEnabledRepository(true),
          new MockAddressRepository(new URL('https://example.com')),
      );
    });

    it('returns matched operation', () => {
      expect(sut.nextOps(Key.fromMapKey('k'))).to.deep.equal([{type: 'scroll.vertically', count: -1}]);
      expect(sut.nextOps(Key.fromMapKey('j'))).to.deep.equal([{type: 'scroll.vertically', count: 1}]);
      expect(sut.nextOps(Key.fromMapKey('g'))).to.be.empty;
      expect(sut.nextOps(Key.fromMapKey('g'))).to.deep.equal([{type: 'scroll.top'}]);
      expect(sut.nextOps(Key.fromMapKey('z'))).to.be.empty;
    });

    it('repeats n-times by numeric prefix and multiple key operations', () => {
      expect(sut.nextOps(Key.fromMapKey('1'))).to.be.empty;
      expect(sut.nextOps(Key.fromMapKey('0'))).to.be.empty;
      expect(sut.nextOps(Key.fromMapKey('g'))).to.be.empty;
      expect(sut.nextOps(Key.fromMapKey('g'))).to.deep.equal(Array(10).fill({type: "scroll.top"}));
    });
  });

  context('when keymaps containing numeric mappings', () => {
    let settings = Settings.fromJSON({
      keymaps: {
        20: {type: "scroll.top"},
        g5: {type: 'scroll.bottom'},
      },
    });

    let sut: KeymapUseCase;

    before(() => {
      sut = new KeymapUseCase(
          new KeymapRepositoryImpl(),
          new MockSettingRepository(settings),
          new MockAddonEnabledRepository(true),
          new MockAddressRepository(new URL('https://example.com')),
      );
    });

    it('returns the matched operation ends with digit', () => {
      expect(sut.nextOps(Key.fromMapKey('g'))).to.be.empty;
      expect(sut.nextOps(Key.fromMapKey('5'))).to.be.deep.equal([{ type: 'scroll.bottom'}]);
    });

    it('returns an operation matched the operation with digit keymaps', () => {
      expect(sut.nextOps(Key.fromMapKey('2'))).to.be.empty;
      expect(sut.nextOps(Key.fromMapKey('0'))).to.be.deep.equal([{ type: 'scroll.top'}]);
    });

    it('returns operations repeated by numeric prefix', () => {
      expect(sut.nextOps(Key.fromMapKey('2'))).to.be.empty;
      expect(sut.nextOps(Key.fromMapKey('g'))).to.be.empty;
      expect(sut.nextOps(Key.fromMapKey('5'))).to.be.deep.equal(Array(2).fill({ type: 'scroll.bottom'}));
    });

    it('does not matches with digit operation with numeric prefix', () => {
      expect(sut.nextOps(Key.fromMapKey('3'))).to.be.empty;
      expect(sut.nextOps(Key.fromMapKey('2'))).to.be.empty;
      expect(sut.nextOps(Key.fromMapKey('0'))).to.be.empty;
      expect(sut.nextOps(Key.fromMapKey('g'))).to.be.empty;
      expect(sut.nextOps(Key.fromMapKey('5'))).to.be.deep.equal(Array(320).fill({ type: 'scroll.bottom'}));
    });
  });

  context('when the keys are mismatched with the operations', () => {
    let settings = Settings.fromJSON({
      keymaps: {
        gg: {type: "scroll.top"},
        G: {type: "scroll.bottom"},
      },
    });

    let sut: KeymapUseCase;

    before(() => {
      sut = new KeymapUseCase(
          new KeymapRepositoryImpl(),
          new MockSettingRepository(settings),
          new MockAddonEnabledRepository(true),
          new MockAddressRepository(new URL('https://example.com')),
      );
    });

    it('clears input keys with no-matched operations', () => {
      expect(sut.nextOps(Key.fromMapKey('g'))).to.be.empty;
      expect(sut.nextOps(Key.fromMapKey('x'))).to.be.empty; // clear
      expect(sut.nextOps(Key.fromMapKey('g'))).to.be.empty;
      expect(sut.nextOps(Key.fromMapKey('g'))).to.deep.equal([{type: "scroll.top"}]);
    });

    it('clears input keys and the prefix with no-matched operations', () => {
      expect(sut.nextOps(Key.fromMapKey('1'))).to.be.empty;
      expect(sut.nextOps(Key.fromMapKey('0'))).to.be.empty;
      expect(sut.nextOps(Key.fromMapKey('g'))).to.be.empty;
      expect(sut.nextOps(Key.fromMapKey('x'))).to.be.empty;   // clear
      expect(sut.nextOps(Key.fromMapKey('1'))).to.be.empty;
      expect(sut.nextOps(Key.fromMapKey('0'))).to.be.empty;
      expect(sut.nextOps(Key.fromMapKey('g'))).to.be.empty;
      expect(sut.nextOps(Key.fromMapKey('g'))).to.deep.equal(Array(10).fill({type: "scroll.top"}));
    });
  });

  context('when the site matches to the blacklist', () => {
    let settings = Settings.fromJSON({
      keymaps: {
        k: {type: 'scroll.vertically', count: -1},
        a: {type: 'addon.enable'},
        b: {type: 'addon.toggle.enabled'},
      },
    });

    let sut: KeymapUseCase;

    before(() => {
      sut = new KeymapUseCase(
          new KeymapRepositoryImpl(),
          new MockSettingRepository(settings),
          new MockAddonEnabledRepository(false),
          new MockAddressRepository(new URL('https://example.com')),
      );
    });

    it('returns only ADDON_ENABLE and ADDON_TOGGLE_ENABLED operation', () => {
      expect(sut.nextOps(Key.fromMapKey('k'))).to.be.empty;
      expect(sut.nextOps(Key.fromMapKey('a'))).to.deep.equal([{type: 'addon.enable'}]);
      expect(sut.nextOps(Key.fromMapKey('b'))).to.deep.equal([{type: 'addon.toggle.enabled'}]);
    });
  });

  context('when the site matches to the partial blacklist', () => {
    let settings = Settings.fromJSON({
      keymaps: {
        k: {type: 'scroll.vertically', count: -1},
        j: {type: 'scroll.vertically', count: 1},
        gg: {type: "scroll.top"},
        G: {type: "scroll.bottom"},
      },
      blacklist: [
        { url: "example.com", keys: ['g'] },
        { url: "example.org", keys: ['<S-G>'] }
      ],
    });

    it('blocks keys in the partial blacklist', () => {
      let sut = new KeymapUseCase(
          new KeymapRepositoryImpl(),
          new MockSettingRepository(settings),
          new MockAddonEnabledRepository(true),
          new MockAddressRepository(new URL('https://example.com')),
      );

      expect(sut.nextOps(Key.fromMapKey('k'))).to.deep.equal([{type: 'scroll.vertically', count: -1}]);
      expect(sut.nextOps(Key.fromMapKey('j'))).to.deep.equal([{type: 'scroll.vertically', count: 1}]);
      expect(sut.nextOps(Key.fromMapKey('g'))).to.be.empty;
      expect(sut.nextOps(Key.fromMapKey('g'))).to.be.empty;
      expect(sut.nextOps(Key.fromMapKey('G'))).to.deep.equal([{type: 'scroll.bottom'}]);

      sut = new KeymapUseCase(
          new KeymapRepositoryImpl(),
          new MockSettingRepository(settings),
          new MockAddonEnabledRepository(true),
          new MockAddressRepository(new URL('https://example.org')),
      );

      expect(sut.nextOps(Key.fromMapKey('g'))).to.be.empty;
      expect(sut.nextOps(Key.fromMapKey('g'))).to.deep.equal([{type: 'scroll.top'}]);
      expect(sut.nextOps(Key.fromMapKey('G'))).to.be.empty;
    });
  });
});