diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2021-10-10 01:42:37 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-10 01:42:37 +0000 |
commit | dfcefe1b84cc96ead1c8d8f9aa65ff05ccd70378 (patch) | |
tree | 12f1a4ed6da8fd96c034d23bcf08b1535bca1113 /test/background/operators/impls/FindPrevOperator.test.ts | |
parent | 24f4f06db6572d81cadfe191f36c433a79985871 (diff) | |
parent | 039095e18562c44edda2c5a83a3d82c2e220b370 (diff) |
Merge pull request #1267 from ueokande/move-to-jest
Move to Jest
Diffstat (limited to 'test/background/operators/impls/FindPrevOperator.test.ts')
-rw-r--r-- | test/background/operators/impls/FindPrevOperator.test.ts | 177 |
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, + }); }); }); }); |