Guest User

Untitled

a guest
Mar 20th, 2018
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. /* =============== CONSTANTS ============================ */
  2.  
  3. export const AWS_SIGN_UP_SAVING = 'AWS_SIGNUP_SAVE';
  4. export const AWS_SIGN_UP_FAIL = 'AWS_SIGN_UP_FAIL';
  5. export const AWS_SIGN_UP_SUCCESS = 'AWS_SIGN_UP_SUCCESS';
  6. export const THROW_ERROR = 'THROW_ERROR';
  7. export const CLEAR_ERROR = 'CLEAR_ERROR';
  8.  
  9.  
  10.  
  11. /* ================== ACTIONS ================================= */
  12. import {
  13. THROW_ERROR,
  14. CLEAR_ERROR,
  15. AWS_SIGN_UP_SAVING,
  16. AWS_SIGN_UP_FAIL,
  17. AWS_SIGN_UP_SUCCESS
  18. } from '../_constants/actionTypes.js'
  19.  
  20. export function throwError(data) {
  21. return {
  22. type: THROW_ERROR,
  23. data
  24. }
  25. };
  26. export function clearError(data) {
  27. return {
  28. type: CLEAR_ERROR,
  29. data
  30. }
  31. };
  32. export function awsSignUpUser(data) {
  33. return {
  34. type: AWS_SIGN_UP_SAVING,
  35. data
  36. }
  37. };
  38. export function awsSignUpUserFailed(data){
  39. return {
  40. type: AWS_SIGN_UP_FAIL,
  41. data
  42. }
  43. }
  44. export function awsSignUpUserSuccess(data){
  45. return {
  46. type: AWS_SIGN_UP_SUCCESS,
  47. data
  48. }
  49. }
  50.  
  51.  
  52. /**
  53. * THUNKS
  54. */
  55. export function AWS_SignUpUser(username, password) {
  56. return (dispatch => {
  57. const data = {username, password};
  58. return dispatch(this.awsSignUpUser(data));
  59. });
  60. };
  61.  
  62.  
  63. /* ====================== TESTS ============================== */
  64.  
  65. import * as ActionTypes from '../../_constants/actionTypes';
  66. import * as ActionCreators from '../registerActions';
  67.  
  68. describe('Actions', () => {
  69. let username = "fake@fake.com";
  70. let password = "@Password1";
  71.  
  72. it("Should start the process of registering a user", () => {
  73. const dispatch = jest.fn();
  74. const expected = {
  75. type: ActionTypes.AWS_SIGN_UP_SAVING,
  76. data: {username, password}
  77. };
  78.  
  79. //we expect this to return a function since it is a thunk
  80. expect(typeof (ActionCreators.AWS_SignUpUser(username, password))).toEqual('function');
  81.  
  82. //then we simulate calling it with dispatch as the store would do
  83. ActionCreators.AWS_SignUpUser(username, password)(dispatch);
  84.  
  85. //finally assert that the dispatch was called with our expected action
  86. expect(dispatch).toBeCalledWith(expected);
  87. });
  88.  
  89. it("should create an action to fail a signup", () => {
  90. const data = {errorCode: 500, message: 'User already exists'};
  91. const expected = {
  92. type: ActionTypes.AWS_SIGN_UP_FAIL,
  93. data: data
  94. }
  95.  
  96. const actual = ActionCreators.awsSignUpUserFailed(data);
  97.  
  98. expect(actual).toEqual(expected);
  99.  
  100. });
  101.  
  102. it("should create an action to signup a user", () => {
  103. const data = {cognito_user: 'fake@fake.com'};
  104. const expected = {
  105. type: ActionTypes.AWS_SIGN_UP_SUCCESS,
  106. data
  107. }
  108.  
  109. const actual = ActionCreators.awsSignUpUserSuccess(data);
  110.  
  111. expect(actual).toEqual(expected);
  112.  
  113. });
  114.  
  115. it("should create an action to throw an error", () => {
  116. const data = {error: {code:500, mesage: 'Something went wrong'}};
  117. const expected = {
  118. type: ActionTypes.THROW_ERROR,
  119. data
  120. }
  121.  
  122. const actual = ActionCreators.throwError(data);
  123.  
  124. expect(actual).toEqual(expected);
  125.  
  126. });
  127.  
  128. it("should create an action to clear an error", () => {
  129. const data = {error: null}
  130. const expected = {
  131. type: ActionTypes.CLEAR_ERROR,
  132. data
  133. }
  134.  
  135. const actual = ActionCreators.clearError(data);
  136.  
  137. expect(actual).toEqual(expected);
  138.  
  139. });
  140.  
  141. });
Add Comment
Please, Sign In to add comment