ReceiversTable.test.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { render } from '@testing-library/react';
  2. import React from 'react';
  3. import { Provider } from 'react-redux';
  4. import { Router } from 'react-router-dom';
  5. import { byRole } from 'testing-library-selector';
  6. import { locationService } from '@grafana/runtime';
  7. import {
  8. AlertManagerCortexConfig,
  9. GrafanaManagedReceiverConfig,
  10. Receiver,
  11. } from 'app/plugins/datasource/alertmanager/types';
  12. import { configureStore } from 'app/store/configureStore';
  13. import { NotifierDTO, NotifierType } from 'app/types';
  14. import { fetchGrafanaNotifiersAction } from '../../state/actions';
  15. import { ReceiversTable } from './ReceiversTable';
  16. const renderReceieversTable = async (receivers: Receiver[], notifiers: NotifierDTO[]) => {
  17. const config: AlertManagerCortexConfig = {
  18. template_files: {},
  19. alertmanager_config: {
  20. receivers,
  21. },
  22. };
  23. const store = configureStore();
  24. await store.dispatch(fetchGrafanaNotifiersAction.fulfilled(notifiers, 'initial'));
  25. return render(
  26. <Provider store={store}>
  27. <Router history={locationService.getHistory()}>
  28. <ReceiversTable config={config} alertManagerName="alertmanager-1" />
  29. </Router>
  30. </Provider>
  31. );
  32. };
  33. const mockGrafanaReceiver = (type: string): GrafanaManagedReceiverConfig => ({
  34. type,
  35. disableResolveMessage: false,
  36. secureFields: {},
  37. settings: {},
  38. name: type,
  39. });
  40. const mockNotifier = (type: NotifierType, name: string): NotifierDTO => ({
  41. type,
  42. name,
  43. description: 'its a mock',
  44. heading: 'foo',
  45. options: [],
  46. });
  47. const ui = {
  48. table: byRole<HTMLTableElement>('table'),
  49. };
  50. describe('ReceiversTable', () => {
  51. it('render receivers with grafana notifiers', async () => {
  52. const receivers: Receiver[] = [
  53. {
  54. name: 'with receivers',
  55. grafana_managed_receiver_configs: [mockGrafanaReceiver('googlechat'), mockGrafanaReceiver('sensugo')],
  56. },
  57. {
  58. name: 'without receivers',
  59. grafana_managed_receiver_configs: [],
  60. },
  61. ];
  62. const notifiers: NotifierDTO[] = [mockNotifier('googlechat', 'Google Chat'), mockNotifier('sensugo', 'Sensu Go')];
  63. await renderReceieversTable(receivers, notifiers);
  64. const table = await ui.table.find();
  65. const rows = table.querySelector('tbody')?.querySelectorAll('tr')!;
  66. expect(rows).toHaveLength(2);
  67. expect(rows[0].querySelectorAll('td')[0]).toHaveTextContent('with receivers');
  68. expect(rows[0].querySelectorAll('td')[1]).toHaveTextContent('Google Chat, Sensu Go');
  69. expect(rows[1].querySelectorAll('td')[0]).toHaveTextContent('without receivers');
  70. expect(rows[1].querySelectorAll('td')[1].textContent).toEqual('');
  71. });
  72. it('render receivers with alertmanager notifers', async () => {
  73. const receivers: Receiver[] = [
  74. {
  75. name: 'with receivers',
  76. email_configs: [
  77. {
  78. to: 'domas.lapinskas@grafana.com',
  79. },
  80. ],
  81. slack_configs: [],
  82. webhook_configs: [
  83. {
  84. url: 'http://example.com',
  85. },
  86. ],
  87. opsgenie_configs: [
  88. {
  89. foo: 'bar',
  90. },
  91. ],
  92. foo_configs: [
  93. {
  94. url: 'bar',
  95. },
  96. ],
  97. },
  98. {
  99. name: 'without receivers',
  100. },
  101. ];
  102. await renderReceieversTable(receivers, []);
  103. const table = await ui.table.find();
  104. const rows = table.querySelector('tbody')?.querySelectorAll('tr')!;
  105. expect(rows).toHaveLength(2);
  106. expect(rows[0].querySelectorAll('td')[0]).toHaveTextContent('with receivers');
  107. expect(rows[0].querySelectorAll('td')[1]).toHaveTextContent('Email, Webhook, OpsGenie, Foo');
  108. expect(rows[1].querySelectorAll('td')[0]).toHaveTextContent('without receivers');
  109. expect(rows[1].querySelectorAll('td')[1].textContent).toEqual('');
  110. });
  111. });