From 83cb277ba2af2bc2f87ace1d97fa582a7043bcd5 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Thu, 14 Sep 2017 22:04:42 +0900 Subject: consome as store/reducers --- test/actions/background.test.js | 14 ----------- test/actions/command.test.js | 51 ----------------------------------------- 2 files changed, 65 deletions(-) delete mode 100644 test/actions/background.test.js delete mode 100644 test/actions/command.test.js (limited to 'test') diff --git a/test/actions/background.test.js b/test/actions/background.test.js deleted file mode 100644 index a3203ee..0000000 --- a/test/actions/background.test.js +++ /dev/null @@ -1,14 +0,0 @@ -import { expect } from "chai"; -import actions from '../../src/actions'; -import * as backgroundActions from '../../src/actions/background'; - -describe("background actions", () => { - describe("requestCompletions", () => { - it('create BACKGROUND_REQUEST_COMPLETIONS action', () => { - let action = backgroundActions.requestCompletions('buffer hoge fuga'); - expect(action.type).to.equal(actions.BACKGROUND_REQUEST_COMPLETIONS); - expect(action.command).to.equal('buffer'); - expect(action.keywords).to.equal('hoge fuga'); - }); - }); -}); diff --git a/test/actions/command.test.js b/test/actions/command.test.js deleted file mode 100644 index 01a67f2..0000000 --- a/test/actions/command.test.js +++ /dev/null @@ -1,51 +0,0 @@ -import { expect } from "chai"; -import actions from '../../src/actions'; -import * as commandActions from '../../src/actions/command'; - -describe("command actions", () => { - describe("exec", () => { - context("open command", () => { - it('create COMMAND_OPEN_URL acion with a full url', () => { - let action = commandActions.exec("open https://github.com/") - expect(action.type).to.equal(actions.COMMAND_OPEN_URL); - expect(action.url).to.equal('https://github.com/'); - }); - - it('create COMMAND_OPEN_URL acion with a domain name', () => { - let action = commandActions.exec("open github.com") - expect(action.type).to.equal(actions.COMMAND_OPEN_URL); - expect(action.url).to.equal('http://github.com'); - }); - }); - - context("tabopen command", () => { - it('create COMMAND_TABOPEN_URL acion with a full url', () => { - let action = commandActions.exec("tabopen https://github.com/") - expect(action.type).to.equal(actions.COMMAND_TABOPEN_URL); - expect(action.url).to.equal('https://github.com/'); - }); - - it('create COMMAND_TABOPEN_URL acion with a domain name', () => { - let action = commandActions.exec("tabopen github.com") - expect(action.type).to.equal(actions.COMMAND_TABOPEN_URL); - expect(action.url).to.equal('http://github.com'); - }); - }); - - context("buffer command", () => { - it('create COMMAND_BUFFER acion with a keywords', () => { - let action = commandActions.exec("buffer foo bar") - expect(action.type).to.equal(actions.COMMAND_BUFFER); - expect(action.keywords).to.equal('foo bar'); - }); - }); - - context("b command", () => { - it('create COMMAND_BUFFER acion with a keywords', () => { - let action = commandActions.exec("b foo bar") - expect(action.type).to.equal(actions.COMMAND_BUFFER); - expect(action.keywords).to.equal('foo bar'); - }); - }); - }); -}); -- cgit v1.2.3 From fb1d3b5962531a004bb14f7f78796f77149ee7e7 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sat, 16 Sep 2017 23:17:42 +0900 Subject: add store test and fix store --- src/store/index.js | 7 ++- test/store/index.test.js | 111 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 test/store/index.test.js (limited to 'test') diff --git a/src/store/index.js b/src/store/index.js index a0a7791..2d08296 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -2,8 +2,12 @@ class Store { constructor(reducer, catcher) { this.reducer = reducer; this.catcher = catcher; - this.state = this.reducer(undefined, {}); this.subscribers = []; + try { + this.state = this.reducer(undefined, {}); + } catch (e) { + catcher(e); + } } dispatch(action, sender) { @@ -20,6 +24,7 @@ class Store { this.catcher(e, sender); } } + return action } getState() { diff --git a/test/store/index.test.js b/test/store/index.test.js new file mode 100644 index 0000000..e19d50e --- /dev/null +++ b/test/store/index.test.js @@ -0,0 +1,111 @@ +import { expect } from "chai"; +import { createStore } from '../../src/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