useManageDashboards.test.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import { renderHook } from '@testing-library/react-hooks';
  2. import { Dispatch } from 'react';
  3. import { setEchoSrv } from '@grafana/runtime/src';
  4. import { Echo } from 'app/core/services/echo/Echo';
  5. import { GENERAL_FOLDER_ID } from '../constants';
  6. import { ManageDashboardsState } from '../reducers/manageDashboards';
  7. import { DashboardQuery, DashboardSearchItemType, DashboardSection, SearchAction } from '../types';
  8. import { useManageDashboards } from './useManageDashboards';
  9. import * as useSearch from './useSearch';
  10. describe('useManageDashboards', () => {
  11. const useSearchMock = jest.spyOn(useSearch, 'useSearch');
  12. const toggle = async (section: DashboardSection) => section;
  13. beforeAll(() => {
  14. setEchoSrv(new Echo());
  15. });
  16. function setupTestContext({ results = [] }: { results?: DashboardSection[] } = {}) {
  17. jest.clearAllMocks();
  18. const state: ManageDashboardsState = {
  19. results,
  20. loading: false,
  21. selectedIndex: 0,
  22. initialLoading: false,
  23. allChecked: false,
  24. };
  25. const dispatch: Dispatch<SearchAction> = null as unknown as Dispatch<SearchAction>;
  26. useSearchMock.mockReturnValue({ state, dispatch, onToggleSection: toggle });
  27. const dashboardQuery: DashboardQuery = {} as unknown as DashboardQuery;
  28. const { result } = renderHook(() => useManageDashboards(dashboardQuery, {}));
  29. return { result };
  30. }
  31. describe('when called and only General folder is selected', () => {
  32. it('then canDelete should be false', () => {
  33. const results: DashboardSection[] = [
  34. { id: 1, checked: false, items: [], title: 'One', type: DashboardSearchItemType.DashFolder, toggle, url: '/' },
  35. {
  36. id: GENERAL_FOLDER_ID,
  37. checked: true,
  38. items: [],
  39. title: 'General',
  40. type: DashboardSearchItemType.DashFolder,
  41. toggle,
  42. url: '/',
  43. },
  44. { id: 2, checked: false, items: [], title: 'Two', type: DashboardSearchItemType.DashFolder, toggle, url: '/' },
  45. ];
  46. const { result } = setupTestContext({ results });
  47. expect(result.current.canDelete).toBe(false);
  48. });
  49. });
  50. describe('when called and General folder and another folder are selected', () => {
  51. it('then canDelete should be false', () => {
  52. const results: DashboardSection[] = [
  53. {
  54. id: 1,
  55. checked: true,
  56. items: [
  57. {
  58. id: 11,
  59. checked: true,
  60. title: 'Eleven',
  61. type: DashboardSearchItemType.DashDB,
  62. url: '/',
  63. isStarred: false,
  64. tags: [],
  65. uri: '',
  66. },
  67. ],
  68. title: 'One',
  69. type: DashboardSearchItemType.DashFolder,
  70. toggle,
  71. url: '/',
  72. },
  73. {
  74. id: GENERAL_FOLDER_ID,
  75. checked: true,
  76. items: [
  77. {
  78. id: 10,
  79. checked: true,
  80. title: 'Ten',
  81. type: DashboardSearchItemType.DashDB,
  82. url: '/',
  83. isStarred: false,
  84. tags: [],
  85. uri: '',
  86. },
  87. ],
  88. title: 'General',
  89. type: DashboardSearchItemType.DashFolder,
  90. toggle,
  91. url: '/',
  92. },
  93. { id: 2, checked: false, items: [], title: 'Two', type: DashboardSearchItemType.DashFolder, toggle, url: '/' },
  94. ];
  95. const { result } = setupTestContext({ results });
  96. expect(result.current.canDelete).toBe(false);
  97. });
  98. });
  99. describe('when called on an empty General folder that is not selected but another folder is selected', () => {
  100. it('then canDelete should be true', () => {
  101. const results: DashboardSection[] = [
  102. {
  103. id: 1,
  104. checked: true,
  105. items: [
  106. {
  107. id: 11,
  108. checked: true,
  109. title: 'Eleven',
  110. type: DashboardSearchItemType.DashDB,
  111. url: '/',
  112. isStarred: false,
  113. tags: [],
  114. uri: '',
  115. },
  116. ],
  117. title: 'One',
  118. type: DashboardSearchItemType.DashFolder,
  119. toggle,
  120. url: '/',
  121. },
  122. {
  123. id: GENERAL_FOLDER_ID,
  124. checked: false,
  125. items: [],
  126. title: 'General',
  127. type: DashboardSearchItemType.DashFolder,
  128. toggle,
  129. url: '/',
  130. },
  131. { id: 2, checked: false, items: [], title: 'Two', type: DashboardSearchItemType.DashFolder, toggle, url: '/' },
  132. ];
  133. const { result } = setupTestContext({ results });
  134. expect(result.current.canDelete).toBe(true);
  135. });
  136. });
  137. describe('when called on a non empty General folder that is not selected dashboard in General folder is selected', () => {
  138. it('then canDelete should be true', () => {
  139. const results: DashboardSection[] = [
  140. {
  141. id: GENERAL_FOLDER_ID,
  142. checked: false,
  143. items: [
  144. {
  145. id: 10,
  146. checked: true,
  147. title: 'Ten',
  148. type: DashboardSearchItemType.DashDB,
  149. url: '/',
  150. isStarred: false,
  151. tags: [],
  152. uri: '',
  153. },
  154. ],
  155. title: 'General',
  156. type: DashboardSearchItemType.DashFolder,
  157. toggle,
  158. url: '/',
  159. },
  160. ];
  161. const { result } = setupTestContext({ results });
  162. expect(result.current.canDelete).toBe(true);
  163. });
  164. });
  165. describe('when called and no folder is selected', () => {
  166. it('then canDelete should be false', () => {
  167. const results: DashboardSection[] = [
  168. { id: 1, checked: false, items: [], title: 'One', type: DashboardSearchItemType.DashFolder, toggle, url: '/' },
  169. {
  170. id: GENERAL_FOLDER_ID,
  171. checked: false,
  172. items: [],
  173. title: 'General',
  174. type: DashboardSearchItemType.DashFolder,
  175. toggle,
  176. url: '/',
  177. },
  178. { id: 2, checked: false, items: [], title: 'Two', type: DashboardSearchItemType.DashFolder, toggle, url: '/' },
  179. ];
  180. const { result } = setupTestContext({ results });
  181. expect(result.current.canDelete).toBe(false);
  182. });
  183. });
  184. describe('when called on an empty folder', () => {
  185. it('then canDelete should be true', () => {
  186. const results: DashboardSection[] = [
  187. { id: 1, checked: true, items: [], title: 'One', type: DashboardSearchItemType.DashFolder, toggle, url: '/' },
  188. {
  189. id: GENERAL_FOLDER_ID,
  190. checked: false,
  191. items: [],
  192. title: 'General',
  193. type: DashboardSearchItemType.DashFolder,
  194. toggle,
  195. url: '/',
  196. },
  197. { id: 2, checked: false, items: [], title: 'Two', type: DashboardSearchItemType.DashFolder, toggle, url: '/' },
  198. ];
  199. const { result } = setupTestContext({ results });
  200. expect(result.current.canDelete).toBe(true);
  201. });
  202. });
  203. });