Skip to content

Instantly share code, notes, and snippets.

@abachuk
Created September 12, 2017 22:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abachuk/1de3422e657ff2524a1dcb8cfd37d334 to your computer and use it in GitHub Desktop.
Save abachuk/1de3422e657ff2524a1dcb8cfd37d334 to your computer and use it in GitHub Desktop.
import reducer from './getPostReducer';
import * as actions from '../actions/posts/getPost';
import { UPDATE_POST_SUCCESS } from '../actions/posts/updatePost';
import expect from 'expect';
import getPostMock from '../mocks/getPostMock';
describe('post reducer', () => {
it('should return the initial state', () => {
expect(reducer(undefined, {})).toEqual({});
});
it('should handle GET_POST_START', () => {
const startAction = {
type: actions.GET_POST_START
};
// it's empty on purpose because it's just starting to fetch posts
expect(reducer({}, startAction)).toEqual({});
});
it('should handle GET_POST_SUCCESS', () => {
const successAction = {
type: actions.GET_POST_SUCCESS,
post: getPostMock.data, // important to pass correct payload, that's what the tests are for ;)
};
expect(reducer({}, successAction)).toEqual(getPostMock.data);
});
it('should handle UPDATE_POST_SUCCESS', () => {
const updateAction = {
type: UPDATE_POST_SUCCESS,
post: getPostMock.data,
};
expect(reducer({}, updateAction)).toEqual(getPostMock.data);
});
it('should handle GET_POST_FAIL', () => {
const failAction = {
type: actions.GET_POST_FAIL,
error: { success: false },
};
expect(reducer({}, failAction)).toEqual({ error: { success: false } });
});
});
@rationality6
Copy link

Thank you for the test code example!

Copy link

ghost commented Mar 25, 2019

what is 'getPostMock'?

@brayanL
Copy link

brayanL commented Aug 1, 2019

Thanks, bro, I would like to know the content of getPostMock file..!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment