Make Search class

This commit is contained in:
Shin'ya UEOKA 2019-10-05 01:08:07 +00:00
parent 410ffbb037
commit 2116ac90a6
9 changed files with 182 additions and 130 deletions

View file

@ -4,6 +4,7 @@ import SettingData, {
import Settings from '../../src/shared/Settings';
import { expect } from 'chai';
import Keymaps from '../../src/shared/settings/Keymaps';
import Search from '../../src/shared/settings/Search';
describe('shared/SettingData', () => {
describe('FormKeymaps', () => {
@ -60,7 +61,7 @@ describe('shared/SettingData', () => {
let settings = JSONTextSettings.fromText(o).toSettings();
expect({
keymaps: settings.keymaps.toJSON(),
search: settings.search,
search: settings.search.toJSON(),
properties: settings.properties,
blacklist: settings.blacklist,
}).to.deep.equal(JSON.parse(o));
@ -71,12 +72,12 @@ describe('shared/SettingData', () => {
it('create from a Settings and create a JSON string', () => {
let o = {
keymaps: Keymaps.fromJSON({}),
search: {
search: Search.fromJSON({
default: "google",
engines: {
google: "https://google.com/search?q={}",
},
},
}),
properties: {
hintchars: "abcdefghijklmnopqrstuvwxyz",
smoothscroll: false,
@ -88,7 +89,7 @@ describe('shared/SettingData', () => {
let json = JSONTextSettings.fromSettings(o).toJSONText();
expect(JSON.parse(json)).to.deep.equal({
keymaps: o.keymaps.toJSON(),
search: o.search,
search: o.search.toJSON(),
properties: o.properties,
blacklist: o.blacklist,
});
@ -121,7 +122,7 @@ describe('shared/SettingData', () => {
let settings = FormSettings.valueOf(data).toSettings();
expect({
keymaps: settings.keymaps.toJSON(),
search: settings.search,
search: settings.search.toJSON(),
properties: settings.properties,
blacklist: settings.blacklist,
}).to.deep.equal({
@ -152,12 +153,12 @@ describe('shared/SettingData', () => {
'j': { type: 'scroll.vertically', count: 1 },
'0': { type: 'scroll.home' },
}),
search: {
search: Search.fromJSON({
default: "google",
engines: {
"google": "https://google.com/search?q={}"
}
},
}),
properties: {
hintchars: "abcdefghijklmnopqrstuvwxyz",
smoothscroll: false,
@ -278,7 +279,7 @@ describe('shared/SettingData', () => {
};
let settings = SettingData.valueOf(data).toSettings();
expect(settings.search.default).to.equal('google');
expect(settings.search.defaultEngine).to.equal('google');
});
it('parse object from form source', () => {
@ -302,7 +303,7 @@ describe('shared/SettingData', () => {
};
let settings = SettingData.valueOf(data).toSettings();
expect(settings.search.default).to.equal('yahoo');
expect(settings.search.defaultEngine).to.equal('yahoo');
});
});
});

View file

@ -1,67 +1,7 @@
import * as settings from '../../src/shared/Settings';
import {expect} from 'chai';
import { expect } from 'chai';
describe('Settings', () => {
describe('#searchValueOf', () => {
it('returns search settings by valid settings', () => {
let search = settings.searchValueOf({
default: "google",
engines: {
"google": "https://google.com/search?q={}",
"yahoo": "https://search.yahoo.com/search?p={}",
}
});
expect(search).to.deep.equal({
default: "google",
engines: {
"google": "https://google.com/search?q={}",
"yahoo": "https://search.yahoo.com/search?p={}",
}
});
});
it('throws a TypeError by invalid settings', () => {
expect(() => settings.searchValueOf(null)).to.throw(TypeError);
expect(() => settings.searchValueOf({})).to.throw(TypeError);
expect(() => settings.searchValueOf([])).to.throw(TypeError);
expect(() => settings.searchValueOf({
default: 123,
engines: {}
})).to.throw(TypeError);
expect(() => settings.searchValueOf({
default: "google",
engines: {
"google": 123456,
}
})).to.throw(TypeError);
expect(() => settings.searchValueOf({
default: "wikipedia",
engines: {
"google": "https://google.com/search?q={}",
"yahoo": "https://search.yahoo.com/search?p={}",
}
})).to.throw(TypeError);
expect(() => settings.searchValueOf({
default: "g o o g l e",
engines: {
"g o o g l e": "https://google.com/search?q={}",
}
})).to.throw(TypeError);
expect(() => settings.searchValueOf({
default: "google",
engines: {
"google": "https://google.com/search",
}
})).to.throw(TypeError);
expect(() => settings.searchValueOf({
default: "google",
engines: {
"google": "https://google.com/search?q={}&r={}",
}
})).to.throw(TypeError);
});
});
describe('#propertiesValueOf', () => {
it('returns with default properties by empty settings', () => {
@ -129,7 +69,7 @@ describe('Settings', () => {
expect({
keymaps: x.keymaps.toJSON(),
search: x.search,
search: x.search.toJSON(),
properties: x.properties,
blacklist: x.blacklist,
}).to.deep.equal({
@ -153,7 +93,7 @@ describe('Settings', () => {
let value = settings.valueOf({});
expect(value.keymaps.toJSON()).to.not.be.empty;
expect(value.properties).to.not.be.empty;
expect(value.search.default).to.be.a('string');
expect(value.search.defaultEngine).to.be.a('string');
expect(value.search.engines).to.be.an('object');
expect(value.blacklist).to.be.empty;
});

View file

@ -0,0 +1,68 @@
import Search from '../../../src/shared/settings/Search';
import { expect } from 'chai';
describe('Search', () => {
it('returns search settings by valid settings', () => {
let search = Search.fromJSON({
default: 'google',
engines: {
'google': 'https://google.com/search?q={}',
'yahoo': 'https://search.yahoo.com/search?p={}',
}
});
expect(search.defaultEngine).to.equal('google')
expect(search.engines).to.deep.equals({
'google': 'https://google.com/search?q={}',
'yahoo': 'https://search.yahoo.com/search?p={}',
});
expect(search.toJSON()).to.deep.equal({
default: 'google',
engines: {
'google': 'https://google.com/search?q={}',
'yahoo': 'https://search.yahoo.com/search?p={}',
}
});
});
it('throws a TypeError by invalid settings', () => {
expect(() => Search.fromJSON(null)).to.throw(TypeError);
expect(() => Search.fromJSON({})).to.throw(TypeError);
expect(() => Search.fromJSON([])).to.throw(TypeError);
expect(() => Search.fromJSON({
default: 123,
engines: {}
})).to.throw(TypeError);
expect(() => Search.fromJSON({
default: 'google',
engines: {
'google': 123456,
}
})).to.throw(TypeError);
expect(() => Search.fromJSON({
default: 'wikipedia',
engines: {
'google': 'https://google.com/search?q={}',
'yahoo': 'https://search.yahoo.com/search?p={}',
}
})).to.throw(TypeError);
expect(() => Search.fromJSON({
default: 'g o o g l e',
engines: {
'g o o g l e': 'https://google.com/search?q={}',
}
})).to.throw(TypeError);
expect(() => Search.fromJSON({
default: 'google',
engines: {
'google': 'https://google.com/search',
}
})).to.throw(TypeError);
expect(() => Search.fromJSON({
default: 'google',
engines: {
'google': 'https://google.com/search?q={}&r={}',
}
})).to.throw(TypeError);
});
});

View file

@ -1,14 +1,16 @@
import * as parsers from 'shared/urls';
import * as parsers from '../../src/shared/urls';
import { expect } from 'chai';
import Search from '../../src/shared/settings/Search';
describe("shared/commands/parsers", () => {
describe('#searchUrl', () => {
const config = {
const config = Search.fromJSON({
default: 'google',
engines: {
google: 'https://google.com/search?q={}',
yahoo: 'https://yahoo.com/search?q={}',
}
};
});
it('convertes search url', () => {
expect(parsers.searchUrl('google.com', config))