Last active
November 2, 2017 16:48
-
-
Save sriramrudraraju/ede041278b0da13c98104b1f30296967 to your computer and use it in GitHub Desktop.
React Redux Unit Testing with Jest part 2: Reducers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @flow | |
import { SET_H1, SET_TITLE } from '../actions/types'; | |
type DomActionType = { | |
type: string, | |
payload: string | |
}; | |
type DomStateType = { | |
h1: string, | |
title: string | |
}; | |
// always heve a default state oresle reducers return undefined, which is a pain to decode | |
const INITIAL_STATE = { | |
h1: '', | |
title: '' | |
}; | |
// dont mutate the state. create new state and return it.. rule of reducer | |
export default function Dom_Reducer( | |
state: DomStateType = INITIAL_STATE, | |
action: DomActionType | |
): DomStateType { | |
switch (action.type) { | |
case SET_H1: | |
// change H1 | |
return { ...state, h1: action.payload }; | |
case SET_TITLE: | |
//change title | |
return { ...state, title: action.payload }; | |
default: | |
return state; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Dom_Reducer from './dom_reducer'; | |
//test suite for all reducers | |
describe('Reducers', () => { | |
//test suite fro dom reduecer | |
describe('dom_reduecer', () => { | |
//checking for case: default | |
it('case: default', () => { | |
expect(Dom_Reducer(undefined, {})).toEqual({ | |
h1: '', | |
title: '' | |
}); | |
}); | |
//checking for case: SET_H1 | |
it('case: SET_H1', () => { | |
let h1 = 'some random header'; | |
expect( | |
Dom_Reducer(undefined, { | |
type: 'SET_H1', | |
payload: h1 | |
}) | |
).toEqual({ | |
h1: h1, | |
title: '' | |
}); | |
}); | |
//checking for case: SET_TITLE | |
it('case: SET_TITLE', () => { | |
let title = 'some random title'; | |
expect( | |
Dom_Reducer(undefined, { | |
type: 'SET_TITLE', | |
payload: title | |
}) | |
).toEqual({ | |
h1: '', | |
title: title | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment