aboutsummaryrefslogtreecommitdiff
path: root/test/background/usecases/SettingUseCase.test.ts
blob: bfa599ce6c8bb6c39c5c538b0c9d140bf5fd0945 (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
import "reflect-metadata";
import SettingUseCase from "../../../src/background/usecases/SettingUseCase";
import SettingRepository from "../../../src/background/repositories/SettingRepository";
import SettingData, {JSONTextSettings} from "../../../src/shared/SettingData";
import CachedSettingRepository from "../../../src/background/repositories/CachedSettingRepository";
import Settings, {DefaultSetting} from "../../../src/shared/settings/Settings";
import Notifier from "../../../src/background/presenters/Notifier";
import {expect} from "chai";
import Properties from "../../../src/shared/settings/Properties";
import sinon from 'sinon';

class MockSettingRepository implements SettingRepository {
  load(): Promise<SettingData | null> {
    throw new Error("not implemented");
  }

  onChange(_: () => void): void {
  }
}

class MockCachedSettingRepository implements CachedSettingRepository {
  private current: Settings = DefaultSetting;

  get(): Promise<Settings> {
    return Promise.resolve(this.current);
  }

  setProperty(name: string, value: string | number | boolean): Promise<void> {
    (this.current.properties as any)[name] = value;
    return Promise.resolve();
  }

  update(value: Settings): Promise<void> {
    this.current = value;
    return Promise.resolve();
  }
}

class NopNotifier implements Notifier {
  notifyInvalidSettings(_onclick: () => void): Promise<void> {
    return Promise.resolve();
  }

  notifyUpdated(_version: string, _onclick: () => void): Promise<void> {
    return Promise.resolve();
  }
}

describe('SettingUseCase', () => {
  let localSettingRepository : SettingRepository;
  let syncSettingRepository : SettingRepository;
  let cachedSettingRepository : CachedSettingRepository;
  let notifier: Notifier;
  let sut : SettingUseCase;

  beforeEach(() => {
    localSettingRepository = new MockSettingRepository();
    syncSettingRepository = new MockSettingRepository();
    cachedSettingRepository = new MockCachedSettingRepository();
    notifier = new NopNotifier();
    sut = new SettingUseCase(
      localSettingRepository,
      syncSettingRepository,
      cachedSettingRepository,
      notifier
    );
  });

  describe('getCached', () => {
    it("returns cached settings", async () => {
      const settings = new Settings({
        keymaps: DefaultSetting.keymaps,
        search: DefaultSetting.search,
        blacklist: DefaultSetting.blacklist,
        properties: new Properties({
          hintchars: "abcd1234"
        }),
      });
      sinon.stub(cachedSettingRepository, "get")
        .returns(Promise.resolve(settings));

      const got = await sut.getCached();
      expect(got.properties.hintchars).to.equal("abcd1234");

    });
  });

  describe("reload", () => {
    context("when sync is not set", () => {
      it("loads settings from local storage", async() => {
        const settings = new Settings({
          keymaps: DefaultSetting.keymaps,
          search: DefaultSetting.search,
          blacklist: DefaultSetting.blacklist,
          properties: new Properties({
            hintchars: "abcd1234"
          }),
        });
        const settingData = SettingData.fromJSON({
          source: "json",
          json: JSONTextSettings.fromSettings(settings).toJSONText(),
        });

        sinon.stub(syncSettingRepository, "load")
          .returns(Promise.resolve(null));
        sinon.stub(localSettingRepository, "load")
          .returns(Promise.resolve(settingData));

        await sut.reload();

        const current = await cachedSettingRepository.get();
        expect(current.properties.hintchars).to.equal("abcd1234");
      });
    });

    context("when local is not set", () => {
      it("loads settings from sync storage", async() => {
        const settings = new Settings({
          keymaps: DefaultSetting.keymaps,
          search: DefaultSetting.search,
          blacklist: DefaultSetting.blacklist,
          properties: new Properties({
            hintchars: "aaaa1111"
          }),
        });
        const settingData = SettingData.fromJSON({
          source: "json",
          json: JSONTextSettings.fromSettings(settings).toJSONText(),
        });

        sinon.stub(syncSettingRepository, "load")
          .returns(Promise.resolve(settingData));
        sinon.stub(localSettingRepository, "load")
          .returns(Promise.resolve(null));

        await sut.reload();

        const current = await cachedSettingRepository.get();
        expect(current.properties.hintchars).to.equal("aaaa1111");
      });
    });

    context("neither local nor sync not set", () => {
      it("loads default settings", async() => {
        it("loads settings from sync storage", async() => {
          sinon.stub(syncSettingRepository, "load")
            .returns(Promise.resolve(null));
          sinon.stub(localSettingRepository, "load")
            .returns(Promise.resolve(null));

          await sut.reload();

          const current = await cachedSettingRepository.get();
          expect(current.properties.hintchars).to.equal(DefaultSetting.properties.hintchars);
        });

      })
    })
  })
});