From 0dec6c641fc11348f89a12680a087ccda1181f66 Mon Sep 17 00:00:00 2001 From: Shin'ya UEOKA Date: Sat, 5 Oct 2019 07:19:48 +0000 Subject: [PATCH] Make Settings class --- .../controllers/SettingController.ts | 2 +- .../infrastructures/ContentMessageListener.ts | 3 +- .../repositories/SettingRepository.ts | 7 +- src/background/usecases/SettingUseCase.ts | 2 +- src/content/client/SettingClient.ts | 4 +- src/content/repositories/SettingRepository.ts | 2 +- src/content/usecases/SettingUseCase.ts | 2 +- src/shared/SettingData.ts | 6 +- src/shared/{ => settings}/Settings.ts | 121 +++++++++++------- .../repositories/SettingRepository.test.ts | 13 +- test/content/usecases/SettingUseCaase.test.ts | 2 +- test/shared/SettingData.test.ts | 39 +++--- test/shared/{ => settings}/Settings.test.ts | 10 +- 13 files changed, 117 insertions(+), 96 deletions(-) rename src/shared/{ => settings}/Settings.ts (64%) rename test/shared/{ => settings}/Settings.test.ts (84%) diff --git a/src/background/controllers/SettingController.ts b/src/background/controllers/SettingController.ts index 34951ff..8d05852 100644 --- a/src/background/controllers/SettingController.ts +++ b/src/background/controllers/SettingController.ts @@ -1,7 +1,7 @@ import { injectable } from 'tsyringe'; import SettingUseCase from '../usecases/SettingUseCase'; import ContentMessageClient from '../infrastructures/ContentMessageClient'; -import Settings from '../../shared/Settings'; +import Settings from '../../shared/settings/Settings'; @injectable() export default class SettingController { diff --git a/src/background/infrastructures/ContentMessageListener.ts b/src/background/infrastructures/ContentMessageListener.ts index f348a74..f80d686 100644 --- a/src/background/infrastructures/ContentMessageListener.ts +++ b/src/background/infrastructures/ContentMessageListener.ts @@ -8,7 +8,6 @@ import AddonEnabledController from '../controllers/AddonEnabledController'; import LinkController from '../controllers/LinkController'; import OperationController from '../controllers/OperationController'; import MarkController from '../controllers/MarkController'; -import { toJSON } from '../../shared/Settings'; @injectable() export default class ContentMessageListener { @@ -103,7 +102,7 @@ export default class ContentMessageListener { } async onSettingsQuery(): Promise { - return toJSON(await this.settingController.getSetting()); + return (await this.settingController.getSetting()).toJSON(); } onFindGetKeyword(): Promise { diff --git a/src/background/repositories/SettingRepository.ts b/src/background/repositories/SettingRepository.ts index a11b65f..e775a32 100644 --- a/src/background/repositories/SettingRepository.ts +++ b/src/background/repositories/SettingRepository.ts @@ -1,6 +1,6 @@ import { injectable } from 'tsyringe'; import MemoryStorage from '../infrastructures/MemoryStorage'; -import Settings, { valueOf, toJSON } from '../../shared/Settings'; +import Settings from '../../shared/settings/Settings'; import Properties from '../../shared/settings/Properties'; const CACHED_SETTING_KEY = 'setting'; @@ -15,12 +15,11 @@ export default class SettingRepository { get(): Promise { let data = this.cache.get(CACHED_SETTING_KEY); - return Promise.resolve(valueOf(data)); + return Promise.resolve(Settings.fromJSON(data)); } update(value: Settings): void { - let data = toJSON(value); - return this.cache.set(CACHED_SETTING_KEY, data); + return this.cache.set(CACHED_SETTING_KEY, value.toJSON()); } async setProperty( diff --git a/src/background/usecases/SettingUseCase.ts b/src/background/usecases/SettingUseCase.ts index d73521f..d78d440 100644 --- a/src/background/usecases/SettingUseCase.ts +++ b/src/background/usecases/SettingUseCase.ts @@ -3,7 +3,7 @@ import PersistentSettingRepository from '../repositories/PersistentSettingRepository'; import SettingRepository from '../repositories/SettingRepository'; import { DefaultSettingData } from '../../shared/SettingData'; -import Settings from '../../shared/Settings'; +import Settings from '../../shared/settings/Settings'; import NotifyPresenter from '../presenters/NotifyPresenter'; @injectable() diff --git a/src/content/client/SettingClient.ts b/src/content/client/SettingClient.ts index a7cd1ee..fc62720 100644 --- a/src/content/client/SettingClient.ts +++ b/src/content/client/SettingClient.ts @@ -1,4 +1,4 @@ -import Settings, { valueOf } from '../../shared/Settings'; +import Settings from '../../shared/settings/Settings'; import * as messages from '../../shared/messages'; export default interface SettingClient { @@ -10,6 +10,6 @@ export class SettingClientImpl { let settings = await browser.runtime.sendMessage({ type: messages.SETTINGS_QUERY, }); - return valueOf(settings); + return Settings.fromJSON(settings); } } diff --git a/src/content/repositories/SettingRepository.ts b/src/content/repositories/SettingRepository.ts index d718794..4ba26e0 100644 --- a/src/content/repositories/SettingRepository.ts +++ b/src/content/repositories/SettingRepository.ts @@ -1,4 +1,4 @@ -import Settings, { DefaultSetting } from '../../shared/Settings'; +import Settings, { DefaultSetting } from '../../shared/settings/Settings'; let current: Settings = DefaultSetting; diff --git a/src/content/usecases/SettingUseCase.ts b/src/content/usecases/SettingUseCase.ts index d5f66c6..4608039 100644 --- a/src/content/usecases/SettingUseCase.ts +++ b/src/content/usecases/SettingUseCase.ts @@ -1,7 +1,7 @@ import { injectable, inject } from 'tsyringe'; import SettingRepository from '../repositories/SettingRepository'; import SettingClient from '../client/SettingClient'; -import Settings from '../../shared/Settings'; +import Settings from '../../shared/settings/Settings'; @injectable() export default class SettingUseCase { diff --git a/src/shared/SettingData.ts b/src/shared/SettingData.ts index 8ef8385..2dedfef 100644 --- a/src/shared/SettingData.ts +++ b/src/shared/SettingData.ts @@ -1,5 +1,5 @@ import * as operations from './operations'; -import Settings, * as settings from './Settings'; +import Settings from './settings/Settings'; import Keymaps from './settings/Keymaps'; import Search from './settings/Search'; import Properties from './settings/Properties'; @@ -118,7 +118,7 @@ export class JSONTextSettings { } toSettings(): Settings { - return settings.valueOf(JSON.parse(this.json)); + return Settings.fromJSON(JSON.parse(this.json)); } toJSONText(): string { @@ -198,7 +198,7 @@ export class FormSettings { } toSettings(): Settings { - return settings.valueOf({ + return Settings.fromJSON({ keymaps: this.keymaps.toKeymaps().toJSON(), search: this.search.toSearchSettings().toJSON(), properties: this.properties.toJSON(), diff --git a/src/shared/Settings.ts b/src/shared/settings/Settings.ts similarity index 64% rename from src/shared/Settings.ts rename to src/shared/settings/Settings.ts index 2767820..116c7d7 100644 --- a/src/shared/Settings.ts +++ b/src/shared/settings/Settings.ts @@ -1,49 +1,76 @@ -import Keymaps from './settings/Keymaps'; -import Search from './settings/Search'; -import Properties from './settings/Properties'; -import Blacklist from './settings/Blacklist'; +import Keymaps, { KeymapsJSON } from './Keymaps'; +import Search, { SearchJSON } from './Search'; +import Properties, { PropertiesJSON } from './Properties'; +import Blacklist, { BlacklistJSON } from './Blacklist'; -export default interface Settings { - keymaps: Keymaps; - search: Search; - properties: Properties; - blacklist: Blacklist; -} +export type SettingsJSON = { + keymaps: KeymapsJSON, + search: SearchJSON, + properties: PropertiesJSON, + blacklist: BlacklistJSON, +}; + +export default class Settings { + public keymaps: Keymaps; + + public search: Search; -export const valueOf = (o: any): Settings => { - let settings = { ...DefaultSetting }; - for (let key of Object.keys(o)) { - switch (key) { - case 'keymaps': - settings.keymaps = Keymaps.fromJSON(o.keymaps); - break; - case 'search': - settings.search = Search.fromJSON(o.search); - break; - case 'properties': - settings.properties = Properties.fromJSON(o.properties); - break; - case 'blacklist': - settings.blacklist = Blacklist.fromJSON(o.blacklist); - break; - default: - throw new TypeError('unknown setting: ' + key); + public properties: Properties; + + public blacklist: Blacklist; + + constructor({ + keymaps, + search, + properties, + blacklist, + }: { + keymaps: Keymaps; + search: Search; + properties: Properties; + blacklist: Blacklist; + }) { + this.keymaps = keymaps; + this.search = search; + this.properties = properties; + this.blacklist = blacklist; + } + + static fromJSON(json: any): Settings { + let settings = { ...DefaultSetting }; + for (let key of Object.keys(json)) { + switch (key) { + case 'keymaps': + settings.keymaps = Keymaps.fromJSON(json.keymaps); + break; + case 'search': + settings.search = Search.fromJSON(json.search); + break; + case 'properties': + settings.properties = Properties.fromJSON(json.properties); + break; + case 'blacklist': + settings.blacklist = Blacklist.fromJSON(json.blacklist); + break; + default: + throw new TypeError('unknown setting: ' + key); + } } + return new Settings(settings); } - return settings; -}; -export const toJSON = (settings: Settings): any => { - return { - keymaps: settings.keymaps.toJSON(), - search: settings.search.toJSON(), - properties: settings.properties.toJSON(), - blacklist: settings.blacklist.toJSON(), - }; -}; + toJSON(): SettingsJSON { + return { + keymaps: this.keymaps.toJSON(), + search: this.search.toJSON(), + properties: this.properties.toJSON(), + blacklist: this.blacklist.toJSON(), + }; + } +} -export const DefaultSetting: Settings = { - keymaps: Keymaps.fromJSON({ +export const DefaultSetting: Settings = Settings.fromJSON({ + keymaps: { '0': { 'type': 'scroll.home' }, ':': { 'type': 'command.show' }, 'o': { 'type': 'command.show.open', 'alter': false }, @@ -106,8 +133,8 @@ export const DefaultSetting: Settings = { 'N': { 'type': 'find.prev' }, '.': { 'type': 'repeat.last' }, '': { 'type': 'addon.toggle.enabled' } - }), - search: Search.fromJSON({ + }, + search: { default: 'google', engines: { 'google': 'https://google.com/search?q={}', @@ -117,11 +144,11 @@ export const DefaultSetting: Settings = { 'twitter': 'https://twitter.com/search?q={}', 'wikipedia': 'https://en.wikipedia.org/w/index.php?search={}' } - }), - properties: Properties.fromJSON({ + }, + properties: { hintchars: 'abcdefghijklmnopqrstuvwxyz', smoothscroll: false, complete: 'sbh' - }), - blacklist: Blacklist.fromJSON([]), -}; + }, + blacklist: [], +}); diff --git a/test/content/repositories/SettingRepository.test.ts b/test/content/repositories/SettingRepository.test.ts index 363fcec..db4c528 100644 --- a/test/content/repositories/SettingRepository.test.ts +++ b/test/content/repositories/SettingRepository.test.ts @@ -1,27 +1,26 @@ import { SettingRepositoryImpl } from '../../../src/content/repositories/SettingRepository'; import { expect } from 'chai'; -import Keymaps from '../../../src/shared/settings/Keymaps'; -import Search from '../../../src/shared/settings/Search'; +import Settings from '../../../src/shared/settings/Settings'; describe('SettingRepositoryImpl', () => { it('updates and gets current value', () => { let sut = new SettingRepositoryImpl(); - let settings = { - keymaps: Keymaps.fromJSON({}), - search: Search.fromJSON({ + let settings = Settings.fromJSON({ + keymaps: {}, + search:{ default: 'google', engines: { google: 'https://google.com/?q={}', } - }), + }, properties: { hintchars: 'abcd1234', smoothscroll: false, complete: 'sbh', }, blacklist: [], - }; + }); sut.set(settings); diff --git a/test/content/usecases/SettingUseCaase.test.ts b/test/content/usecases/SettingUseCaase.test.ts index e9633f4..136c5af 100644 --- a/test/content/usecases/SettingUseCaase.test.ts +++ b/test/content/usecases/SettingUseCaase.test.ts @@ -1,7 +1,7 @@ import SettingRepository from '../../../src/content/repositories/SettingRepository'; import SettingClient from '../../../src/content/client/SettingClient'; import SettingUseCase from '../../../src/content/usecases/SettingUseCase'; -import Settings, { DefaultSetting } from '../../../src/shared/Settings'; +import Settings, { DefaultSetting } from '../../../src/shared/settings/Settings'; import { expect } from 'chai'; class MockSettingRepository implements SettingRepository { diff --git a/test/shared/SettingData.test.ts b/test/shared/SettingData.test.ts index 4391e73..138d768 100644 --- a/test/shared/SettingData.test.ts +++ b/test/shared/SettingData.test.ts @@ -1,12 +1,9 @@ import SettingData, { FormKeymaps, JSONTextSettings, FormSettings, } from '../../src/shared/SettingData'; -import Settings from '../../src/shared/Settings'; +import Settings from '../../src/shared/settings/Settings'; import { expect } from 'chai'; import Keymaps from '../../src/shared/settings/Keymaps'; -import Search from '../../src/shared/settings/Search'; -import Properties from '../../src/shared/settings/Properties'; -import Blacklist from '../../src/shared/settings/Blacklist' describe('shared/SettingData', () => { describe('FormKeymaps', () => { @@ -72,21 +69,21 @@ describe('shared/SettingData', () => { describe('#fromSettings to #toJSON', () => { it('create from a Settings and create a JSON string', () => { - let o = { - keymaps: Keymaps.fromJSON({}), - search: Search.fromJSON({ + let o = Settings.fromJSON({ + keymaps: {}, + search: { default: "google", engines: { google: "https://google.com/search?q={}", }, - }), - properties: Properties.fromJSON({ + }, + properties: { hintchars: "abcdefghijklmnopqrstuvwxyz", smoothscroll: false, complete: "sbh" - }), - blacklist: Blacklist.fromJSON([]), - }; + }, + blacklist: [], + }); let json = JSONTextSettings.fromSettings(o).toJSONText(); expect(JSON.parse(json)).to.deep.equal({ @@ -150,24 +147,24 @@ describe('shared/SettingData', () => { describe('#fromSettings to #toJSON', () => { it('create from a Settings and create a JSON string', () => { - let data: Settings = { - keymaps: Keymaps.fromJSON({ + let data: Settings = Settings.fromJSON({ + keymaps: { 'j': { type: 'scroll.vertically', count: 1 }, '0': { type: 'scroll.home' }, - }), - search: Search.fromJSON({ + }, + search: { default: "google", engines: { "google": "https://google.com/search?q={}" } - }), - properties: Properties.fromJSON({ + }, + properties: { hintchars: "abcdefghijklmnopqrstuvwxyz", smoothscroll: false, complete: "sbh" - }), - blacklist: Blacklist.fromJSON([]), - }; + }, + blacklist: [], + }); let json = FormSettings.fromSettings(data).toJSON(); expect(json).to.deep.equal({ diff --git a/test/shared/Settings.test.ts b/test/shared/settings/Settings.test.ts similarity index 84% rename from test/shared/Settings.test.ts rename to test/shared/settings/Settings.test.ts index 9688798..ab6af04 100644 --- a/test/shared/Settings.test.ts +++ b/test/shared/settings/Settings.test.ts @@ -1,10 +1,10 @@ -import * as settings from '../../src/shared/Settings'; -import {expect} from 'chai'; +import Settings from '../../../src/shared/settings/Settings'; +import { expect } from 'chai'; describe('Settings', () => { describe('#valueOf', () => { it('returns settings by valid settings', () => { - let x = settings.valueOf({ + let x = Settings.fromJSON({ keymaps: {}, "search": { "default": "google", @@ -39,7 +39,7 @@ describe('Settings', () => { }); it('sets default settings', () => { - let value = settings.valueOf({}); + let value = Settings.fromJSON({}); expect(value.keymaps.toJSON()).to.not.be.empty; expect(value.properties.toJSON()).to.not.be.empty; expect(value.search.defaultEngine).to.be.a('string'); @@ -48,7 +48,7 @@ describe('Settings', () => { }); it('throws a TypeError with an unknown field', () => { - expect(() => settings.valueOf({ name: 'alice' })).to.throw(TypeError) + expect(() => Settings.fromJSON({ name: 'alice' })).to.throw(TypeError) }); }); });