Advertisement
Guest User

Untitled

a guest
Nov 13th, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 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 { LOGIN_FAILURE, LOGIN_SUCCESS, LOGIN_REQUEST } from '../actions/types';
  6.  
  7.  
  8. const responsedata_success = {
  9. user: {
  10. email: 'testuser@email.com',
  11. username: 'testuser',
  12. token: 'user-token',
  13. },
  14. };
  15. const responsedata_failure = {
  16. errors: {
  17. error: ['A user with this email and password was not found.'],
  18. },
  19. };
  20. const baseurl = 'http://127.0.0.1:8000';
  21. const userdata = {
  22. user: {
  23. email: 'testuser@email.com',
  24. password: 'Testuser1',
  25. },
  26. };
  27.  
  28.  
  29. describe('sync login actions', () => {
  30. it('has a LOGIN_SUCCESS action', () => {
  31. const expectedAction = {
  32. type: LOGIN_SUCCESS,
  33. payload: responsedata_success,
  34. };
  35. expect(actions.loginSuccess(responsedata_success)).toEqual(expectedAction);
  36. });
  37.  
  38. it('has a LOGIN_FAILURE action', () => {
  39. const expectedAction = {
  40. type: LOGIN_FAILURE,
  41. payload: responsedata_failure,
  42. };
  43. expect(actions.loginFailure(responsedata_failure)).toEqual(expectedAction);
  44. });
  45. });
  46.  
  47. let store;
  48.  
  49. describe('async login actions', () => {
  50. const mockStore = configureStore([thunk]);
  51.  
  52. beforeEach(() => {
  53. store = mockStore({
  54. message: '',
  55. user: {},
  56. status: 'none',
  57. });
  58. });
  59.  
  60. afterEach(() => {
  61. fetchMock.reset();
  62. fetchMock.restore();
  63. });
  64.  
  65. it('creates LOGIN_FAILURE when login is unsuccessful', () => {
  66.  
  67. let data = {
  68. status: 404,
  69. errors: {
  70. error: ['A user with this email and password was not found.'],
  71. },
  72. };
  73. fetchMock.post(`${baseurl}/api/users/login/`, data);
  74. const expectedActions = [
  75. {
  76. type: LOGIN_REQUEST,
  77. },
  78. {
  79. type: LOGIN_FAILURE,
  80. payload: data,
  81. },
  82. ];
  83. return store.dispatch(actions.loginAction(userdata)).then(() => {
  84. expect(store.getActions()).toEqual(expectedActions);
  85. });
  86. });
  87.  
  88. it('creates LOGIN_SUCCESSFUL when login is successful', () => {
  89.  
  90. let data = {
  91. status: 200,
  92. user: {
  93. token: 'user-token',
  94. username: 'testuser',
  95. email: 'testuser@email.com',
  96. },
  97. };
  98.  
  99. fetchMock.post(`${baseurl}/api/users/login/`, data);
  100. const expectedActions = [
  101. {
  102. type: LOGIN_REQUEST,
  103. },
  104. {
  105. type: LOGIN_SUCCESS,
  106. payload: data,
  107. },
  108. ];
  109. return store.dispatch(actions.loginAction(userdata)).then(() => {
  110. expect(store.getActions()).toEqual(expectedActions);
  111. });
  112. });
  113.  
  114.  
  115. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement