settings.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. const homepage = '';
  38. const useHttps = false;
  39. nock('http://localhost', {
  40. reqheaders: {
  41. Authorization: token
  42. }
  43. })
  44. .get('/api/auth/usersettings')
  45. .reply(200, { apikey, customDomain, homepage, useHttps });
  46. const store = mockStore({});
  47. const expectedActions = [
  48. {
  49. type: SET_DOMAIN,
  50. payload: {
  51. customDomain,
  52. homepage: '',
  53. useHttps: false,
  54. }
  55. },
  56. {
  57. type: SET_APIKEY,
  58. payload: apikey
  59. }
  60. ];
  61. store
  62. .dispatch(getUserSettings())
  63. .then(() => {
  64. expect(store.getActions()).to.deep.equal(expectedActions);
  65. done();
  66. })
  67. .catch(error => done(error));
  68. });
  69. });
  70. describe('#setCustomDomain()', () => {
  71. it('should dispatch SET_DOMAIN when setting custom domain has been done', done => {
  72. const customDomain = 'test.com';
  73. const homepage = '';
  74. const useHttps = false;
  75. nock('http://localhost', {
  76. reqheaders: {
  77. Authorization: token
  78. }
  79. })
  80. .post('/api/url/customdomain')
  81. .reply(200, { customDomain, homepage, useHttps });
  82. const store = mockStore({});
  83. const expectedActions = [
  84. { type: DOMAIN_LOADING },
  85. {
  86. type: SET_DOMAIN,
  87. payload: {
  88. customDomain,
  89. homepage: '',
  90. useHttps: false,
  91. }
  92. }
  93. ];
  94. store
  95. .dispatch(setCustomDomain({
  96. customDomain,
  97. homepage: '',
  98. useHttps: false,
  99. }))
  100. .then(() => {
  101. expect(store.getActions()).to.deep.equal(expectedActions);
  102. done();
  103. })
  104. .catch(error => done(error));
  105. });
  106. });
  107. describe('#deleteCustomDomain()', () => {
  108. it('should dispatch DELETE_DOMAIN when deleting custom domain has been done', done => {
  109. const customDomain = 'test.com';
  110. nock('http://localhost', {
  111. reqheaders: {
  112. Authorization: token
  113. }
  114. })
  115. .delete('/api/url/customdomain')
  116. .reply(200, { customDomain });
  117. const store = mockStore({});
  118. const expectedActions = [{ type: DELETE_DOMAIN }];
  119. store
  120. .dispatch(deleteCustomDomain(customDomain))
  121. .then(() => {
  122. expect(store.getActions()).to.deep.equal(expectedActions);
  123. done();
  124. })
  125. .catch(error => done(error));
  126. });
  127. });
  128. describe('#generateApiKey()', () => {
  129. it('should dispatch SET_APIKEY when generating api key has been done', done => {
  130. const apikey = '123';
  131. nock('http://localhost', {
  132. reqheaders: {
  133. Authorization: token
  134. }
  135. })
  136. .post('/api/auth/generateapikey')
  137. .reply(200, { apikey });
  138. const store = mockStore({});
  139. const expectedActions = [
  140. { type: API_LOADING },
  141. {
  142. type: SET_APIKEY,
  143. payload: apikey
  144. }
  145. ];
  146. store
  147. .dispatch(generateApiKey())
  148. .then(() => {
  149. expect(store.getActions()).to.deep.equal(expectedActions);
  150. done();
  151. })
  152. .catch(error => done(error));
  153. });
  154. });
  155. });