url.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 { createShortUrl, getUrlsList, deleteShortUrl } from '../url';
  8. import { ADD_URL, LIST_URLS, DELETE_URL, TABLE_LOADING } from '../actionTypes';
  9. const middlewares = [thunk];
  10. const mockStore = configureMockStore(middlewares);
  11. describe('url actions', () => {
  12. const token =
  13. 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJBcGlBdXRoIiwic3ViIjoidGVzdEBtYWlsLmNvbSIsImRvbWFpbiI6IiIsImlhdCI6MTUyOTEzNzczODcyNSwiZXhwIjoxNTI5MTM3NzM4NzI1fQ.tdI7r11bmSCUmbcJBBKIDt7Hkb7POLMRl8VNJv_8O_s';
  14. let cookieStub;
  15. beforeEach(() => {
  16. cookieStub = sinon.stub(cookie, 'get');
  17. cookieStub.withArgs('token').returns(token);
  18. });
  19. afterEach(() => {
  20. cookieStub.restore();
  21. });
  22. describe('#createShortUrl()', () => {
  23. it('should dispatch ADD_URL when creating short url has been done', done => {
  24. const url = 'test.com';
  25. const mockedItems = {
  26. createdAt: '2018-06-16T15:40:35.243Z',
  27. id: '123',
  28. target: url,
  29. password: false,
  30. reuse: false,
  31. shortUrl: 'http://kutt.it/123'
  32. };
  33. nock('http://localhost', {
  34. reqheaders: {
  35. Authorization: token
  36. }
  37. })
  38. .post('/api/url/submit')
  39. .reply(200, mockedItems);
  40. const store = mockStore({});
  41. const expectedActions = [
  42. {
  43. type: ADD_URL,
  44. payload: mockedItems
  45. }
  46. ];
  47. store
  48. .dispatch(createShortUrl(url))
  49. .then(() => {
  50. expect(store.getActions()).to.deep.equal(expectedActions);
  51. done();
  52. })
  53. .catch(error => done(error));
  54. });
  55. });
  56. describe('#getUrlsList()', () => {
  57. it('should dispatch LIST_URLS when getting urls list has been done', done => {
  58. const mockedQueryParams = {
  59. isShortened: false,
  60. count: 10,
  61. countAll: 1,
  62. page: 1,
  63. search: ''
  64. };
  65. const mockedItems = {
  66. list: [
  67. {
  68. createdAt: '2018-06-16T16:45:28.607Z',
  69. id: 'UkEs33',
  70. target: 'https://kutt.it/',
  71. password: false,
  72. count: 0,
  73. shortUrl: 'http://test.com/UkEs33'
  74. }
  75. ],
  76. countAll: 1
  77. };
  78. nock('http://localhost', {
  79. reqheaders: {
  80. Authorization: token
  81. }
  82. })
  83. .get('/api/url/geturls')
  84. .query(mockedQueryParams)
  85. .reply(200, mockedItems);
  86. const store = mockStore({ url: { list: [], ...mockedQueryParams } });
  87. const expectedActions = [
  88. { type: TABLE_LOADING },
  89. {
  90. type: LIST_URLS,
  91. payload: mockedItems
  92. }
  93. ];
  94. store
  95. .dispatch(getUrlsList())
  96. .then(() => {
  97. expect(store.getActions()).to.deep.equal(expectedActions);
  98. done();
  99. })
  100. .catch(error => done(error));
  101. });
  102. });
  103. describe('#deleteShortUrl()', () => {
  104. it('should dispatch DELETE_URL when deleting short url has been done', done => {
  105. const id = '123';
  106. const mockedItems = [
  107. {
  108. createdAt: '2018-06-16T15:40:35.243Z',
  109. id: '123',
  110. target: 'test.com',
  111. password: false,
  112. reuse: false,
  113. shortUrl: 'http://kutt.it/123'
  114. }
  115. ];
  116. nock('http://localhost', {
  117. reqheaders: {
  118. Authorization: token
  119. }
  120. })
  121. .post('/api/url/deleteurl')
  122. .reply(200, { message: 'Short URL deleted successfully' });
  123. const store = mockStore({ url: { list: mockedItems } });
  124. const expectedActions = [
  125. { type: TABLE_LOADING },
  126. { type: DELETE_URL, payload: id }
  127. ];
  128. store
  129. .dispatch(deleteShortUrl({ id }))
  130. .then(() => {
  131. expect(store.getActions()).to.deep.equal(expectedActions);
  132. done();
  133. })
  134. .catch(error => done(error));
  135. });
  136. });
  137. });