Skip to content
This repository has been archived by the owner on Apr 1, 2021. It is now read-only.

Commit

Permalink
Coverage #6
Browse files Browse the repository at this point in the history
  • Loading branch information
nbdn committed Mar 14, 2018
1 parent ca9a146 commit f086846
Show file tree
Hide file tree
Showing 5 changed files with 338 additions and 3 deletions.
123 changes: 122 additions & 1 deletion src/redux/__spec__/reducer.spec.js
Original file line number Diff line number Diff line change
@@ -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',
Expand Down
212 changes: 212 additions & 0 deletions src/redux/__spec__/selector.spec.js
Original file line number Diff line number Diff line change
@@ -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);
})
);
});
1 change: 1 addition & 0 deletions src/redux/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`;
Expand Down
3 changes: 2 additions & 1 deletion src/redux/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
ADD_BINDER,
MOUNT_BINDER,
REMOVE_BINDER,
RESET_STATE,
UPDATE_BINDER,
UPDATE_PRESS_STATUS,
} from './actions';
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/redux/selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

Expand Down

0 comments on commit f086846

Please sign in to comment.