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..52f5c10 --- /dev/null +++ b/app/src/containers/FeatureFirstContainer/tests/actions.test.js @@ -0,0 +1,56 @@ +import expect from 'expect'; +import * as actions from '../actions'; +import { + LOAD_DATA_INITIATION, + LOAD_DATA_SUCCESS, + LOAD_DATA_FAILURE, + 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 = { + 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..5ed8aab --- /dev/null +++ b/app/src/containers/FeatureFirstContainer/tests/reducer.test.js @@ -0,0 +1,91 @@ +import * as types from '../constants'; +import reducer from '../reducer'; +import expect from 'expect'; + +const initialState = { + isLoading: false, + data: {}, + error: {}, +}; + +// 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); + }); +});