From cf0dcf25226611590d76d263b5a4acdc89510d09 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sat, 7 Jul 2018 17:52:01 +0900 Subject: blacklist as an util --- test/shared/blacklists.test.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 test/shared/blacklists.test.js (limited to 'test/shared') diff --git a/test/shared/blacklists.test.js b/test/shared/blacklists.test.js new file mode 100644 index 0000000..87e89c5 --- /dev/null +++ b/test/shared/blacklists.test.js @@ -0,0 +1,42 @@ +import { includes } from 'shared/blacklists'; + +describe("shared/blacklist", () => { + it('matches by *', () => { + let blacklist = ['*']; + + expect(includes(blacklist, 'https://github.com/abc')).to.be.true; + }) + + it('matches by hostname', () => { + let blacklist = ['github.com']; + + expect(includes(blacklist, 'https://github.com')).to.be.true; + expect(includes(blacklist, 'https://gist.github.com')).to.be.false; + expect(includes(blacklist, 'https://github.com/ueokande')).to.be.true; + expect(includes(blacklist, 'https://github.org')).to.be.false; + expect(includes(blacklist, 'https://google.com/search?q=github.org')).to.be.false; + }) + + it('matches by hostname with wildcard', () => { + let blacklist = ['*.github.com']; + + expect(includes(blacklist, 'https://github.com')).to.be.false; + expect(includes(blacklist, 'https://gist.github.com')).to.be.true; + }) + + it('matches by path', () => { + let blacklist = ['github.com/abc']; + + expect(includes(blacklist, 'https://github.com/abc')).to.be.true; + expect(includes(blacklist, 'https://github.com/abcdef')).to.be.false; + expect(includes(blacklist, 'https://gist.github.com/abc')).to.be.false; + }) + + it('matches by path with wildcard', () => { + let blacklist = ['github.com/abc*']; + + expect(includes(blacklist, 'https://github.com/abc')).to.be.true; + expect(includes(blacklist, 'https://github.com/abcdef')).to.be.true; + expect(includes(blacklist, 'https://gist.github.com/abc')).to.be.false; + }) +}); -- cgit v1.2.3 From 96649fef63f467d45aefee93dfb599546151c21d Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sun, 8 Jul 2018 11:39:56 +0900 Subject: See you my redux --- src/shared/store/index.js | 53 ------------------- src/shared/store/provider.jsx | 15 ------ test/shared/store/index.test.js | 110 ---------------------------------------- 3 files changed, 178 deletions(-) delete mode 100644 src/shared/store/index.js delete mode 100644 src/shared/store/provider.jsx delete mode 100644 test/shared/store/index.test.js (limited to 'test/shared') diff --git a/src/shared/store/index.js b/src/shared/store/index.js deleted file mode 100644 index 2fafdf1..0000000 --- a/src/shared/store/index.js +++ /dev/null @@ -1,53 +0,0 @@ -class Store { - constructor(reducer, catcher) { - this.reducer = reducer; - this.catcher = catcher; - this.subscribers = []; - try { - this.state = this.reducer(undefined, {}); - } catch (e) { - catcher(e); - } - } - - dispatch(action, sender) { - if (action instanceof Promise) { - action.then((a) => { - this.transitNext(a, sender); - }).catch((e) => { - this.catcher(e, sender); - }); - } else { - try { - this.transitNext(action, sender); - } catch (e) { - this.catcher(e, sender); - } - } - return action; - } - - getState() { - return this.state; - } - - subscribe(callback) { - this.subscribers.push(callback); - } - - transitNext(action, sender) { - let newState = this.reducer(this.state, action); - if (JSON.stringify(this.state) !== JSON.stringify(newState)) { - this.state = newState; - this.subscribers.forEach(f => f(sender)); - } - } -} - -const empty = () => {}; - -const createStore = (reducer, catcher = empty) => { - return new Store(reducer, catcher); -}; - -export { createStore }; diff --git a/src/shared/store/provider.jsx b/src/shared/store/provider.jsx deleted file mode 100644 index fe925aa..0000000 --- a/src/shared/store/provider.jsx +++ /dev/null @@ -1,15 +0,0 @@ -import { h, Component } from 'preact'; - -class Provider extends Component { - getChildContext() { - return { store: this.props.store }; - } - - render() { - return
- { this.props.children } -
; - } -} - -export default Provider; diff --git a/test/shared/store/index.test.js b/test/shared/store/index.test.js deleted file mode 100644 index 5b69b40..0000000 --- a/test/shared/store/index.test.js +++ /dev/null @@ -1,110 +0,0 @@ -import { createStore } from 'shared/store'; - -describe("Store class", () => { - const reducer = (state, action) => { - if (state == undefined) { - return 0; - } - return state + action; - }; - - describe("#dispatch", () => { - it('transit status by immediate action', () => { - let store = createStore(reducer); - store.dispatch(10); - expect(store.getState()).to.equal(10); - - store.dispatch(-20); - expect(store.getState()).to.equal(-10); - }); - - it('returns next state by immediate action', () => { - let store = createStore(reducer); - let dispatchedAction = store.dispatch(11); - expect(dispatchedAction).to.equal(11); - }); - - it('transit status by Promise action', () => { - let store = createStore(reducer); - let p1 = Promise.resolve(10); - - return store.dispatch(p1).then(() => { - expect(store.getState()).to.equal(10); - }).then(() => { - store.dispatch(Promise.resolve(-20)); - }).then(() => { - expect(store.getState()).to.equal(-10); - }); - }); - - it('returns next state by promise action', () => { - let store = createStore(reducer); - let dispatchedAction = store.dispatch(Promise.resolve(11)); - return dispatchedAction.then((value) => { - expect(value).to.equal(11); - }); - }); - }); - - describe("#subscribe", () => { - it('invoke callback', (done) => { - let store = createStore(reducer); - store.subscribe(() => { - expect(store.getState()).to.equal(15); - done(); - }); - store.dispatch(15); - }); - - it('propagate sender object', (done) => { - let store = createStore(reducer); - store.subscribe((sender) => { - expect(sender).to.equal('sender'); - done(); - }); - store.dispatch(15, 'sender'); - }); - }) - - describe("catcher", () => { - it('catch an error in reducer on initializing by immediate action', (done) => { - let store = createStore(() => { - throw new Error(); - }, (e) => { - expect(e).to.be.an('error'); - done(); - }); - }); - - it('catch an error in reducer on initializing by immediate action', (done) => { - let store = createStore((state, action) => { - if (state === undefined) return 0; - throw new Error(); - }, (e) => { - expect(e).to.be.an('error'); - done(); - }); - store.dispatch(20); - }); - - it('catch an error in reducer on initializing by promise action', (done) => { - let store = createStore((state, action) => { - if (state === undefined) return 0; - throw new Error(); - }, (e) => { - expect(e).to.be.an('error'); - done(); - }); - store.dispatch(Promise.resolve(20)); - }); - - it('catch an error in promise action', (done) => { - let store = createStore((state, action) => 0, (e) => { - expect(e).to.be.an('error'); - done(); - }); - store.dispatch(new Promise(() => { throw new Error() })); - }); - }) -}); - -- cgit v1.2.3 From 067da88d06fbffca323ecdbaf8b1011f88225219 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Wed, 11 Jul 2018 21:01:48 +0900 Subject: Move versions to background --- src/background/index.js | 2 +- src/background/shared/versions/index.js | 38 +++++++++++++++++++++++ src/background/shared/versions/release-notes.js | 8 +++++ src/background/shared/versions/storage.js | 10 +++++++ src/shared/versions/index.js | 38 ----------------------- src/shared/versions/release-notes.js | 8 ----- src/shared/versions/storage.js | 10 ------- test/background/shared/versions/index.test.js | 40 +++++++++++++++++++++++++ test/background/shared/versions/storage.test.js | 28 +++++++++++++++++ test/shared/versions/index.test.js | 40 ------------------------- test/shared/versions/storage.test.js | 28 ----------------- 11 files changed, 125 insertions(+), 125 deletions(-) create mode 100644 src/background/shared/versions/index.js create mode 100644 src/background/shared/versions/release-notes.js create mode 100644 src/background/shared/versions/storage.js delete mode 100644 src/shared/versions/index.js delete mode 100644 src/shared/versions/release-notes.js delete mode 100644 src/shared/versions/storage.js create mode 100644 test/background/shared/versions/index.test.js create mode 100644 test/background/shared/versions/storage.test.js delete mode 100644 test/shared/versions/index.test.js delete mode 100644 test/shared/versions/storage.test.js (limited to 'test/shared') diff --git a/src/background/index.js b/src/background/index.js index 8c4eafc..1e4c078 100644 --- a/src/background/index.js +++ b/src/background/index.js @@ -6,7 +6,7 @@ import IndicatorComponent from 'background/components/indicator'; import reducers from 'background/reducers'; import { createStore, applyMiddleware } from 'redux'; import promise from 'redux-promise'; -import * as versions from 'shared/versions'; +import * as versions from './shared/versions'; const store = createStore( reducers, diff --git a/src/background/shared/versions/index.js b/src/background/shared/versions/index.js new file mode 100644 index 0000000..aa09c92 --- /dev/null +++ b/src/background/shared/versions/index.js @@ -0,0 +1,38 @@ +import * as storage from './storage'; +import * as releaseNotes from './release-notes'; +import manifest from '../../../../manifest.json'; + +const NOTIFICATION_ID = 'vimvixen-update'; + +const notificationClickListener = (id) => { + if (id !== NOTIFICATION_ID) { + return; + } + + browser.tabs.create({ url: releaseNotes.url(manifest.version) }); + browser.notifications.onClicked.removeListener(notificationClickListener); +}; + +const checkUpdated = async() => { + let prev = await storage.load(); + if (!prev) { + return true; + } + return manifest.version !== prev; +}; + +const notify = () => { + browser.notifications.onClicked.addListener(notificationClickListener); + return browser.notifications.create(NOTIFICATION_ID, { + 'type': 'basic', + 'iconUrl': browser.extension.getURL('resources/icon_48x48.png'), + 'title': 'Vim Vixen ' + manifest.version + ' has been installed', + 'message': 'Click here to see release notes', + }); +}; + +const commit = () => { + storage.save(manifest.version); +}; + +export { checkUpdated, notify, commit }; diff --git a/src/background/shared/versions/release-notes.js b/src/background/shared/versions/release-notes.js new file mode 100644 index 0000000..6ef2335 --- /dev/null +++ b/src/background/shared/versions/release-notes.js @@ -0,0 +1,8 @@ +const url = (version) => { + if (version) { + return 'https://github.com/ueokande/vim-vixen/releases/tag/' + version; + } + return 'https://github.com/ueokande/vim-vixen/releases/'; +}; + +export { url }; diff --git a/src/background/shared/versions/storage.js b/src/background/shared/versions/storage.js new file mode 100644 index 0000000..7883258 --- /dev/null +++ b/src/background/shared/versions/storage.js @@ -0,0 +1,10 @@ +const load = async() => { + let { version } = await browser.storage.local.get('version'); + return version; +}; + +const save = (version) => { + return browser.storage.local.set({ version }); +}; + +export { load, save }; diff --git a/src/shared/versions/index.js b/src/shared/versions/index.js deleted file mode 100644 index ba3d183..0000000 --- a/src/shared/versions/index.js +++ /dev/null @@ -1,38 +0,0 @@ -import * as storage from './storage'; -import * as releaseNotes from './release-notes'; -import manifest from '../../../manifest.json'; - -const NOTIFICATION_ID = 'vimvixen-update'; - -const notificationClickListener = (id) => { - if (id !== NOTIFICATION_ID) { - return; - } - - browser.tabs.create({ url: releaseNotes.url(manifest.version) }); - browser.notifications.onClicked.removeListener(notificationClickListener); -}; - -const checkUpdated = async() => { - let prev = await storage.load(); - if (!prev) { - return true; - } - return manifest.version !== prev; -}; - -const notify = () => { - browser.notifications.onClicked.addListener(notificationClickListener); - return browser.notifications.create(NOTIFICATION_ID, { - 'type': 'basic', - 'iconUrl': browser.extension.getURL('resources/icon_48x48.png'), - 'title': 'Vim Vixen ' + manifest.version + ' has been installed', - 'message': 'Click here to see release notes', - }); -}; - -const commit = () => { - storage.save(manifest.version); -}; - -export { checkUpdated, notify, commit }; diff --git a/src/shared/versions/release-notes.js b/src/shared/versions/release-notes.js deleted file mode 100644 index 6ef2335..0000000 --- a/src/shared/versions/release-notes.js +++ /dev/null @@ -1,8 +0,0 @@ -const url = (version) => { - if (version) { - return 'https://github.com/ueokande/vim-vixen/releases/tag/' + version; - } - return 'https://github.com/ueokande/vim-vixen/releases/'; -}; - -export { url }; diff --git a/src/shared/versions/storage.js b/src/shared/versions/storage.js deleted file mode 100644 index 7883258..0000000 --- a/src/shared/versions/storage.js +++ /dev/null @@ -1,10 +0,0 @@ -const load = async() => { - let { version } = await browser.storage.local.get('version'); - return version; -}; - -const save = (version) => { - return browser.storage.local.set({ version }); -}; - -export { load, save }; diff --git a/test/background/shared/versions/index.test.js b/test/background/shared/versions/index.test.js new file mode 100644 index 0000000..d65dd9a --- /dev/null +++ b/test/background/shared/versions/index.test.js @@ -0,0 +1,40 @@ +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/shared/versions/storage.test.js b/test/background/shared/versions/storage.test.js new file mode 100644 index 0000000..f452516 --- /dev/null +++ b/test/background/shared/versions/storage.test.js @@ -0,0 +1,28 @@ +import * as storage from 'background/shared/versions/storage'; + +describe("shared/versions/storage", () => { + describe('#load', () => { + 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(); + expect(version).to.equal('1.2.3'); + }); + + it('returns undefined if no versions in storage', async() => { + let version = await storage.load(); + expect(version).to.be.a('undefined'); + }); + }); + + describe('#save', () => { + it('saves version string', async() => { + await storage.save('2.3.4'); + let { version } = await browser.storage.local.get('version'); + expect(version).to.equal('2.3.4'); + }); + }); +}); diff --git a/test/shared/versions/index.test.js b/test/shared/versions/index.test.js deleted file mode 100644 index d90f04c..0000000 --- a/test/shared/versions/index.test.js +++ /dev/null @@ -1,40 +0,0 @@ -import * as versions from '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/shared/versions/storage.test.js b/test/shared/versions/storage.test.js deleted file mode 100644 index f541abf..0000000 --- a/test/shared/versions/storage.test.js +++ /dev/null @@ -1,28 +0,0 @@ -import * as storage from 'shared/versions/storage'; - -describe("shared/versions/storage", () => { - describe('#load', () => { - 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(); - expect(version).to.equal('1.2.3'); - }); - - it('returns undefined if no versions in storage', async() => { - let version = await storage.load(); - expect(version).to.be.a('undefined'); - }); - }); - - describe('#save', () => { - it('saves version string', async() => { - await storage.save('2.3.4'); - let { version } = await browser.storage.local.get('version'); - expect(version).to.equal('2.3.4'); - }); - }); -}); -- cgit v1.2.3