auth.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { expect } from 'chai';
  2. import deepFreeze from 'deep-freeze';
  3. import {
  4. AUTH_USER,
  5. AUTH_RENEW,
  6. UNAUTH_USER,
  7. SENT_VERIFICATION
  8. } from '../../actions/actionTypes';
  9. import reducer from '../auth';
  10. describe('auth reducer', () => {
  11. const initialState = {
  12. admin: false,
  13. isAuthenticated: false,
  14. sentVerification: false,
  15. user: '',
  16. renew: false,
  17. };
  18. beforeEach(() => {
  19. deepFreeze(initialState);
  20. });
  21. it('should return the initial state', () => {
  22. expect(reducer(undefined, {})).to.deep.equal(initialState);
  23. });
  24. it('should handle AUTH_USER', () => {
  25. const jwt = {
  26. domain: '',
  27. exp: 1529137738725,
  28. iat: 1529137738725,
  29. iss: 'ApiAuth',
  30. sub: 'test@user.com',
  31. };
  32. const user = 'test@user.com';
  33. const state = reducer(initialState, {
  34. type: AUTH_USER,
  35. payload: jwt
  36. });
  37. expect(state).not.to.be.undefined;
  38. expect(state.isAuthenticated).to.be.true;
  39. expect(state.user).to.be.equal(user);
  40. expect(state.sentVerification).to.be.false;
  41. });
  42. it('should handle AUTH_RENEW', () => {
  43. const state = reducer(initialState, {
  44. type: AUTH_RENEW
  45. });
  46. expect(state).not.to.be.undefined;
  47. expect(state.renew).to.be.true;
  48. });
  49. it('should handle UNAUTH_USER', () => {
  50. const state = reducer(initialState, {
  51. type: UNAUTH_USER
  52. });
  53. expect(state).not.to.be.undefined;
  54. expect(state).to.deep.equal(initialState);
  55. });
  56. it('should handle SENT_VERIFICATION', () => {
  57. const user = 'test@user.com';
  58. const state = reducer(initialState, {
  59. type: SENT_VERIFICATION,
  60. payload: user
  61. });
  62. expect(state).not.to.be.undefined;
  63. expect(state.sentVerification).to.be.true;
  64. expect(state.user).to.be.equal(user);
  65. });
  66. it('should not handle other action types', () => {
  67. const state = reducer(initialState, {
  68. type: 'ANOTHER_ACTION'
  69. });
  70. expect(state).not.to.be.undefined;
  71. expect(state).to.deep.equal(initialState);
  72. });
  73. });