aboutsummaryrefslogtreecommitdiff
path: root/test/background/usecases/StartFindUseCase.test.ts
blob: 22ff9a5524617a86e906ca3298db4a251e3964b5 (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
import * as sinon from "sinon";
import MockFindClient from "../mock/MockFindClient";
import MockFindRepository from "../mock/MockFindRepository";
import MockConsoleClient from "../mock/MockConsoleClient";
import MockFramePresenter from "../mock/MockFramePresenter";
import StartFindUseCase from "../../../src/background/usecases/StartFindUseCase";

describe("StartFindUseCase", () => {
  const currentTabId = 100;
  const frameIds = [0, 100, 101];
  const keyword = "hello";

  const findClient = new MockFindClient();
  const findRepository = new MockFindRepository();
  const consoleClient = new MockConsoleClient();
  const framePresenter = new MockFramePresenter();
  const sut = new StartFindUseCase(
    findClient,
    findRepository,
    consoleClient,
    framePresenter
  );

  beforeEach(async () => {
    sinon.restore();

    sinon
      .stub(framePresenter, "getAllFrameIds")
      .returns(Promise.resolve(frameIds));
  });

  describe("startFind", () => {
    it("starts a find with a keyword", async () => {
      const mockFindClient = sinon.mock(findClient);
      mockFindClient.expects("clearSelection").withArgs(currentTabId, 0);
      mockFindClient.expects("clearSelection").withArgs(currentTabId, 100);
      mockFindClient.expects("clearSelection").withArgs(currentTabId, 101);
      mockFindClient
        .expects("findNext")
        .withArgs(currentTabId, 0, keyword)
        .returns(Promise.resolve(false));
      mockFindClient
        .expects("findNext")
        .withArgs(currentTabId, 100, keyword)
        .returns(Promise.resolve(true));
      const mockFindRepository = sinon.mock(findRepository);
      mockFindRepository
        .expects("setLocalState")
        .withArgs(currentTabId, { frameIds, framePos: 1, keyword });
      const mockConsoleClient = sinon.mock(consoleClient);
      mockConsoleClient
        .expects("showInfo")
        .withArgs(currentTabId, "Pattern found: " + keyword);

      await sut.startFind(currentTabId, keyword);

      mockFindClient.verify();
      mockFindRepository.verify();
      mockConsoleClient.verify();
    });

    it("starts a find with last local state", async () => {
      const mockFindClient = sinon.mock(findClient);
      mockFindClient.expects("clearSelection").withArgs(currentTabId, 0);
      mockFindClient.expects("clearSelection").withArgs(currentTabId, 100);
      mockFindClient.expects("clearSelection").withArgs(currentTabId, 101);
      mockFindClient
        .expects("findNext")
        .withArgs(currentTabId, 0, keyword)
        .returns(Promise.resolve(false));
      mockFindClient
        .expects("findNext")
        .withArgs(currentTabId, 100, keyword)
        .returns(Promise.resolve(true));
      const mockFindRepository = sinon.mock(findRepository);
      mockFindRepository
        .expects("getLocalState")
        .withArgs(currentTabId)
        .returns(Promise.resolve({ keyword, frameIds, framePos: 0 }));
      mockFindRepository
        .expects("setLocalState")
        .withArgs(currentTabId, { frameIds, framePos: 1, keyword });
      const mockConsoleClient = sinon.mock(consoleClient);
      mockConsoleClient
        .expects("showInfo")
        .withArgs(currentTabId, "Pattern found: " + keyword);

      await sut.startFind(currentTabId, undefined);

      mockFindClient.verify();
      mockFindRepository.verify();
      mockConsoleClient.verify();
    });

    it("starts a find with last global state", async () => {
      const mockFindClient = sinon.mock(findClient);
      mockFindClient.expects("clearSelection").withArgs(currentTabId, 0);
      mockFindClient.expects("clearSelection").withArgs(currentTabId, 100);
      mockFindClient.expects("clearSelection").withArgs(currentTabId, 101);
      mockFindClient
        .expects("findNext")
        .withArgs(currentTabId, 0, keyword)
        .returns(Promise.resolve(false));
      mockFindClient
        .expects("findNext")
        .withArgs(currentTabId, 100, keyword)
        .returns(Promise.resolve(true));
      const mockFindRepository = sinon.mock(findRepository);
      mockFindRepository
        .expects("getLocalState")
        .withArgs(currentTabId)
        .returns(Promise.resolve(undefined));
      mockFindRepository
        .expects("getGlobalKeyword")
        .returns(Promise.resolve(keyword));
      mockFindRepository
        .expects("setLocalState")
        .withArgs(currentTabId, { frameIds, framePos: 1, keyword });
      const mockConsoleClient = sinon.mock(consoleClient);
      mockConsoleClient
        .expects("showInfo")
        .withArgs(currentTabId, "Pattern found: " + keyword);

      await sut.startFind(currentTabId, undefined);

      mockFindClient.verify();
      mockFindRepository.verify();
      mockConsoleClient.verify();
    });

    it("shows an error when pattern not found", async () => {
      const mockFindClient = sinon.mock(findClient);
      mockFindClient.expects("clearSelection").withArgs(currentTabId, 0);
      mockFindClient.expects("clearSelection").withArgs(currentTabId, 100);
      mockFindClient.expects("clearSelection").withArgs(currentTabId, 101);
      mockFindClient
        .expects("findNext")
        .withArgs(currentTabId, 0, keyword)
        .returns(Promise.resolve(false));
      mockFindClient
        .expects("findNext")
        .withArgs(currentTabId, 100, keyword)
        .returns(Promise.resolve(false));
      mockFindClient
        .expects("findNext")
        .withArgs(currentTabId, 101, keyword)
        .returns(Promise.resolve(false));
      const mockFindRepository = sinon.mock(findRepository);
      mockFindRepository.expects("setLocalState").never();
      const mockConsoleClient = sinon.mock(consoleClient);
      mockConsoleClient
        .expects("showError")
        .withArgs(currentTabId, "Pattern not found: " + keyword);

      await sut.startFind(currentTabId, keyword);

      mockFindClient.verify();
      mockFindRepository.verify();
      mockConsoleClient.verify();
    });

    it("shows an error when no last keywords", async () => {
      sinon
        .stub(findRepository, "getLocalState")
        .returns(Promise.resolve(undefined));
      sinon
        .stub(findRepository, "getGlobalKeyword")
        .returns(Promise.resolve(undefined));

      const mockConsoleClient = sinon.mock(consoleClient);
      mockConsoleClient
        .expects("showError")
        .withArgs(currentTabId, "No previous search keywords");

      await sut.startFind(currentTabId, undefined);

      mockConsoleClient.verify();
    });
  });
});