diff options
Diffstat (limited to 'test/background/operators/impls')
3 files changed, 327 insertions, 149 deletions
diff --git a/test/background/operators/impls/FindNextOperator.test.ts b/test/background/operators/impls/FindNextOperator.test.ts index 20208ae..0bee3f5 100644 --- a/test/background/operators/impls/FindNextOperator.test.ts +++ b/test/background/operators/impls/FindNextOperator.test.ts @@ -1,90 +1,179 @@ -import sinon from "sinon"; +import * as sinon from "sinon"; import MockTabPresenter from "../../mock/MockTabPresenter"; import FindNextOperator from "../../../../src/background/operators/impls/FindNextOperator"; -import { FindState } from "../../../../src/background/repositories/FindRepository"; import MockFindRepository from "../../mock/MockFindRepository"; import MockFindClient from "../../mock/MockFindClient"; +import MockConsoleClient from "../../mock/MockConsoleClient"; +import MockFramePresenter from "../../mock/MockFramePresenter"; 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 framePresenter = new MockFramePresenter(); + const sut = new FindNextOperator( + tabPresenter, + findRepository, + findClient, + consoleClient, + framePresenter + ); + + let currentTabId: number; + + beforeEach(async () => { + sinon.restore(); + + const currentTab = await tabPresenter.create("https://example.com/", { + active: true, + }); + currentTabId = currentTab.id!; + }); + describe("#run", () => { - it("throws an error on no previous keywords", async () => { - const tabPresenter = new MockTabPresenter(); - const findRepository = new MockFindRepository(); - const findClient = new MockFindClient(); - await tabPresenter.create("https://example.com/"); - - const sut = new FindNextOperator( - tabPresenter, - findRepository, - findClient + 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"); + + await sut.run(); + + mock.verify(); + }); + + it("continues a search on the same frame", async () => { + sinon.stub(findRepository, "getLocalState").returns( + Promise.resolve({ + keyword, + frameIds, + framePos: 1, + }) ); - try { - await sut.run(); - } catch (e) { - return; - } - throw new Error("unexpected reach"); + + 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, frameIds, framePos: 1 }); + + await sut.run(); + + mockFindRepository.verify(); + mockFindClient.verify(); }); - it("select a next next", async () => { - const tabPresenter = new MockTabPresenter(); - const findRepository = new MockFindRepository(); - const findClient = new MockFindClient(); - const currentTab = await tabPresenter.create("https://example.com/"); - - const state: FindState = { - keyword: "Hello, world", - rangeData: [ - { - framePos: 0, - startOffset: 0, - endOffset: 10, - startTextNodePos: 0, - endTextNodePos: 0, - text: "Hello, world", - }, - { - framePos: 1, - startOffset: 0, - endOffset: 10, - startTextNodePos: 1, - endTextNodePos: 1, - text: "Hello, world", - }, - { - framePos: 2, - startOffset: 2, - endOffset: 10, - startTextNodePos: 1, - endTextNodePos: 1, - text: "Hello, world", - }, - ], - highlightPosition: 0, - }; - - await findRepository.setLocalState(currentTab.id!, state); - const mock = sinon.mock(findClient); - mock - .expects("selectKeyword") - .withArgs(currentTab?.id, state.rangeData[1]); - mock - .expects("selectKeyword") - .withArgs(currentTab?.id, state.rangeData[2]); - mock - .expects("selectKeyword") - .withArgs(currentTab?.id, state.rangeData[0]); - const sut = new FindNextOperator( - tabPresenter, - findRepository, - findClient + it("continues a search on next frame", async () => { + sinon.stub(findRepository, "getLocalState").returns( + Promise.resolve({ + keyword, + frameIds, + framePos: 1, + }) ); + 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, frameIds, framePos: 2 }); + await sut.run(); + + mockFindRepository.verify(); + mockFindClient.verify(); + }); + + it("exercise a wrap-search", async () => { + sinon.stub(findRepository, "getLocalState").returns( + Promise.resolve({ + keyword, + frameIds, + framePos: 2, + }) + ); + + 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, frameIds, framePos: 0 }); + await sut.run(); + + mockFindRepository.verify(); + mockFindClient.verify(); + }); + + 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(framePresenter, "getAllFrameIds") + .returns(Promise.resolve(frameIds)); + 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, frameIds, framePos: 0 }); + await sut.run(); - mock.verify(); + mockFindRepository.verify(); + mockFindClient.verify(); }); }); }); diff --git a/test/background/operators/impls/FindPrevOperator.test.ts b/test/background/operators/impls/FindPrevOperator.test.ts index 409c26d..ebac0dc 100644 --- a/test/background/operators/impls/FindPrevOperator.test.ts +++ b/test/background/operators/impls/FindPrevOperator.test.ts @@ -1,90 +1,179 @@ -import sinon from "sinon"; +import * as sinon from "sinon"; import MockTabPresenter from "../../mock/MockTabPresenter"; -import FindNextOperator from "../../../../src/background/operators/impls/FindNextOperator"; -import { FindState } from "../../../../src/background/repositories/FindRepository"; +import FindPrevOperator from "../../../../src/background/operators/impls/FindPrevOperator"; import MockFindRepository from "../../mock/MockFindRepository"; import MockFindClient from "../../mock/MockFindClient"; +import MockConsoleClient from "../../mock/MockConsoleClient"; +import MockFramePresenter from "../../mock/MockFramePresenter"; describe("FindPrevOperator", () => { + 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 framePresenter = new MockFramePresenter(); + const sut = new FindPrevOperator( + tabPresenter, + findRepository, + findClient, + consoleClient, + framePresenter + ); + + let currentTabId: number; + + beforeEach(async () => { + sinon.restore(); + + const currentTab = await tabPresenter.create("https://example.com/", { + active: true, + }); + currentTabId = currentTab.id!; + }); + describe("#run", () => { - it("throws an error on no previous keywords", async () => { - const tabPresenter = new MockTabPresenter(); - const findRepository = new MockFindRepository(); - const findClient = new MockFindClient(); - await tabPresenter.create("https://example.com/"); - - const sut = new FindNextOperator( - tabPresenter, - findRepository, - findClient + 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"); + + await sut.run(); + + mock.verify(); + }); + + it("continues a search on the same frame", async () => { + sinon.stub(findRepository, "getLocalState").returns( + Promise.resolve({ + keyword, + frameIds, + framePos: 1, + }) ); - try { - await sut.run(); - } catch (e) { - return; - } - throw new Error("unexpected reach"); + + 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, frameIds, framePos: 1 }); + + await sut.run(); + + mockFindRepository.verify(); + mockFindClient.verify(); }); - it("select a next next", async () => { - const tabPresenter = new MockTabPresenter(); - const findRepository = new MockFindRepository(); - const findClient = new MockFindClient(); - const currentTab = await tabPresenter.create("https://example.com/"); - - const state: FindState = { - keyword: "Hello, world", - rangeData: [ - { - framePos: 0, - startOffset: 0, - endOffset: 10, - startTextNodePos: 0, - endTextNodePos: 0, - text: "Hello, world", - }, - { - framePos: 1, - startOffset: 0, - endOffset: 10, - startTextNodePos: 1, - endTextNodePos: 1, - text: "Hello, world", - }, - { - framePos: 2, - startOffset: 2, - endOffset: 10, - startTextNodePos: 1, - endTextNodePos: 1, - text: "Hello, world", - }, - ], - highlightPosition: 1, - }; - - await findRepository.setLocalState(currentTab.id!, state); - const mock = sinon.mock(findClient); - mock - .expects("selectKeyword") - .withArgs(currentTab?.id, state.rangeData[0]); - mock - .expects("selectKeyword") - .withArgs(currentTab?.id, state.rangeData[2]); - mock - .expects("selectKeyword") - .withArgs(currentTab?.id, state.rangeData[1]); - const sut = new FindNextOperator( - tabPresenter, - findRepository, - findClient + it("continues a search on next frame", async () => { + sinon.stub(findRepository, "getLocalState").returns( + Promise.resolve({ + keyword, + frameIds, + framePos: 1, + }) ); + 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, frameIds, framePos: 0 }); + await sut.run(); + + mockFindRepository.verify(); + mockFindClient.verify(); + }); + + it("exercise a wrap-search", async () => { + sinon.stub(findRepository, "getLocalState").returns( + Promise.resolve({ + keyword, + frameIds, + framePos: 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, frameIds, framePos: 2 }); + await sut.run(); + + mockFindRepository.verify(); + mockFindClient.verify(); + }); + + 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(framePresenter, "getAllFrameIds") + .returns(Promise.resolve(frameIds)); + 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, frameIds, framePos: 2 }); + await sut.run(); - mock.verify(); + mockFindRepository.verify(); + mockFindClient.verify(); }); }); }); diff --git a/test/background/operators/impls/TabOperatorFactoryChain.test.ts b/test/background/operators/impls/TabOperatorFactoryChain.test.ts index 7ab5de0..a777973 100644 --- a/test/background/operators/impls/TabOperatorFactoryChain.test.ts +++ b/test/background/operators/impls/TabOperatorFactoryChain.test.ts @@ -44,12 +44,12 @@ describe("TabOperatorFactoryChain", () => { expect(sut.create({ type: operations.TAB_FIRST })).to.be.instanceOf( SelectFirstTabOperator ); - expect( - sut.create({ type: operations.TAB_LAST, newTab: false }) - ).to.be.instanceOf(SelectLastTabOperator); - expect( - sut.create({ type: operations.TAB_PREV_SEL, newTab: false }) - ).to.be.instanceOf(SelectPreviousSelectedTabOperator); + expect(sut.create({ type: operations.TAB_LAST })).to.be.instanceOf( + SelectLastTabOperator + ); + expect(sut.create({ type: operations.TAB_PREV_SEL })).to.be.instanceOf( + SelectPreviousSelectedTabOperator + ); expect( sut.create({ type: operations.TAB_RELOAD, cache: false }) ).to.be.instanceOf(ReloadTabOperator); |