diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2020-04-09 10:38:37 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-09 10:38:37 +0900 |
commit | 1656d52d2cefb3846d968c6117484e6aefe7dabe (patch) | |
tree | ab58a99b832d2571e2168f2ee0e328bc12d9580e /test/background/usecases | |
parent | c6c2da8547891b50aef2f08e5f36d258183831ff (diff) | |
parent | 5176643e64d8f4a6be5fc73f0eb48dc65322e496 (diff) |
Merge pull request #730 from ueokande/refactor-console-and-completion
Refactor console and completions
Diffstat (limited to 'test/background/usecases')
-rw-r--r-- | test/background/usecases/NavigateUseCase.test.ts | 127 | ||||
-rw-r--r-- | test/background/usecases/filters.test.ts | 113 |
2 files changed, 103 insertions, 137 deletions
diff --git a/test/background/usecases/NavigateUseCase.test.ts b/test/background/usecases/NavigateUseCase.test.ts index 48a1c5b..7ad0e4f 100644 --- a/test/background/usecases/NavigateUseCase.test.ts +++ b/test/background/usecases/NavigateUseCase.test.ts @@ -1,26 +1,107 @@ +import "reflect-metadata"; import TabPresenter from '../../../src/background/presenters/TabPresenter'; import NavigateUseCase from '../../../src/background/usecases/NavigateUseCase'; import NavigateClient from '../../../src/background/clients/NavigateClient'; -// import { expect } from 'chai'; import * as sinon from 'sinon'; +class MockTabPresenter implements TabPresenter { + create(_url: string, _opts?: object): Promise<browser.tabs.Tab> { + throw new Error("not implemented"); + } + + duplicate(_id: number): Promise<browser.tabs.Tab> { + throw new Error("not implemented"); + } + + getAll(): Promise<browser.tabs.Tab[]> { + throw new Error("not implemented"); + } + + getByKeyword(_keyword: string, _excludePinned: boolean): Promise<browser.tabs.Tab[]> { + throw new Error("not implemented"); + } + + getCurrent(): Promise<browser.tabs.Tab> { + throw new Error("not implemented"); + } + + getLastSelectedId(): Promise<number | undefined> { + throw new Error("not implemented"); + } + + getZoom(_tabId: number): Promise<number> { + throw new Error("not implemented"); + } + + onSelected(_listener: (arg: { tabId: number; windowId: number }) => void): void { + throw new Error("not implemented"); + } + + open(_url: string, _tabId?: number): Promise<browser.tabs.Tab> { + throw new Error("not implemented"); + } + + reload(_tabId: number, _cache: boolean): Promise<void> { + throw new Error("not implemented"); + } + + remove(_ids: number[]): Promise<void> { + throw new Error("not implemented"); + } + + reopen(): Promise<void> { + throw new Error("not implemented"); + } + + select(_tabId: number): Promise<void> { + throw new Error("not implemented"); + } + + setPinned(_tabId: number, _pinned: boolean): Promise<void> { + throw new Error("not implemented"); + } + + setZoom(_tabId: number, _factor: number): Promise<void> { + throw new Error("not implemented"); + } +} + describe('NavigateUseCase', () => { let sut: NavigateUseCase; let tabPresenter: TabPresenter; let navigateClient: NavigateClient; + beforeEach(() => { - tabPresenter = new TabPresenter(); + tabPresenter = new MockTabPresenter(); navigateClient = new NavigateClient(); sut = new NavigateUseCase(tabPresenter, navigateClient); }); + const newTab = (url: string): browser.tabs.Tab => { + return { + index: 0, + title: 'dummy title', + url: url, + active: true, + hidden: false, + highlighted: false, + incognito: false, + isArticle: false, + isInReaderMode: false, + lastAccessed: 1585446733000, + pinned: false, + selected: false, + windowId: 0 + }; + }; + describe('#openParent()', async () => { it('opens parent directory of file', async() => { - const stub = sinon.stub(tabPresenter, 'getCurrent'); - stub.returns(Promise.resolve({ url: 'https://google.com/fruits/yellow/banana' })) + sinon.stub(tabPresenter, 'getCurrent') + .returns(Promise.resolve(newTab('https://google.com/fruits/yellow/banana'))); - const mock = sinon.mock(tabPresenter); - mock.expects('open').withArgs('https://google.com/fruits/yellow/'); + const mock = sinon.mock(tabPresenter) + .expects('open').withArgs('https://google.com/fruits/yellow/'); await sut.openParent(); @@ -28,11 +109,11 @@ describe('NavigateUseCase', () => { }); it('opens parent directory of directory', async() => { - const stub = sinon.stub(tabPresenter, 'getCurrent'); - stub.returns(Promise.resolve({ url: 'https://google.com/fruits/yellow/' })) + sinon.stub(tabPresenter, 'getCurrent') + .returns(Promise.resolve(newTab('https://google.com/fruits/yellow/'))); - const mock = sinon.mock(tabPresenter); - mock.expects('open').withArgs('https://google.com/fruits/'); + const mock = sinon.mock(tabPresenter) + .expects('open').withArgs('https://google.com/fruits/'); await sut.openParent(); @@ -40,11 +121,11 @@ describe('NavigateUseCase', () => { }); it('removes hash', async() => { - const stub = sinon.stub(tabPresenter, 'getCurrent'); - stub.returns(Promise.resolve({ url: 'https://google.com/#top' })) + sinon.stub(tabPresenter, 'getCurrent') + .returns(Promise.resolve(newTab('https://google.com/#top'))); - const mock = sinon.mock(tabPresenter); - mock.expects('open').withArgs('https://google.com/'); + const mock = sinon.mock(tabPresenter) + .expects('open').withArgs('https://google.com/'); await sut.openParent(); @@ -52,11 +133,11 @@ describe('NavigateUseCase', () => { }); it('removes search query', async() => { - const stub = sinon.stub(tabPresenter, 'getCurrent'); - stub.returns(Promise.resolve({ url: 'https://google.com/search?q=apple' })) + sinon.stub(tabPresenter, 'getCurrent') + .returns(Promise.resolve(newTab('https://google.com/search?q=apple'))); - const mock = sinon.mock(tabPresenter); - mock.expects('open').withArgs('https://google.com/search'); + const mock = sinon.mock(tabPresenter) + .expects('open').withArgs('https://google.com/search'); await sut.openParent(); @@ -66,13 +147,11 @@ describe('NavigateUseCase', () => { describe('#openRoot()', () => { it('opens root direectory', async() => { - const stub = sinon.stub(tabPresenter, 'getCurrent'); - stub.returns(Promise.resolve({ - url: 'https://google.com/seach?q=apple', - })) + sinon.stub(tabPresenter, 'getCurrent') + .returns(Promise.resolve(newTab('https://google.com/seach?q=apple'))); - const mock = sinon.mock(tabPresenter); - mock.expects('open').withArgs('https://google.com'); + const mock = sinon.mock(tabPresenter) + .expects('open').withArgs('https://google.com'); await sut.openRoot(); diff --git a/test/background/usecases/filters.test.ts b/test/background/usecases/filters.test.ts deleted file mode 100644 index 90541ff..0000000 --- a/test/background/usecases/filters.test.ts +++ /dev/null @@ -1,113 +0,0 @@ -import * as filters from 'background/usecases/filters'; - -describe("background/usecases/filters", () => { - describe('filterHttp', () => { - it('filters http URLs duplicates to https hosts', () => { - const pages = [ - { url: 'http://i-beam.org/foo' }, - { url: 'https://i-beam.org/bar' }, - { url: 'http://i-beam.net/hoge' }, - { url: 'http://i-beam.net/fuga' }, - ]; - const filtered = filters.filterHttp(pages); - - const urls = filtered.map(x => x.url); - expect(urls).to.deep.equal([ - 'https://i-beam.org/bar', 'http://i-beam.net/hoge', 'http://i-beam.net/fuga' - ]); - }) - }); - - describe('filterBlankTitle', () => { - it('filters blank titles', () => { - const pages = [ - { title: 'hello' }, - { title: '' }, - {}, - ]; - const filtered = filters.filterBlankTitle(pages); - - expect(filtered).to.deep.equal([{ title: 'hello' }]); - }); - }) - - describe('filterByTailingSlash', () => { - it('filters duplicated pathname on tailing slash', () => { - const pages = [ - { url: 'http://i-beam.org/content' }, - { url: 'http://i-beam.org/content/' }, - { url: 'http://i-beam.org/search' }, - { url: 'http://i-beam.org/search?q=apple_banana_cherry' }, - ]; - const filtered = filters.filterByTailingSlash(pages); - - const urls = filtered.map(x => x.url); - expect(urls).to.deep.equal([ - 'http://i-beam.org/content', - 'http://i-beam.org/search', - 'http://i-beam.org/search?q=apple_banana_cherry', - ]); - }); - }) - - describe('filterByPathname', () => { - it('remains items less than minimam length', () => { - const pages = [ - { url: 'http://i-beam.org/search?q=apple' }, - { url: 'http://i-beam.org/search?q=apple_banana' }, - { url: 'http://i-beam.org/search?q=apple_banana_cherry' }, - { url: 'http://i-beam.org/request?q=apple' }, - { url: 'http://i-beam.org/request?q=apple_banana' }, - { url: 'http://i-beam.org/request?q=apple_banana_cherry' }, - ]; - const filtered = filters.filterByPathname(pages, 10); - expect(filtered).to.have.lengthOf(6); - }); - - it('filters by length of pathname', () => { - const pages = [ - { url: 'http://i-beam.org/search?q=apple' }, - { url: 'http://i-beam.org/search?q=apple_banana' }, - { url: 'http://i-beam.org/search?q=apple_banana_cherry' }, - { url: 'http://i-beam.net/search?q=apple' }, - { url: 'http://i-beam.net/search?q=apple_banana' }, - { url: 'http://i-beam.net/search?q=apple_banana_cherry' }, - ]; - const filtered = filters.filterByPathname(pages, 0); - expect(filtered).to.deep.equal([ - { url: 'http://i-beam.org/search?q=apple' }, - { url: 'http://i-beam.net/search?q=apple' }, - ]); - }); - }) - - describe('filterByOrigin', () => { - it('remains items less than minimam length', () => { - const pages = [ - { url: 'http://i-beam.org/search?q=apple' }, - { url: 'http://i-beam.org/search?q=apple_banana' }, - { url: 'http://i-beam.org/search?q=apple_banana_cherry' }, - { url: 'http://i-beam.org/request?q=apple' }, - { url: 'http://i-beam.org/request?q=apple_banana' }, - { url: 'http://i-beam.org/request?q=apple_banana_cherry' }, - ]; - const filtered = filters.filterByOrigin(pages, 10); - expect(filtered).to.have.lengthOf(6); - }); - - it('filters by length of pathname', () => { - const pages = [ - { url: 'http://i-beam.org/search?q=apple' }, - { url: 'http://i-beam.org/search?q=apple_banana' }, - { url: 'http://i-beam.org/search?q=apple_banana_cherry' }, - { url: 'http://i-beam.org/request?q=apple' }, - { url: 'http://i-beam.org/request?q=apple_banana' }, - { url: 'http://i-beam.org/request?q=apple_banana_cherry' }, - ]; - const filtered = filters.filterByOrigin(pages, 0); - expect(filtered).to.deep.equal([ - { url: 'http://i-beam.org/search?q=apple' }, - ]); - }); - }) -}); |