aboutsummaryrefslogtreecommitdiff
path: root/test/background/operators/impls/FindPrevOperator.test.ts
diff options
context:
space:
mode:
Diffstat (limited to 'test/background/operators/impls/FindPrevOperator.test.ts')
-rw-r--r--test/background/operators/impls/FindPrevOperator.test.ts177
1 files changed, 67 insertions, 110 deletions
diff --git a/test/background/operators/impls/FindPrevOperator.test.ts b/test/background/operators/impls/FindPrevOperator.test.ts
index 090f815..b563076 100644
--- a/test/background/operators/impls/FindPrevOperator.test.ts
+++ b/test/background/operators/impls/FindPrevOperator.test.ts
@@ -1,4 +1,3 @@
-import * as sinon from "sinon";
import MockTabPresenter from "../../mock/MockTabPresenter";
import FindPrevOperator from "../../../../src/background/operators/impls/FindPrevOperator";
import MockFindRepository from "../../mock/MockFindRepository";
@@ -23,155 +22,113 @@ describe("FindPrevOperator", () => {
frameRepository
);
+ const findPrevSpy = jest.spyOn(findClient, "findPrev");
+ 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.slice(0)));
+ findPrevSpy.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("findPrev")
- .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,
+ });
+ findPrevSpy.mockResolvedValue(true);
+ const setLocalStateSpy = jest.spyOn(findRepository, "setLocalState");
await sut.run();
- mockFindRepository.verify();
- mockFindClient.verify();
+ expect(findPrevSpy).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,
- })
- );
-
- const mockFindClient = sinon.mock(findClient);
- mockFindClient
- .expects("findPrev")
- .withArgs(currentTabId, 100, keyword)
- .returns(Promise.resolve(false));
- mockFindClient
- .expects("clearSelection")
- .withArgs(currentTabId, 100)
- .returns(Promise.resolve());
- mockFindClient
- .expects("findPrev")
- .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({
+ keyword,
+ frameId: 100,
+ });
+ findPrevSpy.mockResolvedValueOnce(false).mockResolvedValueOnce(true);
+ const setLocalStateSpy = jest.spyOn(findRepository, "setLocalState");
await sut.run();
- mockFindRepository.verify();
- mockFindClient.verify();
+ expect(findPrevSpy).toBeCalledTimes(2);
+ expect(findPrevSpy.mock.calls[0][1]).toEqual(100);
+ expect(findPrevSpy.mock.calls[1][1]).toEqual(0);
+ expect(clearSelectionSpy).toBeCalledWith(currentTabId, 100);
+ expect(setLocalStateSpy).toBeCalledWith(currentTabId, {
+ keyword,
+ frameId: 0,
+ });
});
it("exercise a wrap-search", async () => {
- sinon.stub(findRepository, "getLocalState").returns(
- Promise.resolve({
- keyword,
- frameId: 0,
- })
- );
+ jest.spyOn(findRepository, "getLocalState").mockResolvedValue({
+ keyword,
+ frameId: 0,
+ });
- const mockFindClient = sinon.mock(findClient);
- mockFindClient
- .expects("findPrev")
- .withArgs(currentTabId, 0, keyword)
- .returns(Promise.resolve(false));
- mockFindClient
- .expects("clearSelection")
- .withArgs(currentTabId, 0)
- .returns(Promise.resolve());
- mockFindClient
- .expects("findPrev")
- .withArgs(currentTabId, 101, keyword)
- .returns(Promise.resolve(true));
-
- const mockFindRepository = sinon.mock(findRepository);
- mockFindRepository
- .expects("setLocalState")
- .withArgs(currentTabId, { keyword, frameId: 101 });
+ findPrevSpy.mockResolvedValueOnce(false).mockResolvedValueOnce(true);
+ const setLocalStateSpy = jest.spyOn(findRepository, "setLocalState");
await sut.run();
- mockFindRepository.verify();
- mockFindClient.verify();
+ expect(findPrevSpy).toBeCalledTimes(2);
+ expect(findPrevSpy.mock.calls[0][1]).toEqual(0);
+ expect(findPrevSpy.mock.calls[1][1]).toEqual(101);
+ expect(clearSelectionSpy).toBeCalledWith(currentTabId, 0);
+ expect(setLocalStateSpy).toBeCalledWith(currentTabId, {
+ keyword,
+ frameId: 101,
+ });
});
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("findPrev")
- .withArgs(currentTabId, 101, keyword)
- .returns(Promise.resolve(true));
-
- const mockFindRepository = sinon.mock(findRepository);
- mockFindRepository
- .expects("setLocalState")
- .withArgs(currentTabId, { keyword, frameId: 101 });
+ 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(101);
+ expect(clearSelectionSpy.mock.calls[1][1]).toEqual(100);
+ expect(clearSelectionSpy.mock.calls[2][1]).toEqual(0);
+ expect(findPrevSpy).toBeCalledWith(currentTabId, 101, keyword);
+ expect(setLocalStateSpy).toBeCalledWith(currentTabId, {
+ keyword,
+ frameId: 101,
+ });
});
});
});