RulesGroup.test.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { render } 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 { byTestId, byText } from 'testing-library-selector';
  6. import { contextSrv } from 'app/core/services/context_srv';
  7. import { configureStore } from 'app/store/configureStore';
  8. import { CombinedRuleGroup, CombinedRuleNamespace } from 'app/types/unified-alerting';
  9. import { useHasRuler } from '../../hooks/useHasRuler';
  10. import { disableRBAC, mockCombinedRule, mockDataSource } from '../../mocks';
  11. import { RulesGroup } from './RulesGroup';
  12. jest.mock('../../hooks/useHasRuler');
  13. const mocks = {
  14. useHasRuler: jest.mocked(useHasRuler),
  15. };
  16. function mockUseHasRuler(hasRuler: boolean, rulerRulesLoaded: boolean) {
  17. mocks.useHasRuler.mockReturnValue({
  18. hasRuler: () => hasRuler,
  19. rulerRulesLoaded: () => rulerRulesLoaded,
  20. });
  21. }
  22. beforeEach(() => {
  23. mocks.useHasRuler.mockReset();
  24. });
  25. const ui = {
  26. editGroupButton: byTestId('edit-group'),
  27. deleteGroupButton: byTestId('delete-group'),
  28. confirmDeleteModal: {
  29. header: byText('Delete group'),
  30. confirmButton: byText('Delete'),
  31. },
  32. };
  33. describe('Rules group tests', () => {
  34. const store = configureStore();
  35. function renderRulesGroup(namespace: CombinedRuleNamespace, group: CombinedRuleGroup) {
  36. return render(
  37. <Provider store={store}>
  38. <RulesGroup group={group} namespace={namespace} expandAll={false} />
  39. </Provider>
  40. );
  41. }
  42. describe('When the datasource is grafana', () => {
  43. const group: CombinedRuleGroup = {
  44. name: 'TestGroup',
  45. rules: [mockCombinedRule()],
  46. };
  47. const namespace: CombinedRuleNamespace = {
  48. name: 'TestNamespace',
  49. rulesSource: 'grafana',
  50. groups: [group],
  51. };
  52. it('Should hide delete and edit group buttons', () => {
  53. // Act
  54. mockUseHasRuler(true, true);
  55. renderRulesGroup(namespace, group);
  56. // Assert
  57. expect(ui.deleteGroupButton.query()).not.toBeInTheDocument();
  58. expect(ui.editGroupButton.query()).not.toBeInTheDocument();
  59. });
  60. });
  61. describe('When the datasource is not grafana', () => {
  62. beforeEach(() => {
  63. contextSrv.isEditor = true;
  64. });
  65. const group: CombinedRuleGroup = {
  66. name: 'TestGroup',
  67. rules: [mockCombinedRule()],
  68. };
  69. const namespace: CombinedRuleNamespace = {
  70. name: 'TestNamespace',
  71. rulesSource: mockDataSource(),
  72. groups: [group],
  73. };
  74. disableRBAC();
  75. it('When ruler enabled should display delete and edit group buttons', () => {
  76. // Arrange
  77. mockUseHasRuler(true, true);
  78. // Act
  79. renderRulesGroup(namespace, group);
  80. // Assert
  81. expect(mocks.useHasRuler).toHaveBeenCalled();
  82. expect(ui.deleteGroupButton.get()).toBeInTheDocument();
  83. expect(ui.editGroupButton.get()).toBeInTheDocument();
  84. });
  85. it('When ruler disabled should hide delete and edit group buttons', () => {
  86. // Arrange
  87. mockUseHasRuler(false, false);
  88. // Act
  89. renderRulesGroup(namespace, group);
  90. // Assert
  91. expect(mocks.useHasRuler).toHaveBeenCalled();
  92. expect(ui.deleteGroupButton.query()).not.toBeInTheDocument();
  93. expect(ui.editGroupButton.query()).not.toBeInTheDocument();
  94. });
  95. it('Delete button click should display confirmation modal', async () => {
  96. // Arrange
  97. mockUseHasRuler(true, true);
  98. // Act
  99. renderRulesGroup(namespace, group);
  100. await userEvent.click(ui.deleteGroupButton.get());
  101. // Assert
  102. expect(ui.confirmDeleteModal.header.get()).toBeInTheDocument();
  103. expect(ui.confirmDeleteModal.confirmButton.get()).toBeInTheDocument();
  104. });
  105. });
  106. });