diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2021-06-14 23:14:51 +0900 |
---|---|---|
committer | Shin'ya Ueoka <ueokande@i-beam.org> | 2021-07-05 21:32:43 +0900 |
commit | 65cf6f0842d8d5933dc13b3767b1baf398d68cd5 (patch) | |
tree | df9a8b139fd98adb79f075ba655d1303bdf3fd1d /test/background/operators | |
parent | caced372415a944c4297157397d0027ba629fff0 (diff) |
Implement FindNextOperator
Diffstat (limited to 'test/background/operators')
3 files changed, 203 insertions, 0 deletions
diff --git a/test/background/operators/impls/FindNextOperator.test.ts b/test/background/operators/impls/FindNextOperator.test.ts new file mode 100644 index 0000000..20208ae --- /dev/null +++ b/test/background/operators/impls/FindNextOperator.test.ts @@ -0,0 +1,90 @@ +import 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"; + +describe("FindNextOperator", () => { + 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 + ); + try { + await sut.run(); + } catch (e) { + return; + } + throw new Error("unexpected reach"); + }); + + 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 + ); + + await sut.run(); + await sut.run(); + await sut.run(); + + mock.verify(); + }); + }); +}); diff --git a/test/background/operators/impls/FindOperatorFactoryChain.ts b/test/background/operators/impls/FindOperatorFactoryChain.ts new file mode 100644 index 0000000..0fd234f --- /dev/null +++ b/test/background/operators/impls/FindOperatorFactoryChain.ts @@ -0,0 +1,23 @@ +import "reflect-metadata"; +import { expect } from "chai"; +import TabOperatorFactoryChain from "../../../../src/background/operators/impls/TabOperatorFactoryChain"; +import MockTabPresenter from "../../mock/MockTabPresenter"; +import * as operations from "../../../../src/shared/operations"; +import FindNextOperator from "../../../../src/background/operators/impls/FindNextOperator"; +import FindPrevOperator from "../../../../src/background/operators/impls/FindPrevOperator"; + +describe("FindOperatorFactoryChain", () => { + describe("#create", () => { + it("returns a operator for the operation", async () => { + const tabPresenter = new MockTabPresenter(); + const sut = new TabOperatorFactoryChain(tabPresenter); + + expect(sut.create({ type: operations.FIND_NEXT })).to.be.instanceOf( + FindNextOperator + ); + expect(sut.create({ type: operations.FIND_PREV })).to.be.instanceOf( + FindPrevOperator + ); + }); + }); +}); diff --git a/test/background/operators/impls/FindPrevOperator.test.ts b/test/background/operators/impls/FindPrevOperator.test.ts new file mode 100644 index 0000000..409c26d --- /dev/null +++ b/test/background/operators/impls/FindPrevOperator.test.ts @@ -0,0 +1,90 @@ +import 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"; + +describe("FindPrevOperator", () => { + 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 + ); + try { + await sut.run(); + } catch (e) { + return; + } + throw new Error("unexpected reach"); + }); + + 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 + ); + + await sut.run(); + await sut.run(); + await sut.run(); + + mock.verify(); + }); + }); +}); |