From acfe1b78009b0c3183cd69eebdabb458e2079b73 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Thu, 30 Sep 2021 00:16:32 +0900 Subject: Move mock library to jest --- .../completion/OpenCompletionUseCase.test.ts | 85 ++++---- .../completion/TabCompletionUseCase.test.ts | 89 +++----- .../operators/impls/CancelOperator.test.ts | 10 +- .../operators/impls/FindNextOperator.test.ts | 176 ++++++---------- .../operators/impls/FindPrevOperator.test.ts | 177 ++++++---------- .../impls/NavigateHistoryNextOperator.test.ts | 10 +- .../impls/NavigateHistoryPrevOperator.test.ts | 10 +- .../impls/NavigateLinkNextOperator.test.ts | 7 +- .../impls/NavigateLinkPrevOperator.test.ts | 7 +- .../operators/impls/ReloadTabOperator.test.ts | 16 +- .../operators/impls/ReopenTabOperator.test.ts | 7 +- .../operators/impls/RepeatLastOperator.test.ts | 25 +-- .../operators/impls/ResetZoomOperator.test.ts | 7 +- .../SelectPreviousSelectedTabOperator.test.ts | 13 +- .../impls/ShowAddBookmarkOperator.test.ts | 19 +- .../impls/ShowBufferCommandOperator.test.ts | 10 +- .../operators/impls/ShowCommandOperator.test.ts | 10 +- .../impls/ShowOpenCommandOperator.test.ts | 19 +- .../impls/ShowTabOpenCommandOperator.test.ts | 19 +- .../impls/ShowWinOpenCommandOperator.test.ts | 19 +- .../operators/impls/StartFindOperator.test.ts | 7 +- .../operators/impls/ZoomInOperator.test.ts | 7 +- .../operators/impls/ZoomOutOperator.test.ts | 7 +- test/background/usecases/SettingUseCase.test.ts | 33 +-- test/background/usecases/StartFindUseCase.test.ts | 229 ++++++++++----------- test/content/operators/impls/FocusOperator.test.ts | 7 +- test/content/operators/impls/PasteOperator.test.ts | 22 +- .../operators/impls/StartFollowOperator.test.ts | 10 +- 28 files changed, 447 insertions(+), 610 deletions(-) diff --git a/test/background/completion/OpenCompletionUseCase.test.ts b/test/background/completion/OpenCompletionUseCase.test.ts index 2284470..5ab9d5e 100644 --- a/test/background/completion/OpenCompletionUseCase.test.ts +++ b/test/background/completion/OpenCompletionUseCase.test.ts @@ -11,7 +11,6 @@ import CachedSettingRepository from "../../../src/background/repositories/Cached import Settings, { DefaultSetting, } from "../../../src/shared/settings/Settings"; -import sinon from "sinon"; import Properties from "../../../src/shared/settings/Properties"; import Search from "../../../src/shared/settings/Search"; @@ -60,15 +59,13 @@ describe("OpenCompletionUseCase", () => { describe("#getCompletionTypes", () => { it("returns completion types from the property", async () => { - sinon.stub(settingRepository, "get").returns( - Promise.resolve( - new Settings({ - keymaps: DefaultSetting.keymaps, - search: DefaultSetting.search, - properties: new Properties({ complete: "shb" }), - blacklist: DefaultSetting.blacklist, - }) - ) + jest.spyOn(settingRepository, "get").mockResolvedValueOnce( + new Settings({ + keymaps: DefaultSetting.keymaps, + search: DefaultSetting.search, + properties: new Properties({ complete: "shb" }), + blacklist: DefaultSetting.blacklist, + }) ); const items = await sut.getCompletionTypes(); @@ -82,20 +79,18 @@ describe("OpenCompletionUseCase", () => { describe("#requestSearchEngines", () => { it("returns search engines matches by the query", async () => { - sinon.stub(settingRepository, "get").returns( - Promise.resolve( - new Settings({ - keymaps: DefaultSetting.keymaps, - search: new Search("google", { - google: "https://google.com/search?q={}", - yahoo: "https://search.yahoo.com/search?q={}", - bing: "https://bing.com/search?q={}", - googleja: "https://google.co.jp/search?q={}", - }), - properties: DefaultSetting.properties, - blacklist: DefaultSetting.blacklist, - }) - ) + jest.spyOn(settingRepository, "get").mockResolvedValue( + new Settings({ + keymaps: DefaultSetting.keymaps, + search: new Search("google", { + google: "https://google.com/search?q={}", + yahoo: "https://search.yahoo.com/search?q={}", + bing: "https://bing.com/search?q={}", + googleja: "https://google.co.jp/search?q={}", + }), + properties: DefaultSetting.properties, + blacklist: DefaultSetting.blacklist, + }) ); expect(await sut.requestSearchEngines("")).toEqual([ @@ -114,45 +109,43 @@ describe("OpenCompletionUseCase", () => { describe("#requestBookmarks", () => { it("returns bookmarks from the repository", async () => { - sinon - .stub(bookmarkRepository, "queryBookmarks") - .withArgs("site") - .returns( - Promise.resolve([ - { title: "site1", url: "https://site1.example.com" }, - { title: "site2", url: "https://site2.example.com/" }, - ]) - ) - .withArgs("xyz") - .returns(Promise.resolve([])); + const spy = jest + .spyOn(bookmarkRepository, "queryBookmarks") + .mockResolvedValueOnce([ + { title: "site1", url: "https://site1.example.com" }, + { title: "site2", url: "https://site2.example.com/" }, + ]) + .mockResolvedValueOnce([]); expect(await sut.requestBookmarks("site")).toEqual([ { title: "site1", url: "https://site1.example.com" }, { title: "site2", url: "https://site2.example.com/" }, ]); expect(await sut.requestBookmarks("xyz")).toHaveLength(0); + expect(spy).toBeCalledTimes(2); + expect(spy.mock.calls[0][0]).toEqual("site"); + expect(spy.mock.calls[1][0]).toEqual("xyz"); }); }); describe("#requestHistory", () => { it("returns histories from the repository", async () => { - sinon - .stub(historyRepository, "queryHistories") - .withArgs("site") - .returns( - Promise.resolve([ - { title: "site1", url: "https://site1.example.com" }, - { title: "site2", url: "https://site2.example.com/" }, - ]) - ) - .withArgs("xyz") - .returns(Promise.resolve([])); + const spy = jest + .spyOn(historyRepository, "queryHistories") + .mockResolvedValueOnce([ + { title: "site1", url: "https://site1.example.com" }, + { title: "site2", url: "https://site2.example.com/" }, + ]) + .mockResolvedValueOnce([]); expect(await sut.requestHistory("site")).toEqual([ { title: "site1", url: "https://site1.example.com" }, { title: "site2", url: "https://site2.example.com/" }, ]); expect(await sut.requestHistory("xyz")).toHaveLength(0); + expect(spy).toBeCalledTimes(2); + expect(spy.mock.calls[0][0]).toEqual("site"); + expect(spy.mock.calls[1][0]).toEqual("xyz"); }); }); }); diff --git a/test/background/completion/TabCompletionUseCase.test.ts b/test/background/completion/TabCompletionUseCase.test.ts index 3b2723b..c006b65 100644 --- a/test/background/completion/TabCompletionUseCase.test.ts +++ b/test/background/completion/TabCompletionUseCase.test.ts @@ -3,7 +3,6 @@ import TabRepositoryImpl from "../../../src/background/completion/impl/TabReposi import { Tab } from "../../../src/background/completion/TabRepository"; import TabPresenter from "../../../src/background/presenters/TabPresenter"; import TabCompletionUseCase from "../../../src/background/completion/TabCompletionUseCase"; -import sinon from "sinon"; import TabFlag from "../../../src/shared/TabFlag"; class MockTabRepository implements TabRepositoryImpl { @@ -97,43 +96,39 @@ describe("TabCompletionUseCase", () => { tabPresenter = new MockTabPresenter(); sut = new TabCompletionUseCase(tabRepository, tabPresenter); - sinon.stub(tabPresenter, "getLastSelectedId").returns(Promise.resolve(12)); - sinon.stub(tabRepository, "getAllTabs").returns( - Promise.resolve([ - { - id: 10, - index: 0, - title: "Google", - url: "https://google.com/", - faviconUrl: "https://google.com/favicon.ico", - active: false, - }, - { - id: 11, - index: 1, - title: "Yahoo", - url: "https://yahoo.com/", - faviconUrl: "https://yahoo.com/favicon.ico", - active: true, - }, - { - id: 12, - index: 2, - title: "Bing", - url: "https://bing.com/", - active: false, - }, - ]) - ); + jest.spyOn(tabPresenter, "getLastSelectedId").mockResolvedValue(12); + jest.spyOn(tabRepository, "getAllTabs").mockResolvedValue([ + { + id: 10, + index: 0, + title: "Google", + url: "https://google.com/", + faviconUrl: "https://google.com/favicon.ico", + active: false, + }, + { + id: 11, + index: 1, + title: "Yahoo", + url: "https://yahoo.com/", + faviconUrl: "https://yahoo.com/favicon.ico", + active: true, + }, + { + id: 12, + index: 2, + title: "Bing", + url: "https://bing.com/", + active: false, + }, + ]); }); describe("#queryTabs", () => { it("returns tab items", async () => { - sinon - .stub(tabRepository, "queryTabs") - .withArgs("", false) - .returns( - Promise.resolve([ + jest.spyOn(tabRepository, "queryTabs").mockImplementation((keyword) => + Promise.resolve( + [ { id: 10, index: 0, @@ -157,29 +152,11 @@ describe("TabCompletionUseCase", () => { url: "https://bing.com/", active: false, }, - ]) + ].filter( + (item) => item.title.includes(keyword) || item.url.includes(keyword) + ) ) - .withArgs("oo", false) - .returns( - Promise.resolve([ - { - id: 10, - index: 0, - title: "Google", - url: "https://google.com/", - faviconUrl: "https://google.com/favicon.ico", - active: false, - }, - { - id: 11, - index: 1, - title: "Yahoo", - url: "https://yahoo.com/", - faviconUrl: "https://yahoo.com/favicon.ico", - active: true, - }, - ]) - ); + ); expect(await sut.queryTabs("", false)).toEqual([ { diff --git a/test/background/operators/impls/CancelOperator.test.ts b/test/background/operators/impls/CancelOperator.test.ts index 915becf..a723f6f 100644 --- a/test/background/operators/impls/CancelOperator.test.ts +++ b/test/background/operators/impls/CancelOperator.test.ts @@ -1,4 +1,3 @@ -import sinon from "sinon"; import CancelOperator from "../../../../src/background/operators/impls/CancelOperator"; import MockTabPresenter from "../../mock/MockTabPresenter"; import MockConsoleClient from "../../mock/MockConsoleClient"; @@ -10,15 +9,14 @@ describe("CancelOperator", () => { const currenTab = await tabPresenter.create("https://example.com/"); const consoleClient = new MockConsoleClient(); - const mock = sinon - .mock(consoleClient) - .expects("hide") - .withArgs(currenTab?.id); + const spy = jest + .spyOn(consoleClient, "hide") + .mockResolvedValueOnce(undefined); const sut = new CancelOperator(tabPresenter, consoleClient); await sut.run(); - mock.verify(); + expect(spy).toBeCalledWith(currenTab?.id); }); }); }); 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, + }); }); }); }); 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, + }); }); }); }); diff --git a/test/background/operators/impls/NavigateHistoryNextOperator.test.ts b/test/background/operators/impls/NavigateHistoryNextOperator.test.ts index de8f597..8ea925b 100644 --- a/test/background/operators/impls/NavigateHistoryNextOperator.test.ts +++ b/test/background/operators/impls/NavigateHistoryNextOperator.test.ts @@ -1,4 +1,3 @@ -import sinon from "sinon"; import NavigateHistoryNextOperator from "../../../../src/background/operators/impls/NavigateHistoryNextOperator"; import MockTabPresenter from "../../mock/MockTabPresenter"; import MockNavigateClient from "../../mock/MockNavigateClient"; @@ -7,10 +6,9 @@ describe("NavigateHistoryNextOperator", () => { describe("#run", () => { it("send a message to navigate next in the history", async () => { const navigateClient = new MockNavigateClient(); - const mock = sinon - .mock(navigateClient) - .expects("historyNext") - .withArgs(1); + const historyNextSpy = jest + .spyOn(navigateClient, "historyNext") + .mockReturnValue(Promise.resolve()); const tabPresenter = new MockTabPresenter(); await tabPresenter.create("https://example.com/1", { active: false }); await tabPresenter.create("https://example.com/2", { active: true }); @@ -19,7 +17,7 @@ describe("NavigateHistoryNextOperator", () => { await sut.run(); - mock.verify(); + expect(historyNextSpy).toBeCalledWith(1); }); }); }); diff --git a/test/background/operators/impls/NavigateHistoryPrevOperator.test.ts b/test/background/operators/impls/NavigateHistoryPrevOperator.test.ts index 6ebe71e..8221a5c 100644 --- a/test/background/operators/impls/NavigateHistoryPrevOperator.test.ts +++ b/test/background/operators/impls/NavigateHistoryPrevOperator.test.ts @@ -1,4 +1,3 @@ -import sinon from "sinon"; import NavigateHistoryPrevOperator from "../../../../src/background/operators/impls/NavigateHistoryPrevOperator"; import MockTabPresenter from "../../mock/MockTabPresenter"; import MockNavigateClient from "../../mock/MockNavigateClient"; @@ -7,10 +6,9 @@ describe("NavigateHistoryPrevOperator", () => { describe("#run", () => { it("send a message to navigate previous in the history", async () => { const navigateClient = new MockNavigateClient(); - const mock = sinon - .mock(navigateClient) - .expects("historyPrev") - .withArgs(1); + const historyNextSpy = jest + .spyOn(navigateClient, "historyPrev") + .mockReturnValue(Promise.resolve()); const tabPresenter = new MockTabPresenter(); await tabPresenter.create("https://example.com/1", { active: false }); await tabPresenter.create("https://example.com/2", { active: true }); @@ -19,7 +17,7 @@ describe("NavigateHistoryPrevOperator", () => { await sut.run(); - mock.verify(); + expect(historyNextSpy).toBeCalledWith(1); }); }); }); diff --git a/test/background/operators/impls/NavigateLinkNextOperator.test.ts b/test/background/operators/impls/NavigateLinkNextOperator.test.ts index 09c4907..9f19307 100644 --- a/test/background/operators/impls/NavigateLinkNextOperator.test.ts +++ b/test/background/operators/impls/NavigateLinkNextOperator.test.ts @@ -1,4 +1,3 @@ -import sinon from "sinon"; import NavigateLinkNextOperator from "../../../../src/background/operators/impls/NavigateLinkNextOperator"; import MockTabPresenter from "../../mock/MockTabPresenter"; import MockNavigateClient from "../../mock/MockNavigateClient"; @@ -7,7 +6,9 @@ describe("NavigateLinkNextOperator", () => { describe("#run", () => { it("send a message to navigate next page", async () => { const navigateClient = new MockNavigateClient(); - const mock = sinon.mock(navigateClient).expects("linkNext").withArgs(1); + const linkNextSpy = jest + .spyOn(navigateClient, "linkNext") + .mockReturnValueOnce(Promise.resolve()); const tabPresenter = new MockTabPresenter(); await tabPresenter.create("https://example.com/1", { active: false }); await tabPresenter.create("https://example.com/2", { active: true }); @@ -16,7 +17,7 @@ describe("NavigateLinkNextOperator", () => { await sut.run(); - mock.verify(); + expect(linkNextSpy).toBeCalledWith(1); }); }); }); diff --git a/test/background/operators/impls/NavigateLinkPrevOperator.test.ts b/test/background/operators/impls/NavigateLinkPrevOperator.test.ts index 6b7f791..37999bd 100644 --- a/test/background/operators/impls/NavigateLinkPrevOperator.test.ts +++ b/test/background/operators/impls/NavigateLinkPrevOperator.test.ts @@ -1,4 +1,3 @@ -import sinon from "sinon"; import NavigateLinkPrevOperator from "../../../../src/background/operators/impls/NavigateLinkPrevOperator"; import MockTabPresenter from "../../mock/MockTabPresenter"; import MockNavigateClient from "../../mock/MockNavigateClient"; @@ -7,7 +6,9 @@ describe("NavigateLinkPrevOperator", () => { describe("#run", () => { it("send a message to navigate next page", async () => { const navigateClient = new MockNavigateClient(); - const mock = sinon.mock(navigateClient).expects("linkPrev").withArgs(1); + const linkPrevSpy = jest + .spyOn(navigateClient, "linkPrev") + .mockReturnValueOnce(Promise.resolve()); const tabPresenter = new MockTabPresenter(); await tabPresenter.create("https://example.com/1", { active: false }); await tabPresenter.create("https://example.com/2", { active: true }); @@ -16,7 +17,7 @@ describe("NavigateLinkPrevOperator", () => { await sut.run(); - mock.verify(); + expect(linkPrevSpy).toBeCalledWith(1); }); }); }); diff --git a/test/background/operators/impls/ReloadTabOperator.test.ts b/test/background/operators/impls/ReloadTabOperator.test.ts index e87782b..0cedf3e 100644 --- a/test/background/operators/impls/ReloadTabOperator.test.ts +++ b/test/background/operators/impls/ReloadTabOperator.test.ts @@ -1,4 +1,3 @@ -import sinon from "sinon"; import ReloadTabOperator from "../../../../src/background/operators/impls/ReloadTabOperator"; import MockTabPresenter from "../../mock/MockTabPresenter"; @@ -8,27 +7,28 @@ describe("ReloadTabOperator", () => { const tabPresenter = new MockTabPresenter(); await tabPresenter.create("https://example.com/", { active: true }); await tabPresenter.create("https://example.com/", { active: false }); - const mock = sinon.mock(tabPresenter).expects("reload").withArgs(0, true); + const reloadSpy = jest + .spyOn(tabPresenter, "reload") + .mockReturnValue(Promise.resolve()); const sut = new ReloadTabOperator(tabPresenter, true); await sut.run(); - mock.verify(); + expect(reloadSpy).toBeCalledWith(0, true); }); it("reloads the current tab without cache", async () => { const tabPresenter = new MockTabPresenter(); await tabPresenter.create("https://example.com/", { active: true }); await tabPresenter.create("https://example.com/", { active: false }); - const mock = sinon - .mock(tabPresenter) - .expects("reload") - .withArgs(0, false); + const reloadSpy = jest + .spyOn(tabPresenter, "reload") + .mockReturnValue(Promise.resolve()); const sut = new ReloadTabOperator(tabPresenter, false); await sut.run(); - mock.verify(); + expect(reloadSpy).toBeCalledWith(0, false); }); }); }); diff --git a/test/background/operators/impls/ReopenTabOperator.test.ts b/test/background/operators/impls/ReopenTabOperator.test.ts index 43b1575..5581f9a 100644 --- a/test/background/operators/impls/ReopenTabOperator.test.ts +++ b/test/background/operators/impls/ReopenTabOperator.test.ts @@ -1,4 +1,3 @@ -import sinon from "sinon"; import ReopenTabOperator from "../../../../src/background/operators/impls/ReopenTabOperator"; import MockTabPresenter from "../../mock/MockTabPresenter"; @@ -6,12 +5,14 @@ describe("ReopenTabOperator", () => { describe("#run", () => { it("reopens closed tabs", async () => { const tabPresenter = new MockTabPresenter(); - const mock = sinon.mock(tabPresenter).expects("reopen"); + const reopenSpy = jest + .spyOn(tabPresenter, "reopen") + .mockReturnValue(Promise.resolve()); const sut = new ReopenTabOperator(tabPresenter); await sut.run(); - mock.verify(); + expect(reopenSpy).toBeCalled(); }); }); }); diff --git a/test/background/operators/impls/RepeatLastOperator.test.ts b/test/background/operators/impls/RepeatLastOperator.test.ts index 57f1227..fc37615 100644 --- a/test/background/operators/impls/RepeatLastOperator.test.ts +++ b/test/background/operators/impls/RepeatLastOperator.test.ts @@ -3,7 +3,6 @@ import MockRepeatRepository from "../../mock/MockRepeatRepository"; import OperatorFactory from "../../../../src/background/operators/OperatorFactory"; import * as operations from "../../../../src/shared/operations"; import Operator from "../../../../src/background/operators/Operator"; -import sinon from "sinon"; class MockOperatorFactory implements OperatorFactory { create(_op: operations.Operation): Operator { @@ -21,36 +20,34 @@ describe("RepeatLastOperator", () => { describe("#run", () => { it("repeat last operation", async () => { const operator = new MockOperator(); - const operatorMock = sinon.mock(operator).expects("run").once(); + const runSpy = jest + .spyOn(operator, "run") + .mockReturnValue(Promise.resolve()); + const repeatRepository = new MockRepeatRepository(); repeatRepository.setLastOperation({ type: operations.CANCEL }); const operatorFactory = new MockOperatorFactory(); - const operatorFactoryMock = sinon - .mock(operatorFactory) - .expects("create") - .withArgs({ type: operations.CANCEL }); - operatorFactoryMock.returns(operator); + const createSpy = jest + .spyOn(operatorFactory, "create") + .mockReturnValue(operator); const sut = new RepeatLastOperator(repeatRepository, operatorFactory); await sut.run(); - operatorFactoryMock.verify(); - operatorMock.verify(); + expect(runSpy).toBeCalledTimes(1); + expect(createSpy).toBeCalledWith({ type: operations.CANCEL }); }); it("does nothing if no last operations", async () => { const repeatRepository = new MockRepeatRepository(); const operatorFactory = new MockOperatorFactory(); - const operatorFactoryMock = sinon - .mock(operatorFactory) - .expects("create") - .never(); + const createSpy = jest.spyOn(operatorFactory, "create"); const sut = new RepeatLastOperator(repeatRepository, operatorFactory); await sut.run(); - operatorFactoryMock.verify(); + expect(createSpy).not.toBeCalled(); }); }); }); diff --git a/test/background/operators/impls/ResetZoomOperator.test.ts b/test/background/operators/impls/ResetZoomOperator.test.ts index 68cda05..40c56d9 100644 --- a/test/background/operators/impls/ResetZoomOperator.test.ts +++ b/test/background/operators/impls/ResetZoomOperator.test.ts @@ -1,4 +1,3 @@ -import sinon from "sinon"; import ResetZoomOperator from "../../../../src/background/operators/impls/ResetZoomOperator"; import MockZoomPresenter from "../../mock/MockZoomPresenter"; @@ -6,12 +5,14 @@ describe("ResetZoomOperator", () => { describe("#run", () => { it("resets zoom on the tab", async () => { const zoomPresenter = new MockZoomPresenter(); - const mock = sinon.mock(zoomPresenter).expects("resetZoom").once(); + const resetZoomSpy = jest + .spyOn(zoomPresenter, "resetZoom") + .mockReturnValue(Promise.resolve()); const sut = new ResetZoomOperator(zoomPresenter); await sut.run(); - mock.verify(); + expect(resetZoomSpy).toBeCalled(); }); }); }); diff --git a/test/background/operators/impls/SelectPreviousSelectedTabOperator.test.ts b/test/background/operators/impls/SelectPreviousSelectedTabOperator.test.ts index 5d6827d..7872b2f 100644 --- a/test/background/operators/impls/SelectPreviousSelectedTabOperator.test.ts +++ b/test/background/operators/impls/SelectPreviousSelectedTabOperator.test.ts @@ -1,4 +1,3 @@ -import sinon from "sinon"; import MockTabPresenter from "../../mock/MockTabPresenter"; import SelectPreviousSelectedTabOperator from "../../../../src/background/operators/impls/SelectPreviousSelectedTabOperator"; @@ -9,7 +8,7 @@ describe("SelectPreviousSelectedTabOperator", () => { await tabPresenter.create("https://example.com/1", { active: false }); await tabPresenter.create("https://example.com/2", { active: true }); await tabPresenter.create("https://example.com/3", { active: false }); - sinon.stub(tabPresenter, "getLastSelectedId").returns(Promise.resolve(0)); + jest.spyOn(tabPresenter, "getLastSelectedId").mockResolvedValue(0); const sut = new SelectPreviousSelectedTabOperator(tabPresenter); await sut.run(); @@ -23,15 +22,15 @@ describe("SelectPreviousSelectedTabOperator", () => { await tabPresenter.create("https://example.com/1", { active: false }); await tabPresenter.create("https://example.com/2", { active: true }); await tabPresenter.create("https://example.com/3", { active: false }); - sinon - .stub(tabPresenter, "getLastSelectedId") - .returns(Promise.resolve(undefined)); - const mock = sinon.mock(tabPresenter).expects("select").never(); + jest + .spyOn(tabPresenter, "getLastSelectedId") + .mockResolvedValue(undefined); + const selectSpy = jest.spyOn(tabPresenter, "select"); const sut = new SelectPreviousSelectedTabOperator(tabPresenter); await sut.run(); - mock.verify(); + expect(selectSpy).not.toBeCalled(); }); }); }); diff --git a/test/background/operators/impls/ShowAddBookmarkOperator.test.ts b/test/background/operators/impls/ShowAddBookmarkOperator.test.ts index 1e083c2..aab7451 100644 --- a/test/background/operators/impls/ShowAddBookmarkOperator.test.ts +++ b/test/background/operators/impls/ShowAddBookmarkOperator.test.ts @@ -1,4 +1,3 @@ -import sinon from "sinon"; import ShowAddBookmarkOperator from "../../../../src/background/operators/impls/ShowAddBookmarkOperator"; import MockTabPresenter from "../../mock/MockTabPresenter"; import MockConsoleClient from "../../mock/MockConsoleClient"; @@ -11,10 +10,9 @@ describe("ShowAddBookmarkOperator", () => { await tabPresenter.create("https://example.com/2", { active: true }); await tabPresenter.create("https://example.com/3", { active: false }); const consoleClient = new MockConsoleClient(); - const mock = sinon - .mock(consoleClient) - .expects("showCommand") - .withArgs(1, "addbookmark "); + const showCommandSpy = jest + .spyOn(consoleClient, "showCommand") + .mockReturnValue(Promise.resolve()); const sut = new ShowAddBookmarkOperator( tabPresenter, @@ -23,7 +21,7 @@ describe("ShowAddBookmarkOperator", () => { ); await sut.run(); - mock.verify(); + expect(showCommandSpy).toBeCalledWith(1, "addbookmark "); }); it("show command with addbookmark command and an URL of the current tab", async () => { @@ -32,10 +30,9 @@ describe("ShowAddBookmarkOperator", () => { await tabPresenter.create("https://example.com/2", { active: true }); await tabPresenter.create("https://example.com/3", { active: false }); const consoleClient = new MockConsoleClient(); - const mock = sinon - .mock(consoleClient) - .expects("showCommand") - .withArgs(1, "addbookmark welcome, world"); + const showCommandSpy = jest + .spyOn(consoleClient, "showCommand") + .mockReturnValue(Promise.resolve()); const sut = new ShowAddBookmarkOperator( tabPresenter, @@ -44,7 +41,7 @@ describe("ShowAddBookmarkOperator", () => { ); await sut.run(); - mock.verify(); + expect(showCommandSpy).toBeCalledWith(1, "addbookmark welcome, world"); }); }); }); diff --git a/test/background/operators/impls/ShowBufferCommandOperator.test.ts b/test/background/operators/impls/ShowBufferCommandOperator.test.ts index 91455b3..fbd1429 100644 --- a/test/background/operators/impls/ShowBufferCommandOperator.test.ts +++ b/test/background/operators/impls/ShowBufferCommandOperator.test.ts @@ -1,4 +1,3 @@ -import sinon from "sinon"; import ShowBufferCommandOperator from "../../../../src/background/operators/impls/ShowBufferCommandOperator"; import MockTabPresenter from "../../mock/MockTabPresenter"; import MockConsoleClient from "../../mock/MockConsoleClient"; @@ -11,15 +10,14 @@ describe("ShowBufferCommandOperator", () => { await tabPresenter.create("https://example.com/2", { active: true }); await tabPresenter.create("https://example.com/3", { active: false }); const consoleClient = new MockConsoleClient(); - const mock = sinon - .mock(consoleClient) - .expects("showCommand") - .withArgs(1, "buffer "); + const showCommandSpy = jest + .spyOn(consoleClient, "showCommand") + .mockReturnValue(Promise.resolve()); const sut = new ShowBufferCommandOperator(tabPresenter, consoleClient); await sut.run(); - mock.verify(); + expect(showCommandSpy).toBeCalledWith(1, "buffer "); }); }); }); diff --git a/test/background/operators/impls/ShowCommandOperator.test.ts b/test/background/operators/impls/ShowCommandOperator.test.ts index 83b028c..c07176d 100644 --- a/test/background/operators/impls/ShowCommandOperator.test.ts +++ b/test/background/operators/impls/ShowCommandOperator.test.ts @@ -1,4 +1,3 @@ -import sinon from "sinon"; import ShowCommandOperator from "../../../../src/background/operators/impls/ShowCommandOperator"; import MockTabPresenter from "../../mock/MockTabPresenter"; import MockConsoleClient from "../../mock/MockConsoleClient"; @@ -11,15 +10,14 @@ describe("ShowCommandOperator", () => { await tabPresenter.create("https://example.com/2", { active: true }); await tabPresenter.create("https://example.com/3", { active: false }); const consoleClient = new MockConsoleClient(); - const mock = sinon - .mock(consoleClient) - .expects("showCommand") - .withArgs(1, ""); + const showCommandSpy = jest + .spyOn(consoleClient, "showCommand") + .mockReturnValue(Promise.resolve()); const sut = new ShowCommandOperator(tabPresenter, consoleClient); await sut.run(); - mock.verify(); + expect(showCommandSpy).toBeCalledWith(1, ""); }); }); }); diff --git a/test/background/operators/impls/ShowOpenCommandOperator.test.ts b/test/background/operators/impls/ShowOpenCommandOperator.test.ts index 2c2105a..b4f631a 100644 --- a/test/background/operators/impls/ShowOpenCommandOperator.test.ts +++ b/test/background/operators/impls/ShowOpenCommandOperator.test.ts @@ -1,4 +1,3 @@ -import sinon from "sinon"; import ShowOpenCommandOperator from "../../../../src/background/operators/impls/ShowOpenCommandOperator"; import MockTabPresenter from "../../mock/MockTabPresenter"; import MockConsoleClient from "../../mock/MockConsoleClient"; @@ -11,10 +10,9 @@ describe("ShowOpenCommandOperator", () => { await tabPresenter.create("https://example.com/2", { active: true }); await tabPresenter.create("https://example.com/3", { active: false }); const consoleClient = new MockConsoleClient(); - const mock = sinon - .mock(consoleClient) - .expects("showCommand") - .withArgs(1, "open "); + const showCommandSpy = jest + .spyOn(consoleClient, "showCommand") + .mockReturnValue(Promise.resolve()); const sut = new ShowOpenCommandOperator( tabPresenter, @@ -23,7 +21,7 @@ describe("ShowOpenCommandOperator", () => { ); await sut.run(); - mock.verify(); + expect(showCommandSpy).toBeCalledWith(1, "open "); }); it("show command with open command and an URL of the current tab", async () => { @@ -32,10 +30,9 @@ describe("ShowOpenCommandOperator", () => { await tabPresenter.create("https://example.com/2", { active: true }); await tabPresenter.create("https://example.com/3", { active: false }); const consoleClient = new MockConsoleClient(); - const mock = sinon - .mock(consoleClient) - .expects("showCommand") - .withArgs(1, "open https://example.com/2"); + const showCommandSpy = jest + .spyOn(consoleClient, "showCommand") + .mockReturnValue(Promise.resolve()); const sut = new ShowOpenCommandOperator( tabPresenter, @@ -44,7 +41,7 @@ describe("ShowOpenCommandOperator", () => { ); await sut.run(); - mock.verify(); + expect(showCommandSpy).toBeCalledWith(1, "open https://example.com/2"); }); }); }); diff --git a/test/background/operators/impls/ShowTabOpenCommandOperator.test.ts b/test/background/operators/impls/ShowTabOpenCommandOperator.test.ts index e291d05..1896a08 100644 --- a/test/background/operators/impls/ShowTabOpenCommandOperator.test.ts +++ b/test/background/operators/impls/ShowTabOpenCommandOperator.test.ts @@ -1,4 +1,3 @@ -import sinon from "sinon"; import ShowTabOpenCommandOperator from "../../../../src/background/operators/impls/ShowTabOpenCommandOperator"; import MockTabPresenter from "../../mock/MockTabPresenter"; import MockConsoleClient from "../../mock/MockConsoleClient"; @@ -11,10 +10,9 @@ describe("ShowTabOpenCommandOperator", () => { await tabPresenter.create("https://example.com/2", { active: true }); await tabPresenter.create("https://example.com/3", { active: false }); const consoleClient = new MockConsoleClient(); - const mock = sinon - .mock(consoleClient) - .expects("showCommand") - .withArgs(1, "tabopen "); + const showCommandSpy = jest + .spyOn(consoleClient, "showCommand") + .mockReturnValue(Promise.resolve()); const sut = new ShowTabOpenCommandOperator( tabPresenter, @@ -23,7 +21,7 @@ describe("ShowTabOpenCommandOperator", () => { ); await sut.run(); - mock.verify(); + expect(showCommandSpy).toBeCalledWith(1, "tabopen "); }); it("show command with tabopen command and an URL of the current tab", async () => { @@ -32,10 +30,9 @@ describe("ShowTabOpenCommandOperator", () => { await tabPresenter.create("https://example.com/2", { active: true }); await tabPresenter.create("https://example.com/3", { active: false }); const consoleClient = new MockConsoleClient(); - const mock = sinon - .mock(consoleClient) - .expects("showCommand") - .withArgs(1, "tabopen https://example.com/2"); + const showCommandSpy = jest + .spyOn(consoleClient, "showCommand") + .mockReturnValue(Promise.resolve()); const sut = new ShowTabOpenCommandOperator( tabPresenter, @@ -44,7 +41,7 @@ describe("ShowTabOpenCommandOperator", () => { ); await sut.run(); - mock.verify(); + expect(showCommandSpy).toBeCalledWith(1, "tabopen https://example.com/2"); }); }); }); diff --git a/test/background/operators/impls/ShowWinOpenCommandOperator.test.ts b/test/background/operators/impls/ShowWinOpenCommandOperator.test.ts index c81a2d4..ce2a5b8 100644 --- a/test/background/operators/impls/ShowWinOpenCommandOperator.test.ts +++ b/test/background/operators/impls/ShowWinOpenCommandOperator.test.ts @@ -1,4 +1,3 @@ -import sinon from "sinon"; import ShowWinOpenCommandOperator from "../../../../src/background/operators/impls/ShowWinOpenCommandOperator"; import MockTabPresenter from "../../mock/MockTabPresenter"; import MockConsoleClient from "../../mock/MockConsoleClient"; @@ -11,10 +10,9 @@ describe("ShowWinOpenCommandOperator", () => { await tabPresenter.create("https://example.com/2", { active: true }); await tabPresenter.create("https://example.com/3", { active: false }); const consoleClient = new MockConsoleClient(); - const mock = sinon - .mock(consoleClient) - .expects("showCommand") - .withArgs(1, "winopen "); + const showCommandSpy = jest + .spyOn(consoleClient, "showCommand") + .mockReturnValue(Promise.resolve()); const sut = new ShowWinOpenCommandOperator( tabPresenter, @@ -23,7 +21,7 @@ describe("ShowWinOpenCommandOperator", () => { ); await sut.run(); - mock.verify(); + expect(showCommandSpy).toBeCalledWith(1, "winopen "); }); it("show command with winopen command and an URL of the current tab", async () => { @@ -32,10 +30,9 @@ describe("ShowWinOpenCommandOperator", () => { await tabPresenter.create("https://example.com/2", { active: true }); await tabPresenter.create("https://example.com/3", { active: false }); const consoleClient = new MockConsoleClient(); - const mock = sinon - .mock(consoleClient) - .expects("showCommand") - .withArgs(1, "winopen https://example.com/2"); + const showCommandSpy = jest + .spyOn(consoleClient, "showCommand") + .mockReturnValue(Promise.resolve()); const sut = new ShowWinOpenCommandOperator( tabPresenter, @@ -44,7 +41,7 @@ describe("ShowWinOpenCommandOperator", () => { ); await sut.run(); - mock.verify(); + expect(showCommandSpy).toBeCalledWith(1, "winopen https://example.com/2"); }); }); }); diff --git a/test/background/operators/impls/StartFindOperator.test.ts b/test/background/operators/impls/StartFindOperator.test.ts index 7764520..23ebda8 100644 --- a/test/background/operators/impls/StartFindOperator.test.ts +++ b/test/background/operators/impls/StartFindOperator.test.ts @@ -1,4 +1,3 @@ -import sinon from "sinon"; import StartFindOperator from "../../../../src/background/operators/impls/StartFindOperator"; import MockTabPresenter from "../../mock/MockTabPresenter"; import MockConsoleClient from "../../mock/MockConsoleClient"; @@ -11,12 +10,14 @@ describe("StartFindOperator", () => { await tabPresenter.create("https://example.com/2", { active: true }); await tabPresenter.create("https://example.com/3", { active: false }); const consoleClient = new MockConsoleClient(); - const mock = sinon.mock(consoleClient).expects("showFind").withArgs(1); + const showFindSpy = jest + .spyOn(consoleClient, "showFind") + .mockReturnValue(Promise.resolve()); const sut = new StartFindOperator(tabPresenter, consoleClient); await sut.run(); - mock.verify(); + expect(showFindSpy).toBeCalledWith(1); }); }); }); diff --git a/test/background/operators/impls/ZoomInOperator.test.ts b/test/background/operators/impls/ZoomInOperator.test.ts index 097e760..1d42421 100644 --- a/test/background/operators/impls/ZoomInOperator.test.ts +++ b/test/background/operators/impls/ZoomInOperator.test.ts @@ -1,4 +1,3 @@ -import sinon from "sinon"; import ZoomInOperator from "../../../../src/background/operators/impls/ZoomInOperator"; import MockZoomPresenter from "../../mock/MockZoomPresenter"; @@ -6,12 +5,14 @@ describe("ZoomInOperator", () => { describe("#run", () => { it("zoom-out the current tab", async () => { const zoomPresenter = new MockZoomPresenter(); - const mock = sinon.mock(zoomPresenter).expects("zoomIn").once(); + const zoomInSpy = jest + .spyOn(zoomPresenter, "zoomIn") + .mockReturnValue(Promise.resolve()); const sut = new ZoomInOperator(zoomPresenter); await sut.run(); - mock.verify(); + expect(zoomInSpy).toBeCalled(); }); }); }); diff --git a/test/background/operators/impls/ZoomOutOperator.test.ts b/test/background/operators/impls/ZoomOutOperator.test.ts index e0bbcd9..3165707 100644 --- a/test/background/operators/impls/ZoomOutOperator.test.ts +++ b/test/background/operators/impls/ZoomOutOperator.test.ts @@ -1,4 +1,3 @@ -import sinon from "sinon"; import ZoomOutOperator from "../../../../src/background/operators/impls/ZoomOutOperator"; import MockZoomPresenter from "../../mock/MockZoomPresenter"; @@ -6,12 +5,14 @@ describe("ZoomOutOperator", () => { describe("#run", () => { it("zoom-in the current tab", async () => { const zoomPresenter = new MockZoomPresenter(); - const mock = sinon.mock(zoomPresenter).expects("zoomOut").once(); + const zoomOutSpy = jest + .spyOn(zoomPresenter, "zoomOut") + .mockReturnValue(Promise.resolve()); const sut = new ZoomOutOperator(zoomPresenter); await sut.run(); - mock.verify(); + expect(zoomOutSpy).toBeCalled(); }); }); }); diff --git a/test/background/usecases/SettingUseCase.test.ts b/test/background/usecases/SettingUseCase.test.ts index af2fb1e..395e8a4 100644 --- a/test/background/usecases/SettingUseCase.test.ts +++ b/test/background/usecases/SettingUseCase.test.ts @@ -8,7 +8,6 @@ import Settings, { } from "../../../src/shared/settings/Settings"; import Notifier from "../../../src/background/presenters/Notifier"; import Properties from "../../../src/shared/settings/Properties"; -import sinon from "sinon"; class MockSettingRepository implements SettingRepository { load(): Promise { @@ -76,9 +75,7 @@ describe("SettingUseCase", () => { hintchars: "abcd1234", }), }); - sinon - .stub(cachedSettingRepository, "get") - .returns(Promise.resolve(settings)); + jest.spyOn(cachedSettingRepository, "get").mockResolvedValue(settings); const got = await sut.getCached(); expect(got.properties.hintchars).toEqual("abcd1234"); @@ -101,12 +98,10 @@ describe("SettingUseCase", () => { json: JSONTextSettings.fromSettings(settings).toJSONText(), }); - sinon - .stub(syncSettingRepository, "load") - .returns(Promise.resolve(null)); - sinon - .stub(localSettingRepository, "load") - .returns(Promise.resolve(settingData)); + jest.spyOn(syncSettingRepository, "load").mockResolvedValue(null); + jest + .spyOn(localSettingRepository, "load") + .mockResolvedValue(settingData); await sut.reload(); @@ -130,12 +125,10 @@ describe("SettingUseCase", () => { json: JSONTextSettings.fromSettings(settings).toJSONText(), }); - sinon - .stub(syncSettingRepository, "load") - .returns(Promise.resolve(settingData)); - sinon - .stub(localSettingRepository, "load") - .returns(Promise.resolve(null)); + jest + .spyOn(syncSettingRepository, "load") + .mockResolvedValue(settingData); + jest.spyOn(localSettingRepository, "load").mockResolvedValue(null); await sut.reload(); @@ -146,12 +139,8 @@ describe("SettingUseCase", () => { describe("neither local nor sync not set", () => { it("loads settings from sync storage", async () => { - sinon - .stub(syncSettingRepository, "load") - .returns(Promise.resolve(null)); - sinon - .stub(localSettingRepository, "load") - .returns(Promise.resolve(null)); + jest.spyOn(syncSettingRepository, "load").mockResolvedValue(null); + jest.spyOn(localSettingRepository, "load").mockResolvedValue(null); await sut.reload(); diff --git a/test/background/usecases/StartFindUseCase.test.ts b/test/background/usecases/StartFindUseCase.test.ts index 24e1fdc..0bfe18a 100644 --- a/test/background/usecases/StartFindUseCase.test.ts +++ b/test/background/usecases/StartFindUseCase.test.ts @@ -1,4 +1,3 @@ -import * as sinon from "sinon"; import MockFindClient from "../mock/MockFindClient"; import MockFindRepository from "../mock/MockFindRepository"; import MockConsoleClient from "../mock/MockConsoleClient"; @@ -21,160 +20,144 @@ describe("StartFindUseCase", () => { frameRepository ); - beforeEach(async () => { - sinon.restore(); + const getFrameIdsSpy = jest + .spyOn(frameRepository, "getFrameIds") + .mockResolvedValue(frameIds); + const clearSelectionSpy = jest + .spyOn(findClient, "clearSelection") + .mockReturnValue(Promise.resolve()); + const findNextSpy = jest.spyOn(findClient, "findNext"); + const setLocalStateSpy = jest + .spyOn(findRepository, "setLocalState") + .mockReturnValue(Promise.resolve()); - sinon - .stub(frameRepository, "getFrameIds") - .returns(Promise.resolve(frameIds)); + beforeEach(async () => { + getFrameIdsSpy.mockClear(); + clearSelectionSpy.mockClear(); + findNextSpy.mockClear(); + setLocalStateSpy.mockClear(); }); describe("startFind", () => { it("starts a find with a keyword", async () => { - 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(false)); - mockFindClient - .expects("findNext") - .withArgs(currentTabId, 100, keyword) - .returns(Promise.resolve(true)); - const mockFindRepository = sinon.mock(findRepository); - mockFindRepository - .expects("setLocalState") - .withArgs(currentTabId, { keyword, frameId: 100 }); - const mockConsoleClient = sinon.mock(consoleClient); - mockConsoleClient - .expects("showInfo") - .withArgs(currentTabId, "Pattern found: " + keyword); + findNextSpy.mockResolvedValueOnce(false).mockResolvedValueOnce(true); + const showInfoSpy = jest + .spyOn(consoleClient, "showInfo") + .mockReturnValue(Promise.resolve()); await sut.startFind(currentTabId, keyword); - mockFindClient.verify(); - mockFindRepository.verify(); - mockConsoleClient.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).toBeCalledTimes(2); + expect(findNextSpy.mock.calls[0][1]).toEqual(0); + expect(findNextSpy.mock.calls[1][1]).toEqual(100); + expect(setLocalStateSpy).toBeCalledWith(currentTabId, { + keyword, + frameId: 100, + }); + expect(showInfoSpy).toBeCalledWith( + currentTabId, + "Pattern found: " + keyword + ); }); it("starts a find with last local state", async () => { - 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(false)); - mockFindClient - .expects("findNext") - .withArgs(currentTabId, 100, keyword) - .returns(Promise.resolve(true)); - const mockFindRepository = sinon.mock(findRepository); - mockFindRepository - .expects("getLocalState") - .withArgs(currentTabId) - .returns(Promise.resolve({ keyword, frameId: 0 })); - mockFindRepository - .expects("setLocalState") - .withArgs(currentTabId, { keyword, frameId: 100 }); - const mockConsoleClient = sinon.mock(consoleClient); - mockConsoleClient - .expects("showInfo") - .withArgs(currentTabId, "Pattern found: " + keyword); + findNextSpy.mockResolvedValueOnce(false).mockResolvedValueOnce(true); + const getLocalStateSpy = jest + .spyOn(findRepository, "getLocalState") + .mockResolvedValue({ keyword, frameId: 0 }); + const showInfoSpy = jest + .spyOn(consoleClient, "showInfo") + .mockReturnValue(Promise.resolve()); await sut.startFind(currentTabId, undefined); - mockFindClient.verify(); - mockFindRepository.verify(); - mockConsoleClient.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).toBeCalledTimes(2); + expect(findNextSpy.mock.calls[0][1]).toEqual(0); + expect(findNextSpy.mock.calls[1][1]).toEqual(100); + expect(getLocalStateSpy).toBeCalledWith(currentTabId); + expect(setLocalStateSpy).toBeCalledWith(currentTabId, { + keyword, + frameId: 100, + }); + expect(showInfoSpy).toBeCalledWith( + currentTabId, + "Pattern found: " + keyword + ); }); it("starts a find with last global state", async () => { - 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(false)); - mockFindClient - .expects("findNext") - .withArgs(currentTabId, 100, keyword) - .returns(Promise.resolve(true)); - const mockFindRepository = sinon.mock(findRepository); - mockFindRepository - .expects("getLocalState") - .withArgs(currentTabId) - .returns(Promise.resolve(undefined)); - mockFindRepository - .expects("getGlobalKeyword") - .returns(Promise.resolve(keyword)); - mockFindRepository - .expects("setLocalState") - .withArgs(currentTabId, { keyword, frameId: 100 }); - const mockConsoleClient = sinon.mock(consoleClient); - mockConsoleClient - .expects("showInfo") - .withArgs(currentTabId, "Pattern found: " + keyword); + findNextSpy.mockResolvedValueOnce(false).mockResolvedValueOnce(true); + const getLocalStateSpy = jest + .spyOn(findRepository, "getLocalState") + .mockResolvedValue(undefined); + jest.spyOn(findRepository, "getGlobalKeyword").mockResolvedValue(keyword); + const showInfoSpy = jest + .spyOn(consoleClient, "showInfo") + .mockReturnValue(Promise.resolve()); await sut.startFind(currentTabId, undefined); - mockFindClient.verify(); - mockFindRepository.verify(); - mockConsoleClient.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).toBeCalledTimes(2); + expect(findNextSpy.mock.calls[0][1]).toEqual(0); + expect(findNextSpy.mock.calls[1][1]).toEqual(100); + expect(getLocalStateSpy).toBeCalledWith(currentTabId); + expect(setLocalStateSpy).toBeCalledWith(currentTabId, { + keyword, + frameId: 100, + }); + expect(showInfoSpy).toBeCalledWith( + currentTabId, + "Pattern found: " + keyword + ); }); it("shows an error when pattern not found", async () => { - 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(false)); - mockFindClient - .expects("findNext") - .withArgs(currentTabId, 100, keyword) - .returns(Promise.resolve(false)); - mockFindClient - .expects("findNext") - .withArgs(currentTabId, 101, keyword) - .returns(Promise.resolve(false)); - const mockFindRepository = sinon.mock(findRepository); - mockFindRepository.expects("setLocalState").never(); - const mockConsoleClient = sinon.mock(consoleClient); - mockConsoleClient - .expects("showError") - .withArgs(currentTabId, "Pattern not found: " + keyword); + findNextSpy.mockResolvedValue(false); + const showErrorSpy = jest + .spyOn(consoleClient, "showError") + .mockReturnValue(Promise.resolve()); await sut.startFind(currentTabId, keyword); - mockFindClient.verify(); - mockFindRepository.verify(); - mockConsoleClient.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(setLocalStateSpy).not.toBeCalled(); + expect(showErrorSpy).toBeCalledWith( + currentTabId, + "Pattern not found: " + keyword + ); }); it("shows an error when no last keywords", async () => { - sinon - .stub(findRepository, "getLocalState") - .returns(Promise.resolve(undefined)); - sinon - .stub(findRepository, "getGlobalKeyword") - .returns(Promise.resolve(undefined)); - - const mockConsoleClient = sinon.mock(consoleClient); - mockConsoleClient - .expects("showError") - .withArgs(currentTabId, "No previous search keywords"); + jest.spyOn(findRepository, "getLocalState").mockResolvedValue(undefined); + jest + .spyOn(findRepository, "getGlobalKeyword") + .mockResolvedValue(undefined); + + const showErrorSpy = jest + .spyOn(consoleClient, "showError") + .mockReturnValue(Promise.resolve()); await sut.startFind(currentTabId, undefined); - mockConsoleClient.verify(); + expect(showErrorSpy).toBeCalledWith( + currentTabId, + "No previous search keywords" + ); }); }); }); diff --git a/test/content/operators/impls/FocusOperator.test.ts b/test/content/operators/impls/FocusOperator.test.ts index a0eb53b..2d72620 100644 --- a/test/content/operators/impls/FocusOperator.test.ts +++ b/test/content/operators/impls/FocusOperator.test.ts @@ -1,4 +1,3 @@ -import sinon from "sinon"; import FocusOperator from "../../../../src/content/operators/impls/FocusOperator"; import MockFocusPresenter from "../../mock/MockFocusPresenter"; @@ -6,12 +5,14 @@ describe("FocusOperator", () => { describe("#run", () => { it("focus a first input", async () => { const presenter = new MockFocusPresenter(); - const mock = sinon.mock(presenter).expects("focusFirstElement"); + const focusFirstElementSpy = jest + .spyOn(presenter, "focusFirstElement") + .mockReturnValue(true); const sut = new FocusOperator(presenter); await sut.run(); - mock.verify(); + expect(focusFirstElementSpy).toBeCalled(); }); }); }); diff --git a/test/content/operators/impls/PasteOperator.test.ts b/test/content/operators/impls/PasteOperator.test.ts index 8a3a374..fa42576 100644 --- a/test/content/operators/impls/PasteOperator.test.ts +++ b/test/content/operators/impls/PasteOperator.test.ts @@ -1,4 +1,3 @@ -import sinon from "sinon"; import PasteOperator from "../../../../src/content/operators/impls/PasteOperator"; import MockClipboardRepository from "../../mock/MockClipboardRepository"; import MockSettingRepository from "../../mock/MockSettingRepository"; @@ -10,10 +9,9 @@ describe("PasteOperator", () => { const clipboardRepository = new MockClipboardRepository("apple"); const settingRepository = new MockSettingRepository(); const operationClient = new MockOperationClient(); - const mockOperationClient = sinon - .mock(operationClient) - .expects("internalOpenUrl") - .withArgs("https://google.com/search?q=apple"); + const internalOpenUrlSpy = jest + .spyOn(operationClient, "internalOpenUrl") + .mockReturnValue(Promise.resolve()); const sut = new PasteOperator( clipboardRepository, settingRepository, @@ -23,7 +21,10 @@ describe("PasteOperator", () => { await sut.run(); - mockOperationClient.verify(); + expect(internalOpenUrlSpy).toBeCalledWith( + "https://google.com/search?q=apple", + false + ); }); it("open a url", async () => { @@ -32,10 +33,9 @@ describe("PasteOperator", () => { ); const settingRepository = new MockSettingRepository(); const operationClient = new MockOperationClient(); - const mockOperationClient = sinon - .mock(operationClient) - .expects("internalOpenUrl") - .withArgs("https://example.com/"); + const internalOpenUrlSpy = jest + .spyOn(operationClient, "internalOpenUrl") + .mockReturnValue(Promise.resolve()); const sut = new PasteOperator( clipboardRepository, settingRepository, @@ -45,7 +45,7 @@ describe("PasteOperator", () => { await sut.run(); - mockOperationClient.verify(); + expect(internalOpenUrlSpy).toBeCalledWith("https://example.com/", false); }); }); }); diff --git a/test/content/operators/impls/StartFollowOperator.test.ts b/test/content/operators/impls/StartFollowOperator.test.ts index 8f9bd2d..4216cd0 100644 --- a/test/content/operators/impls/StartFollowOperator.test.ts +++ b/test/content/operators/impls/StartFollowOperator.test.ts @@ -1,4 +1,3 @@ -import sinon from "sinon"; import StartFollowOperator from "../../../../src/content/operators/impls/StartFollowOperator"; import MockFollowMasterClient from "../../mock/MockFollowMasterClient"; @@ -6,15 +5,14 @@ describe("StartFollowOperator", () => { describe("#run", () => { it("starts following links", async () => { const client = new MockFollowMasterClient(); - const mock = sinon - .mock(client) - .expects("startFollow") - .withArgs(true, false); + const startFollowSpy = jest + .spyOn(client, "startFollow") + .mockReturnValue(); const sut = new StartFollowOperator(client, true, false); await sut.run(); - mock.verify(); + expect(startFollowSpy).toBeCalledWith(true, false); }); }); }); -- cgit v1.2.3