Skip to content

Instantly share code, notes, and snippets.

@Nfinley
Last active March 19, 2018 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nfinley/07b021f6aabeb2df84861d0b22ce6163 to your computer and use it in GitHub Desktop.
Save Nfinley/07b021f6aabeb2df84861d0b22ce6163 to your computer and use it in GitHub Desktop.
A jest snippet for testing a reducer
import $reducerName$ from './$reducerName$';
import * as $actionName$ from '../actions/$actionName$';
//A reducer should return the new state after applying the action to the previous state
describe('$reducerName$ Reducer', () => {
const initialState = {
id: '',
name: 'test'
};
const templates = [{
id: 3,
name: "test1",
},
{
id: 4,
name: "test2",
}
];
const updatedState = {
id: 3,
name: "test1",
};
it('Should return an initial state', () => {
expect(nameOfReducer(undefined, {})).toEqual(initialState);
});
it('Should handle ACTION_NAME', () => {
const action = $reducerName$.actionName(templates);
const newState = nameOfReducer(initialState, action);
expect(newState).toEqual(updatedState);
});
it('Should handle ACTION_NAME', () => {
const newText = "New Text";
const updateState = {
};
const action = $reducerName$.actionName(newText);
const newState = nameOfReducer(initialState, action);
expect(newState).toEqual(updateState);
});
//
it('Should handle ACTION_NAME', () => {
const response = {
id: 1,
};
const addState = {
id: 1,
name: 'test2',
info: "more information etc"
};
const action = $reducerName$.actionName(response);
const newState = nameOfReducer(initialState, action);
expect(newState).toEqual(addState);
});
it('Should handle UACTION_NAME', () => {
const response = {
id: 1,
};
const updatedState = {
id: 1,
name: 'test2',
info: "more information etc"
};
const action = $reducerName$.actionName(response);
const newState = nameOfReducer(initialState, action);
expect(newState).toEqual(updatedState);
});
it('Should return default state', () => {
const action = { type: 'UNSPECIFIED_ACTION', blah: 'blah' };
const newState = nameOfReducer({}, action);
expect(newState).toEqual({});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment