manageDashboards.test.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { sections } from '../testData';
  2. import { DashboardSection, UidsToDelete } from '../types';
  3. import { TOGGLE_ALL_CHECKED, TOGGLE_CHECKED, DELETE_ITEMS, MOVE_ITEMS } from './actionTypes';
  4. import { manageDashboardsReducer as reducer, manageDashboardsState as state } from './manageDashboards';
  5. // Remove Recent and Starred sections as they're not used in manage dashboards
  6. const results = sections.slice(2);
  7. describe('Manage dashboards reducer', () => {
  8. it('should return the initial state', () => {
  9. expect(reducer(state, {} as any)).toEqual(state);
  10. });
  11. it('should handle TOGGLE_ALL_CHECKED', () => {
  12. const newState = reducer({ ...state, results }, { type: TOGGLE_ALL_CHECKED });
  13. expect(newState.results.every((result: any) => result.checked === true)).toBe(true);
  14. expect(newState.results.every((result: any) => result.items.every((item: any) => item.checked === true))).toBe(
  15. true
  16. );
  17. expect(newState.allChecked).toBe(true);
  18. const newState2 = reducer({ ...newState, results }, { type: TOGGLE_ALL_CHECKED });
  19. expect(newState2.results.every((result: any) => result.checked === false)).toBe(true);
  20. expect(newState2.results.every((result: any) => result.items.every((item: any) => item.checked === false))).toBe(
  21. true
  22. );
  23. expect(newState2.allChecked).toBe(false);
  24. });
  25. it('should handle TOGGLE_CHECKED sections', () => {
  26. const newState = reducer({ ...state, results }, { type: TOGGLE_CHECKED, payload: results[0] });
  27. expect(newState.results[0].checked).toBe(true);
  28. expect(newState.results[1].checked).toBeFalsy();
  29. const newState2 = reducer(newState, { type: TOGGLE_CHECKED, payload: results[1] });
  30. expect(newState2.results[0].checked).toBe(true);
  31. expect(newState2.results[1].checked).toBe(true);
  32. });
  33. it('should handle TOGGLE_CHECKED items', () => {
  34. const newState = reducer({ ...state, results }, { type: TOGGLE_CHECKED, payload: { id: 4069 } });
  35. expect(newState.results[3].items[0].checked).toBe(true);
  36. const newState2 = reducer(newState, { type: TOGGLE_CHECKED, payload: { id: 1 } });
  37. expect(newState2.results[3].items[0].checked).toBe(true);
  38. expect(newState2.results[3].items[1].checked).toBeFalsy();
  39. expect(newState2.results[3].items[2].checked).toBe(true);
  40. });
  41. it('should handle DELETE_ITEMS', () => {
  42. const toDelete: UidsToDelete = { dashboards: ['OzAIf_rWz', 'lBdLINUWk'], folders: ['search-test-data'] };
  43. const newState = reducer({ ...state, results }, { type: DELETE_ITEMS, payload: toDelete });
  44. expect(newState.results).toHaveLength(3);
  45. expect(newState.results[1].id).toEqual(4074);
  46. expect(newState.results[2].items).toHaveLength(1);
  47. expect(newState.results[2].items[0].id).toEqual(4069);
  48. });
  49. it('should handle MOVE_ITEMS', () => {
  50. // Move 2 dashboards to a folder with id 2
  51. const toMove = {
  52. dashboards: [
  53. {
  54. id: 4072,
  55. uid: 'OzAIf_rWz',
  56. title: 'New dashboard Copy 3',
  57. type: 'dash-db',
  58. isStarred: false,
  59. },
  60. {
  61. id: 1,
  62. uid: 'lBdLINUWk',
  63. title: 'Prom dash',
  64. type: 'dash-db',
  65. isStarred: true,
  66. },
  67. ],
  68. folder: { id: 2 },
  69. };
  70. const newState = reducer({ ...state, results }, { type: MOVE_ITEMS, payload: toMove });
  71. expect(newState.results[0].items).toHaveLength(2);
  72. expect(newState.results[0].items[0].uid).toEqual('OzAIf_rWz');
  73. expect(newState.results[0].items[1].uid).toEqual('lBdLINUWk');
  74. expect(newState.results[3].items).toHaveLength(1);
  75. expect(newState.results[3].items[0].uid).toEqual('LCFWfl9Zz');
  76. });
  77. it('should not display dashboards in a non-expanded folder', () => {
  78. const general = results.find((res) => res.id === 0);
  79. const toMove = { dashboards: general?.items, folder: { id: 4074 } };
  80. const newState = reducer({ ...state, results }, { type: MOVE_ITEMS, payload: toMove });
  81. expect(newState.results.find((res: DashboardSection) => res.id === 4074).items).toHaveLength(0);
  82. expect(newState.results.find((res: DashboardSection) => res.id === 0).items).toHaveLength(0);
  83. });
  84. });