Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2018
127
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. password: 'Testuser1',
  15. },
  16. };
  17. let store;
  18.  
  19.  
  20. describe('async login actions', () => {
  21. const mockStore = configureStore([thunk]);
  22.  
  23. beforeEach(() => {
  24. store = mockStore({
  25. message: '',
  26. user: {},
  27. status: 'none',
  28. });
  29. });
  30.  
  31. afterEach(() => {
  32. fetchMock.reset();
  33. fetchMock.restore();
  34. });
  35.  
  36. it('creates LOGIN_FAILURE when login is unsuccessful', () => {
  37. const data = {
  38. status: 404,
  39. errors: {
  40. error: ['A user with this email and password was not found.'],
  41. },
  42. };
  43. fetchMock.post(`${baseurl}/api/users/login/`, data);
  44. const expectedActions = [
  45. {
  46. type: LOGIN_REQUEST,
  47. },
  48. {
  49. type: LOGIN_FAILURE,
  50. payload: data,
  51. },
  52. ];
  53. return store.dispatch(actions.loginAction(userdata)).then(() => {
  54. expect(store.getActions()).toEqual(expectedActions);
  55. });
  56. });
  57.  
  58. it('creates LOGIN_SUCCESSFUL when login is successful', () => {
  59. const data = {
  60. status: 200,
  61. user: {
  62. token: 'user-token',
  63. username: 'testuser',
  64. },
  65. };
  66.  
  67. fetchMock.post(`${baseurl}/api/users/login/`, data);
  68. const expectedActions = [
  69. {
  70. type: LOGIN_REQUEST,
  71. },
  72. {
  73. type: LOGIN_SUCCESS,
  74. payload: data,
  75. },
  76. ];
  77. return store.dispatch(actions.loginAction(userdata)).then(() => {
  78. expect(store.getActions()).toEqual(expectedActions);
  79. });
  80. });
  81. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement