aboutsummaryrefslogtreecommitdiff
path: root/test/content/usecases/KeymapUseCase.test.ts
blob: 5f2febab70b02018af2222f789c9df6f2721899a (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
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', () => {
  it('returns matched operation', () => {
    let settings = Settings.fromJSON({
      keymaps: {
        k: {type: 'scroll.vertically', count: -1},
        j: {type: 'scroll.vertically', count: 1},
        gg: {type: 'scroll.top'},
      },
    });
    let sut = new KeymapUseCase(
      new KeymapRepositoryImpl(),
      new MockSettingRepository(settings),
      new MockAddonEnabledRepository(true),
      new MockAddressRepository(new URL('https://example.com')),
    );

    expect(sut.nextOp(Key.fromMapKey('k'))).to.deep.equal({type: 'scroll.vertically', count: -1});
    expect(sut.nextOp(Key.fromMapKey('j'))).to.deep.equal({type: 'scroll.vertically', count: 1});
    expect(sut.nextOp(Key.fromMapKey('g'))).to.be.null;
    expect(sut.nextOp(Key.fromMapKey('g'))).to.deep.equal({type: 'scroll.top'});
    expect(sut.nextOp(Key.fromMapKey('z'))).to.be.null;
  });

  it('returns only ADDON_ENABLE and ADDON_TOGGLE_ENABLED operation', () => {
    let settings = Settings.fromJSON({
      keymaps: {
        k: {type: 'scroll.vertically', count: -1},
        a: {type: 'addon.enable'},
        b: {type: 'addon.toggle.enabled'},
      },
    });
    let sut = new KeymapUseCase(
      new KeymapRepositoryImpl(),
      new MockSettingRepository(settings),
      new MockAddonEnabledRepository(false),
      new MockAddressRepository(new URL('https://example.com')),
    );

    expect(sut.nextOp(Key.fromMapKey('k'))).to.be.null;
    expect(sut.nextOp(Key.fromMapKey('a'))).to.deep.equal({type: 'addon.enable'});
    expect(sut.nextOp(Key.fromMapKey('b'))).to.deep.equal({type: 'addon.toggle.enabled'});
  });

  it('blocks keys in 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>'] }
      ],
    });

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

    expect(sut.nextOp(Key.fromMapKey('k'))).to.deep.equal({type: 'scroll.vertically', count: -1});
    expect(sut.nextOp(Key.fromMapKey('j'))).to.deep.equal({type: 'scroll.vertically', count: 1});
    expect(sut.nextOp(Key.fromMapKey('g'))).to.be.null;
    expect(sut.nextOp(Key.fromMapKey('g'))).to.be.null;
    expect(sut.nextOp(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.nextOp(Key.fromMapKey('g'))).to.be.null;
    expect(sut.nextOp(Key.fromMapKey('g'))).to.deep.equal({type: 'scroll.top'});
    expect(sut.nextOp(Key.fromMapKey('G'))).to.be.null;
  });
});