settings.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import nock from 'nock';
  2. import sinon from 'sinon';
  3. import { expect } from 'chai';
  4. import cookie from 'js-cookie';
  5. import thunk from 'redux-thunk';
  6. import configureMockStore from 'redux-mock-store';
  7. import {
  8. getUserSettings,
  9. setCustomDomain,
  10. deleteCustomDomain,
  11. generateApiKey
  12. } from '../settings';
  13. import {
  14. DELETE_DOMAIN,
  15. DOMAIN_LOADING,
  16. API_LOADING,
  17. SET_DOMAIN,
  18. SET_APIKEY
  19. } from '../actionTypes';
  20. const middlewares = [thunk];
  21. const mockStore = configureMockStore(middlewares);
  22. describe('settings actions', () => {
  23. const token =
  24. 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJBcGlBdXRoIiwic3ViIjoidGVzdEBtYWlsLmNvbSIsImRvbWFpbiI6IiIsImlhdCI6MTUyOTEzNzczODcyNSwiZXhwIjoxNTI5MTM3NzM4NzI1fQ.tdI7r11bmSCUmbcJBBKIDt7Hkb7POLMRl8VNJv_8O_s';
  25. let cookieStub;
  26. beforeEach(() => {
  27. cookieStub = sinon.stub(cookie, 'get');
  28. cookieStub.withArgs('token').returns(token);
  29. });
  30. afterEach(() => {
  31. cookieStub.restore();
  32. });
  33. describe('#getUserSettings()', () => {
  34. it('should dispatch SET_APIKEY and SET_DOMAIN when getting user settings have been done', done => {
  35. const apikey = '123';
  36. const customDomain = 'test.com';
  37. nock('http://localhost', {
  38. reqheaders: {
  39. Authorization: token
  40. }
  41. })
  42. .get('/api/auth/usersettings')
  43. .reply(200, { apikey, customDomain });
  44. const store = mockStore({});
  45. const expectedActions = [
  46. {
  47. type: SET_DOMAIN,
  48. payload: customDomain
  49. },
  50. {
  51. type: SET_APIKEY,
  52. payload: apikey
  53. }
  54. ];
  55. store
  56. .dispatch(getUserSettings())
  57. .then(() => {
  58. expect(store.getActions()).to.deep.equal(expectedActions);
  59. done();
  60. })
  61. .catch(error => done(error));
  62. });
  63. });
  64. describe('#setCustomDomain()', () => {
  65. it('should dispatch SET_DOMAIN when setting custom domain has been done', done => {
  66. const customDomain = 'test.com';
  67. nock('http://localhost', {
  68. reqheaders: {
  69. Authorization: token
  70. }
  71. })
  72. .post('/api/url/customdomain')
  73. .reply(200, { customDomain });
  74. const store = mockStore({});
  75. const expectedActions = [
  76. { type: DOMAIN_LOADING },
  77. {
  78. type: SET_DOMAIN,
  79. payload: customDomain
  80. }
  81. ];
  82. store
  83. .dispatch(setCustomDomain(customDomain))
  84. .then(() => {
  85. expect(store.getActions()).to.deep.equal(expectedActions);
  86. done();
  87. })
  88. .catch(error => done(error));
  89. });
  90. });
  91. describe('#deleteCustomDomain()', () => {
  92. it('should dispatch DELETE_DOMAIN when deleting custom domain has been done', done => {
  93. const customDomain = 'test.com';
  94. nock('http://localhost', {
  95. reqheaders: {
  96. Authorization: token
  97. }
  98. })
  99. .delete('/api/url/customdomain')
  100. .reply(200, { customDomain });
  101. const store = mockStore({});
  102. const expectedActions = [{ type: DELETE_DOMAIN }];
  103. store
  104. .dispatch(deleteCustomDomain(customDomain))
  105. .then(() => {
  106. expect(store.getActions()).to.deep.equal(expectedActions);
  107. done();
  108. })
  109. .catch(error => done(error));
  110. });
  111. });
  112. describe('#generateApiKey()', () => {
  113. it('should dispatch SET_APIKEY when generating api key has been done', done => {
  114. const apikey = '123';
  115. nock('http://localhost', {
  116. reqheaders: {
  117. Authorization: token
  118. }
  119. })
  120. .post('/api/auth/generateapikey')
  121. .reply(200, { apikey });
  122. const store = mockStore({});
  123. const expectedActions = [
  124. { type: API_LOADING },
  125. {
  126. type: SET_APIKEY,
  127. payload: apikey
  128. }
  129. ];
  130. store
  131. .dispatch(generateApiKey())
  132. .then(() => {
  133. expect(store.getActions()).to.deep.equal(expectedActions);
  134. done();
  135. })
  136. .catch(error => done(error));
  137. });
  138. });
  139. });