Skip to content

Commit

Permalink
Now with more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maartenlterpstra committed May 24, 2017
1 parent 37bd8c2 commit 56416b0
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 33 deletions.
12 changes: 3 additions & 9 deletions src/actions/countActions.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// This is where action creators are put
import { INCREMENT_COUNT, DOUBLE_COUNT, RESET_COUNTER } from '../constants/countLabels';
import { INCREMENT_COUNT, DOUBLE_COUNT } from '../constants/countLabels';

const incrementCounter = () => {
return {
type: INCREMENT_COUNT
};
};

/* istanbul ignore next */
const doubleCounter = () => {
return (dispatch, getState) => {
return new Promise((resolve) => {
Expand All @@ -20,16 +21,9 @@ const doubleCounter = () => {
};
};

const resetCounter = () => {
return {
type: RESET_COUNTER
};
};

const actions = {
doubleCounter,
incrementCounter,
resetCounter
incrementCounter
};

export default actions;
12 changes: 0 additions & 12 deletions src/actions/countActions.spec.js

This file was deleted.

5 changes: 2 additions & 3 deletions src/components/CounterComponent.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ export default class CounterComponent extends Component {
return (
<div>
{count}
<button onClick={() => dispatch(actions.incrementCounter())}>Increment</button>
<button onClick={() => dispatch(actions.doubleCounter())}>Double</button>
<button onClick={() => dispatch(actions.resetCounter())}>Reset</button>
<button id='incrementButton' onClick={() => dispatch(actions.incrementCounter())}>Increment</button>
<button id='doubleButton' onClick={() => dispatch(actions.doubleCounter())}>Double</button>
</div>);
}
}
Expand Down
26 changes: 26 additions & 0 deletions src/components/CounterComponent.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React from 'react';
import CounterComponent from './CounterComponent';
import { mount } from 'enzyme';
import sinon from 'sinon';

describe('(Component) CounterComponent', () => {
let _component;
const _dispatch = sinon.spy();
const incrementFunc = sinon.spy();
const _actions = {
incrementCounter: incrementFunc
};
beforeEach(() => {
_component = mount(<CounterComponent count={0} dispatch={_dispatch} actions={_actions} />);
});

it('Can mount', () => {
expect(_component.type()).to.equal(CounterComponent);
});

it('Can increment the counter', () => {
_component.find('#incrementButton').first().simulate('click');
expect(_dispatch).to.have.been.calledOnce();
expect(incrementFunc).to.have.been.calledOnce();
});
});
21 changes: 21 additions & 0 deletions src/components/MainComponent.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import React from 'react';
import MainComponent from './MainComponent';
import { mount } from 'enzyme';
import sinon from 'sinon';

describe('(Component) MainComponent', () => {
let _component;
const _dispatch = sinon.spy();
const incrementFunc = sinon.spy();
const _actions = {
incrementCounter: incrementFunc
};
beforeEach(() => {
_component = mount(<MainComponent count={0} dispatch={_dispatch} actions={_actions} />);
});

it('Can mount', () => {
expect(_component.type()).to.equal(MainComponent);
expect(_component.html()).to.equal('<div>hi!</div>');
});
});
1 change: 0 additions & 1 deletion src/constants/countLabels.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export const INCREMENT_COUNT = 'INCREMENT_COUNT';
export const DOUBLE_COUNT = 'DOUBLE_COUNT';
export const RESET_COUNTER = 'RESET_COUNTER';
15 changes: 15 additions & 0 deletions src/containers/AppContainer.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react';
import AppContainer from './AppContainer';
import { shallow } from 'enzyme';
import { Provider } from 'react-redux';

describe('(Component) AppContainer', () => {
let _component;
beforeEach(() => {
_component = shallow(<AppContainer routes={{}} store={{ subscribe: () => {} }} />);
});

it('Can mount', () => {
expect(_component.type()).to.equal(Provider);
});
});
1 change: 0 additions & 1 deletion src/layouts/BaseLayout/BaseLayout.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { IndexLink, Link } from 'react-router';
class BaseLayout extends Component {
render () {
const { mainContent } = this.props;
console.log(this.props);
return (
<div className='container text-center'>
<h1>KNMI React Redux Starter Kit</h1>
Expand Down
12 changes: 5 additions & 7 deletions src/reducers/countReducer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { INCREMENT_COUNT, DOUBLE_COUNT, RESET_COUNTER } from '../constants/countLabels';
import { INCREMENT_COUNT, DOUBLE_COUNT } from '../constants/countLabels';

const incrementCount = (state) => {
return Object.assign({}, state, { count: state.count + 1 });
Expand All @@ -8,21 +8,19 @@ const doubleCount = (state) => {
return Object.assign({}, state, { count: state.count * 2 });
};

const resetCounter = (state) => {
return { count: 0 };
};

const ACTION_HANDLERS = {
[INCREMENT_COUNT] : (state) => incrementCount(state),
[DOUBLE_COUNT] : (state) => doubleCount(state),
[RESET_COUNTER] : (state) => resetCounter(state)
[DOUBLE_COUNT] : (state) => doubleCount(state)
};

// ------------------------------------
// Reducer
// ------------------------------------
const initialState = { count: 0 };
export default function countReducer (state = initialState, action) {
if (!action) {
return state;
}
const handler = ACTION_HANDLERS[action.type];
return handler ? handler(state, action) : state;
}
33 changes: 33 additions & 0 deletions src/reducers/countReducer.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import countReducer from './countReducer';
import sinon from 'sinon';
import actions from '../actions/countActions';
describe('(Store) countReducer', () => {
let _globalState;
let _dispatchSpy;
let _getStateSpy;
beforeEach(() => {
_globalState = countReducer();
_dispatchSpy = sinon.spy((action) => {
_globalState = {
..._globalState,
...countReducer(_globalState, action)
};
});
_getStateSpy = sinon.spy(() => {
return _globalState;
});
});
it('starts with a count of 0', () => {
expect(_globalState).to.exist();
expect(_globalState).to.have.property('count');
expect(_globalState.count).to.equal(0);
});

it('will increment when the increment action is called', () => {
_dispatchSpy(actions.incrementCounter());
const incrementedState = _getStateSpy();
expect(incrementedState).to.exist();
expect(incrementedState).to.have.property('count');
expect(incrementedState.count).to.equal(1);
});
});
15 changes: 15 additions & 0 deletions src/store/createStore.spec.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,17 @@
import createStore from './createStore';
describe('(Store) createStore', () => {
let _store;

beforeEach(() => {
_store = createStore();
});

it('Creates a store object with functions', () => {
expect(_store).to.exist();
expect(_store).to.be.an('object');
expect(_store).to.have.property('dispatch');
expect(_store.dispatch).to.be.a('function');
expect(_store).to.have.property('subscribe');
expect(_store.subscribe).to.be.a('function');
});
});

0 comments on commit 56416b0

Please sign in to comment.