aboutsummaryrefslogtreecommitdiff
path: root/test/background
diff options
context:
space:
mode:
Diffstat (limited to 'test/background')
-rw-r--r--test/background/actions/find.test.js12
-rw-r--r--test/background/actions/tab.test.js13
-rw-r--r--test/background/infrastructures/memory-storage.test.js46
-rw-r--r--test/background/reducers/find.test.js18
-rw-r--r--test/background/reducers/setting.test.js35
-rw-r--r--test/background/reducers/tab.test.js22
-rw-r--r--test/background/repositories/version.js (renamed from test/background/shared/versions/storage.test.js)18
-rw-r--r--test/background/shared/versions/index.test.js40
-rw-r--r--test/background/usecases/filters.test.js113
-rw-r--r--test/background/usecases/parsers.test.js73
10 files changed, 244 insertions, 146 deletions
diff --git a/test/background/actions/find.test.js b/test/background/actions/find.test.js
deleted file mode 100644
index 6b0b846..0000000
--- a/test/background/actions/find.test.js
+++ /dev/null
@@ -1,12 +0,0 @@
-import actions from 'background/actions';
-import * as findActions from 'background/actions/find';
-
-describe("find actions", () => {
- describe("setKeyword", () => {
- it('create FIND_SET_KEYWORD action', () => {
- let action = findActions.setKeyword('banana');
- expect(action.type).to.equal(actions.FIND_SET_KEYWORD);
- expect(action.keyword).to.equal('banana');
- });
- });
-});
diff --git a/test/background/actions/tab.test.js b/test/background/actions/tab.test.js
deleted file mode 100644
index ab57374..0000000
--- a/test/background/actions/tab.test.js
+++ /dev/null
@@ -1,13 +0,0 @@
-import actions from 'background/actions';
-import * as tabActions from 'background/actions/tab';
-
-describe("tab actions", () => {
- describe("selected", () => {
- it('create TAB_SELECTED action', () => {
- let action = tabActions.selected(123);
- expect(action.type).to.equal(actions.TAB_SELECTED);
- expect(action.tabId).to.equal(123);
- });
- });
-});
-
diff --git a/test/background/infrastructures/memory-storage.test.js b/test/background/infrastructures/memory-storage.test.js
new file mode 100644
index 0000000..0fea895
--- /dev/null
+++ b/test/background/infrastructures/memory-storage.test.js
@@ -0,0 +1,46 @@
+import MemoryStorage from 'background/infrastructures/memory-storage';
+
+describe("background/infrastructures/memory-storage", () => {
+ let versionRepository;
+
+ it('stores values', () => {
+ let cache = new MemoryStorage();
+ cache.set('number', 123);
+ expect(cache.get('number')).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' });
+ });
+
+ it('returns undefined if no keys', () => {
+ let cache = new MemoryStorage();
+ expect(cache.get('no-keys')).to.be.undefined;
+ })
+
+ it('stored on shared memory', () => {
+ let cache = new MemoryStorage();
+ cache.set('red', 'apple');
+
+ cache = new MemoryStorage();
+ let got = cache.get('red');
+ expect(got).to.equal('apple');
+ });
+
+ it('stored cloned objects', () => {
+ let cache = new MemoryStorage();
+ let recipe = { sugar: '300g' };
+ cache.set('recipe', recipe);
+
+ recipe.salt = '20g'
+ let got = cache.get('recipe', recipe);
+ expect(got).to.deep.equal({ sugar: '300g' });
+ });
+
+ it('throws an error with unserializable objects', () => {
+ let cache = new MemoryStorage();
+ expect(() => cache.set('fn', setTimeout)).to.throw();
+ })
+});
diff --git a/test/background/reducers/find.test.js b/test/background/reducers/find.test.js
deleted file mode 100644
index c366223..0000000
--- a/test/background/reducers/find.test.js
+++ /dev/null
@@ -1,18 +0,0 @@
-import actions from 'background/actions';
-import findReducer from 'background/reducers/find';
-
-describe("find reducer", () => {
- it('return the initial state', () => {
- let state = findReducer(undefined, {});
- expect(state).to.have.deep.property('keyword', null);
- });
-
- it('return next state for FIND_SET_KEYWORD', () => {
- let action = {
- type: actions.FIND_SET_KEYWORD,
- keyword: 'cherry',
- };
- let state = findReducer(undefined, action);
- expect(state).to.have.deep.property('keyword', 'cherry')
- });
-});
diff --git a/test/background/reducers/setting.test.js b/test/background/reducers/setting.test.js
deleted file mode 100644
index 24d02ea..0000000
--- a/test/background/reducers/setting.test.js
+++ /dev/null
@@ -1,35 +0,0 @@
-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);
-
- expect(state.value.properties).to.have.property('smoothscroll', true);
- expect(state.value.properties).to.have.property('encoding', 'utf-8');
- });
-});
diff --git a/test/background/reducers/tab.test.js b/test/background/reducers/tab.test.js
deleted file mode 100644
index 09fa8a7..0000000
--- a/test/background/reducers/tab.test.js
+++ /dev/null
@@ -1,22 +0,0 @@
-import actions from 'background/actions';
-import tabReducer from 'background/reducers/tab';
-
-describe("tab reducer", () => {
- it('return the initial state', () => {
- let state = tabReducer(undefined, {});
- expect(state.previousSelected).to.equal(-1);
- expect(state.currentSelected).to.equal(-1);
- });
-
- it('return next state for TAB_SELECTED', () => {
- let state = undefined;
-
- state = tabReducer(state, { type: actions.TAB_SELECTED, tabId: 123 });
- expect(state.previousSelected).to.equal(-1);
- expect(state.currentSelected).to.equal(123);
-
- state = tabReducer(state, { type: actions.TAB_SELECTED, tabId: 456 });
- expect(state.previousSelected).to.equal(123);
- expect(state.currentSelected).to.equal(456);
- });
-});
diff --git a/test/background/shared/versions/storage.test.js b/test/background/repositories/version.js
index f452516..8510ae6 100644
--- a/test/background/shared/versions/storage.test.js
+++ b/test/background/repositories/version.js
@@ -1,14 +1,20 @@
-import * as storage from 'background/shared/versions/storage';
+import VersionRepository from 'background/repositories/version';
-describe("shared/versions/storage", () => {
- describe('#load', () => {
+describe("background/repositories/version", () => {
+ let versionRepository;
+
+ beforeEach(() => {
+ versionRepository = new VersionRepository;
+ });
+
+ describe('#get', () => {
beforeEach(() => {
return browser.storage.local.remove('version');
});
it('loads saved version', async() => {
await browser.storage.local.set({ version: '1.2.3' });
- let version = await storage.load();
+ let version = await this.versionRepository.get();
expect(version).to.equal('1.2.3');
});
@@ -18,9 +24,9 @@ describe("shared/versions/storage", () => {
});
});
- describe('#save', () => {
+ describe('#update', () => {
it('saves version string', async() => {
- await storage.save('2.3.4');
+ await versionRepository.update('2.3.4');
let { version } = await browser.storage.local.get('version');
expect(version).to.equal('2.3.4');
});
diff --git a/test/background/shared/versions/index.test.js b/test/background/shared/versions/index.test.js
deleted file mode 100644
index d65dd9a..0000000
--- a/test/background/shared/versions/index.test.js
+++ /dev/null
@@ -1,40 +0,0 @@
-import * as versions from 'background/shared/versions';
-import manifest from '../../../../manifest.json';
-
-describe("shared/versions/storage", () => {
- describe('#checkUpdated', () => {
- beforeEach(() => {
- return browser.storage.local.remove('version');
- });
-
- it('return true if no previous versions', async() => {
- let updated = await versions.checkUpdated();
- expect(updated).to.be.true;
- });
-
- it('return true if updated', async() => {
- await browser.storage.local.set({ version: '0.001' });
- let updated = await versions.checkUpdated();
- expect(updated).to.be.true;
- });
-
- it('return false if not updated', async() => {
- await browser.storage.local.set({ version: manifest.version });
- let updated = await versions.checkUpdated();
- expect(updated).to.be.false;
- });
- });
-
- describe('#commit', () => {
- beforeEach(() => {
- return browser.storage.local.remove('version');
- });
-
- it('saves current version from manifest.json', async() => {
- await versions.commit();
- let { version } = await browser.storage.local.get('version');
- expect(version).to.be.a('string');
- expect(version).to.equal(manifest.version);
- });
- });
-});
diff --git a/test/background/usecases/filters.test.js b/test/background/usecases/filters.test.js
new file mode 100644
index 0000000..bdfb0be
--- /dev/null
+++ b/test/background/usecases/filters.test.js
@@ -0,0 +1,113 @@
+import * as filters from 'background/usecases/filters';
+
+describe("background/usecases/filters", () => {
+ describe('filterHttp', () => {
+ it('filters http URLs duplicates to https hosts', () => {
+ let pages = [
+ { url: 'http://i-beam.org/foo' },
+ { url: 'https://i-beam.org/bar' },
+ { url: 'http://i-beam.net/hoge' },
+ { url: 'http://i-beam.net/fuga' },
+ ];
+ let filtered = filters.filterHttp(pages);
+
+ let urls = filtered.map(x => x.url);
+ expect(urls).to.deep.equal([
+ 'https://i-beam.org/bar', 'http://i-beam.net/hoge', 'http://i-beam.net/fuga'
+ ]);
+ })
+ });
+
+ describe('filterBlankTitle', () => {
+ it('filters blank titles', () => {
+ let pages = [
+ { title: 'hello' },
+ { title: '' },
+ {},
+ ];
+ let filtered = filters.filterBlankTitle(pages);
+
+ expect(filtered).to.deep.equal([{ title: 'hello' }]);
+ });
+ })
+
+ describe('filterByTailingSlash', () => {
+ it('filters duplicated pathname on tailing slash', () => {
+ let pages = [
+ { url: 'http://i-beam.org/content' },
+ { url: 'http://i-beam.org/content/' },
+ { url: 'http://i-beam.org/search' },
+ { url: 'http://i-beam.org/search?q=apple_banana_cherry' },
+ ];
+ let filtered = filters.filterByTailingSlash(pages);
+
+ let urls = filtered.map(x => x.url);
+ expect(urls).to.deep.equal([
+ 'http://i-beam.org/content',
+ 'http://i-beam.org/search',
+ 'http://i-beam.org/search?q=apple_banana_cherry',
+ ]);
+ });
+ })
+
+ describe('filterByPathname', () => {
+ it('remains items less than minimam length', () => {
+ let pages = [
+ { url: 'http://i-beam.org/search?q=apple' },
+ { url: 'http://i-beam.org/search?q=apple_banana' },
+ { url: 'http://i-beam.org/search?q=apple_banana_cherry' },
+ { url: 'http://i-beam.org/request?q=apple' },
+ { url: 'http://i-beam.org/request?q=apple_banana' },
+ { url: 'http://i-beam.org/request?q=apple_banana_cherry' },
+ ];
+ let filtered = filters.filterByPathname(pages, 10);
+ expect(filtered).to.have.lengthOf(6);
+ });
+
+ it('filters by length of pathname', () => {
+ let pages = [
+ { url: 'http://i-beam.org/search?q=apple' },
+ { url: 'http://i-beam.org/search?q=apple_banana' },
+ { url: 'http://i-beam.org/search?q=apple_banana_cherry' },
+ { url: 'http://i-beam.net/search?q=apple' },
+ { url: 'http://i-beam.net/search?q=apple_banana' },
+ { url: 'http://i-beam.net/search?q=apple_banana_cherry' },
+ ];
+ let filtered = filters.filterByPathname(pages, 0);
+ expect(filtered).to.deep.equal([
+ { url: 'http://i-beam.org/search?q=apple' },
+ { url: 'http://i-beam.net/search?q=apple' },
+ ]);
+ });
+ })
+
+ describe('filterByOrigin', () => {
+ it('remains items less than minimam length', () => {
+ let pages = [
+ { url: 'http://i-beam.org/search?q=apple' },
+ { url: 'http://i-beam.org/search?q=apple_banana' },
+ { url: 'http://i-beam.org/search?q=apple_banana_cherry' },
+ { url: 'http://i-beam.org/request?q=apple' },
+ { url: 'http://i-beam.org/request?q=apple_banana' },
+ { url: 'http://i-beam.org/request?q=apple_banana_cherry' },
+ ];
+ let filtered = filters.filterByOrigin(pages, 10);
+ expect(filtered).to.have.lengthOf(6);
+ });
+
+ it('filters by length of pathname', () => {
+ let pages = [
+ { url: 'http://i-beam.org/search?q=apple' },
+ { url: 'http://i-beam.org/search?q=apple_banana' },
+ { url: 'http://i-beam.org/search?q=apple_banana_cherry' },
+ { url: 'http://i-beam.org/request?q=apple' },
+ { url: 'http://i-beam.org/request?q=apple_banana' },
+ { url: 'http://i-beam.org/request?q=apple_banana_cherry' },
+ ];
+ let filtered = filters.filterByOrigin(pages, 0);
+ expect(filtered).to.deep.equal([
+ { url: 'http://i-beam.org/search?q=apple' },
+ ]);
+ });
+ })
+});
diff --git a/test/background/usecases/parsers.test.js b/test/background/usecases/parsers.test.js
new file mode 100644
index 0000000..a58c4a3
--- /dev/null
+++ b/test/background/usecases/parsers.test.js
@@ -0,0 +1,73 @@
+import * as parsers from 'background/usecases/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');
+ });
+ });
+});