aboutsummaryrefslogtreecommitdiff
path: root/test/background/operators/impls/FindNextOperator.test.ts
diff options
context:
space:
mode:
Diffstat (limited to 'test/background/operators/impls/FindNextOperator.test.ts')
-rw-r--r--test/background/operators/impls/FindNextOperator.test.ts176
1 files changed, 67 insertions, 109 deletions
diff --git a/test/background/operators/impls/FindNextOperator.test.ts b/test/background/operators/impls/FindNextOperator.test.ts
index 7509ef4..3bb47f0 100644
--- a/test/background/operators/impls/FindNextOperator.test.ts
+++ b/test/background/operators/impls/FindNextOperator.test.ts
@@ -1,4 +1,3 @@
-import * as sinon from "sinon";
import MockTabPresenter from "../../mock/MockTabPresenter";
import FindNextOperator from "../../../../src/background/operators/impls/FindNextOperator";
import MockFindRepository from "../../mock/MockFindRepository";
@@ -23,155 +22,114 @@ describe("FindNextOperator", () => {
frameRepository
);
+ const findNextSpy = jest.spyOn(findClient, "findNext");
+ const clearSelectionSpy = jest.spyOn(findClient, "clearSelection");
+
let currentTabId: number;
beforeEach(async () => {
- sinon.restore();
-
const currentTab = await tabPresenter.create("https://example.com/", {
active: true,
});
currentTabId = currentTab.id!;
- sinon
- .stub(frameRepository, "getFrameIds")
- .returns(Promise.resolve(frameIds));
+ findNextSpy.mockClear();
+ clearSelectionSpy.mockClear().mockReturnValue(Promise.resolve());
+ jest.spyOn(frameRepository, "getFrameIds").mockResolvedValue(frameIds);
});
describe("#run", () => {
it("shows errors if no previous keywords", async () => {
- sinon
- .stub(findRepository, "getLocalState")
- .returns(Promise.resolve(undefined));
-
- const mock = sinon.mock(consoleClient);
- mock
- .expects("showError")
- .withArgs(currentTabId, "No previous search keywords");
+ jest.spyOn(findRepository, "getLocalState").mockResolvedValue(undefined);
+ const showErrorSpy = jest
+ .spyOn(consoleClient, "showError")
+ .mockReturnValue(Promise.resolve());
await sut.run();
- mock.verify();
+ expect(showErrorSpy).toBeCalledWith(
+ currentTabId,
+ "No previous search keywords"
+ );
});
it("continues a search on the same frame", async () => {
- sinon.stub(findRepository, "getLocalState").returns(
- Promise.resolve({
- keyword,
- frameId: 100,
- })
- );
-
- const mockFindClient = sinon.mock(findClient);
- mockFindClient
- .expects("findNext")
- .withArgs(currentTabId, 100, keyword)
- .returns(Promise.resolve(true));
-
- const mockFindRepository = sinon.mock(findRepository);
- mockFindRepository
- .expects("setLocalState")
- .withArgs(currentTabId, { keyword, frameId: 100 });
+ jest.spyOn(findRepository, "getLocalState").mockResolvedValue({
+ keyword,
+ frameId: 100,
+ });
+ findNextSpy.mockResolvedValue(true);
+ const setLocalStateSpy = jest.spyOn(findRepository, "setLocalState");
await sut.run();
- mockFindRepository.verify();
- mockFindClient.verify();
+ expect(findNextSpy).toBeCalledWith(currentTabId, 100, keyword);
+ expect(setLocalStateSpy).toBeCalledWith(currentTabId, {
+ keyword,
+ frameId: 100,
+ });
});
it("continues a search on next frame", async () => {
- sinon.stub(findRepository, "getLocalState").returns(
- Promise.resolve({
- keyword,
- frameId: 100,
- })
- );
+ jest.spyOn(findRepository, "getLocalState").mockResolvedValue({
+ keyword,
+ frameId: 100,
+ });
- const mockFindClient = sinon.mock(findClient);
- mockFindClient
- .expects("findNext")
- .withArgs(currentTabId, 100, keyword)
- .returns(Promise.resolve(false));
- mockFindClient
- .expects("clearSelection")
- .withArgs(currentTabId, 100)
- .returns(Promise.resolve());
- mockFindClient
- .expects("findNext")
- .withArgs(currentTabId, 101, keyword)
- .returns(Promise.resolve(true));
-
- const mockFindRepository = sinon.mock(findRepository);
- mockFindRepository
- .expects("setLocalState")
- .withArgs(currentTabId, { keyword, frameId: 101 });
+ findNextSpy.mockResolvedValueOnce(false).mockResolvedValueOnce(true);
+ const setLocalStateSpy = jest.spyOn(findRepository, "setLocalState");
await sut.run();
- mockFindRepository.verify();
- mockFindClient.verify();
+ 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 () => {
- sinon.stub(findRepository, "getLocalState").returns(
- Promise.resolve({
- keyword,
- frameId: 101,
- })
- );
+ jest.spyOn(findRepository, "getLocalState").mockResolvedValue({
+ keyword,
+ frameId: 101,
+ });
- const mockFindClient = sinon.mock(findClient);
- mockFindClient
- .expects("findNext")
- .withArgs(currentTabId, 101, keyword)
- .returns(Promise.resolve(false));
- mockFindClient
- .expects("clearSelection")
- .withArgs(currentTabId, 101)
- .returns(Promise.resolve());
- mockFindClient
- .expects("findNext")
- .withArgs(currentTabId, 0, keyword)
- .returns(Promise.resolve(true));
-
- const mockFindRepository = sinon.mock(findRepository);
- mockFindRepository
- .expects("setLocalState")
- .withArgs(currentTabId, { keyword, frameId: 0 });
+ findNextSpy.mockResolvedValueOnce(false).mockResolvedValueOnce(true);
+ const setLocalStateSpy = jest.spyOn(findRepository, "setLocalState");
await sut.run();
- mockFindRepository.verify();
- mockFindClient.verify();
+ 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 () => {
- sinon
- .stub(findRepository, "getLocalState")
- .returns(Promise.resolve(undefined));
- sinon
- .stub(findRepository, "getGlobalKeyword")
- .returns(Promise.resolve(keyword));
- sinon.stub(consoleClient, "showInfo").returns(Promise.resolve());
-
- 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(true));
-
- const mockFindRepository = sinon.mock(findRepository);
- mockFindRepository
- .expects("setLocalState")
- .withArgs(currentTabId, { keyword, frameId: 0 });
+ 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();
- mockFindRepository.verify();
- mockFindClient.verify();
+ 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,
+ });
});
});
});