AlertsFolderView.test.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 { MemoryRouter } from 'react-router-dom';
  6. import { byTestId } from 'testing-library-selector';
  7. import { configureStore } from 'app/store/configureStore';
  8. import { FolderState } from 'app/types';
  9. import { CombinedRuleNamespace } from 'app/types/unified-alerting';
  10. import { AlertsFolderView } from './AlertsFolderView';
  11. import { mockCombinedRule } from './mocks';
  12. import { GRAFANA_RULES_SOURCE_NAME } from './utils/datasource';
  13. const ui = {
  14. filter: {
  15. name: byTestId('name-filter'),
  16. label: byTestId('label-filter'),
  17. },
  18. ruleList: {
  19. row: byTestId('alert-card-row'),
  20. },
  21. };
  22. const combinedNamespaceMock = jest.fn<CombinedRuleNamespace[], any>();
  23. jest.mock('./hooks/useCombinedRuleNamespaces', () => ({
  24. useCombinedRuleNamespaces: () => combinedNamespaceMock(),
  25. }));
  26. const mockFolder = (folderOverride: Partial<FolderState> = {}): FolderState => {
  27. return {
  28. id: 1,
  29. title: 'Folder with alerts',
  30. uid: 'folder-1',
  31. hasChanged: false,
  32. canSave: false,
  33. url: '/folder-1',
  34. version: 1,
  35. permissions: [],
  36. canViewFolderPermissions: false,
  37. canDelete: false,
  38. ...folderOverride,
  39. };
  40. };
  41. describe('AlertsFolderView tests', () => {
  42. it('Should display grafana alert rules when the namespace name matches the folder name', () => {
  43. // Arrange
  44. const store = configureStore();
  45. const folder = mockFolder();
  46. const grafanaNamespace: CombinedRuleNamespace = {
  47. name: folder.title,
  48. rulesSource: GRAFANA_RULES_SOURCE_NAME,
  49. groups: [
  50. {
  51. name: 'group1',
  52. rules: [
  53. mockCombinedRule({ name: 'Test Alert 1' }),
  54. mockCombinedRule({ name: 'Test Alert 2' }),
  55. mockCombinedRule({ name: 'Test Alert 3' }),
  56. ],
  57. },
  58. {
  59. name: 'group2',
  60. rules: [
  61. mockCombinedRule({ name: 'Test Alert 4' }),
  62. mockCombinedRule({ name: 'Test Alert 5' }),
  63. mockCombinedRule({ name: 'Test Alert 6' }),
  64. ],
  65. },
  66. ],
  67. };
  68. combinedNamespaceMock.mockReturnValue([grafanaNamespace]);
  69. // Act
  70. render(
  71. <Provider store={store}>
  72. <MemoryRouter>
  73. <AlertsFolderView folder={folder} />
  74. </MemoryRouter>
  75. </Provider>
  76. );
  77. // Assert
  78. const alertRows = ui.ruleList.row.queryAll();
  79. expect(alertRows).toHaveLength(6);
  80. expect(alertRows[0]).toHaveTextContent('Test Alert 1');
  81. expect(alertRows[1]).toHaveTextContent('Test Alert 2');
  82. expect(alertRows[2]).toHaveTextContent('Test Alert 3');
  83. expect(alertRows[3]).toHaveTextContent('Test Alert 4');
  84. expect(alertRows[4]).toHaveTextContent('Test Alert 5');
  85. expect(alertRows[5]).toHaveTextContent('Test Alert 6');
  86. });
  87. it('Should not display alert rules when the namespace name does not match the folder name', () => {
  88. // Arrange
  89. const store = configureStore();
  90. const folder = mockFolder();
  91. const grafanaNamespace: CombinedRuleNamespace = {
  92. name: 'Folder without alerts',
  93. rulesSource: GRAFANA_RULES_SOURCE_NAME,
  94. groups: [
  95. {
  96. name: 'default',
  97. rules: [
  98. mockCombinedRule({ name: 'Test Alert from other folder 1' }),
  99. mockCombinedRule({ name: 'Test Alert from other folder 2' }),
  100. ],
  101. },
  102. ],
  103. };
  104. combinedNamespaceMock.mockReturnValue([grafanaNamespace]);
  105. // Act
  106. render(
  107. <Provider store={store}>
  108. <MemoryRouter>
  109. <AlertsFolderView folder={folder} />
  110. </MemoryRouter>
  111. </Provider>
  112. );
  113. // Assert
  114. expect(ui.ruleList.row.queryAll()).toHaveLength(0);
  115. });
  116. it('Should filter alert rules by the name, case insensitive', async () => {
  117. // Arrange
  118. const store = configureStore();
  119. const folder = mockFolder();
  120. const grafanaNamespace: CombinedRuleNamespace = {
  121. name: folder.title,
  122. rulesSource: GRAFANA_RULES_SOURCE_NAME,
  123. groups: [
  124. {
  125. name: 'default',
  126. rules: [mockCombinedRule({ name: 'CPU Alert' }), mockCombinedRule({ name: 'RAM usage alert' })],
  127. },
  128. ],
  129. };
  130. combinedNamespaceMock.mockReturnValue([grafanaNamespace]);
  131. // Act
  132. render(
  133. <Provider store={store}>
  134. <MemoryRouter>
  135. <AlertsFolderView folder={folder} />
  136. </MemoryRouter>
  137. </Provider>
  138. );
  139. await userEvent.type(ui.filter.name.get(), 'cpu');
  140. // Assert
  141. expect(ui.ruleList.row.queryAll()).toHaveLength(1);
  142. expect(ui.ruleList.row.get()).toHaveTextContent('CPU Alert');
  143. });
  144. it('Should filter alert rule by labels', async () => {
  145. // Arrange
  146. const store = configureStore();
  147. const folder = mockFolder();
  148. const grafanaNamespace: CombinedRuleNamespace = {
  149. name: folder.title,
  150. rulesSource: GRAFANA_RULES_SOURCE_NAME,
  151. groups: [
  152. {
  153. name: 'default',
  154. rules: [
  155. mockCombinedRule({ name: 'CPU Alert', labels: {} }),
  156. mockCombinedRule({ name: 'RAM usage alert', labels: { severity: 'critical' } }),
  157. ],
  158. },
  159. ],
  160. };
  161. combinedNamespaceMock.mockReturnValue([grafanaNamespace]);
  162. // Act
  163. render(
  164. <Provider store={store}>
  165. <MemoryRouter>
  166. <AlertsFolderView folder={folder} />
  167. </MemoryRouter>
  168. </Provider>
  169. );
  170. await userEvent.type(ui.filter.label.get(), 'severity=critical');
  171. // Assert
  172. expect(ui.ruleList.row.queryAll()).toHaveLength(1);
  173. expect(ui.ruleList.row.get()).toHaveTextContent('RAM usage alert');
  174. });
  175. });