utils.test.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import { sections, searchResults, checkedGeneralFolder, checkedOtherFolder, folderViewAllChecked } from './testData';
  2. import { SearchQueryParams } from './types';
  3. import {
  4. findSelected,
  5. getCheckedDashboardsUids,
  6. getCheckedUids,
  7. getFlattenedSections,
  8. markSelected,
  9. mergeReducers,
  10. parseRouteParams,
  11. } from './utils';
  12. describe('Search utils', () => {
  13. describe('getFlattenedSections', () => {
  14. it('should return an array of items plus children for expanded items', () => {
  15. const flatSections = getFlattenedSections(sections as any[]);
  16. expect(flatSections).toHaveLength(10);
  17. expect(flatSections).toEqual([
  18. 'Starred',
  19. 'Starred-1',
  20. 'Recent',
  21. '2',
  22. '2568',
  23. '4074',
  24. '0',
  25. '0-4069',
  26. '0-4072',
  27. '0-1',
  28. ]);
  29. });
  30. describe('markSelected', () => {
  31. it('should correctly mark the section item without id as selected', () => {
  32. const results = markSelected(sections as any, 'Recent');
  33. //@ts-ignore
  34. expect(results[1].selected).toBe(true);
  35. });
  36. it('should correctly mark the section item with id as selected', () => {
  37. const results = markSelected(sections as any, '4074');
  38. //@ts-ignore
  39. expect(results[4].selected).toBe(true);
  40. });
  41. it('should mark all other sections as not selected', () => {
  42. const results = markSelected(sections as any, 'Starred');
  43. const newResults = markSelected(results as any, '0');
  44. //@ts-ignore
  45. expect(newResults[0].selected).toBeFalsy();
  46. expect(newResults[5].selected).toBeTruthy();
  47. });
  48. it('should correctly mark an item of a section as selected', () => {
  49. const results = markSelected(sections as any, '0-4072');
  50. expect(results[5].items[1].selected).toBeTruthy();
  51. });
  52. it('should not mark an item as selected for non-expanded section', () => {
  53. const results = markSelected(sections as any, 'Recent-4072');
  54. expect(results[1].items[0].selected).toBeFalsy();
  55. });
  56. it('should mark all other items as not selected', () => {
  57. const results = markSelected(sections as any, '0-4069');
  58. const newResults = markSelected(results as any, '0-1');
  59. //@ts-ignore
  60. expect(newResults[5].items[0].selected).toBeFalsy();
  61. expect(newResults[5].items[1].selected).toBeFalsy();
  62. expect(newResults[5].items[2].selected).toBeTruthy();
  63. });
  64. it('should correctly select one of the same items in different sections', () => {
  65. const results = markSelected(sections as any, 'Starred-1');
  66. expect(results[0].items[0].selected).toBeTruthy();
  67. // Same item in diff section
  68. expect(results[5].items[2].selected).toBeFalsy();
  69. // Switch order
  70. const newResults = markSelected(sections as any, '0-1');
  71. expect(newResults[0].items[0].selected).toBeFalsy();
  72. // Same item in diff section
  73. expect(newResults[5].items[2].selected).toBeTruthy();
  74. });
  75. });
  76. describe('findSelected', () => {
  77. it('should find selected section', () => {
  78. const results = [...sections, { id: 'Test', selected: true }];
  79. const found = findSelected(results);
  80. expect(found?.id).toEqual('Test');
  81. });
  82. it('should find selected item', () => {
  83. const results = [{ expanded: true, id: 'Test', items: [{ id: 1 }, { id: 2, selected: true }, { id: 3 }] }];
  84. const found = findSelected(results);
  85. expect(found?.id).toEqual(2);
  86. });
  87. });
  88. });
  89. describe('mergeReducers', () => {
  90. const reducer1 = (state: any = { reducer1: false }, action: any) => {
  91. if (action.type === 'reducer1') {
  92. return { ...state, reducer1: !state.reducer1 };
  93. }
  94. return state;
  95. };
  96. const reducer2 = (state: any = { reducer2: false }, action: any) => {
  97. if (action.type === 'reducer2') {
  98. return { ...state, reducer2: !state.reducer2 };
  99. }
  100. return state;
  101. };
  102. const mergedReducers = mergeReducers([reducer1, reducer2]);
  103. it('should merge state from all reducers into one without nesting', () => {
  104. expect(mergedReducers({ reducer1: false }, { type: '' })).toEqual({ reducer1: false });
  105. });
  106. it('should correctly set state from multiple reducers', () => {
  107. const state = { reducer1: false, reducer2: true };
  108. const newState = mergedReducers(state, { type: 'reducer2' });
  109. expect(newState).toEqual({ reducer1: false, reducer2: false });
  110. const newState2 = mergedReducers(newState, { type: 'reducer1' });
  111. expect(newState2).toEqual({ reducer1: true, reducer2: false });
  112. });
  113. });
  114. describe('getCheckedUids', () => {
  115. it('should not return any UIDs if no items are checked', () => {
  116. expect(getCheckedUids(sections)).toEqual({ folders: [], dashboards: [] });
  117. });
  118. it('should return only dashboard UIDs if the General folder is checked', () => {
  119. expect(getCheckedUids(checkedGeneralFolder)).toEqual({
  120. folders: [],
  121. dashboards: ['general-abc', 'general-def', 'general-ghi'],
  122. });
  123. });
  124. it('should return only dashboard UIDs if all items are checked when viewing a folder', () => {
  125. expect(getCheckedUids(folderViewAllChecked)).toEqual({
  126. folders: [],
  127. dashboards: ['other-folder-dash-abc', 'other-folder-dash-def'],
  128. });
  129. });
  130. it('should return folder + dashboard UIDs when folder is checked in the root view', () => {
  131. expect(getCheckedUids(checkedOtherFolder)).toEqual({
  132. folders: ['other-folder-abc'],
  133. dashboards: ['other-folder-dash-abc', 'other-folder-dash-def'],
  134. });
  135. });
  136. });
  137. describe('getCheckedDashboardsUids', () => {
  138. it('should get uids of all checked dashboards', () => {
  139. expect(getCheckedDashboardsUids(searchResults)).toEqual(['lBdLINUWk', '8DY63kQZk']);
  140. });
  141. });
  142. describe('parseRouteParams', () => {
  143. it('should remove all undefined keys', () => {
  144. const params: Partial<SearchQueryParams> = { sort: undefined, tag: undefined, query: 'test' };
  145. expect(parseRouteParams(params)).toEqual({
  146. query: 'test',
  147. });
  148. });
  149. it('should return tag as array, if present', () => {
  150. //@ts-ignore
  151. const params = { sort: undefined, tag: 'test', query: 'test' };
  152. expect(parseRouteParams(params)).toEqual({
  153. query: 'test',
  154. tag: ['test'],
  155. });
  156. const params2: Partial<SearchQueryParams> = { sort: undefined, tag: ['test'], query: 'test' };
  157. expect(parseRouteParams(params2)).toEqual({
  158. query: 'test',
  159. tag: ['test'],
  160. });
  161. });
  162. it('should return sort as a SelectableValue', () => {
  163. const params: Partial<SearchQueryParams> = { sort: 'test' };
  164. expect(parseRouteParams(params)).toEqual({
  165. sort: { value: 'test' },
  166. });
  167. });
  168. it('should prepend folder:{folder} to the query if folder is present', () => {
  169. expect(parseRouteParams({ folder: 'current' })).toEqual({
  170. folder: 'current',
  171. query: 'folder:current ',
  172. });
  173. // Prepend to exiting query
  174. const params: Partial<SearchQueryParams> = { query: 'test', folder: 'current' };
  175. expect(parseRouteParams(params)).toEqual({
  176. folder: 'current',
  177. query: 'folder:current test',
  178. });
  179. });
  180. });
  181. });