From e1dac618a8b8929f601c7ec8aca3842c5ebf9d03 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Mon, 13 Apr 2020 20:37:36 +0900 Subject: Use plugin:prettier/recommended --- .../completion/OpenCompletionUseCase.test.ts | 128 +++++++----- .../completion/PropertyCompletionUseCase.test.ts | 21 +- .../completion/TabCompletionUseCase.test.ts | 232 ++++++++++++++++----- .../completion/impl/PrefetchAndCache.test.ts | 80 ++++--- test/background/completion/impl/filters.test.ts | 98 ++++----- .../infrastructures/MemoryStorage.test.ts | 48 ++--- test/background/repositories/Mark.test.ts | 16 +- test/background/usecases/NavigateUseCase.test.ts | 94 +++++---- test/background/usecases/SettingUseCase.test.ts | 73 ++++--- test/background/usecases/parsers.test.ts | 47 +++-- 10 files changed, 526 insertions(+), 311 deletions(-) (limited to 'test/background') diff --git a/test/background/completion/OpenCompletionUseCase.test.ts b/test/background/completion/OpenCompletionUseCase.test.ts index 421ce69..a0d8b44 100644 --- a/test/background/completion/OpenCompletionUseCase.test.ts +++ b/test/background/completion/OpenCompletionUseCase.test.ts @@ -1,42 +1,48 @@ import "reflect-metadata"; import CompletionType from "../../../src/shared/CompletionType"; -import BookmarkRepository, {BookmarkItem} from "../../../src/background/completion/BookmarkRepository"; -import HistoryRepository, {HistoryItem} from "../../../src/background/completion/HistoryRepository"; +import BookmarkRepository, { + BookmarkItem, +} from "../../../src/background/completion/BookmarkRepository"; +import HistoryRepository, { + HistoryItem, +} from "../../../src/background/completion/HistoryRepository"; import OpenCompletionUseCase from "../../../src/background/completion/OpenCompletionUseCase"; import CachedSettingRepository from "../../../src/background/repositories/CachedSettingRepository"; -import Settings, {DefaultSetting} from "../../../src/shared/settings/Settings"; -import { expect } from 'chai'; -import sinon from 'sinon'; +import Settings, { + DefaultSetting, +} from "../../../src/shared/settings/Settings"; +import { expect } from "chai"; +import sinon from "sinon"; import Properties from "../../../src/shared/settings/Properties"; import Search from "../../../src/shared/settings/Search"; class MockBookmarkRepository implements BookmarkRepository { queryBookmarks(_query: string): Promise { - throw new Error("not implemented") + throw new Error("not implemented"); } } class MockHistoryRepository implements HistoryRepository { queryHistories(_keywords: string): Promise { - throw new Error("not implemented") + throw new Error("not implemented"); } } class MockSettingRepository implements CachedSettingRepository { get(): Promise { - throw new Error("not implemented") + throw new Error("not implemented"); } setProperty(_name: string, _value: string | number | boolean): Promise { - throw new Error("not implemented") + throw new Error("not implemented"); } update(_value: Settings): Promise { - throw new Error("not implemented") + throw new Error("not implemented"); } } -describe('OpenCompletionUseCase', () => { +describe("OpenCompletionUseCase", () => { let bookmarkRepository: MockBookmarkRepository; let historyRepository: MockHistoryRepository; let settingRepository: MockSettingRepository; @@ -46,40 +52,52 @@ describe('OpenCompletionUseCase', () => { bookmarkRepository = new MockBookmarkRepository(); historyRepository = new MockHistoryRepository(); settingRepository = new MockSettingRepository(); - sut = new OpenCompletionUseCase(bookmarkRepository, historyRepository, settingRepository) + sut = new OpenCompletionUseCase( + bookmarkRepository, + historyRepository, + settingRepository + ); }); - describe('#getCompletionTypes', () => { + 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, - }))); + sinon.stub(settingRepository, "get").returns( + Promise.resolve( + new Settings({ + keymaps: DefaultSetting.keymaps, + search: DefaultSetting.search, + properties: new Properties({ complete: "shb" }), + blacklist: DefaultSetting.blacklist, + }) + ) + ); const items = await sut.getCompletionTypes(); expect(items).to.deep.equal([ CompletionType.SearchEngines, CompletionType.History, - CompletionType.Bookmarks + CompletionType.Bookmarks, ]); }); }); - describe('#requestSearchEngines', () => { + 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={}", - "google_ja": "https://google.co.jp/search?q={}", - }), - properties: DefaultSetting.properties, - blacklist: DefaultSetting.blacklist, - }))); + 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={}", + google_ja: "https://google.co.jp/search?q={}", + }), + properties: DefaultSetting.properties, + blacklist: DefaultSetting.blacklist, + }) + ) + ); expect(await sut.requestSearchEngines("")).to.deep.equal([ "google", @@ -92,17 +110,22 @@ describe('OpenCompletionUseCase', () => { "google_ja", ]); expect(await sut.requestSearchEngines("x")).to.be.empty; - }) + }); }); - 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([])); + 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([])); expect(await sut.requestBookmarks("site")).to.deep.equal([ { title: "site1", url: "https://site1.example.com" }, @@ -112,14 +135,19 @@ describe('OpenCompletionUseCase', () => { }); }); - 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([])); + 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([])); expect(await sut.requestHistory("site")).to.deep.equal([ { title: "site1", url: "https://site1.example.com" }, @@ -128,4 +156,4 @@ describe('OpenCompletionUseCase', () => { expect(await sut.requestHistory("xyz")).to.be.empty; }); }); -}); \ No newline at end of file +}); diff --git a/test/background/completion/PropertyCompletionUseCase.test.ts b/test/background/completion/PropertyCompletionUseCase.test.ts index 57f5bff..dfc989f 100644 --- a/test/background/completion/PropertyCompletionUseCase.test.ts +++ b/test/background/completion/PropertyCompletionUseCase.test.ts @@ -1,15 +1,18 @@ -import 'reflect-metadata'; +import "reflect-metadata"; import PropertyCompletionUseCase from "../../../src/background/completion/PropertyCompletionUseCase"; -import { expect } from 'chai'; +import { expect } from "chai"; -describe('PropertyCompletionUseCase', () => { - describe('getProperties', () => { - it('returns property types', async () => { +describe("PropertyCompletionUseCase", () => { + describe("getProperties", () => { + it("returns property types", async () => { const sut = new PropertyCompletionUseCase(); const properties = await sut.getProperties(); - expect(properties).to.deep.contain({ name: 'smoothscroll', type: 'boolean' }); - expect(properties).to.deep.contain({ name: 'complete', type: 'string' }); - }) + expect(properties).to.deep.contain({ + name: "smoothscroll", + type: "boolean", + }); + expect(properties).to.deep.contain({ name: "complete", type: "string" }); + }); }); -}); \ No newline at end of file +}); diff --git a/test/background/completion/TabCompletionUseCase.test.ts b/test/background/completion/TabCompletionUseCase.test.ts index b9dc60b..d8aa385 100644 --- a/test/background/completion/TabCompletionUseCase.test.ts +++ b/test/background/completion/TabCompletionUseCase.test.ts @@ -1,85 +1,90 @@ -import "reflect-metadata" +import "reflect-metadata"; import TabRepositoryImpl from "../../../src/background/completion/impl/TabRepositoryImpl"; -import {Tab} from "../../../src/background/completion/TabRepository"; +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 { expect } from 'chai'; +import sinon from "sinon"; +import { expect } from "chai"; import TabFlag from "../../../src/shared/TabFlag"; class MockTabRepository implements TabRepositoryImpl { async queryTabs(_query: string, _excludePinned: boolean): Promise { - throw new Error("not implemented") + throw new Error("not implemented"); } async getAllTabs(_excludePinned: boolean): Promise { - throw new Error("not implemented") + throw new Error("not implemented"); } } class MockTabPresenter implements TabPresenter { create(_url: string, _opts?: object): Promise { - throw new Error("not implemented") + throw new Error("not implemented"); } duplicate(_id: number): Promise { - throw new Error("not implemented") + throw new Error("not implemented"); } getAll(): Promise { - throw new Error("not implemented") + throw new Error("not implemented"); } - getByKeyword(_keyword: string, _excludePinned: boolean): Promise { - throw new Error("not implemented") + getByKeyword( + _keyword: string, + _excludePinned: boolean + ): Promise { + throw new Error("not implemented"); } getCurrent(): Promise { - throw new Error("not implemented") + throw new Error("not implemented"); } getLastSelectedId(): Promise { - throw new Error("not implemented") + throw new Error("not implemented"); } getZoom(_tabId: number): Promise { - throw new Error("not implemented") + throw new Error("not implemented"); } - onSelected(_listener: (arg: { tabId: number; windowId: number }) => void): void { - 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 { - throw new Error("not implemented") + throw new Error("not implemented"); } reload(_tabId: number, _cache: boolean): Promise { - throw new Error("not implemented") + throw new Error("not implemented"); } remove(_ids: number[]): Promise { - throw new Error("not implemented") + throw new Error("not implemented"); } reopen(): Promise { - throw new Error("not implemented") + throw new Error("not implemented"); } select(_tabId: number): Promise { - throw new Error("not implemented") + throw new Error("not implemented"); } setPinned(_tabId: number, _pinned: boolean): Promise { - throw new Error("not implemented") + throw new Error("not implemented"); } setZoom(_tabId: number, _factor: number): Promise { - throw new Error("not implemented") + throw new Error("not implemented"); } } -describe('TabCompletionUseCase', () => { +describe("TabCompletionUseCase", () => { let tabRepository: MockTabRepository; let tabPresenter: TabPresenter; let sut: TabCompletionUseCase; @@ -89,56 +94,169 @@ 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 }, - ])); + 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, + }, + ]) + ); }); - describe('#queryTabs', () => { + describe("#queryTabs", () => { it("returns tab items", async () => { - sinon.stub(tabRepository, 'queryTabs').withArgs('', 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 }, - { id: 12, index: 2, title: 'Bing', url: 'https://bing.com/', active: false }, - ])).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)).to.deep.equal([ - { index: 1, title: 'Google', url: 'https://google.com/', faviconUrl: 'https://google.com/favicon.ico', flag: TabFlag.None }, - { index: 2, title: 'Yahoo', url: 'https://yahoo.com/', faviconUrl: 'https://yahoo.com/favicon.ico', flag: TabFlag.CurrentTab }, - { index: 3, title: 'Bing', url: 'https://bing.com/', faviconUrl: undefined, flag: TabFlag.LastTab }, + sinon + .stub(tabRepository, "queryTabs") + .withArgs("", 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, + }, + { + id: 12, + index: 2, + title: "Bing", + url: "https://bing.com/", + active: false, + }, + ]) + ) + .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)).to.deep.equal([ + { + index: 1, + title: "Google", + url: "https://google.com/", + faviconUrl: "https://google.com/favicon.ico", + flag: TabFlag.None, + }, + { + index: 2, + title: "Yahoo", + url: "https://yahoo.com/", + faviconUrl: "https://yahoo.com/favicon.ico", + flag: TabFlag.CurrentTab, + }, + { + index: 3, + title: "Bing", + url: "https://bing.com/", + faviconUrl: undefined, + flag: TabFlag.LastTab, + }, ]); - expect(await sut.queryTabs('oo', false)).to.deep.equal([ - { index: 1, title: 'Google', url: 'https://google.com/', faviconUrl: 'https://google.com/favicon.ico', flag: TabFlag.None }, - { index: 2, title: 'Yahoo', url: 'https://yahoo.com/', faviconUrl: 'https://yahoo.com/favicon.ico', flag: TabFlag.CurrentTab }, + expect(await sut.queryTabs("oo", false)).to.deep.equal([ + { + index: 1, + title: "Google", + url: "https://google.com/", + faviconUrl: "https://google.com/favicon.ico", + flag: TabFlag.None, + }, + { + index: 2, + title: "Yahoo", + url: "https://yahoo.com/", + faviconUrl: "https://yahoo.com/favicon.ico", + flag: TabFlag.CurrentTab, + }, ]); }); it("returns a tab by the index", async () => { - expect(await sut.queryTabs('1', false)).to.deep.equal([ - { index: 1, title: 'Google', url: 'https://google.com/', faviconUrl: 'https://google.com/favicon.ico', flag: TabFlag.None }, + expect(await sut.queryTabs("1", false)).to.deep.equal([ + { + index: 1, + title: "Google", + url: "https://google.com/", + faviconUrl: "https://google.com/favicon.ico", + flag: TabFlag.None, + }, ]); - expect(await sut.queryTabs('10', false)).to.be.empty; - expect(await sut.queryTabs('-1', false)).to.be.empty; + expect(await sut.queryTabs("10", false)).to.be.empty; + expect(await sut.queryTabs("-1", false)).to.be.empty; }); it("returns the current tab by % flag", async () => { - expect(await sut.queryTabs('%', false)).to.deep.equal([ - { index: 2, title: 'Yahoo', url: 'https://yahoo.com/', faviconUrl: 'https://yahoo.com/favicon.ico', flag: TabFlag.CurrentTab }, + expect(await sut.queryTabs("%", false)).to.deep.equal([ + { + index: 2, + title: "Yahoo", + url: "https://yahoo.com/", + faviconUrl: "https://yahoo.com/favicon.ico", + flag: TabFlag.CurrentTab, + }, ]); }); it("returns the current tab by # flag", async () => { - expect(await sut.queryTabs('#', false)).to.deep.equal([ - { index: 3, title: 'Bing', url: 'https://bing.com/', faviconUrl: undefined, flag: TabFlag.LastTab }, + expect(await sut.queryTabs("#", false)).to.deep.equal([ + { + index: 3, + title: "Bing", + url: "https://bing.com/", + faviconUrl: undefined, + flag: TabFlag.LastTab, + }, ]); - }) + }); }); -}); \ No newline at end of file +}); diff --git a/test/background/completion/impl/PrefetchAndCache.test.ts b/test/background/completion/impl/PrefetchAndCache.test.ts index 23e3879..b24dfa9 100644 --- a/test/background/completion/impl/PrefetchAndCache.test.ts +++ b/test/background/completion/impl/PrefetchAndCache.test.ts @@ -1,39 +1,39 @@ -import PrefetchAndCache, {shortKey} from "../../../../src/background/completion/impl/PrefetchAndCache"; -import { expect } from 'chai'; +import PrefetchAndCache, { + shortKey, +} from "../../../../src/background/completion/impl/PrefetchAndCache"; +import { expect } from "chai"; class MockRepository { public history: string[] = []; - constructor( - private items: string[], - ) { - } + constructor(private items: string[]) {} get(query: string): Promise { this.history.push(query); if (query.length === 0) { return Promise.resolve(this.items); } else { - return Promise.resolve(this.items.filter(item => item.includes(query))); + return Promise.resolve(this.items.filter((item) => item.includes(query))); } } } -const filter = (items: string[], query: string) => query.length === 0 ? items : items.filter(item => item.includes(query)); +const filter = (items: string[], query: string) => + query.length === 0 ? items : items.filter((item) => item.includes(query)); -describe('shortKey', () => { - it('returns query excluding the last word', () => { - const query = "hello\t world good bye"; - const shorten = shortKey(query); - expect(shorten).to.equal("hello world good") +describe("shortKey", () => { + it("returns query excluding the last word", () => { + const query = "hello\t world good bye"; + const shorten = shortKey(query); + expect(shorten).to.equal("hello world good"); }); - it('returns half-length of the query', () => { + it("returns half-length of the query", () => { const query = "the-query-with-super-long-word"; const shorten = shortKey(query); - expect(shorten).to.equal("the-query-with-") + expect(shorten).to.equal("the-query-with-"); }); - it('returns shorten URL', () => { + it("returns shorten URL", () => { let query = "https://example.com/path/to/resource?q=hello"; let shorten = shortKey(query); expect(shorten).to.equal("https://example.com/path/to/"); @@ -45,20 +45,39 @@ describe('shortKey', () => { query = "https://www.google.c"; shorten = shortKey(query); expect(shorten).to.equal("https://ww"); - }) + }); }); -describe('PrefetchAndCache', () => { - describe('get', () => { - it('returns cached request', async() => { - const repo = new MockRepository(["apple", "apple pie", "apple juice", "banana", "banana pudding", "cherry"]); +describe("PrefetchAndCache", () => { + describe("get", () => { + it("returns cached request", async () => { + const repo = new MockRepository([ + "apple", + "apple pie", + "apple juice", + "banana", + "banana pudding", + "cherry", + ]); const sut = new PrefetchAndCache(repo.get.bind(repo), filter); expect(await sut.get("apple pie")).deep.equal(["apple pie"]); - expect(await sut.get("apple ")).deep.equal(["apple", "apple pie", "apple juice"]); - expect(await sut.get("apple")).deep.equal(["apple", "apple pie", "apple juice"]); - expect(await sut.get("appl")).deep.equal(["apple", "apple pie", "apple juice"]); - expect(repo.history).to.deep.equal(["apple", 'ap']); + expect(await sut.get("apple ")).deep.equal([ + "apple", + "apple pie", + "apple juice", + ]); + expect(await sut.get("apple")).deep.equal([ + "apple", + "apple pie", + "apple juice", + ]); + expect(await sut.get("appl")).deep.equal([ + "apple", + "apple pie", + "apple juice", + ]); + expect(repo.history).to.deep.equal(["apple", "ap"]); expect(await sut.get("banana")).deep.equal(["banana", "banana pudding"]); expect(repo.history).to.deep.equal(["apple", "ap", "ban"]); @@ -68,7 +87,14 @@ describe('PrefetchAndCache', () => { expect(repo.history).to.deep.equal(["apple", "ap", "ban", "banana", "b"]); expect(await sut.get("")).to.have.lengthOf(6); - expect(repo.history).to.deep.equal(["apple", "ap", "ban", "banana", "b", ""]); + expect(repo.history).to.deep.equal([ + "apple", + "ap", + "ban", + "banana", + "b", + "", + ]); }); }); -}); \ No newline at end of file +}); diff --git a/test/background/completion/impl/filters.test.ts b/test/background/completion/impl/filters.test.ts index a181f60..70c2663 100644 --- a/test/background/completion/impl/filters.test.ts +++ b/test/background/completion/impl/filters.test.ts @@ -1,87 +1,89 @@ -import * as filters from '../../../../src/background/completion/impl/filters' -import { expect } from 'chai'; +import * as filters from "../../../../src/background/completion/impl/filters"; +import { expect } from "chai"; -describe('background/usecases/filters', () => { - describe('filterHttp', () => { - it('filters http URLs duplicates to https hosts', () => { +describe("background/usecases/filters", () => { + describe("filterHttp", () => { + it("filters http URLs duplicates to https hosts", () => { const pages = [ - { id: '0', url: 'http://i-beam.org/foo' }, - { id: '1', url: 'https://i-beam.org/bar' }, - { id: '2', url: 'http://i-beam.net/hoge' }, - { id: '3', url: 'http://i-beam.net/fuga' }, + { id: "0", url: "http://i-beam.org/foo" }, + { id: "1", url: "https://i-beam.org/bar" }, + { id: "2", url: "http://i-beam.net/hoge" }, + { id: "3", url: "http://i-beam.net/fuga" }, ]; const filtered = filters.filterHttp(pages); - const urls = filtered.map(x => x.url); + 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' + "https://i-beam.org/bar", + "http://i-beam.net/hoge", + "http://i-beam.net/fuga", ]); - }) + }); }); - describe('filterBlankTitle', () => { - it('filters blank titles', () => { + describe("filterBlankTitle", () => { + it("filters blank titles", () => { const pages = [ - { id: '0', title: 'hello' }, - { id: '1', title: '' }, - { id: '2' }, + { id: "0", title: "hello" }, + { id: "1", title: "" }, + { id: "2" }, ]; const filtered = filters.filterBlankTitle(pages); - expect(filtered).to.deep.equal([{ id: '0', title: 'hello' }]); + expect(filtered).to.deep.equal([{ id: "0", title: "hello" }]); }); }); - describe('filterByTailingSlash', () => { - it('filters duplicated pathname on tailing slash', () => { + describe("filterByTailingSlash", () => { + it("filters duplicated pathname on tailing slash", () => { const pages = [ - { id: '0', url: 'http://i-beam.org/content' }, - { id: '1', url: 'http://i-beam.org/content/' }, - { id: '2', url: 'http://i-beam.org/search' }, - { id: '3', url: 'http://i-beam.org/search?q=apple_banana_cherry' }, + { id: "0", url: "http://i-beam.org/content" }, + { id: "1", url: "http://i-beam.org/content/" }, + { id: "2", url: "http://i-beam.org/search" }, + { id: "3", url: "http://i-beam.org/search?q=apple_banana_cherry" }, ]; const filtered = filters.filterByTailingSlash(pages); - const urls = filtered.map(x => x.url); + 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', + "http://i-beam.org/content", + "http://i-beam.org/search", + "http://i-beam.org/search?q=apple_banana_cherry", ]); }); - }) + }); - describe('filterByPathname', () => { - it('filters by length of pathname', () => { + describe("filterByPathname", () => { + it("filters by length of pathname", () => { const pages = [ - { id: '0', url: 'http://i-beam.org/search?q=apple' }, - { id: '1', url: 'http://i-beam.org/search?q=apple_banana' }, - { id: '2', url: 'http://i-beam.org/search?q=apple_banana_cherry' }, - { id: '3', url: 'http://i-beam.net/search?q=apple' }, - { id: '4', url: 'http://i-beam.net/search?q=apple_banana' }, - { id: '5', url: 'http://i-beam.net/search?q=apple_banana_cherry' }, + { id: "0", url: "http://i-beam.org/search?q=apple" }, + { id: "1", url: "http://i-beam.org/search?q=apple_banana" }, + { id: "2", url: "http://i-beam.org/search?q=apple_banana_cherry" }, + { id: "3", url: "http://i-beam.net/search?q=apple" }, + { id: "4", url: "http://i-beam.net/search?q=apple_banana" }, + { id: "5", url: "http://i-beam.net/search?q=apple_banana_cherry" }, ]; const filtered = filters.filterByPathname(pages); expect(filtered).to.deep.equal([ - { id: '0', url: 'http://i-beam.org/search?q=apple' }, - { id: '3', url: 'http://i-beam.net/search?q=apple' }, + { id: "0", url: "http://i-beam.org/search?q=apple" }, + { id: "3", url: "http://i-beam.net/search?q=apple" }, ]); }); }); - describe('filterByOrigin', () => { - it('filters by length of pathname', () => { + describe("filterByOrigin", () => { + it("filters by length of pathname", () => { const pages = [ - { id: '0', url: 'http://i-beam.org/search?q=apple' }, - { id: '1', url: 'http://i-beam.org/search?q=apple_banana' }, - { id: '2', url: 'http://i-beam.org/search?q=apple_banana_cherry' }, - { id: '3', url: 'http://i-beam.org/request?q=apple' }, - { id: '4', url: 'http://i-beam.org/request?q=apple_banana' }, - { id: '5', url: 'http://i-beam.org/request?q=apple_banana_cherry' }, + { id: "0", url: "http://i-beam.org/search?q=apple" }, + { id: "1", url: "http://i-beam.org/search?q=apple_banana" }, + { id: "2", url: "http://i-beam.org/search?q=apple_banana_cherry" }, + { id: "3", url: "http://i-beam.org/request?q=apple" }, + { id: "4", url: "http://i-beam.org/request?q=apple_banana" }, + { id: "5", url: "http://i-beam.org/request?q=apple_banana_cherry" }, ]; const filtered = filters.filterByOrigin(pages); expect(filtered).to.deep.equal([ - { id: '0', url: 'http://i-beam.org/search?q=apple' }, + { id: "0", url: "http://i-beam.org/search?q=apple" }, ]); }); }); diff --git a/test/background/infrastructures/MemoryStorage.test.ts b/test/background/infrastructures/MemoryStorage.test.ts index ccdf9f0..5f8be2a 100644 --- a/test/background/infrastructures/MemoryStorage.test.ts +++ b/test/background/infrastructures/MemoryStorage.test.ts @@ -1,44 +1,44 @@ -import MemoryStorage from 'background/infrastructures/MemoryStorage'; +import MemoryStorage from "background/infrastructures/MemoryStorage"; describe("background/infrastructures/memory-storage", () => { - it('stores values', () => { + it("stores values", () => { const cache = new MemoryStorage(); - cache.set('number', 123); - expect(cache.get('number')).to.equal(123); + cache.set("number", 123); + expect(cache.get("number")).to.equal(123); - cache.set('string', '123'); - expect(cache.get('string')).to.equal('123'); + cache.set("string", "123"); + expect(cache.get("string")).to.equal("123"); - cache.set('object', { hello: '123' }); - expect(cache.get('object')).to.deep.equal({ hello: '123' }); + cache.set("object", { hello: "123" }); + expect(cache.get("object")).to.deep.equal({ hello: "123" }); }); - it('returns undefined if no keys', () => { + it("returns undefined if no keys", () => { const cache = new MemoryStorage(); - expect(cache.get('no-keys')).to.be.undefined; - }) + expect(cache.get("no-keys")).to.be.undefined; + }); - it('stored on shared memory', () => { + it("stored on shared memory", () => { let cache = new MemoryStorage(); - cache.set('red', 'apple'); + cache.set("red", "apple"); cache = new MemoryStorage(); - const got = cache.get('red'); - expect(got).to.equal('apple'); + const got = cache.get("red"); + expect(got).to.equal("apple"); }); - it('stored cloned objects', () => { + it("stored cloned objects", () => { const cache = new MemoryStorage(); - const recipe = { sugar: '300g' }; - cache.set('recipe', recipe); + const recipe = { sugar: "300g" }; + cache.set("recipe", recipe); - recipe.salt = '20g' - const got = cache.get('recipe', recipe); - expect(got).to.deep.equal({ sugar: '300g' }); + recipe.salt = "20g"; + const got = cache.get("recipe", recipe); + expect(got).to.deep.equal({ sugar: "300g" }); }); - it('throws an error with unserializable objects', () => { + it("throws an error with unserializable objects", () => { const cache = new MemoryStorage(); - expect(() => cache.set('fn', setTimeout)).to.throw(); - }) + expect(() => cache.set("fn", setTimeout)).to.throw(); + }); }); diff --git a/test/background/repositories/Mark.test.ts b/test/background/repositories/Mark.test.ts index ed1a68e..b1bbcf1 100644 --- a/test/background/repositories/Mark.test.ts +++ b/test/background/repositories/Mark.test.ts @@ -1,24 +1,24 @@ -import MarkRepository from 'background/repositories/MarkRepository'; +import MarkRepository from "background/repositories/MarkRepository"; -describe('background/repositories/mark', () => { +describe("background/repositories/mark", () => { let repository: MarkRepository; beforeEach(() => { repository = new MarkRepository(); }); - it('get and set', async() => { - const mark = { tabId: 1, url: 'http://example.com', x: 10, y: 30 }; + it("get and set", async () => { + const mark = { tabId: 1, url: "http://example.com", x: 10, y: 30 }; - repository.setMark('A', mark); + repository.setMark("A", mark); - let got = await repository.getMark('A'); + let got = await repository.getMark("A"); expect(got.tabId).to.equal(1); - expect(got.url).to.equal('http://example.com'); + expect(got.url).to.equal("http://example.com"); expect(got.x).to.equal(10); expect(got.y).to.equal(30); - got = await repository.getMark('B'); + got = await repository.getMark("B"); expect(got).to.be.undefined; }); }); diff --git a/test/background/usecases/NavigateUseCase.test.ts b/test/background/usecases/NavigateUseCase.test.ts index 7ad0e4f..7263627 100644 --- a/test/background/usecases/NavigateUseCase.test.ts +++ b/test/background/usecases/NavigateUseCase.test.ts @@ -1,8 +1,8 @@ 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 * as sinon from 'sinon'; +import TabPresenter from "../../../src/background/presenters/TabPresenter"; +import NavigateUseCase from "../../../src/background/usecases/NavigateUseCase"; +import NavigateClient from "../../../src/background/clients/NavigateClient"; +import * as sinon from "sinon"; class MockTabPresenter implements TabPresenter { create(_url: string, _opts?: object): Promise { @@ -17,7 +17,10 @@ class MockTabPresenter implements TabPresenter { throw new Error("not implemented"); } - getByKeyword(_keyword: string, _excludePinned: boolean): Promise { + getByKeyword( + _keyword: string, + _excludePinned: boolean + ): Promise { throw new Error("not implemented"); } @@ -33,7 +36,9 @@ class MockTabPresenter implements TabPresenter { throw new Error("not implemented"); } - onSelected(_listener: (arg: { tabId: number; windowId: number }) => void): void { + onSelected( + _listener: (arg: { tabId: number; windowId: number }) => void + ): void { throw new Error("not implemented"); } @@ -66,7 +71,7 @@ class MockTabPresenter implements TabPresenter { } } -describe('NavigateUseCase', () => { +describe("NavigateUseCase", () => { let sut: NavigateUseCase; let tabPresenter: TabPresenter; let navigateClient: NavigateClient; @@ -80,7 +85,7 @@ describe('NavigateUseCase', () => { const newTab = (url: string): browser.tabs.Tab => { return { index: 0, - title: 'dummy title', + title: "dummy title", url: url, active: true, hidden: false, @@ -91,53 +96,67 @@ describe('NavigateUseCase', () => { lastAccessed: 1585446733000, pinned: false, selected: false, - windowId: 0 + windowId: 0, }; }; - describe('#openParent()', async () => { - it('opens parent directory of file', async() => { - sinon.stub(tabPresenter, 'getCurrent') - .returns(Promise.resolve(newTab('https://google.com/fruits/yellow/banana'))); + describe("#openParent()", async () => { + it("opens parent directory of file", async () => { + sinon + .stub(tabPresenter, "getCurrent") + .returns( + Promise.resolve(newTab("https://google.com/fruits/yellow/banana")) + ); - const mock = sinon.mock(tabPresenter) - .expects('open').withArgs('https://google.com/fruits/yellow/'); + const mock = sinon + .mock(tabPresenter) + .expects("open") + .withArgs("https://google.com/fruits/yellow/"); await sut.openParent(); mock.verify(); }); - it('opens parent directory of directory', async() => { - sinon.stub(tabPresenter, 'getCurrent') - .returns(Promise.resolve(newTab('https://google.com/fruits/yellow/'))); + it("opens parent directory of directory", async () => { + sinon + .stub(tabPresenter, "getCurrent") + .returns(Promise.resolve(newTab("https://google.com/fruits/yellow/"))); - const mock = sinon.mock(tabPresenter) - .expects('open').withArgs('https://google.com/fruits/'); + const mock = sinon + .mock(tabPresenter) + .expects("open") + .withArgs("https://google.com/fruits/"); await sut.openParent(); mock.verify(); }); - it('removes hash', async() => { - sinon.stub(tabPresenter, 'getCurrent') - .returns(Promise.resolve(newTab('https://google.com/#top'))); + it("removes hash", async () => { + sinon + .stub(tabPresenter, "getCurrent") + .returns(Promise.resolve(newTab("https://google.com/#top"))); - const mock = sinon.mock(tabPresenter) - .expects('open').withArgs('https://google.com/'); + const mock = sinon + .mock(tabPresenter) + .expects("open") + .withArgs("https://google.com/"); await sut.openParent(); mock.verify(); }); - it('removes search query', async() => { - sinon.stub(tabPresenter, 'getCurrent') - .returns(Promise.resolve(newTab('https://google.com/search?q=apple'))); + it("removes search query", async () => { + sinon + .stub(tabPresenter, "getCurrent") + .returns(Promise.resolve(newTab("https://google.com/search?q=apple"))); - const mock = sinon.mock(tabPresenter) - .expects('open').withArgs('https://google.com/search'); + const mock = sinon + .mock(tabPresenter) + .expects("open") + .withArgs("https://google.com/search"); await sut.openParent(); @@ -145,13 +164,16 @@ describe('NavigateUseCase', () => { }); }); - describe('#openRoot()', () => { - it('opens root direectory', async() => { - sinon.stub(tabPresenter, 'getCurrent') - .returns(Promise.resolve(newTab('https://google.com/seach?q=apple'))); + describe("#openRoot()", () => { + it("opens root direectory", async () => { + sinon + .stub(tabPresenter, "getCurrent") + .returns(Promise.resolve(newTab("https://google.com/seach?q=apple"))); - const mock = sinon.mock(tabPresenter) - .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/SettingUseCase.test.ts b/test/background/usecases/SettingUseCase.test.ts index bfa599c..c604e91 100644 --- a/test/background/usecases/SettingUseCase.test.ts +++ b/test/background/usecases/SettingUseCase.test.ts @@ -1,21 +1,22 @@ import "reflect-metadata"; import SettingUseCase from "../../../src/background/usecases/SettingUseCase"; import SettingRepository from "../../../src/background/repositories/SettingRepository"; -import SettingData, {JSONTextSettings} from "../../../src/shared/SettingData"; +import SettingData, { JSONTextSettings } from "../../../src/shared/SettingData"; import CachedSettingRepository from "../../../src/background/repositories/CachedSettingRepository"; -import Settings, {DefaultSetting} from "../../../src/shared/settings/Settings"; +import Settings, { + DefaultSetting, +} from "../../../src/shared/settings/Settings"; import Notifier from "../../../src/background/presenters/Notifier"; -import {expect} from "chai"; +import { expect } from "chai"; import Properties from "../../../src/shared/settings/Properties"; -import sinon from 'sinon'; +import sinon from "sinon"; class MockSettingRepository implements SettingRepository { load(): Promise { throw new Error("not implemented"); } - onChange(_: () => void): void { - } + onChange(_: () => void): void {} } class MockCachedSettingRepository implements CachedSettingRepository { @@ -46,12 +47,12 @@ class NopNotifier implements Notifier { } } -describe('SettingUseCase', () => { - let localSettingRepository : SettingRepository; - let syncSettingRepository : SettingRepository; - let cachedSettingRepository : CachedSettingRepository; +describe("SettingUseCase", () => { + let localSettingRepository: SettingRepository; + let syncSettingRepository: SettingRepository; + let cachedSettingRepository: CachedSettingRepository; let notifier: Notifier; - let sut : SettingUseCase; + let sut: SettingUseCase; beforeEach(() => { localSettingRepository = new MockSettingRepository(); @@ -66,34 +67,34 @@ describe('SettingUseCase', () => { ); }); - describe('getCached', () => { + describe("getCached", () => { it("returns cached settings", async () => { const settings = new Settings({ keymaps: DefaultSetting.keymaps, search: DefaultSetting.search, blacklist: DefaultSetting.blacklist, properties: new Properties({ - hintchars: "abcd1234" + hintchars: "abcd1234", }), }); - sinon.stub(cachedSettingRepository, "get") + sinon + .stub(cachedSettingRepository, "get") .returns(Promise.resolve(settings)); const got = await sut.getCached(); expect(got.properties.hintchars).to.equal("abcd1234"); - }); }); describe("reload", () => { context("when sync is not set", () => { - it("loads settings from local storage", async() => { + it("loads settings from local storage", async () => { const settings = new Settings({ keymaps: DefaultSetting.keymaps, search: DefaultSetting.search, blacklist: DefaultSetting.blacklist, properties: new Properties({ - hintchars: "abcd1234" + hintchars: "abcd1234", }), }); const settingData = SettingData.fromJSON({ @@ -101,9 +102,11 @@ describe('SettingUseCase', () => { json: JSONTextSettings.fromSettings(settings).toJSONText(), }); - sinon.stub(syncSettingRepository, "load") + sinon + .stub(syncSettingRepository, "load") .returns(Promise.resolve(null)); - sinon.stub(localSettingRepository, "load") + sinon + .stub(localSettingRepository, "load") .returns(Promise.resolve(settingData)); await sut.reload(); @@ -114,13 +117,13 @@ describe('SettingUseCase', () => { }); context("when local is not set", () => { - it("loads settings from sync storage", async() => { + it("loads settings from sync storage", async () => { const settings = new Settings({ keymaps: DefaultSetting.keymaps, search: DefaultSetting.search, blacklist: DefaultSetting.blacklist, properties: new Properties({ - hintchars: "aaaa1111" + hintchars: "aaaa1111", }), }); const settingData = SettingData.fromJSON({ @@ -128,9 +131,11 @@ describe('SettingUseCase', () => { json: JSONTextSettings.fromSettings(settings).toJSONText(), }); - sinon.stub(syncSettingRepository, "load") + sinon + .stub(syncSettingRepository, "load") .returns(Promise.resolve(settingData)); - sinon.stub(localSettingRepository, "load") + sinon + .stub(localSettingRepository, "load") .returns(Promise.resolve(null)); await sut.reload(); @@ -141,21 +146,23 @@ describe('SettingUseCase', () => { }); context("neither local nor sync not set", () => { - it("loads default settings", async() => { - it("loads settings from sync storage", async() => { - sinon.stub(syncSettingRepository, "load") + it("loads default settings", async () => { + it("loads settings from sync storage", async () => { + sinon + .stub(syncSettingRepository, "load") .returns(Promise.resolve(null)); - sinon.stub(localSettingRepository, "load") + 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); + expect(current.properties.hintchars).to.equal( + DefaultSetting.properties.hintchars + ); }); - - }) - }) - }) + }); + }); + }); }); - diff --git a/test/background/usecases/parsers.test.ts b/test/background/usecases/parsers.test.ts index d08de0d..bee0d07 100644 --- a/test/background/usecases/parsers.test.ts +++ b/test/background/usecases/parsers.test.ts @@ -1,34 +1,43 @@ -import * as parsers from 'background/usecases/parsers'; +import * as parsers from "background/usecases/parsers"; 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'); + it("parse set string", () => { + const [key, value] = parsers.parseSetOption("hintchars=abcdefgh"); + expect(key).to.equal("hintchars"); + expect(value).to.equal("abcdefgh"); }); - it('parse set empty string', () => { - const [key, value] = parsers.parseSetOption('hintchars='); - expect(key).to.equal('hintchars'); - expect(value).to.equal(''); + it("parse set empty string", () => { + const [key, value] = parsers.parseSetOption("hintchars="); + expect(key).to.equal("hintchars"); + expect(value).to.equal(""); }); - it('parse set boolean', () => { - let [key, value] = parsers.parseSetOption('smoothscroll'); - expect(key).to.equal('smoothscroll'); + it("parse set boolean", () => { + let [key, value] = parsers.parseSetOption("smoothscroll"); + expect(key).to.equal("smoothscroll"); expect(value).to.be.true; - [key, value] = parsers.parseSetOption('nosmoothscroll'); - expect(key).to.equal('smoothscroll'); + [key, value] = parsers.parseSetOption("nosmoothscroll"); + expect(key).to.equal("smoothscroll"); expect(value).to.be.false; }); - it('throws error on unknown property', () => { - expect(() => parsers.parseSetOption('encoding=utf-8')).to.throw(Error, '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, 'Invalid argument'); + it("throws error on unknown property", () => { + expect(() => parsers.parseSetOption("encoding=utf-8")).to.throw( + Error, + "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, + "Invalid argument" + ); }); }); }); -- cgit v1.2.3 From 20d40d8017284f80876bde8e28cbde47c3651886 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Mon, 13 Apr 2020 20:42:02 +0900 Subject: fix test --- test/background/completion/OpenCompletionUseCase.test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'test/background') diff --git a/test/background/completion/OpenCompletionUseCase.test.ts b/test/background/completion/OpenCompletionUseCase.test.ts index a0d8b44..f43e6c1 100644 --- a/test/background/completion/OpenCompletionUseCase.test.ts +++ b/test/background/completion/OpenCompletionUseCase.test.ts @@ -91,7 +91,7 @@ describe("OpenCompletionUseCase", () => { google: "https://google.com/search?q={}", yahoo: "https://search.yahoo.com/search?q={}", bing: "https://bing.com/search?q={}", - google_ja: "https://google.co.jp/search?q={}", + googleja: "https://google.co.jp/search?q={}", }), properties: DefaultSetting.properties, blacklist: DefaultSetting.blacklist, @@ -103,11 +103,11 @@ describe("OpenCompletionUseCase", () => { "google", "yahoo", "bing", - "google_ja", + "googleja", ]); expect(await sut.requestSearchEngines("go")).to.deep.equal([ "google", - "google_ja", + "googleja", ]); expect(await sut.requestSearchEngines("x")).to.be.empty; }); -- cgit v1.2.3 From b0fe06bc2e739cc252a559f666da65b27769959d Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Wed, 15 Apr 2020 19:35:15 +0900 Subject: Fix types in tests --- .../infrastructures/MemoryStorage.test.ts | 9 +-- test/background/repositories/Mark.test.ts | 9 +-- test/background/usecases/parsers.test.ts | 3 +- test/console/actions/console.test.ts | 8 +++ .../console/components/console/Completion.test.tsx | 50 ++++++++------ test/console/reducers/console.test.ts | 76 ++++++++++++++-------- test/content/InputDriver.test.ts | 2 - test/content/presenters/Hint.test.ts | 10 +-- .../content/presenters/NavigationPresenter.test.ts | 8 ++- test/content/usecases/SettingUseCaase.test.ts | 4 +- test/main.ts | 5 -- .../components/form/BlacklistForm.test.tsx | 38 +++++++---- test/settings/components/form/KeymapsForm.test.tsx | 9 +-- .../components/form/PropertiesForm.test.tsx | 18 +++-- .../components/form/SearchEngineForm.test.tsx | 24 ++++--- test/settings/components/ui/input.test.tsx | 32 ++++----- test/settings/reducers/setting.test.ts | 51 ++++++++------- test/shared/operations.test.ts | 13 ++-- 18 files changed, 221 insertions(+), 148 deletions(-) (limited to 'test/background') diff --git a/test/background/infrastructures/MemoryStorage.test.ts b/test/background/infrastructures/MemoryStorage.test.ts index 5f8be2a..1c67b18 100644 --- a/test/background/infrastructures/MemoryStorage.test.ts +++ b/test/background/infrastructures/MemoryStorage.test.ts @@ -1,4 +1,5 @@ -import MemoryStorage from "background/infrastructures/MemoryStorage"; +import MemoryStorage from "../../../src/background/infrastructures/MemoryStorage"; +import { expect } from "chai"; describe("background/infrastructures/memory-storage", () => { it("stores values", () => { @@ -29,12 +30,12 @@ describe("background/infrastructures/memory-storage", () => { it("stored cloned objects", () => { const cache = new MemoryStorage(); - const recipe = { sugar: "300g" }; + const recipe = { sugar: "300g", salt: "10g" }; cache.set("recipe", recipe); recipe.salt = "20g"; - const got = cache.get("recipe", recipe); - expect(got).to.deep.equal({ sugar: "300g" }); + const got = cache.get("recipe"); + expect(got).to.deep.equal({ sugar: "300g", salt: "10g" }); }); it("throws an error with unserializable objects", () => { diff --git a/test/background/repositories/Mark.test.ts b/test/background/repositories/Mark.test.ts index b1bbcf1..5cee5b6 100644 --- a/test/background/repositories/Mark.test.ts +++ b/test/background/repositories/Mark.test.ts @@ -1,4 +1,5 @@ -import MarkRepository from "background/repositories/MarkRepository"; +import MarkRepository from "../../../src/background/repositories/MarkRepository"; +import { expect } from "chai"; describe("background/repositories/mark", () => { let repository: MarkRepository; @@ -10,15 +11,15 @@ describe("background/repositories/mark", () => { it("get and set", async () => { const mark = { tabId: 1, url: "http://example.com", x: 10, y: 30 }; - repository.setMark("A", mark); + await repository.setMark("A", mark); - let got = await repository.getMark("A"); + let got = (await repository.getMark("A"))!!; expect(got.tabId).to.equal(1); expect(got.url).to.equal("http://example.com"); expect(got.x).to.equal(10); expect(got.y).to.equal(30); - got = await repository.getMark("B"); + got = (await repository.getMark("B"))!!; expect(got).to.be.undefined; }); }); diff --git a/test/background/usecases/parsers.test.ts b/test/background/usecases/parsers.test.ts index bee0d07..019b56e 100644 --- a/test/background/usecases/parsers.test.ts +++ b/test/background/usecases/parsers.test.ts @@ -1,4 +1,5 @@ -import * as parsers from "background/usecases/parsers"; +import * as parsers from "../../../src/background/usecases/parsers"; +import { expect } from "chai"; describe("shared/commands/parsers", () => { describe("#parsers.parseSetOption", () => { diff --git a/test/console/actions/console.test.ts b/test/console/actions/console.test.ts index 5a531a6..3169c4b 100644 --- a/test/console/actions/console.test.ts +++ b/test/console/actions/console.test.ts @@ -2,7 +2,15 @@ import * as actions from "../../../src/console/actions"; import * as consoleActions from "../../../src/console/actions/console"; import { expect } from "chai"; +// eslint-disable-next-line @typescript-eslint/ban-ts-ignore +// @ts-ignore +import browserFake from "webextensions-api-fake"; + describe("console actions", () => { + beforeEach(() => { + (global as any).browser = browserFake(); + }); + describe("hide", () => { it("create CONSOLE_HIDE action", () => { const action = consoleActions.hide(); diff --git a/test/console/components/console/Completion.test.tsx b/test/console/components/console/Completion.test.tsx index 200bb1a..921720b 100644 --- a/test/console/components/console/Completion.test.tsx +++ b/test/console/components/console/Completion.test.tsx @@ -1,6 +1,9 @@ import React from "react"; -import Completion from "console/components/console/Completion"; -import ReactTestRenderer from "react-test-renderer"; +import Completion from "../../../../src/console/components/console/Completion"; +import ReactTestRenderer, { ReactTestInstance } from "react-test-renderer"; +import { expect } from "chai"; +import CompletionTitle from "../../../../src/console/components/console/CompletionTitle"; +import CompletionItem from "../../../../src/console/components/console/CompletionItem"; describe("console/components/console/completion", () => { const completions = [ @@ -24,13 +27,22 @@ describe("console/components/console/completion", () => { it("renders Completion component", () => { const root = ReactTestRenderer.create( - + ).root; - expect(root.children).to.have.lengthOf(1); - - const children = root.children[0].children; + // const children = root.findByType('ul').children as Array; + const children = root.findByType("ul").children as Array; expect(children).to.have.lengthOf(8); + expect(children.map((e) => e.type)).to.deep.equal([ + CompletionTitle, + CompletionItem, + CompletionItem, + CompletionItem, + CompletionTitle, + CompletionItem, + CompletionItem, + CompletionItem, + ]); expect(children[0].props.title).to.equal("Fruit"); expect(children[1].props.caption).to.equal("apple"); expect(children[2].props.caption).to.equal("banana"); @@ -46,7 +58,7 @@ describe("console/components/console/completion", () => { ).root; - const children = root.children[0].children; + const children = root.findByType("ul").children as Array; expect(children[5].props.highlight).to.be.true; }); @@ -55,10 +67,8 @@ describe("console/components/console/completion", () => { ).root; - const children = root.children[0].children; - for (const li of children[0].children) { - expect(li.props.highlight).not.to.be.ok; - } + const children = root.findByType("ul").findAllByType(CompletionItem); + expect(children.every((e) => e.props.highlight === false)).to.be.true; }); it("limits completion items", () => { @@ -66,7 +76,7 @@ describe("console/components/console/completion", () => { ).root; - let children = root.children[0].children; + let children = root.findByType("ul").children as Array; expect(children).to.have.lengthOf(3); expect(children[0].props.title).to.equal("Fruit"); @@ -77,7 +87,7 @@ describe("console/components/console/completion", () => { ).root; - children = root.children[0].children; + children = root.findByType("ul").children as Array; expect(children[1].props.highlight).to.be.true; }); @@ -87,7 +97,7 @@ describe("console/components/console/completion", () => { ); const root = component.root; - let children = root.children[0].children; + let children = root.findByType("ul").children as Array; expect(children).to.have.lengthOf(3); expect(children[0].props.title).to.equal("Fruit"); expect(children[1].props.caption).to.equal("apple"); @@ -97,7 +107,7 @@ describe("console/components/console/completion", () => { ); - children = root.children[0].children; + children = root.findByType("ul").children as Array; expect(children).to.have.lengthOf(3); expect(children[0].props.caption).to.equal("apple"); expect(children[1].props.caption).to.equal("banana"); @@ -108,7 +118,7 @@ describe("console/components/console/completion", () => { ); - children = root.children[0].children; + children = root.findByType("ul").children as Array; expect(children).to.have.lengthOf(3); expect(children[0].props.caption).to.equal("cherry"); expect(children[1].props.title).to.equal("Element"); @@ -122,7 +132,7 @@ describe("console/components/console/completion", () => { ); const root = component.root; - let children = root.children[0].children; + let children = root.findByType("ul").children as Array; expect(children).to.have.lengthOf(3); expect(children[0].props.caption).to.equal("argon"); expect(children[1].props.caption).to.equal("boron"); @@ -132,21 +142,21 @@ describe("console/components/console/completion", () => { ); - children = root.children[0].children; + children = root.findByType("ul").children as Array; expect(children[1].props.highlight).to.be.true; component.update( ); - children = root.children[0].children; + children = root.findByType("ul").children as Array; expect(children[0].props.highlight).to.be.true; component.update( ); - children = root.children[0].children; + children = root.findByType("ul").children as Array; expect(children[0].props.caption).to.equal("cherry"); expect(children[1].props.title).to.equal("Element"); expect(children[2].props.caption).to.equal("argon"); diff --git a/test/console/reducers/console.test.ts b/test/console/reducers/console.test.ts index e8432c5..64e8eb3 100644 --- a/test/console/reducers/console.test.ts +++ b/test/console/reducers/console.test.ts @@ -1,9 +1,12 @@ -import * as actions from "console/actions"; -import reducer from "console/reducers"; +import * as actions from "../../../src/console/actions"; +import reducer, { State } from "../../../src/console/reducers"; +import { expect } from "chai"; +import CompletionType from "../../../src/shared/CompletionType"; +import { ConsoleAction } from "../../../src/console/actions"; describe("console reducer", () => { it("return the initial state", () => { - const state = reducer(undefined, {}); + const state = reducer(undefined, {} as any); expect(state).to.have.property("mode", ""); expect(state).to.have.property("messageText", ""); expect(state).to.have.property("consoleText", ""); @@ -12,68 +15,85 @@ describe("console reducer", () => { }); it("return next state for CONSOLE_HIDE", () => { - const action = { type: actions.CONSOLE_HIDE }; - const state = reducer({ mode: "error" }, action); + const initialState = reducer(undefined, {} as any); + const action: actions.ConsoleAction = { type: actions.CONSOLE_HIDE }; + const state = reducer({ ...initialState, mode: "error" }, action); expect(state).to.have.property("mode", ""); }); it("return next state for CONSOLE_SHOW_COMMAND", () => { - const action = { type: actions.CONSOLE_SHOW_COMMAND, text: "open " }; - const state = reducer({}, action); + const action: actions.ConsoleAction = { + type: actions.CONSOLE_SHOW_COMMAND, + completionTypes: [CompletionType.SearchEngines, CompletionType.History], + text: "open ", + }; + const state = reducer(undefined, action); expect(state).to.have.property("mode", "command"); expect(state).to.have.property("consoleText", "open "); }); it("return next state for CONSOLE_SHOW_INFO", () => { - const action = { type: actions.CONSOLE_SHOW_INFO, text: "an info" }; - const state = reducer({}, action); + const action: actions.ConsoleAction = { + type: actions.CONSOLE_SHOW_INFO, + text: "an info", + }; + const state = reducer(undefined, action); expect(state).to.have.property("mode", "info"); expect(state).to.have.property("messageText", "an info"); }); it("return next state for CONSOLE_SHOW_ERROR", () => { - const action = { type: actions.CONSOLE_SHOW_ERROR, text: "an error" }; - const state = reducer({}, action); + const action: actions.ConsoleAction = { + type: actions.CONSOLE_SHOW_ERROR, + text: "an error", + }; + const state = reducer(undefined, action); expect(state).to.have.property("mode", "error"); expect(state).to.have.property("messageText", "an error"); }); it("return next state for CONSOLE_HIDE_COMMAND", () => { - const action = { type: actions.CONSOLE_HIDE_COMMAND }; - let state = reducer({ mode: "command" }, action); + const initialState = reducer(undefined, {} as any); + const action: actions.ConsoleAction = { + type: actions.CONSOLE_HIDE_COMMAND, + }; + let state = reducer({ ...initialState, mode: "command" }, action); expect(state).to.have.property("mode", ""); - state = reducer({ mode: "error" }, action); + state = reducer({ ...initialState, mode: "error" }, action); expect(state).to.have.property("mode", "error"); }); it("return next state for CONSOLE_SET_CONSOLE_TEXT", () => { - const action = { + const action: actions.ConsoleAction = { type: actions.CONSOLE_SET_CONSOLE_TEXT, consoleText: "hello world", }; - const state = reducer({}, action); + const state = reducer(undefined, action); expect(state).to.have.property("consoleText", "hello world"); }); it("return next state for CONSOLE_SET_COMPLETIONS", () => { - let state = { + const initialState = reducer(undefined, {} as any); + let state: State = { + ...initialState, select: 0, completions: [], }; - const action = { + const action: actions.ConsoleAction = { type: actions.CONSOLE_SET_COMPLETIONS, completions: [ { name: "Apple", - items: [1, 2, 3], + items: [{}, {}, {}], }, { name: "Banana", - items: [4, 5, 6], + items: [{}, {}, {}], }, ], + completionSource: "", }; state = reducer(state, action); expect(state).to.have.property("completions", action.completions); @@ -81,17 +101,19 @@ describe("console reducer", () => { }); it("return next state for CONSOLE_COMPLETION_NEXT", () => { - const action = { type: actions.CONSOLE_COMPLETION_NEXT }; + const initialState = reducer(undefined, {} as any); + const action: ConsoleAction = { type: actions.CONSOLE_COMPLETION_NEXT }; let state = { + ...initialState, select: -1, completions: [ { name: "Apple", - items: [1, 2], + items: [{}, {}], }, { name: "Banana", - items: [3], + items: [{}], }, ], }; @@ -110,17 +132,19 @@ describe("console reducer", () => { }); it("return next state for CONSOLE_COMPLETION_PREV", () => { - const action = { type: actions.CONSOLE_COMPLETION_PREV }; + const initialState = reducer(undefined, {} as any); + const action: ConsoleAction = { type: actions.CONSOLE_COMPLETION_PREV }; let state = { + ...initialState, select: -1, completions: [ { name: "Apple", - items: [1, 2], + items: [{}, {}], }, { name: "Banana", - items: [3], + items: [{}], }, ], }; diff --git a/test/content/InputDriver.test.ts b/test/content/InputDriver.test.ts index bfada87..f464dac 100644 --- a/test/content/InputDriver.test.ts +++ b/test/content/InputDriver.test.ts @@ -16,8 +16,6 @@ describe("InputDriver", () => { afterEach(() => { target.remove(); - target = null; - driver = null; }); it("register callbacks", (done) => { diff --git a/test/content/presenters/Hint.test.ts b/test/content/presenters/Hint.test.ts index e64c39f..f961f88 100644 --- a/test/content/presenters/Hint.test.ts +++ b/test/content/presenters/Hint.test.ts @@ -13,17 +13,17 @@ describe("Hint", () => { describe("#constructor", () => { it("creates a hint element with tag name", () => { - const link = document.getElementById("test-link"); + const link = document.getElementById("test-link")!!; new Hint(link, "abc"); const elem = document.querySelector(".vimvixen-hint"); - expect(elem.textContent.trim()).to.be.equal("abc"); + expect(elem!!.textContent!!.trim()).to.be.equal("abc"); }); }); describe("#show", () => { it("shows an element", () => { - const link = document.getElementById("test-link"); + const link = document.getElementById("test-link")!!; const hint = new Hint(link, "abc"); hint.hide(); hint.show(); @@ -46,10 +46,10 @@ describe("Hint", () => { describe("#remove", () => { it("removes an element", () => { - const link = document.getElementById("test-link"); + const link = document.getElementById("test-link")!!; const hint = new Hint(link, "abc"); - const elem = document.querySelector(".vimvixen-hint"); + const elem = document.querySelector(".vimvixen-hint")!!; expect(elem.parentElement).to.not.be.null; hint.remove(); expect(elem.parentElement).to.be.null; diff --git a/test/content/presenters/NavigationPresenter.test.ts b/test/content/presenters/NavigationPresenter.test.ts index b362d8d..af3b487 100644 --- a/test/content/presenters/NavigationPresenter.test.ts +++ b/test/content/presenters/NavigationPresenter.test.ts @@ -4,7 +4,7 @@ import { expect } from "chai"; describe("NavigationPresenterImpl", () => { let sut: NavigationPresenterImpl; - const testRel = (done, rel, html) => { + const testRel = (done: () => void, rel: string, html: string) => { const method = rel === "prev" ? sut.openLinkPrev.bind(sut) : sut.openLinkNext.bind(sut); document.body.innerHTML = html; @@ -14,8 +14,10 @@ describe("NavigationPresenterImpl", () => { done(); }, 0); }; - const testPrev = (html) => (done) => testRel(done, "prev", html); - const testNext = (html) => (done) => testRel(done, "next", html); + const testPrev = (html: string) => (done: () => void) => + testRel(done, "prev", html); + const testNext = (html: string) => (done: () => void) => + testRel(done, "next", html); before(() => { sut = new NavigationPresenterImpl(); diff --git a/test/content/usecases/SettingUseCaase.test.ts b/test/content/usecases/SettingUseCaase.test.ts index 19a167a..1cc1e8a 100644 --- a/test/content/usecases/SettingUseCaase.test.ts +++ b/test/content/usecases/SettingUseCaase.test.ts @@ -40,7 +40,7 @@ describe("AddonEnabledUseCase", () => { let sut: SettingUseCase; beforeEach(() => { - const testSettings = { + const testSettings = Settings.fromJSON({ keymaps: {}, search: { default: "google", @@ -54,7 +54,7 @@ describe("AddonEnabledUseCase", () => { complete: "sbh", }, blacklist: [], - }; + }); repository = new MockSettingRepository(); client = new MockSettingClient(testSettings); diff --git a/test/main.ts b/test/main.ts index 8097975..fded23a 100644 --- a/test/main.ts +++ b/test/main.ts @@ -1,6 +1 @@ import "reflect-metadata"; -import { expect } from "chai"; -import browserFake from "webextensions-api-fake"; - -global.expect = expect; -global.browser = browserFake(); diff --git a/test/settings/components/form/BlacklistForm.test.tsx b/test/settings/components/form/BlacklistForm.test.tsx index 4d79383..e34802a 100644 --- a/test/settings/components/form/BlacklistForm.test.tsx +++ b/test/settings/components/form/BlacklistForm.test.tsx @@ -6,6 +6,7 @@ import { expect } from "chai"; import BlacklistForm from "../../../../src/settings/components/form/BlacklistForm"; import Blacklist from "../../../../src/shared/settings/Blacklist"; +import AddButton from "../../../../src/settings/components/ui/AddButton"; describe("settings/form/BlacklistForm", () => { describe("render", () => { @@ -16,26 +17,32 @@ describe("settings/form/BlacklistForm", () => { /> ).root; - const children = root.children[0].children; - expect(children).to.have.lengthOf(3); - expect(children[0].children[0].props.value).to.equal("*.slack.com"); - expect(children[1].children[0].props.value).to.equal( - "www.google.com/maps" - ); - expect(children[2].props.name).to.equal("add"); + const rows = root.findAllByProps({ + className: "form-blacklist-form-row", + }); + expect(rows).to.have.lengthOf(2); + expect( + rows[0].findByProps({ className: "column-url" }).props.value + ).to.equal("*.slack.com"); + expect( + rows[1].findByProps({ className: "column-url" }).props.value + ).to.equal("www.google.com/maps"); + + expect(() => root.findByType(AddButton)).not.throw(); }); it("renders blank value", () => { const root = ReactTestRenderer.create().root; - const children = root.children[0].children; - expect(children).to.have.lengthOf(1); - expect(children[0].props.name).to.equal("add"); + const rows = root.findAllByProps({ + className: "form-blacklist-form-row", + }); + expect(rows).to.be.empty; }); }); describe("onChange", () => { - let container; + let container: HTMLDivElement; beforeEach(() => { container = document.createElement("div"); @@ -44,7 +51,6 @@ describe("settings/form/BlacklistForm", () => { afterEach(() => { document.body.removeChild(container); - container = null; }); it("invokes onChange event on edit", (done) => { @@ -65,7 +71,9 @@ describe("settings/form/BlacklistForm", () => { ); }); - const input = document.querySelectorAll("input[type=text]")[0]; + const input = document.querySelectorAll( + "input[type=text]" + )[0] as HTMLInputElement; input.value = "gitter.im"; ReactTestUtils.Simulate.change(input); }); @@ -104,7 +112,9 @@ describe("settings/form/BlacklistForm", () => { ); }); - const button = document.querySelector("input[type=button].ui-add-button"); + const button = document.querySelector( + "input[type=button].ui-add-button" + ) as HTMLButtonElement; ReactTestUtils.Simulate.click(button); }); }); diff --git a/test/settings/components/form/KeymapsForm.test.tsx b/test/settings/components/form/KeymapsForm.test.tsx index 0a88db5..1cec889 100644 --- a/test/settings/components/form/KeymapsForm.test.tsx +++ b/test/settings/components/form/KeymapsForm.test.tsx @@ -3,7 +3,7 @@ import ReactDOM from "react-dom"; import ReactTestRenderer from "react-test-renderer"; import ReactTestUtils from "react-dom/test-utils"; import KeymapsForm from "../../../../src/settings/components/form/KeymapsForm"; -import { FormKeymaps } from "shared/SettingData"; +import { FormKeymaps } from "../../../../src/shared/SettingData"; import { expect } from "chai"; describe("settings/form/KeymapsForm", () => { @@ -37,7 +37,7 @@ describe("settings/form/KeymapsForm", () => { }); describe("onChange event", () => { - let container; + let container: HTMLDivElement; beforeEach(() => { container = document.createElement("div"); @@ -46,7 +46,6 @@ describe("settings/form/KeymapsForm", () => { afterEach(() => { document.body.removeChild(container); - container = null; }); it("invokes onChange event on edit", (done) => { @@ -68,7 +67,9 @@ describe("settings/form/KeymapsForm", () => { ); }); - const input = document.getElementById('scroll.vertically?{"count":1}'); + const input = document.getElementById( + 'scroll.vertically?{"count":1}' + ) as HTMLInputElement; input.value = "jjj"; ReactTestUtils.Simulate.change(input); }); diff --git a/test/settings/components/form/PropertiesForm.test.tsx b/test/settings/components/form/PropertiesForm.test.tsx index 4dc00a2..acf02b8 100644 --- a/test/settings/components/form/PropertiesForm.test.tsx +++ b/test/settings/components/form/PropertiesForm.test.tsx @@ -2,7 +2,8 @@ import React from "react"; import ReactDOM from "react-dom"; import ReactTestRenderer from "react-test-renderer"; import ReactTestUtils from "react-dom/test-utils"; -import PropertiesForm from "settings/components/form/PropertiesForm"; +import PropertiesForm from "../../../../src/settings/components/form/PropertiesForm"; +import { expect } from "chai"; describe("settings/form/PropertiesForm", () => { describe("render", () => { @@ -38,7 +39,7 @@ describe("settings/form/PropertiesForm", () => { }); describe("onChange", () => { - let container; + let container: HTMLDivElement; beforeEach(() => { container = document.createElement("div"); @@ -47,7 +48,6 @@ describe("settings/form/PropertiesForm", () => { afterEach(() => { document.body.removeChild(container); - container = null; }); it("invokes onChange event on text changed", (done) => { @@ -65,7 +65,9 @@ describe("settings/form/PropertiesForm", () => { ); }); - const input = document.querySelector("input[name=myvalue]"); + const input = document.querySelector( + "input[name=myvalue]" + ) as HTMLInputElement; input.value = "abcd"; ReactTestUtils.Simulate.change(input); }); @@ -85,7 +87,9 @@ describe("settings/form/PropertiesForm", () => { ); }); - const input = document.querySelector("input[name=myvalue]"); + const input = document.querySelector( + "input[name=myvalue]" + ) as HTMLInputElement; input.value = "1234"; ReactTestUtils.Simulate.change(input); }); @@ -105,7 +109,9 @@ describe("settings/form/PropertiesForm", () => { ); }); - const input = document.querySelector("input[name=myvalue]"); + const input = document.querySelector( + "input[name=myvalue]" + ) as HTMLInputElement; input.checked = true; ReactTestUtils.Simulate.change(input); }); diff --git a/test/settings/components/form/SearchEngineForm.test.tsx b/test/settings/components/form/SearchEngineForm.test.tsx index ccbd197..5f835cc 100644 --- a/test/settings/components/form/SearchEngineForm.test.tsx +++ b/test/settings/components/form/SearchEngineForm.test.tsx @@ -2,8 +2,9 @@ import React from "react"; import ReactDOM from "react-dom"; import ReactTestRenderer from "react-test-renderer"; import ReactTestUtils from "react-dom/test-utils"; -import SearchForm from "settings/components/form/SearchForm"; -import { FormSearch } from "shared/SettingData"; +import SearchForm from "../../../../src/settings/components/form/SearchForm"; +import { FormSearch } from "../../../../src/shared/SettingData"; +import { expect } from "chai"; describe("settings/form/SearchForm", () => { describe("render", () => { @@ -33,7 +34,7 @@ describe("settings/form/SearchForm", () => { }); describe("onChange event", () => { - let container; + let container: HTMLDivElement; beforeEach(() => { container = document.createElement("div"); @@ -42,7 +43,6 @@ describe("settings/form/SearchForm", () => { afterEach(() => { document.body.removeChild(container); - container = null; }); it("invokes onChange event on edit", (done) => { @@ -71,10 +71,14 @@ describe("settings/form/SearchForm", () => { ); }); - const radio = document.querySelectorAll("input[type=radio]"); + const radio = document.querySelector( + "input[type=radio]" + ) as HTMLInputElement; radio.checked = true; - const name = document.querySelector("input[name=name]"); + const name = document.querySelector( + "input[name=name]" + ) as HTMLInputElement; name.value = "louvre"; ReactTestUtils.Simulate.change(name); @@ -105,7 +109,9 @@ describe("settings/form/SearchForm", () => { ); }); - const button = document.querySelector("input[type=button]"); + const button = document.querySelector( + "input[type=button]" + ) as HTMLInputElement; ReactTestUtils.Simulate.click(button); }); @@ -132,7 +138,9 @@ describe("settings/form/SearchForm", () => { ); }); - const button = document.querySelector("input[type=button].ui-add-button"); + const button = document.querySelector( + "input[type=button].ui-add-button" + ) as HTMLInputElement; ReactTestUtils.Simulate.click(button); }); }); diff --git a/test/settings/components/ui/input.test.tsx b/test/settings/components/ui/input.test.tsx index 2f2b075..191bfed 100644 --- a/test/settings/components/ui/input.test.tsx +++ b/test/settings/components/ui/input.test.tsx @@ -1,10 +1,11 @@ import React from "react"; import ReactDOM from "react-dom"; import ReactTestUtils from "react-dom/test-utils"; -import Input from "settings/components/ui/Input"; +import Input from "../../../../src/settings/components/ui/Input"; +import { expect } from "chai"; describe("settings/ui/Input", () => { - let container; + let container: HTMLDivElement; beforeEach(() => { container = document.createElement("div"); @@ -13,7 +14,6 @@ describe("settings/ui/Input", () => { afterEach(() => { document.body.removeChild(container); - container = null; }); context("type=text", () => { @@ -25,8 +25,8 @@ describe("settings/ui/Input", () => { ); }); - const label = document.querySelector("label"); - const input = document.querySelector("input"); + const label = document.querySelector("label")!!; + const input = document.querySelector("input")!!; expect(label.textContent).to.contain("myfield"); expect(input.type).to.contain("text"); expect(input.name).to.contain("myname"); @@ -42,7 +42,7 @@ describe("settings/ui/Input", () => { label="myfield" value="myvalue" onChange={(e) => { - expect(e.target.value).to.equal("newvalue"); + expect((e.target as HTMLInputElement).value).to.equal("newvalue"); done(); }} />, @@ -50,7 +50,7 @@ describe("settings/ui/Input", () => { ); }); - const input = document.querySelector("input"); + const input = document.querySelector("input")!!; input.value = "newvalue"; ReactTestUtils.Simulate.change(input); }); @@ -65,8 +65,8 @@ describe("settings/ui/Input", () => { ); }); - const label = document.querySelector("label"); - const input = document.querySelector("input"); + const label = document.querySelector("label")!!; + const input = document.querySelector("input")!!; expect(label.textContent).to.contain("myfield"); expect(input.type).to.contain("radio"); expect(input.name).to.contain("myname"); @@ -82,7 +82,7 @@ describe("settings/ui/Input", () => { label="myfield" value="myvalue" onChange={(e) => { - expect(e.target.checked).to.be.true; + expect((e.target as HTMLInputElement).checked).to.be.true; done(); }} />, @@ -90,7 +90,7 @@ describe("settings/ui/Input", () => { ); }); - const input = document.querySelector("input"); + const input = document.querySelector("input") as HTMLInputElement; input.checked = true; ReactTestUtils.Simulate.change(input); }); @@ -111,9 +111,9 @@ describe("settings/ui/Input", () => { ); }); - const label = document.querySelector("label"); - const textarea = document.querySelector("textarea"); - const error = document.querySelector(".settings-ui-input-error"); + const label = document.querySelector("label")!!; + const textarea = document.querySelector("textarea")!!; + const error = document.querySelector(".settings-ui-input-error")!!; expect(label.textContent).to.contain("myfield"); expect(textarea.nodeName).to.contain("TEXTAREA"); expect(textarea.name).to.contain("myname"); @@ -130,7 +130,7 @@ describe("settings/ui/Input", () => { label="myfield" value="myvalue" onChange={(e) => { - expect(e.target.value).to.equal("newvalue"); + expect((e.target as HTMLInputElement).value).to.equal("newvalue"); done(); }} />, @@ -138,7 +138,7 @@ describe("settings/ui/Input", () => { ); }); - const input = document.querySelector("textarea"); + const input = document.querySelector("textarea")!!; input.value = "newvalue"; ReactTestUtils.Simulate.change(input); }); diff --git a/test/settings/reducers/setting.test.ts b/test/settings/reducers/setting.test.ts index bb5cfa5..99c4c80 100644 --- a/test/settings/reducers/setting.test.ts +++ b/test/settings/reducers/setting.test.ts @@ -1,54 +1,61 @@ -import * as actions from "settings/actions"; -import settingReducer from "settings/reducers/setting"; +import * as actions from "../../../src/settings/actions"; +import settingReducer from "../../../src/settings/reducers/setting"; +import { expect } from "chai"; +import { + FormSettings, + JSONTextSettings, + SettingSource, +} from "../../../src/shared/SettingData"; +import { DefaultSetting } from "../../../src/shared/settings/Settings"; describe("settings setting reducer", () => { it("return the initial state", () => { - const state = settingReducer(undefined, {}); + const state = settingReducer(undefined, {} as any); expect(state).to.have.deep.property("source", "json"); expect(state).to.have.deep.property("error", ""); }); it("return next state for SETTING_SET_SETTINGS", () => { - const action = { + const action: actions.SettingAction = { type: actions.SETTING_SET_SETTINGS, - source: "json", - json: '{ "key": "value" }', - form: {}, + source: SettingSource.JSON, + json: JSONTextSettings.fromText('{ "key": "value" }'), + form: FormSettings.fromSettings(DefaultSetting), }; const state = settingReducer(undefined, action); - expect(state).to.have.deep.property("source", "json"); - expect(state).to.have.deep.property("json", '{ "key": "value" }'); - expect(state).to.have.deep.property("form", {}); + expect(state.source).to.equal("json"); + expect(state.json!!.toJSONText()).to.equal('{ "key": "value" }'); + expect(state.form).to.deep.equal(action.form); }); it("return next state for SETTING_SHOW_ERROR", () => { - const action = { + const action: actions.SettingAction = { type: actions.SETTING_SHOW_ERROR, error: "bad value", - json: "{}", + json: JSONTextSettings.fromText("{}"), }; const state = settingReducer(undefined, action); - expect(state).to.have.deep.property("error", "bad value"); - expect(state).to.have.deep.property("json", "{}"); + expect(state.error).to.equal("bad value"); + expect(state.json!!.toJSONText()).to.equal("{}"); }); it("return next state for SETTING_SWITCH_TO_FORM", () => { - const action = { + const action: actions.SettingAction = { type: actions.SETTING_SWITCH_TO_FORM, - form: {}, + form: FormSettings.fromSettings(DefaultSetting), }; const state = settingReducer(undefined, action); - expect(state).to.have.deep.property("form", {}); - expect(state).to.have.deep.property("source", "form"); + expect(state.form).to.deep.equal(action.form); + expect(state.source).to.equal("form"); }); it("return next state for SETTING_SWITCH_TO_JSON", () => { - const action = { + const action: actions.SettingAction = { type: actions.SETTING_SWITCH_TO_JSON, - json: "{}", + json: JSONTextSettings.fromText("{}"), }; const state = settingReducer(undefined, action); - expect(state).to.have.deep.property("json", "{}"); - expect(state).to.have.deep.property("source", "json"); + expect(state.json!!.toJSONText()).to.equal("{}"); + expect(state.source).to.equal(SettingSource.JSON); }); }); diff --git a/test/shared/operations.test.ts b/test/shared/operations.test.ts index 94986ac..449b25e 100644 --- a/test/shared/operations.test.ts +++ b/test/shared/operations.test.ts @@ -1,12 +1,13 @@ -import * as operations from "shared/operations"; +import * as operations from "../../src/shared/operations"; +import { expect } from "chai"; describe("operations", () => { describe("#valueOf", () => { it("returns an Operation", () => { - const op: operations.Operation = operations.valueOf({ + const op = operations.valueOf({ type: operations.SCROLL_VERTICALLY, count: 10, - }); + }) as operations.ScrollVerticallyOperation; expect(op.type).to.equal(operations.SCROLL_VERTICALLY); expect(op.count).to.equal(10); }); @@ -20,9 +21,9 @@ describe("operations", () => { }); it("fills default valus of optional parameter", () => { - const op: operations.Operation = operations.valueOf({ + const op = operations.valueOf({ type: operations.COMMAND_SHOW_OPEN, - }); + }) as operations.CommandShowOpenOperation; expect(op.type).to.equal(operations.COMMAND_SHOW_OPEN); expect(op.alter).to.be.false; @@ -37,7 +38,7 @@ describe("operations", () => { ).to.throw(TypeError); expect(() => - valueOf({ + operations.valueOf({ type: operations.COMMAND_SHOW_OPEN, alter: "true", }) -- cgit v1.2.3