diff options
Diffstat (limited to 'test/background/usecases')
| -rw-r--r-- | test/background/usecases/SettingUseCase.test.ts | 64 | ||||
| -rw-r--r-- | test/background/usecases/StartFindUseCase.test.ts | 163 | ||||
| -rw-r--r-- | test/background/usecases/parsers.test.ts | 30 | 
3 files changed, 200 insertions, 57 deletions
| diff --git a/test/background/usecases/SettingUseCase.test.ts b/test/background/usecases/SettingUseCase.test.ts index 8a4c2b2..395e8a4 100644 --- a/test/background/usecases/SettingUseCase.test.ts +++ b/test/background/usecases/SettingUseCase.test.ts @@ -7,9 +7,7 @@ import Settings, {    DefaultSetting,  } from "../../../src/shared/settings/Settings";  import Notifier from "../../../src/background/presenters/Notifier"; -import { expect } from "chai";  import Properties from "../../../src/shared/settings/Properties"; -import sinon from "sinon";  class MockSettingRepository implements SettingRepository {    load(): Promise<SettingData | null> { @@ -77,17 +75,15 @@ 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).to.equal("abcd1234"); +      expect(got.properties.hintchars).toEqual("abcd1234");      });    });    describe("reload", () => { -    context("when sync is not set", () => { +    describe("when sync is not set", () => {        it("loads settings from local storage", async () => {          const settings = new Settings({            keymaps: DefaultSetting.keymaps, @@ -102,21 +98,19 @@ 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();          const current = await cachedSettingRepository.get(); -        expect(current.properties.hintchars).to.equal("abcd1234"); +        expect(current.properties.hintchars).toEqual("abcd1234");        });      }); -    context("when local is not set", () => { +    describe("when local is not set", () => {        it("loads settings from sync storage", async () => {          const settings = new Settings({            keymaps: DefaultSetting.keymaps, @@ -131,37 +125,29 @@ 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();          const current = await cachedSettingRepository.get(); -        expect(current.properties.hintchars).to.equal("aaaa1111"); +        expect(current.properties.hintchars).toEqual("aaaa1111");        });      }); -    context("neither local nor sync not set", () => { -      it("loads default settings", async () => { -        it("loads settings from sync storage", async () => { -          sinon -            .stub(syncSettingRepository, "load") -            .returns(Promise.resolve(null)); -          sinon -            .stub(localSettingRepository, "load") -            .returns(Promise.resolve(null)); - -          await sut.reload(); - -          const current = await cachedSettingRepository.get(); -          expect(current.properties.hintchars).to.equal( -            DefaultSetting.properties.hintchars -          ); -        }); +    describe("neither local nor sync not set", () => { +      it("loads settings from sync storage", async () => { +        jest.spyOn(syncSettingRepository, "load").mockResolvedValue(null); +        jest.spyOn(localSettingRepository, "load").mockResolvedValue(null); + +        await sut.reload(); + +        const current = await cachedSettingRepository.get(); +        expect(current.properties.hintchars).toEqual( +          DefaultSetting.properties.hintchars +        );        });      });    }); diff --git a/test/background/usecases/StartFindUseCase.test.ts b/test/background/usecases/StartFindUseCase.test.ts new file mode 100644 index 0000000..0bfe18a --- /dev/null +++ b/test/background/usecases/StartFindUseCase.test.ts @@ -0,0 +1,163 @@ +import MockFindClient from "../mock/MockFindClient"; +import MockFindRepository from "../mock/MockFindRepository"; +import MockConsoleClient from "../mock/MockConsoleClient"; +import MockReadyFrameRepository from "../mock/MockReadyFrameRepository"; +import StartFindUseCase from "../../../src/background/usecases/StartFindUseCase"; + +describe("StartFindUseCase", () => { +  const currentTabId = 100; +  const frameIds = [0, 100, 101]; +  const keyword = "hello"; + +  const findClient = new MockFindClient(); +  const findRepository = new MockFindRepository(); +  const consoleClient = new MockConsoleClient(); +  const frameRepository = new MockReadyFrameRepository(); +  const sut = new StartFindUseCase( +    findClient, +    findRepository, +    consoleClient, +    frameRepository +  ); + +  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()); + +  beforeEach(async () => { +    getFrameIdsSpy.mockClear(); +    clearSelectionSpy.mockClear(); +    findNextSpy.mockClear(); +    setLocalStateSpy.mockClear(); +  }); + +  describe("startFind", () => { +    it("starts a find with a keyword", async () => { +      findNextSpy.mockResolvedValueOnce(false).mockResolvedValueOnce(true); +      const showInfoSpy = jest +        .spyOn(consoleClient, "showInfo") +        .mockReturnValue(Promise.resolve()); + +      await sut.startFind(currentTabId, keyword); + +      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 () => { +      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); + +      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 () => { +      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); + +      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 () => { +      findNextSpy.mockResolvedValue(false); +      const showErrorSpy = jest +        .spyOn(consoleClient, "showError") +        .mockReturnValue(Promise.resolve()); + +      await sut.startFind(currentTabId, keyword); + +      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 () => { +      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); + +      expect(showErrorSpy).toBeCalledWith( +        currentTabId, +        "No previous search keywords" +      ); +    }); +  }); +}); diff --git a/test/background/usecases/parsers.test.ts b/test/background/usecases/parsers.test.ts index 019b56e..db26b7a 100644 --- a/test/background/usecases/parsers.test.ts +++ b/test/background/usecases/parsers.test.ts @@ -1,42 +1,36 @@  import * as parsers from "../../../src/background/usecases/parsers"; -import { expect } from "chai";  describe("shared/commands/parsers", () => {    describe("#parsers.parseSetOption", () => {      it("parse set string", () => {        const [key, value] = parsers.parseSetOption("hintchars=abcdefgh"); -      expect(key).to.equal("hintchars"); -      expect(value).to.equal("abcdefgh"); +      expect(key).toEqual("hintchars"); +      expect(value).toEqual("abcdefgh");      });      it("parse set empty string", () => {        const [key, value] = parsers.parseSetOption("hintchars="); -      expect(key).to.equal("hintchars"); -      expect(value).to.equal(""); +      expect(key).toEqual("hintchars"); +      expect(value).toEqual("");      });      it("parse set boolean", () => {        let [key, value] = parsers.parseSetOption("smoothscroll"); -      expect(key).to.equal("smoothscroll"); -      expect(value).to.be.true; +      expect(key).toEqual("smoothscroll"); +      expect(value).toBeTruthy;        [key, value] = parsers.parseSetOption("nosmoothscroll"); -      expect(key).to.equal("smoothscroll"); -      expect(value).to.be.false; +      expect(key).toEqual("smoothscroll"); +      expect(value).toBeFalsy;      });      it("throws error on unknown property", () => { -      expect(() => parsers.parseSetOption("encoding=utf-8")).to.throw( -        Error, +      expect(() => parsers.parseSetOption("encoding=utf-8")).toThrowError(          "Unknown"        ); -      expect(() => parsers.parseSetOption("paste")).to.throw(Error, "Unknown"); -      expect(() => parsers.parseSetOption("nopaste")).to.throw( -        Error, -        "Unknown" -      ); -      expect(() => parsers.parseSetOption("smoothscroll=yes")).to.throw( -        Error, +      expect(() => parsers.parseSetOption("paste")).toThrowError("Unknown"); +      expect(() => parsers.parseSetOption("nopaste")).toThrowError("Unknown"); +      expect(() => parsers.parseSetOption("smoothscroll=yes")).toThrowError(          "Invalid argument"        );      }); | 
