aboutsummaryrefslogtreecommitdiff
path: root/test/background/usecases/SettingUseCase.test.ts
blob: 395e8a4f8f3d6a8659a8e88b98a32932de0a394e (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
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 Properties from "../../../src/shared/settings/Properties";

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(_error: Error, _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",
        }),
      });
      jest.spyOn(cachedSettingRepository, "get").mockResolvedValue(settings);

      const got = await sut.getCached();
      expect(got.properties.hintchars).toEqual("abcd1234");
    });
  });

  describe("reload", () => {
    describe("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(),
        });

        jest.spyOn(syncSettingRepository, "load").mockResolvedValue(null);
        jest
          .spyOn(localSettingRepository, "load")
          .mockResolvedValue(settingData);

        await sut.reload();

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

    describe("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(),
        });

        jest
          .spyOn(syncSettingRepository, "load")
          .mockResolvedValue(settingData);
        jest.spyOn(localSettingRepository, "load").mockResolvedValue(null);

        await sut.reload();

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

    describe("neither local nor sync not set", () => {
      it("loads settings from sync storage", async () => {
        jest.spyOn(syncSettingRepository, "load").mockResolvedValue(null);
        jest.spyOn(localSettingRepository, "load").mockResolvedValue(null);

        await sut.reload();

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