From 731db1991d268361060723a6dc358428d7a3ff2b Mon Sep 17 00:00:00 2001 From: Vesa Luusua Date: Wed, 18 Jan 2017 20:21:14 +0200 Subject: [PATCH] Add tests to ducks --- src/containers/SearchPage/SearchPage.js | 4 +- src/containers/SearchPage/SearchPage.test.js | 50 +++++++++++-- src/containers/SearchPage/SearchPageDucks.js | 10 +-- .../__snapshots__/SearchPage.test.js.snap | 2 +- src/ducks/FlashNotification.test.js | 74 +++++++++++++++++++ src/ducks/index.js | 2 +- 6 files changed, 128 insertions(+), 14 deletions(-) create mode 100644 src/ducks/FlashNotification.test.js diff --git a/src/containers/SearchPage/SearchPage.js b/src/containers/SearchPage/SearchPage.js index 51d8daf6..358f6aef 100644 --- a/src/containers/SearchPage/SearchPage.js +++ b/src/containers/SearchPage/SearchPage.js @@ -5,7 +5,7 @@ import { Page } from '../../components'; import { addFlashNotification } from '../../ducks/FlashNotification'; import { addFilter } from './SearchPageDucks'; -export const SearchPage = props => ( +export const SearchPageComponent = () => ( Nice studio in Helsinki @@ -25,4 +25,4 @@ const mapDispatchToProps = function mapDispatchToProps(dispatch) { }; }; -export default connect(mapStateToProps, mapDispatchToProps)(SearchPage); +export default connect(mapStateToProps, mapDispatchToProps)(SearchPageComponent) diff --git a/src/containers/SearchPage/SearchPage.test.js b/src/containers/SearchPage/SearchPage.test.js index 81419d85..ab212ad8 100644 --- a/src/containers/SearchPage/SearchPage.test.js +++ b/src/containers/SearchPage/SearchPage.test.js @@ -1,16 +1,56 @@ import React from 'react'; import { BrowserRouter } from 'react-router'; import renderer from 'react-test-renderer'; -import { SearchPage } from './SearchPage'; +import { SearchPageComponent } from './SearchPage'; +import reducer, { addFilter } from './SearchPageDucks'; -describe('SearchPage', () => { +describe('SearchPageComponent', () => { it('matches snapshot', () => { const component = renderer.create( - - - , + ( + + + + ), ); const tree = component.toJSON(); expect(tree).toMatchSnapshot(); }); }); + +describe('SearchPageDucs', () => { + const ADD_FILTER = 'ADD_FILTER'; + + describe('actions', () => { + it('should create an action to add a filter', () => { + const expectedAction = { type: ADD_FILTER, payload: { location: 'helsinki' } }; + const serializedExpectations = JSON.stringify(expectedAction); + expect(JSON.stringify(addFilter('location', 'helsinki'))).toEqual(serializedExpectations); + }); + }); + + describe('reducer', () => { + it('should return the initial state', () => { + const initial = reducer(undefined, {}); + expect(initial).toEqual({}); + }); + + it('should handle ADD_FILTER', () => { + const filter1 = { location: 'helsinki' }; + const filter2 = { gears: 3 }; + const addFilter1 = { type: ADD_FILTER, payload: filter1 }; + const addFilter2 = { type: ADD_FILTER, payload: filter2 }; + const reduced = reducer([], addFilter1); + const reducedWithInitialContent = reducer({ filters: [filter1] }, addFilter2); + expect(reduced).toEqual({ filters: [filter1] }); + expect(reducedWithInitialContent).toEqual({ filters: [filter1, filter2] }); + }); + + it('should handle duplicates ADD_FILTER', () => { + const filter = { location: 'helsinki' }; + const addFilter = { type: ADD_FILTER, payload: filter }; + const reducedWithInitialContent = reducer({ filters: [filter] }, addFilter); + expect(reducedWithInitialContent).toEqual({ filters: [filter] }); + }); + }); +}); diff --git a/src/containers/SearchPage/SearchPageDucks.js b/src/containers/SearchPage/SearchPageDucks.js index 8f2a3ecd..2316e6c8 100644 --- a/src/containers/SearchPage/SearchPageDucks.js +++ b/src/containers/SearchPage/SearchPageDucks.js @@ -13,14 +13,14 @@ export default function reducer(state = {}, action = {}) { const { type, payload } = action; switch (type) { case ADD_FILTER: - const stateFilters = state.filters || []; - return Object.assign({}, state, { filters: unionBy(stateFilters, [ payload ]) }); + { + const stateFilters = state.filters || []; + return Object.assign({}, state, { filters: unionBy(stateFilters, [ payload ]) }); + } default: return state; } } // Action Creators -export const addFilter = (key, value) => { - return { type: ADD_FILTER, payload: { [key]: value } }; -}; +export const addFilter = (key, value) => ({ type: ADD_FILTER, payload: { [key]: value } }); diff --git a/src/containers/SearchPage/__snapshots__/SearchPage.test.js.snap b/src/containers/SearchPage/__snapshots__/SearchPage.test.js.snap index cf17ef7e..3009aeb6 100644 --- a/src/containers/SearchPage/__snapshots__/SearchPage.test.js.snap +++ b/src/containers/SearchPage/__snapshots__/SearchPage.test.js.snap @@ -1,4 +1,4 @@ -exports[`SearchPage matches snapshot 1`] = ` +exports[`SearchPageComponent matches snapshot 1`] = `
{ + const ADD_FLASH_NOTIFICATION = 'FLASH::ADD_NOTIFICATION'; + const REMOVE_FLASH_NOTIFICATION = 'FLASH::REMOVE_NOTIFICATION'; + + describe('actions', () => { + it('should create an action to add a filter', () => { + const content = 'Error message'; + const type = 'error'; + const expectedAction = { + type: ADD_FLASH_NOTIFICATION, + payload: { + id: 'note_1', + type, + content, + isRead: false, + } + }; + const serializedExpectations = JSON.stringify(expectedAction); + const received = JSON.stringify(addFlashNotification(type, content)); + expect(received).toEqual(serializedExpectations); + }); + + it('should create an action to remove a notification', () => { + const expectedAction = { + type: REMOVE_FLASH_NOTIFICATION, + payload: { id: 1 }, + }; + + expect(removeFlashNotification(1)).toEqual(expectedAction); + }); + }); + + describe('reducer', () => { + it('should return the initial state', () => { + const initial = reducer(undefined, {}); + expect(initial).toEqual([]); + }); + + it('should handle ADD_FLASH_NOTIFICATION', () => { + const flashNote1 = { + id: 0, + type: 'error', + content: 'Run the tests', + isRead: false, + }; + const flashNote2 = { + id: 0, + type: 'error', + content: 'Run the tests again', + isRead: false, + }; + const addFlashNote1 = { type: ADD_FLASH_NOTIFICATION, payload: flashNote1 }; + const addFlashNote2 = { type: ADD_FLASH_NOTIFICATION, payload: flashNote2 }; + const reduced = reducer([], addFlashNote1); + const reducedWithInitialContent = reducer([flashNote1], addFlashNote2); + expect(reduced).toEqual([flashNote1]); + expect(reducedWithInitialContent).toEqual([flashNote1, flashNote2]); + }); + + it('should handle duplicates ADD_FILTER', () => { + const flashNote = { + id: 0, + type: 'error', + content: 'Run the tests', + isRead: false, + }; + const addFlashNote = { type: ADD_FLASH_NOTIFICATION, payload: flashNote }; + const reducedWithInitialContent = reducer([flashNote], addFlashNote); + expect(reducedWithInitialContent).toEqual([flashNote]); + }); + }); +}); diff --git a/src/ducks/index.js b/src/ducks/index.js index 3f31556a..85fe9511 100644 --- a/src/ducks/index.js +++ b/src/ducks/index.js @@ -6,4 +6,4 @@ import FlashNotification from './FlashNotification'; -export { FlashNotification }; +export { FlashNotification }; // eslint-disable-line import/prefer-default-export