aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/background/reducers/setting.test.js37
-rw-r--r--test/settings/components/form/properties-form.test.jsx86
-rw-r--r--test/shared/commands/parsers.test.js85
-rw-r--r--test/shared/settings/validator.test.js (renamed from test/shared/validators/setting.test.js)2
-rw-r--r--test/shared/settings/values.test.js29
5 files changed, 236 insertions, 3 deletions
diff --git a/test/background/reducers/setting.test.js b/test/background/reducers/setting.test.js
new file mode 100644
index 0000000..2ef98cb
--- /dev/null
+++ b/test/background/reducers/setting.test.js
@@ -0,0 +1,37 @@
+import { expect } from "chai";
+import actions from 'background/actions';
+import settingReducer from 'background/reducers/setting';
+
+describe("setting reducer", () => {
+ it('return the initial state', () => {
+ let state = settingReducer(undefined, {});
+ expect(state).to.have.deep.property('value', {});
+ });
+
+ it('return next state for SETTING_SET_SETTINGS', () => {
+ let action = {
+ type: actions.SETTING_SET_SETTINGS,
+ value: { key: 123 },
+ };
+ let state = settingReducer(undefined, action);
+ expect(state).to.have.deep.property('value', { key: 123 });
+ });
+
+ it('return next state for SETTING_SET_PROPERTY', () => {
+ let state = {
+ value: {
+ properties: { smoothscroll: true }
+ }
+ }
+ let action = {
+ type: actions.SETTING_SET_PROPERTY,
+ name: 'encoding',
+ value: 'utf-8',
+ };
+ state = settingReducer(state, action);
+
+ console.log(state);
+ expect(state.value.properties).to.have.property('smoothscroll', true);
+ expect(state.value.properties).to.have.property('encoding', 'utf-8');
+ });
+});
diff --git a/test/settings/components/form/properties-form.test.jsx b/test/settings/components/form/properties-form.test.jsx
new file mode 100644
index 0000000..4807361
--- /dev/null
+++ b/test/settings/components/form/properties-form.test.jsx
@@ -0,0 +1,86 @@
+import { expect } from 'chai';
+import { h, render } from 'preact';
+import PropertiesForm from 'settings/components/form/properties-form'
+
+describe("settings/form/PropertiesForm", () => {
+ beforeEach(() => {
+ document.body.innerHTML = '';
+ });
+
+ describe('render', () => {
+ it('renders PropertiesForm', () => {
+ let types = {
+ mystr: 'string',
+ mynum: 'number',
+ mybool: 'boolean',
+ empty: 'string',
+ }
+ let value = {
+ mystr: 'abc',
+ mynum: 123,
+ mybool: true,
+ };
+ render(<PropertiesForm types={types} value={value} />, document.body);
+
+ let strInput = document.querySelector('input[name=mystr]');
+ let numInput = document.querySelector('input[name=mynum]');
+ let boolInput = document.querySelector('input[name=mybool]');
+ let emptyInput = document.querySelector('input[name=empty]');
+
+ expect(strInput.type).to.equals('text');
+ expect(strInput.value).to.equal('abc');
+ expect(numInput.type).to.equals('number');
+ expect(numInput.value).to.equal('123');
+ expect(boolInput.type).to.equals('checkbox');
+ expect(boolInput.checked).to.be.true;
+ expect(emptyInput.type).to.equals('text');
+ expect(emptyInput.value).to.be.empty;
+ });
+ });
+
+ describe('onChange', () => {
+ it('invokes onChange event on text changed', (done) => {
+ render(<PropertiesForm
+ types={{ 'myvalue': 'string' }}
+ value={{ 'myvalue': 'abc' }}
+ onChange={value => {
+ expect(value).to.have.property('myvalue', 'abcd');
+ done();
+ }}
+ />, document.body);
+
+ let input = document.querySelector('input[name=myvalue]');
+ input.value = 'abcd'
+ input.dispatchEvent(new Event('change'))
+ });
+
+ it('invokes onChange event on number changeed', (done) => {
+ render(<PropertiesForm
+ types={{ 'myvalue': 'number' }}
+ value={{ '': 123 }}
+ onChange={value => {
+ expect(value).to.have.property('myvalue', 1234);
+ done();
+ }}
+ />, document.body);
+
+ let input = document.querySelector('input[name=myvalue]');
+ input.value = '1234'
+ input.dispatchEvent(new Event('change'))
+ });
+
+ it('invokes onChange event on checkbox changed', (done) => {
+ render(<PropertiesForm
+ types={{ 'myvalue': 'boolean' }}
+ value={{ 'myvalue': false }}
+ onChange={value => {
+ expect(value).to.have.property('myvalue', true);
+ done();
+ }}
+ />, document.body);
+
+ let input = document.querySelector('input[name=myvalue]');
+ input.click();
+ });
+ });
+});
diff --git a/test/shared/commands/parsers.test.js b/test/shared/commands/parsers.test.js
new file mode 100644
index 0000000..0a1960c
--- /dev/null
+++ b/test/shared/commands/parsers.test.js
@@ -0,0 +1,85 @@
+import { expect } from "chai";
+import * as parsers from 'shared/commands/parsers';
+
+describe("shared/commands/parsers", () => {
+ describe("#parsers.parseSetOption", () => {
+ it('parse set string', () => {
+ let [key, value] = parsers.parseSetOption('encoding=utf-8', { encoding: 'string' });
+ expect(key).to.equal('encoding');
+ expect(value).to.equal('utf-8');
+ });
+
+ it('parse set empty string', () => {
+ let [key, value] = parsers.parseSetOption('encoding=', { encoding: 'string' });
+ expect(key).to.equal('encoding');
+ expect(value).to.equal('');
+ });
+
+ it('parse set string', () => {
+ let [key, value] = parsers.parseSetOption('history=50', { history: 'number' });
+ expect(key).to.equal('history');
+ expect(value).to.equal(50);
+ });
+
+ it('parse set boolean', () => {
+ let [key, value] = parsers.parseSetOption('paste', { paste: 'boolean' });
+ expect(key).to.equal('paste');
+ expect(value).to.be.true;
+
+ [key, value] = parsers.parseSetOption('nopaste', { paste: 'boolean' });
+ expect(key).to.equal('paste');
+ expect(value).to.be.false;
+ });
+
+ it('throws error on unknown property', () => {
+ expect(() => parsers.parseSetOption('charset=utf-8', {})).to.throw(Error, 'Unknown');
+ expect(() => parsers.parseSetOption('smoothscroll', {})).to.throw(Error, 'Unknown');
+ expect(() => parsers.parseSetOption('nosmoothscroll', {})).to.throw(Error, 'Unknown');
+ })
+
+ it('throws error on invalid property', () => {
+ expect(() => parsers.parseSetOption('charset=utf-8', { charset: 'number' })).to.throw(Error, 'Not number');
+ expect(() => parsers.parseSetOption('charset=utf-8', { charset: 'boolean' })).to.throw(Error, 'Invalid');
+ expect(() => parsers.parseSetOption('charset=', { charset: 'boolean' })).to.throw(Error, 'Invalid');
+ expect(() => parsers.parseSetOption('smoothscroll', { smoothscroll: 'string' })).to.throw(Error, 'Invalid');
+ expect(() => parsers.parseSetOption('smoothscroll', { smoothscroll: 'number' })).to.throw(Error, 'Invalid');
+ })
+ });
+
+ describe('#normalizeUrl', () => {
+ const config = {
+ default: 'google',
+ engines: {
+ google: 'https://google.com/search?q={}',
+ yahoo: 'https://yahoo.com/search?q={}',
+ }
+ };
+
+ it('convertes search url', () => {
+ expect(parsers.normalizeUrl(['google', 'apple'], config))
+ .to.equal('https://google.com/search?q=apple');
+ expect(parsers.normalizeUrl(['yahoo', 'apple'], config))
+ .to.equal('https://yahoo.com/search?q=apple');
+ expect(parsers.normalizeUrl(['google', 'apple', 'banana'], config))
+ .to.equal('https://google.com/search?q=apple%20banana');
+ expect(parsers.normalizeUrl(['yahoo', 'C++CLI'], config))
+ .to.equal('https://yahoo.com/search?q=C%2B%2BCLI');
+ });
+
+ it('user default search engine', () => {
+ expect(parsers.normalizeUrl(['apple', 'banana'], config))
+ .to.equal('https://google.com/search?q=apple%20banana');
+ });
+ });
+
+ describe('#parseCommandLine', () => {
+ it('parse command line as name and args', () => {
+ expect(parsers.parseCommandLine('open google apple')).to.deep.equal(['open', ['google', 'apple']]);
+ expect(parsers.parseCommandLine(' open google apple ')).to.deep.equal(['open', ['google', 'apple']]);
+ expect(parsers.parseCommandLine('')).to.deep.equal(['', []]);
+ expect(parsers.parseCommandLine(' ')).to.deep.equal(['', []]);
+ expect(parsers.parseCommandLine('exit')).to.deep.equal(['exit', []]);
+ expect(parsers.parseCommandLine(' exit ')).to.deep.equal(['exit', []]);
+ });
+ });
+});
diff --git a/test/shared/validators/setting.test.js b/test/shared/settings/validator.test.js
index 15d6a10..61d976a 100644
--- a/test/shared/validators/setting.test.js
+++ b/test/shared/settings/validator.test.js
@@ -1,5 +1,5 @@
import { expect } from "chai";
-import { validate } from 'shared/validators/setting';
+import { validate } from 'shared/settings/validator';
describe("setting validator", () => {
describe("unknown top keys", () => {
diff --git a/test/shared/settings/values.test.js b/test/shared/settings/values.test.js
index 2632cd7..62cfb5f 100644
--- a/test/shared/settings/values.test.js
+++ b/test/shared/settings/values.test.js
@@ -7,13 +7,21 @@ describe("settings values", () => {
let json = `{
"keymaps": { "0": {"type": "scroll.home"}},
"search": { "default": "google", "engines": { "google": "https://google.com/search?q={}" }},
- "blacklist": [ "*.slack.com"]
+ "blacklist": [ "*.slack.com"],
+ "properties": {
+ "mystr": "value",
+ "mynum": 123,
+ "mybool": true
+ }
}`;
let value = values.valueFromJson(json);
expect(value.keymaps).to.deep.equal({ 0: {type: "scroll.home"}});
expect(value.search).to.deep.equal({ default: "google", engines: { google: "https://google.com/search?q={}"} });
expect(value.blacklist).to.deep.equal(["*.slack.com"]);
+ expect(value.properties).to.have.property('mystr', 'value');
+ expect(value.properties).to.have.property('mynum', 123);
+ expect(value.properties).to.have.property('mybool', true);
});
});
@@ -29,6 +37,11 @@ describe("settings values", () => {
engines: [['google', 'https://google.com/search?q={}']],
},
blacklist: ['*.slack.com'],
+ "properties": {
+ "mystr": "value",
+ "mynum": 123,
+ "mybool": true,
+ }
};
let value = values.valueFromForm(form);
@@ -37,6 +50,9 @@ describe("settings values", () => {
expect(JSON.stringify(value.search)).to.deep.equal(JSON.stringify({ default: "google", engines: { google: "https://google.com/search?q={}"} }));
expect(value.search).to.deep.equal({ default: "google", engines: { google: "https://google.com/search?q={}"} });
expect(value.blacklist).to.deep.equal(["*.slack.com"]);
+ expect(value.properties).to.have.property('mystr', 'value');
+ expect(value.properties).to.have.property('mynum', 123);
+ expect(value.properties).to.have.property('mybool', true);
});
it('convert from empty form', () => {
@@ -45,6 +61,7 @@ describe("settings values", () => {
expect(value).to.not.have.key('keymaps');
expect(value).to.not.have.key('search');
expect(value).to.not.have.key('blacklist');
+ expect(value).to.not.have.key('properties');
});
it('override keymaps', () => {
@@ -96,7 +113,12 @@ describe("settings values", () => {
0: { type: 'scroll.home' },
},
search: { default: 'google', engines: { google: 'https://google.com/search?q={}' }},
- blacklist: [ '*.slack.com']
+ blacklist: [ '*.slack.com'],
+ properties: {
+ "mystr": "value",
+ "mynum": 123,
+ "mybool": true,
+ }
};
let allowed = ['scroll.vertically?{"count":1}', 'scroll.home' ];
let form = values.formFromValue(value, allowed);
@@ -109,6 +131,9 @@ describe("settings values", () => {
expect(form.search).to.have.deep.property('engines', [['google', 'https://google.com/search?q={}']]);
expect(form.blacklist).to.have.lengthOf(1);
expect(form.blacklist).to.include('*.slack.com');
+ expect(form.properties).to.have.property('mystr', 'value');
+ expect(form.properties).to.have.property('mynum', 123);
+ expect(form.properties).to.have.property('mybool', true);
});
});
});