Created
March 11, 2018 14:55
-
-
Save wesleyduff/81365800916ead14e5c6531a14b95d0d to your computer and use it in GitHub Desktop.
Testing with JEST : Redux Actions and Thunks
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
/* =============== CONSTANTS ============================ */ | |
export const AWS_SIGN_UP_SAVING = 'AWS_SIGNUP_SAVE'; | |
export const AWS_SIGN_UP_FAIL = 'AWS_SIGN_UP_FAIL'; | |
export const AWS_SIGN_UP_SUCCESS = 'AWS_SIGN_UP_SUCCESS'; | |
export const THROW_ERROR = 'THROW_ERROR'; | |
export const CLEAR_ERROR = 'CLEAR_ERROR'; | |
/* ================== ACTIONS ================================= */ | |
import { | |
THROW_ERROR, | |
CLEAR_ERROR, | |
AWS_SIGN_UP_SAVING, | |
AWS_SIGN_UP_FAIL, | |
AWS_SIGN_UP_SUCCESS | |
} from '../_constants/actionTypes.js' | |
export function throwError(data) { | |
return { | |
type: THROW_ERROR, | |
data | |
} | |
}; | |
export function clearError(data) { | |
return { | |
type: CLEAR_ERROR, | |
data | |
} | |
}; | |
export function awsSignUpUser(data) { | |
return { | |
type: AWS_SIGN_UP_SAVING, | |
data | |
} | |
}; | |
export function awsSignUpUserFailed(data){ | |
return { | |
type: AWS_SIGN_UP_FAIL, | |
data | |
} | |
} | |
export function awsSignUpUserSuccess(data){ | |
return { | |
type: AWS_SIGN_UP_SUCCESS, | |
data | |
} | |
} | |
/** | |
* THUNKS | |
*/ | |
export function AWS_SignUpUser(username, password) { | |
return (dispatch => { | |
const data = {username, password}; | |
return dispatch(this.awsSignUpUser(data)); | |
}); | |
}; | |
/* ====================== TESTS ============================== */ | |
import * as ActionTypes from '../../_constants/actionTypes'; | |
import * as ActionCreators from '../registerActions'; | |
describe('Actions', () => { | |
let username = "[email protected]"; | |
let password = "@Password1"; | |
it("Should start the process of registering a user", () => { | |
const dispatch = jest.fn(); | |
const expected = { | |
type: ActionTypes.AWS_SIGN_UP_SAVING, | |
data: {username, password} | |
}; | |
//we expect this to return a function since it is a thunk | |
expect(typeof (ActionCreators.AWS_SignUpUser(username, password))).toEqual('function'); | |
//then we simulate calling it with dispatch as the store would do | |
ActionCreators.AWS_SignUpUser(username, password)(dispatch); | |
//finally assert that the dispatch was called with our expected action | |
expect(dispatch).toBeCalledWith(expected); | |
}); | |
it("should create an action to fail a signup", () => { | |
const data = {errorCode: 500, message: 'User already exists'}; | |
const expected = { | |
type: ActionTypes.AWS_SIGN_UP_FAIL, | |
data: data | |
} | |
const actual = ActionCreators.awsSignUpUserFailed(data); | |
expect(actual).toEqual(expected); | |
}); | |
it("should create an action to signup a user", () => { | |
const data = {cognito_user: '[email protected]'}; | |
const expected = { | |
type: ActionTypes.AWS_SIGN_UP_SUCCESS, | |
data | |
} | |
const actual = ActionCreators.awsSignUpUserSuccess(data); | |
expect(actual).toEqual(expected); | |
}); | |
it("should create an action to throw an error", () => { | |
const data = {error: {code:500, mesage: 'Something went wrong'}}; | |
const expected = { | |
type: ActionTypes.THROW_ERROR, | |
data | |
} | |
const actual = ActionCreators.throwError(data); | |
expect(actual).toEqual(expected); | |
}); | |
it("should create an action to clear an error", () => { | |
const data = {error: null} | |
const expected = { | |
type: ActionTypes.CLEAR_ERROR, | |
data | |
} | |
const actual = ActionCreators.clearError(data); | |
expect(actual).toEqual(expected); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment