Guest User

Untitled

a guest
Nov 11th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. import configureMockStore from 'redux-mock-store';
  2. import thunk from 'redux-thunk';
  3. import fetchMock from 'fetch-mock';
  4. import keyMirror from 'keymirror';
  5. import configureStore from 'redux-mock-store';
  6. import * as actions from '../actions/loginActions';
  7. import {
  8. LOGIN_FAILURE,
  9. LOGIN_SUCCESS,
  10. LOGIN_REQUEST,
  11. } from '../actions/types';
  12.  
  13.  
  14. const responsedata_success = {
  15. user: {
  16. email: 'testuser@email.com',
  17. username: 'testuser',
  18. token: 'user-token',
  19. },
  20. };
  21.  
  22. const responsedata_failure = {
  23. errors: {
  24. error: ['A user with this email and password was not found.'],
  25. },
  26. };
  27.  
  28. const types = keyMirror({
  29. LOGIN_FAILURE: null,
  30. LOGIN_SUCCESS: null,
  31. });
  32.  
  33. describe('sync login actions', () => {
  34. it('has a LOGIN_SUCCESS action', () => {
  35. const expectedAction = {
  36. type: LOGIN_SUCCESS,
  37. payload: responsedata_success,
  38. };
  39. expect(actions.loginSuccess(responsedata_success)).toEqual(expectedAction);
  40. });
  41.  
  42. it('has a LOGIN_FAILURE action', () => {
  43. const expectedAction = {
  44. type: LOGIN_FAILURE,
  45. payload: responsedata_failure,
  46. };
  47. expect(actions.loginFailure(responsedata_failure)).toEqual(expectedAction);
  48. });
  49. });
  50.  
  51.  
  52. describe('async login actions', () => {
  53. const mockStore = configureStore([thunk]);
  54. let store;
  55.  
  56. beforeEach(() => {
  57. store = mockStore({
  58. message: '',
  59. user: {},
  60. status: 'none',
  61. });
  62. });
  63.  
  64. afterEach(() => {
  65. fetchMock.reset();
  66. fetchMock.restore();
  67. });
  68.  
  69. it('creates LOGIN_SUCCESSFUL when login is successful', () => {
  70. const userdata = {
  71. user: {
  72. email: 'testuser@email.com',
  73. password: 'Testuser1',
  74. },
  75. };
  76. fetchMock.getOnce('/api/u sers/login/', {
  77. method: 'POST',
  78. headers: {
  79. 'Content-type': 'application/json',
  80. },
  81. body: JSON.stringify(userdata),
  82. });
  83. const expectedActions = [
  84. {
  85. type: LOGIN_REQUEST,
  86. },
  87. {
  88. type: LOGIN_SUCCESS,
  89. payload: responsedata_success,
  90. }];
  91. const store = mockStore;
  92.  
  93. return store.dispatch(actions.loginAction(userdata)).then(() => {
  94. expect(store.getActions()).toEqual(expectedActions);
  95. });
  96. });
  97. });
Add Comment
Please, Sign In to add comment