diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2021-09-30 00:16:32 +0900 |
---|---|---|
committer | Shin'ya Ueoka <ueokande@i-beam.org> | 2021-09-30 00:24:35 +0900 |
commit | acfe1b78009b0c3183cd69eebdabb458e2079b73 (patch) | |
tree | 20b6c71228976173f4086d34aec552feadb461fc /test/background/completion | |
parent | c843809ba29bd70c28b9a19e4cb3f1002edee781 (diff) |
Move mock library to jest
Diffstat (limited to 'test/background/completion')
-rw-r--r-- | test/background/completion/OpenCompletionUseCase.test.ts | 85 | ||||
-rw-r--r-- | test/background/completion/TabCompletionUseCase.test.ts | 89 |
2 files changed, 72 insertions, 102 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([ { |