ManageActions.test.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import { render, screen } from '@testing-library/react';
  2. import userEvent from '@testing-library/user-event';
  3. import React from 'react';
  4. import { Provider } from 'react-redux';
  5. import configureMockStore from 'redux-mock-store';
  6. import { contextSrv } from 'app/core/services/context_srv';
  7. import { ManageActions } from './ManageActions';
  8. jest.mock('app/core/services/context_srv', () => ({
  9. contextSrv: {
  10. hasEditPermissionInFolders: false,
  11. },
  12. }));
  13. jest.mock('app/core/components/Select/FolderPicker', () => {
  14. return {
  15. FolderPicker: () => null,
  16. };
  17. });
  18. describe('ManageActions', () => {
  19. describe('when user has edit permission in folders', () => {
  20. // Permissions
  21. contextSrv.hasEditPermissionInFolders = true;
  22. // Mock selected dashboards
  23. const mockItemsSelected = new Map();
  24. const mockDashboardsUIDsSelected = new Set();
  25. mockDashboardsUIDsSelected.add('uid1');
  26. mockDashboardsUIDsSelected.add('uid2');
  27. mockItemsSelected.set('dashboard', mockDashboardsUIDsSelected);
  28. //Mock store redux for old MoveDashboards state action
  29. const mockStore = configureMockStore<any, any>();
  30. const store = mockStore({ dashboard: { panels: [] } });
  31. const onChange = jest.fn();
  32. const clearSelection = jest.fn();
  33. it('should show move when user click the move button', async () => {
  34. render(
  35. <Provider store={store}>
  36. <ManageActions items={mockItemsSelected} onChange={onChange} clearSelection={clearSelection} />
  37. </Provider>
  38. );
  39. expect(screen.getByTestId('manage-actions')).toBeInTheDocument();
  40. expect(await screen.findByRole('button', { name: 'Move', hidden: true })).not.toBeDisabled();
  41. expect(await screen.findByRole('button', { name: 'Delete', hidden: true })).not.toBeDisabled();
  42. // open Move modal
  43. await userEvent.click(screen.getByRole('button', { name: 'Move', hidden: true }));
  44. expect(screen.getByText(/Move the 2 selected dashboards to the following folder:/i)).toBeInTheDocument();
  45. });
  46. it('should show delete modal when user click the delete button', async () => {
  47. render(
  48. <Provider store={store}>
  49. <ManageActions items={mockItemsSelected} onChange={onChange} clearSelection={clearSelection} />
  50. </Provider>
  51. );
  52. expect(screen.getByTestId('manage-actions')).toBeInTheDocument();
  53. // open Delete modal
  54. await userEvent.click(screen.getByRole('button', { name: 'Delete', hidden: true }));
  55. expect(screen.getByText(/Do you want to delete the 2 selected dashboards\?/i)).toBeInTheDocument();
  56. });
  57. });
  58. describe('when user has not edit permission in folders', () => {
  59. it('should have disabled the Move button', async () => {
  60. contextSrv.hasEditPermissionInFolders = false;
  61. const mockItemsSelected = new Map();
  62. const mockDashboardsUIDsSelected = new Set();
  63. mockDashboardsUIDsSelected.add('uid1');
  64. mockDashboardsUIDsSelected.add('uid2');
  65. mockItemsSelected.set('dashboard', mockDashboardsUIDsSelected);
  66. //Mock store
  67. const mockStore = configureMockStore<any, any>();
  68. const store = mockStore({ dashboard: { panels: [] } });
  69. const onChange = jest.fn();
  70. const clearSelection = jest.fn();
  71. render(
  72. <Provider store={store}>
  73. <ManageActions items={mockItemsSelected} onChange={onChange} clearSelection={clearSelection} />
  74. </Provider>
  75. );
  76. expect(screen.getByTestId('manage-actions')).toBeInTheDocument();
  77. expect(await screen.findByRole('button', { name: 'Move', hidden: true })).toBeDisabled();
  78. await userEvent.click(screen.getByRole('button', { name: 'Move', hidden: true }));
  79. expect(screen.queryByText(/Choose Dashboard Folder/i)).toBeNull();
  80. });
  81. });
  82. describe('When user has selected General folder', () => {
  83. contextSrv.hasEditPermissionInFolders = true;
  84. const mockItemsSelected = new Map();
  85. const mockFolderUIDSelected = new Set();
  86. mockFolderUIDSelected.add('general');
  87. mockItemsSelected.set('folder', mockFolderUIDSelected);
  88. //Mock store
  89. const mockStore = configureMockStore<any, any>();
  90. const store = mockStore({ dashboard: { panels: [] } });
  91. const onChange = jest.fn();
  92. const clearSelection = jest.fn();
  93. it('should disable the Delete button', async () => {
  94. render(
  95. <Provider store={store}>
  96. <ManageActions items={mockItemsSelected} onChange={onChange} clearSelection={clearSelection} />
  97. </Provider>
  98. );
  99. expect(screen.getByTestId('manage-actions')).toBeInTheDocument();
  100. expect(await screen.findByRole('button', { name: 'Delete', hidden: true })).toBeDisabled();
  101. });
  102. });
  103. });