aboutsummaryrefslogtreecommitdiff
path: root/test/background/usecases/FindUseCase.test.ts
blob: eef211b8a6c470e031e449ee03e5ca8192556eab (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
import "reflect-metadata";
import sinon from "sinon";
import FindClient from "../../../src/background/clients/FindClient";
import StartFindUseCase from "../../../src/background/usecases/StartFindUseCase";
import FindRepository from "../../../src/background/repositories/FindRepository";
import { expect } from "chai";
import MockFindClient from "../mock/MockFindClient";
import MockFindRepository from "../mock/MockFindRepository";

describe("FindUseCase", () => {
  let findClient: FindClient;
  let findRepository: FindRepository;
  let sut: StartFindUseCase;

  const rangeData = (count: number): browser.find.RangeData[] => {
    const data = {
      text: "Hello, world",
      framePos: 0,
      startTextNodePos: 0,
      endTextNodePos: 0,
      startOffset: 0,
      endOffset: 0,
    };
    return Array(count).fill(data);
  };

  beforeEach(() => {
    findClient = new MockFindClient();
    findRepository = new MockFindRepository();
    sut = new StartFindUseCase(findClient, findRepository);
  });

  describe("startFind", function () {
    context("with a search keyword", () => {
      it("starts find and store last used keyword", async () => {
        const startFind = sinon
          .stub(findClient, "startFind")
          .returns(Promise.resolve({ count: 10, rangeData: rangeData(10) }));
        const highlightAll = sinon
          .mock(findClient)
          .expects("highlightAll")
          .once();
        const selectKeyword = sinon
          .mock(findClient)
          .expects("selectKeyword")
          .once();

        await sut.startFind(10, "Hello, world");

        expect(startFind.calledWith("Hello, world")).to.be.true;
        expect(await findRepository.getGlobalKeyword()).to.equals(
          "Hello, world"
        );
        expect((await findRepository.getLocalState(10))?.keyword).to.equal(
          "Hello, world"
        );
        highlightAll.verify();
        selectKeyword.verify();
      });

      it("throws an error if no matched", (done) => {
        sinon
          .stub(findClient, "startFind")
          .returns(Promise.resolve({ count: 0, rangeData: [] }));

        sut.startFind(10, "Hello, world").catch((e) => {
          expect(e).instanceof(Error);
          done();
        });
      });
    });

    context("without a search keyword", () => {
      it("starts find with last used keyword in the tab", async () => {
        const startFind = sinon
          .stub(findClient, "startFind")
          .returns(Promise.resolve({ count: 10, rangeData: rangeData(10) }));
        await findRepository.setLocalState(10, {
          keyword: "Hello, world",
          rangeData: rangeData(10),
          highlightPosition: 0,
        });
        const highlightAll = sinon
          .mock(findClient)
          .expects("highlightAll")
          .once();
        const selectKeyword = sinon
          .mock(findClient)
          .expects("selectKeyword")
          .once();

        await sut.startFind(10, undefined);

        expect(startFind.calledWith("Hello, world")).to.be.true;
        highlightAll.verify();
        selectKeyword.verify();
      });

      it("starts find with last used keyword in global", async () => {
        const startFind = sinon
          .stub(findClient, "startFind")
          .returns(Promise.resolve({ count: 10, rangeData: rangeData(10) }));
        await findRepository.setGlobalKeyword("Hello, world");
        const highlightAll = sinon
          .mock(findClient)
          .expects("highlightAll")
          .once();
        const selectKeyword = sinon
          .mock(findClient)
          .expects("selectKeyword")
          .once();

        await sut.startFind(10, undefined);

        expect(startFind.calledWith("Hello, world")).to.be.true;
        highlightAll.verify();
        selectKeyword.verify();
      });
    });
  });
});