settings.js 4.2 KB

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