Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. import thunk from 'redux-thunk';
  2. import fetchMock from 'fetch-mock';
  3. import configureStore from 'redux-mock-store';
  4. import * as actions from '../actions/loginActions';
  5. import {
  6. LOGIN_FAILURE,
  7. LOGIN_SUCCESS,
  8. LOGIN_REQUEST,
  9. } from '../actions/types';
  10.  
  11. const baseurl = 'https://ah-backend-stark-staging.herokuapp.com';
  12. const userdata = {
  13. user: {
  14. email: 'testuser@email.com',
  15. password: 'Testuser1',
  16. },
  17. };
  18. let store;
  19.  
  20.  
  21. describe('async login actions', () => {
  22. const mockStore = configureStore([thunk]);
  23.  
  24. beforeEach(() => {
  25. store = mockStore({
  26. message: '',
  27. user: {},
  28. status: 'none',
  29. });
  30. });
  31.  
  32. afterEach(() => {
  33. fetchMock.reset();
  34. fetchMock.restore();
  35. });
  36.  
  37. it('creates LOGIN_FAILURE when login is unsuccessful', () => {
  38. const data = {
  39. status: 404,
  40. errors: {
  41. error: ['A user with this email and password was not found.'],
  42. },
  43. };
  44. fetchMock.post(`${baseurl}/api/users/login/`, data);
  45. const expectedActions = [
  46. {
  47. type: LOGIN_REQUEST,
  48. },
  49. {
  50. type: LOGIN_FAILURE,
  51. payload: data,
  52. },
  53. ];
  54. return store.dispatch(actions.loginAction(userdata)).then(() => {
  55. expect(store.getActions()).toEqual(expectedActions);
  56. });
  57. });
  58.  
  59. it('creates LOGIN_SUCCESSFUL when login is successful', () => {
  60. const data = {
  61. status: 200,
  62. user: {
  63. token: 'user-token',
  64. username: 'testuser',
  65. email: 'testuser@email.com',
  66. },
  67. };
  68.  
  69. fetchMock.post(`${baseurl}/api/users/login/`, data);
  70. const expectedActions = [
  71. {
  72. type: LOGIN_REQUEST,
  73. },
  74. {
  75. type: LOGIN_SUCCESS,
  76. payload: data,
  77. },
  78. ];
  79. return store.dispatch(actions.loginAction(userdata)).then(() => {
  80. expect(store.getActions()).toEqual(expectedActions);
  81. });
  82. });
  83. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement