From 69665145f6b5b9a79a0e625887459a27d11f9c2d Mon Sep 17 00:00:00 2001 From: Ryan Collins Date: Tue, 23 Aug 2016 14:31:30 -0400 Subject: [PATCH 1/2] Feat: add action and reducer tests --- .../tests/actions.test.js | 51 +++++++++++++++++++ .../tests/reducer.test.js | 17 +++++++ 2 files changed, 68 insertions(+) create mode 100644 app/src/containers/FeatureFirstContainer/tests/actions.test.js create mode 100644 app/src/containers/FeatureFirstContainer/tests/reducer.test.js diff --git a/app/src/containers/FeatureFirstContainer/tests/actions.test.js b/app/src/containers/FeatureFirstContainer/tests/actions.test.js new file mode 100644 index 0000000..d139ff9 --- /dev/null +++ b/app/src/containers/FeatureFirstContainer/tests/actions.test.js @@ -0,0 +1,51 @@ +import expect from 'expect'; +import * as actions from '../actions'; +import { + LOAD_DATA_INITIATION, + LOAD_DATA_SUCCESS, + LOAD_DATA_FAILURE, + CLEAR_DATA_ERROR, +} from '../constants'; + +describe('FeatureFirstContainer actions', () => { + it('should dispatch an action to initiate the loading process', () => { + const expectedAction = { + type: LOAD_DATA_INITIATION, + }; + expect( + actions.loadDataInitiation() + ).toEqual(expectedAction); + }); + it('should dispatch an action to successfully finish loading', () => { + const data = { + items: [], + }; + const expectedAction = { + type: LOAD_DATA_SUCCESS, + data, + }; + expect( + actions.loadDataSuccess(data) + ).toEqual(expectedAction); + }); + it('should dispatch an action with an error describing a failure to load data', () => { + const error = { + message: 'An error occured', + }; + const expectedAction = { + type: LOAD_DATA_FAILURE, + error, + }; + expect( + actions.loadDataFailure(error) + ).toEqual(expectedAction); + }); + it('should dispatch an action to clear the error', () => { + const expectedAction = { + type: CLEAR_DATA_ERROR, + }; + expect( + actions.clearDataError() + ).toEqual(expectedAction); + }); +}); diff --git a/app/src/containers/FeatureFirstContainer/tests/reducer.test.js b/app/src/containers/FeatureFirstContainer/tests/reducer.test.js new file mode 100644 index 0000000..1edd993 --- /dev/null +++ b/app/src/containers/FeatureFirstContainer/tests/reducer.test.js @@ -0,0 +1,17 @@ +import * as types from '../constants'; +import reducer from '../reducer'; +import expect from 'expect'; + +const initialState = { + isLoading: false, + data: {}, + error: {}, +}; + +describe('FeatureFirstContainer Reducer', () => { + it('should return the initialState', () => { + expect( + reducer(undefined, {}) + ).toEqual(initialState); + }); +}); From 5abd185704c2bc5c21040dbca850fcf0b8c35cc9 Mon Sep 17 00:00:00 2001 From: Ryan Collins Date: Tue, 23 Aug 2016 14:38:49 -0400 Subject: [PATCH 2/2] Feat: add reducer and action tests --- .../tests/actions.test.js | 5 ++ .../tests/reducer.test.js | 76 ++++++++++++++++++- 2 files changed, 80 insertions(+), 1 deletion(-) diff --git a/app/src/containers/FeatureFirstContainer/tests/actions.test.js b/app/src/containers/FeatureFirstContainer/tests/actions.test.js index d139ff9..52f5c10 100644 --- a/app/src/containers/FeatureFirstContainer/tests/actions.test.js +++ b/app/src/containers/FeatureFirstContainer/tests/actions.test.js @@ -7,6 +7,11 @@ import { CLEAR_DATA_ERROR, } from '../constants'; +// Testing actions is as easy as validating that the actions are dispatch +// The way you think they are being dispatched. +// Just test that your expected Action object is what is actually dispatched. +// If you need help, +// See here: http://redux.js.org/docs/recipes/WritingTests.html describe('FeatureFirstContainer actions', () => { it('should dispatch an action to initiate the loading process', () => { const expectedAction = { diff --git a/app/src/containers/FeatureFirstContainer/tests/reducer.test.js b/app/src/containers/FeatureFirstContainer/tests/reducer.test.js index 1edd993..5ed8aab 100644 --- a/app/src/containers/FeatureFirstContainer/tests/reducer.test.js +++ b/app/src/containers/FeatureFirstContainer/tests/reducer.test.js @@ -8,10 +8,84 @@ const initialState = { error: {}, }; -describe('FeatureFirstContainer Reducer', () => { +// testing reducers is really simple. +// pass the reducer initial state, an action and assert the output. +// Easy as cake, but if you need help +// See here: http://redux.js.org/docs/recipes/WritingTests.html +describe('featureComponent reducer', () => { it('should return the initialState', () => { expect( reducer(undefined, {}) ).toEqual(initialState); }); + it('should initiate loading', () => { + const stateAfter = { + isLoading: true, + data: {}, + error: {}, + }; + expect( + reducer(initialState, { + type: types.LOAD_DATA_INITIATION, + }) + ).toEqual(stateAfter); + }); + it('should load data successfully', () => { + const data = { + items: ['🤓', '😎', '🤔'], + }; + const stateAfter = { + isLoading: false, + data, + error: {}, + }; + expect( + reducer( + initialState, + { + type: types.LOAD_DATA_SUCCESS, + data, + } + ) + ).toEqual(stateAfter); + }); + it('should fail gracefully when the data doesn\'t load', () => { + const error = { + message: 'An error occured', + }; + const stateAfter = { + isLoading: false, + data: {}, + error, + }; + expect( + reducer( + initialState, + { + type: types.LOAD_DATA_FAILURE, + error, + } + ) + ).toEqual(stateAfter); + }); + it('should clear the errors', () => { + const stateBefore = { + isLoading: false, + error: { message: 'An error has occured' }, + data: {}, + }; + const stateAfter = { + isLoading: false, + error: {}, + data: {}, + }; + expect( + reducer( + stateBefore, + { + type: types.CLEAR_DATA_ERROR, + } + ) + ).toEqual(stateAfter); + }); });