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
|
import MockTabPresenter from "../../mock/MockTabPresenter";
import FindNextOperator from "../../../../src/background/operators/impls/FindNextOperator";
import MockFindRepository from "../../mock/MockFindRepository";
import MockFindClient from "../../mock/MockFindClient";
import MockConsoleClient from "../../mock/MockConsoleClient";
import MockReadyFrameRepository from "../../mock/MockReadyFrameRepository";
describe("FindNextOperator", () => {
const keyword = "hello";
const frameIds = [0, 100, 101];
const tabPresenter = new MockTabPresenter();
const findRepository = new MockFindRepository();
const findClient = new MockFindClient();
const consoleClient = new MockConsoleClient();
const frameRepository = new MockReadyFrameRepository();
const sut = new FindNextOperator(
tabPresenter,
findRepository,
findClient,
consoleClient,
frameRepository
);
const findNextSpy = jest.spyOn(findClient, "findNext");
const clearSelectionSpy = jest.spyOn(findClient, "clearSelection");
let currentTabId: number;
beforeEach(async () => {
const currentTab = await tabPresenter.create("https://example.com/", {
active: true,
});
currentTabId = currentTab.id!;
findNextSpy.mockClear();
clearSelectionSpy.mockClear().mockReturnValue(Promise.resolve());
jest.spyOn(frameRepository, "getFrameIds").mockResolvedValue(frameIds);
});
describe("#run", () => {
it("shows errors if no previous keywords", async () => {
jest.spyOn(findRepository, "getLocalState").mockResolvedValue(undefined);
const showErrorSpy = jest
.spyOn(consoleClient, "showError")
.mockReturnValue(Promise.resolve());
await sut.run();
expect(showErrorSpy).toBeCalledWith(
currentTabId,
"No previous search keywords"
);
});
it("continues a search on the same frame", async () => {
jest.spyOn(findRepository, "getLocalState").mockResolvedValue({
keyword,
frameId: 100,
});
findNextSpy.mockResolvedValue(true);
const setLocalStateSpy = jest.spyOn(findRepository, "setLocalState");
await sut.run();
expect(findNextSpy).toBeCalledWith(currentTabId, 100, keyword);
expect(setLocalStateSpy).toBeCalledWith(currentTabId, {
keyword,
frameId: 100,
});
});
it("continues a search on next frame", async () => {
jest.spyOn(findRepository, "getLocalState").mockResolvedValue({
keyword,
frameId: 100,
});
findNextSpy.mockResolvedValueOnce(false).mockResolvedValueOnce(true);
const setLocalStateSpy = jest.spyOn(findRepository, "setLocalState");
await sut.run();
expect(findNextSpy).toBeCalledTimes(2);
expect(findNextSpy.mock.calls[0][1]).toEqual(100);
expect(findNextSpy.mock.calls[1][1]).toEqual(101);
expect(clearSelectionSpy).toBeCalledWith(currentTabId, 100);
expect(setLocalStateSpy).toBeCalledWith(currentTabId, {
keyword,
frameId: 101,
});
});
it("exercise a wrap-search", async () => {
jest.spyOn(findRepository, "getLocalState").mockResolvedValue({
keyword,
frameId: 101,
});
findNextSpy.mockResolvedValueOnce(false).mockResolvedValueOnce(true);
const setLocalStateSpy = jest.spyOn(findRepository, "setLocalState");
await sut.run();
expect(findNextSpy).toBeCalledTimes(2);
expect(findNextSpy.mock.calls[0][1]).toEqual(101);
expect(findNextSpy.mock.calls[1][1]).toEqual(0);
expect(clearSelectionSpy).toBeCalledWith(currentTabId, 101);
expect(setLocalStateSpy).toBeCalledWith(currentTabId, {
keyword,
frameId: 0,
});
});
it("starts a search with last keywords", async () => {
jest.spyOn(findRepository, "getLocalState").mockResolvedValue(undefined);
jest.spyOn(findRepository, "getGlobalKeyword").mockResolvedValue(keyword);
jest.spyOn(consoleClient, "showInfo").mockReturnValue(Promise.resolve());
const setLocalStateSpy = jest.spyOn(findRepository, "setLocalState");
await sut.run();
expect(clearSelectionSpy).toBeCalledTimes(3);
expect(clearSelectionSpy.mock.calls[0][1]).toEqual(0);
expect(clearSelectionSpy.mock.calls[1][1]).toEqual(100);
expect(clearSelectionSpy.mock.calls[2][1]).toEqual(101);
expect(findNextSpy).toBeCalledWith(currentTabId, 0, keyword);
expect(setLocalStateSpy).toBeCalledWith(currentTabId, {
keyword,
frameId: 0,
});
});
});
});
|