From f0868462369ae43a0dde13279fc036a6abb5f008 Mon Sep 17 00:00:00 2001 From: nbdn Date: Wed, 14 Mar 2018 15:53:35 +0100 Subject: [PATCH] Coverage #6 --- src/redux/__spec__/reducer.spec.js | 123 +++++++++++++++- src/redux/__spec__/selector.spec.js | 212 ++++++++++++++++++++++++++++ src/redux/actions.js | 1 + src/redux/reducer.js | 3 +- src/redux/selector.js | 2 +- 5 files changed, 338 insertions(+), 3 deletions(-) create mode 100644 src/redux/__spec__/selector.spec.js diff --git a/src/redux/__spec__/reducer.spec.js b/src/redux/__spec__/reducer.spec.js index 6fecf98..801da0e 100644 --- a/src/redux/__spec__/reducer.spec.js +++ b/src/redux/__spec__/reducer.spec.js @@ -1,7 +1,128 @@ import { reducer, initialKeysSate } from '../reducer'; import sinon from 'sinon'; -import { MOUNT_BINDER } from '../actions'; +import { + ACTIVE_BINDER, + ADD_BINDER, + MOUNT_BINDER, + REMOVE_BINDER, + RESET_STATE, + UPDATE_BINDER, + UPDATE_PRESS_STATUS, +} from '../actions'; + describe('redux/reducer', () => { + describe('INITIAL STATE', () => { + it('should return the initial state', () => { + reducer(undefined, {}).should.eql(initialKeysSate); + }); + }); + + describe('ACTIVE_BINDER', () => { + it( + 'should add binder', + sinon.test(function() { + const binder = { id: '1', priority: 1 }; + const binder2 = { id: '2', priority: 1 }; + const state = { ...initialKeysSate, binders: [binder, binder2] }; + const action = { + type: ACTIVE_BINDER, + binder: { id: '2', priority: 1, sleep: false }, + }; + const result = reducer(state, action); + result.should.eqls({ + ...initialKeysSate, + binders: [ + { id: '1', priority: 1, mounted: false }, + { + id: '2', + priority: 1, + sleep: false, + mounted: true, + mountedTime: 0, + }, + ], + current: { binderId: '2', selectedId: undefined }, + }); + }) + ); + }); + + describe('ADD_BINDER', () => { + it( + 'should add binder', + sinon.test(function() { + const binder = { id: '1', priority: 1 }; + const state = { ...initialKeysSate, binders: [binder] }; + const action = { + type: ADD_BINDER, + binder: { id: '2', priority: 1, sleep: false }, + }; + const result = reducer(state, action); + result.should.eqls({ + ...initialKeysSate, + binders: [binder, action.binder], + }); + }) + ); + }); + + describe('UPDATE_BINDER', () => { + it( + 'should update binder', + sinon.test(function() { + const binder = { id: '1', priority: 1 }; + const state = { ...initialKeysSate, binders: [binder] }; + const action = { + type: UPDATE_BINDER, + binder: { id: '1', priority: 3, sleep: false }, + }; + const result = reducer(state, action); + result.should.eqls({ ...initialKeysSate, binders: [action.binder] }); + }) + ); + }); + + describe('REMOVE_BINDER', () => { + it( + 'should remove binder', + sinon.test(function() { + const binder = { id: '1', priority: 1 }; + const state = { ...initialKeysSate, binders: [binder] }; + const action = { type: REMOVE_BINDER, binderId: '1', force: true }; + const result = reducer(state, action); + result.should.eqls({ ...initialKeysSate, binders: [] }); + }) + ); + }); + + describe('RESET_STATE', () => { + it( + 'should reset state', + sinon.test(function() { + const binder = { id: '1', priority: 1 }; + const state = { ...initialKeysSate, binders: [binder] }; + const action = { type: RESET_STATE }; + const result = reducer(state, action); + result.should.eqls({ ...initialKeysSate }); + }) + ); + }); + + describe('UPDATE_PRESS_STATUS', () => { + it( + 'should update press status', + sinon.test(function() { + const state = { ...initialKeysSate }; + const action = { type: UPDATE_PRESS_STATUS, press: true, keyCode: 432 }; + const result = reducer(state, action); + result.should.eqls({ + ...initialKeysSate, + PRESS: { press: true, keyCode: 432 }, + }); + }) + ); + }); + describe('MOUNT_BINDER', () => { it( 'should mount binder with fresh priority from props', diff --git a/src/redux/__spec__/selector.spec.js b/src/redux/__spec__/selector.spec.js new file mode 100644 index 0000000..eb0e519 --- /dev/null +++ b/src/redux/__spec__/selector.spec.js @@ -0,0 +1,212 @@ +import sinon from 'sinon'; +import { + _isCurrentBinder, + _getBinders, + _getBinderMarginLeft, + _getBinderMarginTop, + _getBinderSelectedId, + _getCurrentBinder, + _getCurrentBinderId, + _getCurrentSelectedId, + _getKeyCode, + _isBinderActive, + _isLongPress, + _isVisibleInBinder, +} from '../selector'; +import { NAME } from '../../constants'; +import * as ensure from '../../ensure'; +import * as store from '../../store'; + +describe('redux/selector', () => { + it( + 'should return true if binder is current', + sinon.test(function() { + this.stub(ensure, 'ensureState').returns(true); + + this.stub(store, 'getStore').returns({ current: { binderId: 'batman' } }); + + _isCurrentBinder('batman')().should.equal(true); + }) + ); + + it( + 'should return false if binder is current', + sinon.test(function() { + this.stub(ensure, 'ensureState').returns(true); + + this.stub(store, 'getStore').returns({ + current: { binderId: 'superman' }, + }); + + _isCurrentBinder('batman')().should.equal(false); + }) + ); + + it( + 'should return binders state', + sinon.test(function() { + this.stub(ensure, 'ensureState').returns(true); + + this.stub(store, 'getBinders').returns({ + binders: [{ id: 'batman' }, { id: 'spiderman' }], + }); + + _getBinders()().should.eql({ + binders: [{ id: 'batman' }, { id: 'spiderman' }], + }); + }) + ); + + it( + 'should return binder current selected id state', + sinon.test(function() { + this.stub(ensure, 'ensureState').returns(true); + + this.stub(store, 'getStore').returns({ current: { selectedId: '6' } }); + + _getCurrentSelectedId()().should.equal('6'); + }) + ); + + it( + 'should return current binder state', + sinon.test(function() { + this.stub(ensure, 'ensureState').returns(true); + + const fakeState = { + binders: [{ id: 'batman' }, { id: 'spiderman' }], + current: { + binderId: 'batman', + }, + }; + + this.stub(store, 'getStore').returns(fakeState); + + _getCurrentBinder()().should.eql({ id: 'batman' }); + }) + ); + + it( + 'should return current binder id state', + sinon.test(function() { + this.stub(ensure, 'ensureState').returns(true); + + this.stub(store, 'getStore').returns({ + current: { binderId: 'superman' }, + }); + + _getCurrentBinderId()().should.equal('superman'); + }) + ); + + it( + 'should return current key code state', + sinon.test(function() { + this.stub(ensure, 'ensureState').returns(true); + + this.stub(store, 'getStore').returns({ PRESS: { keyCode: 42 } }); + + _getKeyCode()().should.equal(42); + }) + ); + + it( + 'should return is long press state', + sinon.test(function() { + this.stub(ensure, 'ensureState').returns(true); + + this.stub(store, 'getStore').returns({ PRESS: { press: true } }); + + _isLongPress()().should.equal(true); + }) + ); + + it( + 'should return binder selected id state', + sinon.test(function() { + this.stub(ensure, 'ensureState').returns(true); + + this.stub(store, 'getBinders').returns([ + { id: 'batman', selectedId: '3' }, + { id: 'spiderman' }, + ]); + + _getBinderSelectedId('batman')().should.equal('3'); + }) + ); + + it( + 'should return binder margin left state', + sinon.test(function() { + this.stub(ensure, 'ensureState').returns(true); + + this.stub(store, 'getBinders').returns([ + { id: 'batman', marginLeft: 10, selectedId: '3' }, + { id: 'spiderman', marginLeft: 15 }, + ]); + + _getBinderMarginLeft('batman')().should.equal(10); + }) + ); + + it( + 'should return binder margin top state', + sinon.test(function() { + this.stub(ensure, 'ensureState').returns(true); + + this.stub(store, 'getBinders').returns([ + { id: 'batman', marginTop: 10, selectedId: '3' }, + { id: 'spiderman', marginTop: 15 }, + ]); + + _getBinderMarginTop('batman')().should.equal(10); + }) + ); + + it( + 'should return binder active state', + sinon.test(function() { + this.stub(ensure, 'ensureState').returns(true); + + this.stub(store, 'getBinders').returns([ + { id: 'batman', mounted: true }, + { id: 'spiderman', marginTop: 15 }, + ]); + + _isBinderActive('batman')().should.equal(true); + }) + ); + + it( + 'should return if binder is visible', + sinon.test(function() { + this.stub(ensure, 'ensureState').returns(true); + + this.stub(store, 'getBinders').returns([ + { id: 'batman', mounted: true }, + { id: 'spiderman', marginTop: 15 }, + ]); + + _isBinderActive('batman')().should.equal(true); + }) + ); + + it( + 'should return if binder is visible', + sinon.test(function() { + this.stub(ensure, 'ensureState').returns(true); + + this.stub(store, 'getBinders').returns([ + { + id: 'batman', + mounted: true, + elements: [{ id: '3', isVisible: true }], + }, + ]); + + _isVisibleInBinder(null, '3')().should.equal(false); + _isVisibleInBinder('batman', '1')().should.equal(false); + _isVisibleInBinder('batman', '3')().should.equal(true); + }) + ); +}); diff --git a/src/redux/actions.js b/src/redux/actions.js index cb04dc8..ec1dd76 100644 --- a/src/redux/actions.js +++ b/src/redux/actions.js @@ -15,6 +15,7 @@ export const ADD_BINDER = `${NAME}/ADD_BINDER`; export const MOUNT_BINDER = `${NAME}/MOUNT_BINDER`; export const UPDATE_BINDER = `${NAME}/UPDATE_BINDER`; export const REMOVE_BINDER = `${NAME}/REMOVE_BINDER`; +export const RESET_STATE = `${NAME}/RESET_STATE`; export const ACTIVE_BINDER = `${NAME}/ACTIVE_BINDER`; export const UPDATE_PRESS_STATUS = `${NAME}/UPDATE_PRESS_STATUS`; diff --git a/src/redux/reducer.js b/src/redux/reducer.js index 8e2eed0..ee957ea 100755 --- a/src/redux/reducer.js +++ b/src/redux/reducer.js @@ -3,6 +3,7 @@ import { ADD_BINDER, MOUNT_BINDER, REMOVE_BINDER, + RESET_STATE, UPDATE_BINDER, UPDATE_PRESS_STATUS, } from './actions'; @@ -73,7 +74,7 @@ export function reducer(state = initialKeysSate, action) { ...state, PRESS: { press: action.press, keyCode: action.keyCode }, }; - case 'RESET_STATE': + case RESET_STATE: return initialKeysSate; default: return state; diff --git a/src/redux/selector.js b/src/redux/selector.js index e477c7b..74f3f01 100644 --- a/src/redux/selector.js +++ b/src/redux/selector.js @@ -28,7 +28,7 @@ export const _getBinders = () => () => { export const _getCurrentBinder = () => () => { ensureState(); - const { binders, current } = globalStore.getState()[NAME]; + const { binders, current } = getStore(); return findBinder(binders, current.binderId); };