RuleViewer.test.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import { act, render, screen } from '@testing-library/react';
  2. import React from 'react';
  3. import { Provider } from 'react-redux';
  4. import { Router } from 'react-router-dom';
  5. import { DataSourceJsonData, PluginMeta } from '@grafana/data';
  6. import { locationService } from '@grafana/runtime';
  7. import { GrafanaRouteComponentProps } from 'app/core/navigation/types';
  8. import { configureStore } from 'app/store/configureStore';
  9. import { CombinedRule } from 'app/types/unified-alerting';
  10. import { GrafanaAlertStateDecision } from 'app/types/unified-alerting-dto';
  11. import { RuleViewer } from './RuleViewer';
  12. import { useCombinedRule } from './hooks/useCombinedRule';
  13. import { GRAFANA_RULES_SOURCE_NAME } from './utils/datasource';
  14. jest.mock('./hooks/useCombinedRule');
  15. jest.mock('@grafana/runtime', () => ({
  16. ...(jest.requireActual('@grafana/runtime') as any),
  17. getDataSourceSrv: () => {
  18. return {
  19. getInstanceSettings: () => ({ name: 'prometheus' }),
  20. get: () =>
  21. Promise.resolve({
  22. filterQuery: () => true,
  23. }),
  24. };
  25. },
  26. }));
  27. const store = configureStore();
  28. const renderRuleViewer = () => {
  29. return act(async () => {
  30. render(
  31. <Provider store={store}>
  32. <Router history={locationService.getHistory()}>
  33. <RuleViewer {...mockRoute} />
  34. </Router>
  35. </Provider>
  36. );
  37. });
  38. };
  39. describe('RuleViewer', () => {
  40. afterEach(() => {
  41. jest.resetAllMocks();
  42. });
  43. it('should render page with grafana alert', async () => {
  44. jest.mocked(useCombinedRule).mockReturnValue({
  45. result: mockGrafanaRule as CombinedRule,
  46. loading: false,
  47. dispatched: true,
  48. requestId: 'A',
  49. error: undefined,
  50. });
  51. await renderRuleViewer();
  52. expect(screen.getByText('Alerting / View rule')).toBeInTheDocument();
  53. expect(screen.getByText('Test alert')).toBeInTheDocument();
  54. });
  55. it('should render page with cloud alert', async () => {
  56. jest.mocked(useCombinedRule).mockReturnValue({
  57. result: mockCloudRule as CombinedRule,
  58. loading: false,
  59. dispatched: true,
  60. requestId: 'A',
  61. error: undefined,
  62. });
  63. await renderRuleViewer();
  64. expect(screen.getByText('Alerting / View rule')).toBeInTheDocument();
  65. expect(screen.getByText('Cloud test alert')).toBeInTheDocument();
  66. });
  67. });
  68. const mockGrafanaRule = {
  69. name: 'Test alert',
  70. query: 'up',
  71. labels: {},
  72. annotations: {},
  73. group: {
  74. name: 'Prom up alert',
  75. rules: [],
  76. },
  77. namespace: {
  78. rulesSource: GRAFANA_RULES_SOURCE_NAME,
  79. name: 'Alerts',
  80. groups: [],
  81. },
  82. rulerRule: {
  83. for: '',
  84. annotations: {},
  85. labels: {},
  86. grafana_alert: {
  87. condition: 'B',
  88. exec_err_state: GrafanaAlertStateDecision.Alerting,
  89. namespace_id: 11,
  90. namespace_uid: 'namespaceuid123',
  91. no_data_state: GrafanaAlertStateDecision.NoData,
  92. title: 'Test alert',
  93. uid: 'asdf23',
  94. data: [],
  95. },
  96. },
  97. };
  98. const mockCloudRule = {
  99. name: 'Cloud test alert',
  100. labels: {},
  101. query: 'up == 0',
  102. annotations: {},
  103. group: {
  104. name: 'test',
  105. rules: [],
  106. },
  107. promRule: {
  108. health: 'ok',
  109. name: 'cloud up alert',
  110. query: 'up == 0',
  111. type: 'alerting',
  112. },
  113. namespace: {
  114. name: 'prom test alerts',
  115. groups: [],
  116. rulesSource: {
  117. name: 'prom test',
  118. type: 'prometheus',
  119. uid: 'asdf23',
  120. id: 1,
  121. meta: {} as PluginMeta,
  122. jsonData: {} as DataSourceJsonData,
  123. access: 'proxy',
  124. },
  125. },
  126. };
  127. const mockRoute: GrafanaRouteComponentProps<{ id?: string; sourceName?: string }> = {
  128. route: {
  129. path: '/',
  130. component: RuleViewer,
  131. },
  132. queryParams: { returnTo: '/alerting/list' },
  133. match: { params: { id: 'test1', sourceName: 'grafana' }, isExact: false, url: 'asdf', path: '' },
  134. history: locationService.getHistory(),
  135. location: { pathname: '', hash: '', search: '', state: '' },
  136. staticContext: {},
  137. };