RuleDetailsMatchingInstances.test.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import { render } from '@testing-library/react';
  2. import userEvent from '@testing-library/user-event';
  3. import React from 'react';
  4. import { byLabelText, byRole, byTestId } from 'testing-library-selector';
  5. import { CombinedRuleNamespace } from '../../../../../types/unified-alerting';
  6. import { GrafanaAlertState, PromAlertingRuleState } from '../../../../../types/unified-alerting-dto';
  7. import { mockCombinedRule, mockDataSource, mockPromAlert, mockPromAlertingRule } from '../../mocks';
  8. import { alertStateToReadable } from '../../utils/rules';
  9. import { RuleDetailsMatchingInstances } from './RuleDetailsMatchingInstances';
  10. const ui = {
  11. stateFilter: byTestId('alert-instance-state-filter'),
  12. stateButton: byRole('radio'),
  13. grafanaStateButton: {
  14. normal: byLabelText('Normal'),
  15. alerting: byLabelText('Alerting'),
  16. pending: byLabelText('Pending'),
  17. noData: byLabelText('NoData'),
  18. error: byLabelText('Error'),
  19. },
  20. cloudStateButton: {
  21. firing: byLabelText('Firing'),
  22. pending: byLabelText('Pending'),
  23. },
  24. instanceRow: byTestId('row'),
  25. };
  26. describe('RuleDetailsMatchingInstances', () => {
  27. describe('Filtering', () => {
  28. it('For Grafana Managed rules instances filter should contain five states', () => {
  29. const rule = mockCombinedRule();
  30. render(<RuleDetailsMatchingInstances rule={rule} />);
  31. const stateFilter = ui.stateFilter.get();
  32. expect(stateFilter).toBeInTheDocument();
  33. const stateButtons = ui.stateButton.getAll(stateFilter);
  34. expect(stateButtons).toHaveLength(5);
  35. expect(ui.grafanaStateButton.normal.get(stateFilter)).toBeInTheDocument();
  36. expect(ui.grafanaStateButton.alerting.get(stateFilter)).toBeInTheDocument();
  37. expect(ui.grafanaStateButton.pending.get(stateFilter)).toBeInTheDocument();
  38. expect(ui.grafanaStateButton.noData.get(stateFilter)).toBeInTheDocument();
  39. expect(ui.grafanaStateButton.error.get(stateFilter)).toBeInTheDocument();
  40. });
  41. it.each(Object.values(GrafanaAlertState))('Should filter grafana rules by %s state', async (state) => {
  42. const rule = mockCombinedRule({
  43. promRule: mockPromAlertingRule({
  44. alerts: [
  45. mockPromAlert({ state: GrafanaAlertState.Normal }),
  46. mockPromAlert({ state: GrafanaAlertState.Alerting }),
  47. mockPromAlert({ state: GrafanaAlertState.Pending }),
  48. mockPromAlert({ state: GrafanaAlertState.NoData }),
  49. mockPromAlert({ state: GrafanaAlertState.Error }),
  50. ],
  51. }),
  52. });
  53. const buttons = {
  54. [GrafanaAlertState.Normal]: ui.grafanaStateButton.normal,
  55. [GrafanaAlertState.Alerting]: ui.grafanaStateButton.alerting,
  56. [GrafanaAlertState.Pending]: ui.grafanaStateButton.pending,
  57. [GrafanaAlertState.NoData]: ui.grafanaStateButton.noData,
  58. [GrafanaAlertState.Error]: ui.grafanaStateButton.error,
  59. };
  60. render(<RuleDetailsMatchingInstances rule={rule} />);
  61. await userEvent.click(buttons[state].get());
  62. expect(ui.instanceRow.getAll()).toHaveLength(1);
  63. expect(ui.instanceRow.get()).toHaveTextContent(alertStateToReadable(state));
  64. });
  65. it('For Cloud rules instances filter should contain two states', () => {
  66. const rule = mockCombinedRule({
  67. namespace: mockPromNamespace(),
  68. });
  69. render(<RuleDetailsMatchingInstances rule={rule} />);
  70. const stateFilter = ui.stateFilter.get();
  71. expect(stateFilter).toBeInTheDocument();
  72. const stateButtons = ui.stateButton.getAll(stateFilter);
  73. expect(stateButtons).toHaveLength(2);
  74. expect(ui.cloudStateButton.firing.get(stateFilter)).toBeInTheDocument();
  75. expect(ui.cloudStateButton.pending.get(stateFilter)).toBeInTheDocument();
  76. });
  77. it.each([PromAlertingRuleState.Pending, PromAlertingRuleState.Firing] as const)(
  78. 'Should filter cloud rules by %s state',
  79. async (state) => {
  80. const rule = mockCombinedRule({
  81. namespace: mockPromNamespace(),
  82. promRule: mockPromAlertingRule({
  83. alerts: [
  84. mockPromAlert({ state: PromAlertingRuleState.Firing }),
  85. mockPromAlert({ state: PromAlertingRuleState.Pending }),
  86. ],
  87. }),
  88. });
  89. render(<RuleDetailsMatchingInstances rule={rule} />);
  90. await userEvent.click(ui.cloudStateButton[state].get());
  91. expect(ui.instanceRow.getAll()).toHaveLength(1);
  92. expect(ui.instanceRow.get()).toHaveTextContent(alertStateToReadable(state));
  93. }
  94. );
  95. });
  96. });
  97. function mockPromNamespace(): CombinedRuleNamespace {
  98. return {
  99. rulesSource: mockDataSource(),
  100. groups: [{ name: 'Prom rules group', rules: [] }],
  101. name: 'Prometheus-test',
  102. };
  103. }