url.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import { expect } from 'chai';
  2. import deepFreeze from 'deep-freeze';
  3. import {
  4. ADD_URL,
  5. UPDATE_URL_LIST,
  6. LIST_URLS,
  7. DELETE_URL,
  8. UNAUTH_USER
  9. } from '../../actions/actionTypes';
  10. import reducer from '../url';
  11. describe('url reducer', () => {
  12. const initialState = {
  13. list: [],
  14. isShortened: false,
  15. count: 10,
  16. countAll: 0,
  17. page: 1,
  18. search: ''
  19. };
  20. beforeEach(() => {
  21. deepFreeze(initialState);
  22. });
  23. it('should return the initial state', () => {
  24. expect(reducer(undefined, {})).to.deep.equal(initialState);
  25. });
  26. it('should handle ADD_URL', () => {
  27. const item = {
  28. createdAt: '2018-06-12T19:23:00.272Z',
  29. id: 'YufjdS',
  30. target: 'https://kutt.it/',
  31. password: false,
  32. reuse: false,
  33. shortLink: 'https://kutt.it/YufjdS'
  34. };
  35. const state = reducer(initialState, {
  36. type: ADD_URL,
  37. payload: item
  38. });
  39. expect(state.list).to.be.an('array');
  40. expect(state.list).to.have.lengthOf(1);
  41. expect(state.list).to.include(item);
  42. expect(state.isShortened).to.be.true;
  43. });
  44. it('should handle UPDATE_URL_LIST', () => {
  45. const count = 10;
  46. const page = 1;
  47. const search = 'test url';
  48. const allParamsState = reducer(initialState, {
  49. type: UPDATE_URL_LIST,
  50. payload: { count, page, search }
  51. });
  52. expect(allParamsState).not.to.be.undefined;
  53. expect(allParamsState.count).to.be.equal(count);
  54. expect(allParamsState.page).to.be.equal(page);
  55. expect(allParamsState.search).to.be.equal(search);
  56. const countState = reducer(initialState, {
  57. type: UPDATE_URL_LIST,
  58. payload: { count }
  59. });
  60. expect(countState).not.to.be.undefined;
  61. expect(countState.count).to.be.equal(count);
  62. const pageState = reducer(initialState, {
  63. type: UPDATE_URL_LIST,
  64. payload: { page }
  65. });
  66. expect(pageState).not.to.be.undefined;
  67. expect(pageState.page).to.be.equal(page);
  68. const searchState = reducer(initialState, {
  69. type: UPDATE_URL_LIST,
  70. payload: { search }
  71. });
  72. expect(searchState).not.to.be.undefined;
  73. expect(searchState.search).to.be.equal(search);
  74. const state = reducer(initialState, {
  75. type: UPDATE_URL_LIST
  76. });
  77. expect(state).not.to.be.undefined;
  78. expect(state).to.deep.equal(initialState);
  79. });
  80. it('should handle LIST_URLS', () => {
  81. const list = [
  82. {
  83. createdAt: '2018-06-12T19:23:00.272Z',
  84. id: 'YufjdS',
  85. target: 'https://kutt.it/',
  86. password: false,
  87. reuse: false,
  88. shortLink: 'https://kutt.it/YufjdS'
  89. },
  90. {
  91. createdAt: '2018-06-12T19:51:56.435Z',
  92. id: '1gCdbC',
  93. target: 'https://kutt.it/',
  94. password: false,
  95. reuse: false,
  96. shortLink: 'https://kutt.it/1gCdbC'
  97. }
  98. ];
  99. const countAll = list.length;
  100. const state = reducer(initialState, {
  101. type: LIST_URLS,
  102. payload: { list, countAll }
  103. });
  104. expect(state.list).to.be.an('array');
  105. expect(state.list).to.have.lengthOf(2);
  106. expect(state.list).to.not.include(list);
  107. expect(state.countAll).to.be.equal(countAll);
  108. expect(state.isShortened).to.be.false;
  109. });
  110. it('should handle DELETE_URL', () => {
  111. const itemsState = {
  112. list: [
  113. {
  114. createdAt: '2018-06-12T19:23:00.272Z',
  115. id: 'YufjdS',
  116. target: 'https://kutt.it/',
  117. password: false,
  118. reuse: false,
  119. shortLink: 'https://kutt.it/YufjdS'
  120. },
  121. {
  122. createdAt: '2018-06-12T19:51:56.435Z',
  123. id: '1gCdbC',
  124. target: 'https://kutt.it/',
  125. password: false,
  126. reuse: false,
  127. shortLink: 'https://kutt.it/1gCdbC'
  128. }
  129. ],
  130. isShortened: true,
  131. count: 10,
  132. countAll: 2,
  133. page: 1,
  134. search: ''
  135. };
  136. deepFreeze(itemsState);
  137. const state = reducer(itemsState, {
  138. type: DELETE_URL,
  139. payload: 'YufjdS'
  140. });
  141. expect(state.list).to.be.an('array');
  142. expect(state.list).to.have.lengthOf(1);
  143. expect(state.list).to.not.include({
  144. createdAt: '2018-06-12T19:23:00.272Z',
  145. id: 'YufjdS',
  146. target: 'https://kutt.it/',
  147. password: false,
  148. reuse: false,
  149. shortLink: 'https://kutt.it/YufjdS'
  150. });
  151. });
  152. it('should handle UNAUTH_USER', () => {
  153. const state = reducer(initialState, {
  154. type: UNAUTH_USER
  155. });
  156. expect(state).not.to.be.undefined;
  157. expect(state).to.deep.equal(initialState);
  158. });
  159. it('should not handle other action types', () => {
  160. const state = reducer(initialState, {
  161. type: 'ANOTHER_ACTION'
  162. });
  163. expect(state).not.to.be.undefined;
  164. expect(state).to.deep.equal(initialState);
  165. });
  166. });