aboutsummaryrefslogtreecommitdiff
path: root/test/background/completion/CompletionUseCase.test.ts
blob: 0d58e45660cd1ebd11732d7fea07dcc1535647dc (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
import "reflect-metadata";
import CompletionType from "../../../src/shared/CompletionType";
import BookmarkRepository, {BookmarkItem} from "../../../src/background/completion/BookmarkRepository";
import HistoryRepository, {HistoryItem} from "../../../src/background/completion/HistoryRepository";
import CompletionUseCase from "../../../src/background/completion/CompletionUseCase";
import CachedSettingRepository from "../../../src/background/repositories/CachedSettingRepository";
import Settings, {DefaultSetting} from "../../../src/shared/settings/Settings";
import { expect } from 'chai';
import sinon from 'sinon';
import Properties from "../../../src/shared/settings/Properties";
import Search from "../../../src/shared/settings/Search";

class MockBookmarkRepository implements BookmarkRepository {
  queryBookmarks(_query: string): Promise<BookmarkItem[]> {
    throw new Error("not implemented")
  }
}

class MockHistoryRepository implements HistoryRepository {
  queryHistories(_keywords: string): Promise<HistoryItem[]> {
    throw new Error("not implemented")
  }
}

class MockSettingRepository implements CachedSettingRepository {
  get(): Promise<Settings> {
    throw new Error("not implemented")
  }

  setProperty(_name: string, _value: string | number | boolean): Promise<void> {
    throw new Error("not implemented")
  }

  update(_value: Settings): Promise<void> {
    throw new Error("not implemented")
  }
}

describe('CompletionUseCase', () => {
  let bookmarkRepository: MockBookmarkRepository;
  let historyRepository: MockHistoryRepository;
  let settingRepository: MockSettingRepository;
  let sut: CompletionUseCase;

  beforeEach(() => {
    bookmarkRepository = new MockBookmarkRepository();
    historyRepository = new MockHistoryRepository();
    settingRepository = new MockSettingRepository();
    sut = new CompletionUseCase(bookmarkRepository, historyRepository, settingRepository)
  });

  describe('#getCompletionTypes', () => {
    it("returns completion types from the property", async () => {
      sinon.stub(settingRepository, 'get').returns(Promise.resolve(new Settings({
        keymaps: DefaultSetting.keymaps,
        search: DefaultSetting.search,
        properties: new Properties({ complete: "shb" }),
        blacklist: DefaultSetting.blacklist,
      })));

      const items = await sut.getCompletionTypes();
      expect(items).to.deep.equal([
        CompletionType.SearchEngines,
        CompletionType.History,
        CompletionType.Bookmarks
      ]);
    });
  });

  describe('#requestSearchEngines', () => {
    it("returns search engines matches by the query", async () => {
      sinon.stub(settingRepository, 'get').returns(Promise.resolve(new Settings({
        keymaps: DefaultSetting.keymaps,
        search: new Search("google", {
          "google": "https://google.com/search?q={}",
          "yahoo": "https://search.yahoo.com/search?q={}",
          "bing": "https://bing.com/search?q={}",
          "google_ja": "https://google.co.jp/search?q={}",
        }),
        properties: DefaultSetting.properties,
        blacklist: DefaultSetting.blacklist,
      })));

      expect(await sut.requestSearchEngines("")).to.deep.equal([
        "google",
        "yahoo",
        "bing",
        "google_ja",
      ]);
      expect(await sut.requestSearchEngines("go")).to.deep.equal([
        "google",
        "google_ja",
      ]);
      expect(await sut.requestSearchEngines("x")).to.be.empty;
    })
  });

  describe('#requestBookmarks', () => {
    it("returns bookmarks from the repository", async() => {
      sinon.stub(bookmarkRepository, 'queryBookmarks')
        .withArgs("site").returns(Promise.resolve([
          { title: "site1", url: "https://site1.example.com" },
          { title: "site2", url: "https://site2.example.com/" },
        ]))
        .withArgs("xyz").returns(Promise.resolve([]));

      expect(await sut.requestBookmarks("site")).to.deep.equal([
        { title: "site1", url: "https://site1.example.com" },
        { title: "site2", url: "https://site2.example.com/" },
      ]);
      expect(await sut.requestBookmarks("xyz")).to.be.empty;
    });
  });

  describe('#requestHistory', () => {
    it("returns histories from the repository", async() => {
      sinon.stub(historyRepository, 'queryHistories')
        .withArgs("site").returns(Promise.resolve([
        { title: "site1", url: "https://site1.example.com" },
        { title: "site2", url: "https://site2.example.com/" },
      ]))
        .withArgs("xyz").returns(Promise.resolve([]));

      expect(await sut.requestHistory("site")).to.deep.equal([
        { title: "site1", url: "https://site1.example.com" },
        { title: "site2", url: "https://site2.example.com/" },
      ]);
      expect(await sut.requestHistory("xyz")).to.be.empty;
    });
  });
});