diff options
Diffstat (limited to 'test/background')
6 files changed, 86 insertions, 42 deletions
diff --git a/test/background/mock/MockReadyFrameRepository.ts b/test/background/mock/MockReadyFrameRepository.ts new file mode 100644 index 0000000..4a5ec52 --- /dev/null +++ b/test/background/mock/MockReadyFrameRepository.ts @@ -0,0 +1,15 @@ +import ReadyFrameRepository from "../../../src/background/repositories/ReadyFrameRepository"; + +export default class MockReadyFrameRepository implements ReadyFrameRepository { + addFrameId(_tabId: number, _fraemId: number): Promise<void> { + throw new Error("not implemented"); + } + + removeFrameId(_tabId: number, _frameId: number): Promise<void> { + throw new Error("not implemented"); + } + + getFrameIds(_tabId: number): Promise<number[] | undefined> { + throw new Error("not implemented"); + } +} diff --git a/test/background/operators/impls/FindNextOperator.test.ts b/test/background/operators/impls/FindNextOperator.test.ts index 0bee3f5..7509ef4 100644 --- a/test/background/operators/impls/FindNextOperator.test.ts +++ b/test/background/operators/impls/FindNextOperator.test.ts @@ -4,7 +4,7 @@ import FindNextOperator from "../../../../src/background/operators/impls/FindNex import MockFindRepository from "../../mock/MockFindRepository"; import MockFindClient from "../../mock/MockFindClient"; import MockConsoleClient from "../../mock/MockConsoleClient"; -import MockFramePresenter from "../../mock/MockFramePresenter"; +import MockReadyFrameRepository from "../../mock/MockReadyFrameRepository"; describe("FindNextOperator", () => { const keyword = "hello"; @@ -14,13 +14,13 @@ describe("FindNextOperator", () => { const findRepository = new MockFindRepository(); const findClient = new MockFindClient(); const consoleClient = new MockConsoleClient(); - const framePresenter = new MockFramePresenter(); + const frameRepository = new MockReadyFrameRepository(); const sut = new FindNextOperator( tabPresenter, findRepository, findClient, consoleClient, - framePresenter + frameRepository ); let currentTabId: number; @@ -32,6 +32,10 @@ describe("FindNextOperator", () => { active: true, }); currentTabId = currentTab.id!; + + sinon + .stub(frameRepository, "getFrameIds") + .returns(Promise.resolve(frameIds)); }); describe("#run", () => { @@ -54,8 +58,7 @@ describe("FindNextOperator", () => { sinon.stub(findRepository, "getLocalState").returns( Promise.resolve({ keyword, - frameIds, - framePos: 1, + frameId: 100, }) ); @@ -68,7 +71,7 @@ describe("FindNextOperator", () => { const mockFindRepository = sinon.mock(findRepository); mockFindRepository .expects("setLocalState") - .withArgs(currentTabId, { keyword, frameIds, framePos: 1 }); + .withArgs(currentTabId, { keyword, frameId: 100 }); await sut.run(); @@ -80,8 +83,7 @@ describe("FindNextOperator", () => { sinon.stub(findRepository, "getLocalState").returns( Promise.resolve({ keyword, - frameIds, - framePos: 1, + frameId: 100, }) ); @@ -102,7 +104,7 @@ describe("FindNextOperator", () => { const mockFindRepository = sinon.mock(findRepository); mockFindRepository .expects("setLocalState") - .withArgs(currentTabId, { keyword, frameIds, framePos: 2 }); + .withArgs(currentTabId, { keyword, frameId: 101 }); await sut.run(); @@ -114,8 +116,7 @@ describe("FindNextOperator", () => { sinon.stub(findRepository, "getLocalState").returns( Promise.resolve({ keyword, - frameIds, - framePos: 2, + frameId: 101, }) ); @@ -136,7 +137,7 @@ describe("FindNextOperator", () => { const mockFindRepository = sinon.mock(findRepository); mockFindRepository .expects("setLocalState") - .withArgs(currentTabId, { keyword, frameIds, framePos: 0 }); + .withArgs(currentTabId, { keyword, frameId: 0 }); await sut.run(); @@ -151,9 +152,6 @@ describe("FindNextOperator", () => { 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); @@ -168,7 +166,7 @@ describe("FindNextOperator", () => { const mockFindRepository = sinon.mock(findRepository); mockFindRepository .expects("setLocalState") - .withArgs(currentTabId, { keyword, frameIds, framePos: 0 }); + .withArgs(currentTabId, { keyword, frameId: 0 }); await sut.run(); diff --git a/test/background/operators/impls/FindPrevOperator.test.ts b/test/background/operators/impls/FindPrevOperator.test.ts index ebac0dc..090f815 100644 --- a/test/background/operators/impls/FindPrevOperator.test.ts +++ b/test/background/operators/impls/FindPrevOperator.test.ts @@ -4,7 +4,7 @@ import FindPrevOperator from "../../../../src/background/operators/impls/FindPre import MockFindRepository from "../../mock/MockFindRepository"; import MockFindClient from "../../mock/MockFindClient"; import MockConsoleClient from "../../mock/MockConsoleClient"; -import MockFramePresenter from "../../mock/MockFramePresenter"; +import MockReadyFrameRepository from "../../mock/MockReadyFrameRepository"; describe("FindPrevOperator", () => { const keyword = "hello"; @@ -14,13 +14,13 @@ describe("FindPrevOperator", () => { const findRepository = new MockFindRepository(); const findClient = new MockFindClient(); const consoleClient = new MockConsoleClient(); - const framePresenter = new MockFramePresenter(); + const frameRepository = new MockReadyFrameRepository(); const sut = new FindPrevOperator( tabPresenter, findRepository, findClient, consoleClient, - framePresenter + frameRepository ); let currentTabId: number; @@ -32,6 +32,10 @@ describe("FindPrevOperator", () => { active: true, }); currentTabId = currentTab.id!; + + sinon + .stub(frameRepository, "getFrameIds") + .returns(Promise.resolve(frameIds.slice(0))); }); describe("#run", () => { @@ -54,8 +58,7 @@ describe("FindPrevOperator", () => { sinon.stub(findRepository, "getLocalState").returns( Promise.resolve({ keyword, - frameIds, - framePos: 1, + frameId: 100, }) ); @@ -68,7 +71,7 @@ describe("FindPrevOperator", () => { const mockFindRepository = sinon.mock(findRepository); mockFindRepository .expects("setLocalState") - .withArgs(currentTabId, { keyword, frameIds, framePos: 1 }); + .withArgs(currentTabId, { keyword, frameId: 100 }); await sut.run(); @@ -80,8 +83,7 @@ describe("FindPrevOperator", () => { sinon.stub(findRepository, "getLocalState").returns( Promise.resolve({ keyword, - frameIds, - framePos: 1, + frameId: 100, }) ); @@ -102,7 +104,7 @@ describe("FindPrevOperator", () => { const mockFindRepository = sinon.mock(findRepository); mockFindRepository .expects("setLocalState") - .withArgs(currentTabId, { keyword, frameIds, framePos: 0 }); + .withArgs(currentTabId, { keyword, frameId: 0 }); await sut.run(); @@ -114,8 +116,7 @@ describe("FindPrevOperator", () => { sinon.stub(findRepository, "getLocalState").returns( Promise.resolve({ keyword, - frameIds, - framePos: 0, + frameId: 0, }) ); @@ -136,7 +137,7 @@ describe("FindPrevOperator", () => { const mockFindRepository = sinon.mock(findRepository); mockFindRepository .expects("setLocalState") - .withArgs(currentTabId, { keyword, frameIds, framePos: 2 }); + .withArgs(currentTabId, { keyword, frameId: 101 }); await sut.run(); @@ -151,9 +152,6 @@ describe("FindPrevOperator", () => { 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); @@ -168,7 +166,7 @@ describe("FindPrevOperator", () => { const mockFindRepository = sinon.mock(findRepository); mockFindRepository .expects("setLocalState") - .withArgs(currentTabId, { keyword, frameIds, framePos: 2 }); + .withArgs(currentTabId, { keyword, frameId: 101 }); await sut.run(); diff --git a/test/background/repositories/FindRepository.test.ts b/test/background/repositories/FindRepository.test.ts index a08dc6d..d8c9506 100644 --- a/test/background/repositories/FindRepository.test.ts +++ b/test/background/repositories/FindRepository.test.ts @@ -25,12 +25,12 @@ describe("background/repositories/FindRepositoryImpl", () => { await sut.setLocalState(10, { keyword: "Hello, world", - frameIds: [20, 21], - framePos: 0, + frameId: 11, }); const state = await sut.getLocalState(10); expect(state?.keyword).to.equal("Hello, world"); + expect(state?.frameId).to.equal(11); expect(await sut.getLocalState(20)).to.be.undefined; }); diff --git a/test/background/repositories/ReadyFrameRepository.test.ts b/test/background/repositories/ReadyFrameRepository.test.ts new file mode 100644 index 0000000..71f20af --- /dev/null +++ b/test/background/repositories/ReadyFrameRepository.test.ts @@ -0,0 +1,33 @@ +import { expect } from "chai"; +import { ReadyFrameRepositoryImpl } from "../../../src/background/repositories/ReadyFrameRepository"; + +describe("background/repositories/ReadyFrameRepositoryImpl", () => { + let sut: ReadyFrameRepositoryImpl; + + beforeEach(() => { + sut = new ReadyFrameRepositoryImpl(); + }); + + it("get and set a keyword", async () => { + expect(await sut.getFrameIds(1)).to.be.undefined; + + await sut.addFrameId(1, 10); + await sut.addFrameId(1, 12); + await sut.addFrameId(1, 11); + await sut.addFrameId(2, 20); + await sut.addFrameId(2, 21); + await sut.addFrameId(2, 21); + + expect(await sut.getFrameIds(1)).to.deep.equal([10, 11, 12]); + expect(await sut.getFrameIds(2)).to.deep.equal([20, 21]); + + await sut.removeFrameId(2, 21); + expect(await sut.getFrameIds(2)).to.deep.equal([20, 21]); + + await sut.removeFrameId(2, 21); + expect(await sut.getFrameIds(2)).to.deep.equal([20]); + + await sut.removeFrameId(2, 20); + expect(await sut.getFrameIds(2)).to.be.undefined; + }); +}); diff --git a/test/background/usecases/StartFindUseCase.test.ts b/test/background/usecases/StartFindUseCase.test.ts index 22ff9a5..24e1fdc 100644 --- a/test/background/usecases/StartFindUseCase.test.ts +++ b/test/background/usecases/StartFindUseCase.test.ts @@ -2,7 +2,7 @@ import * as sinon from "sinon"; import MockFindClient from "../mock/MockFindClient"; import MockFindRepository from "../mock/MockFindRepository"; import MockConsoleClient from "../mock/MockConsoleClient"; -import MockFramePresenter from "../mock/MockFramePresenter"; +import MockReadyFrameRepository from "../mock/MockReadyFrameRepository"; import StartFindUseCase from "../../../src/background/usecases/StartFindUseCase"; describe("StartFindUseCase", () => { @@ -13,19 +13,19 @@ describe("StartFindUseCase", () => { const findClient = new MockFindClient(); const findRepository = new MockFindRepository(); const consoleClient = new MockConsoleClient(); - const framePresenter = new MockFramePresenter(); + const frameRepository = new MockReadyFrameRepository(); const sut = new StartFindUseCase( findClient, findRepository, consoleClient, - framePresenter + frameRepository ); beforeEach(async () => { sinon.restore(); sinon - .stub(framePresenter, "getAllFrameIds") + .stub(frameRepository, "getFrameIds") .returns(Promise.resolve(frameIds)); }); @@ -46,7 +46,7 @@ describe("StartFindUseCase", () => { const mockFindRepository = sinon.mock(findRepository); mockFindRepository .expects("setLocalState") - .withArgs(currentTabId, { frameIds, framePos: 1, keyword }); + .withArgs(currentTabId, { keyword, frameId: 100 }); const mockConsoleClient = sinon.mock(consoleClient); mockConsoleClient .expects("showInfo") @@ -76,10 +76,10 @@ describe("StartFindUseCase", () => { mockFindRepository .expects("getLocalState") .withArgs(currentTabId) - .returns(Promise.resolve({ keyword, frameIds, framePos: 0 })); + .returns(Promise.resolve({ keyword, frameId: 0 })); mockFindRepository .expects("setLocalState") - .withArgs(currentTabId, { frameIds, framePos: 1, keyword }); + .withArgs(currentTabId, { keyword, frameId: 100 }); const mockConsoleClient = sinon.mock(consoleClient); mockConsoleClient .expects("showInfo") @@ -115,7 +115,7 @@ describe("StartFindUseCase", () => { .returns(Promise.resolve(keyword)); mockFindRepository .expects("setLocalState") - .withArgs(currentTabId, { frameIds, framePos: 1, keyword }); + .withArgs(currentTabId, { keyword, frameId: 100 }); const mockConsoleClient = sinon.mock(consoleClient); mockConsoleClient .expects("showInfo") |