useAlertManagerSourceName.test.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { renderHook } from '@testing-library/react-hooks';
  2. import { createMemoryHistory } from 'history';
  3. import React from 'react';
  4. import { MemoryRouter, Router } from 'react-router-dom';
  5. import store from 'app/core/store';
  6. import { ALERTMANAGER_NAME_LOCAL_STORAGE_KEY } from '../utils/constants';
  7. import { AlertManagerDataSource, GRAFANA_RULES_SOURCE_NAME } from '../utils/datasource';
  8. import { useAlertManagerSourceName } from './useAlertManagerSourceName';
  9. const grafanaAm: AlertManagerDataSource = {
  10. name: GRAFANA_RULES_SOURCE_NAME,
  11. imgUrl: '',
  12. };
  13. const externalAmProm: AlertManagerDataSource = {
  14. name: 'PrometheusAm',
  15. imgUrl: '',
  16. };
  17. const externalAmMimir: AlertManagerDataSource = {
  18. name: 'MimirAm',
  19. imgUrl: '',
  20. };
  21. describe('useAlertManagerSourceName', () => {
  22. it('Should return undefined alert manager name when there are no available alert managers', () => {
  23. const wrapper: React.FC = ({ children }) => <MemoryRouter>{children}</MemoryRouter>;
  24. const { result } = renderHook(() => useAlertManagerSourceName([]), { wrapper });
  25. const [alertManager] = result.current;
  26. expect(alertManager).toBeUndefined();
  27. });
  28. it('Should return Grafana AM when it is available and no alert manager query param exists', () => {
  29. const wrapper: React.FC = ({ children }) => <MemoryRouter>{children}</MemoryRouter>;
  30. const availableAMs = [grafanaAm, externalAmProm, externalAmMimir];
  31. const { result } = renderHook(() => useAlertManagerSourceName(availableAMs), { wrapper });
  32. const [alertManager] = result.current;
  33. expect(alertManager).toBe(grafanaAm.name);
  34. });
  35. it('Should return alert manager included in the query param when available', () => {
  36. const history = createMemoryHistory();
  37. history.push({ search: `alertmanager=${externalAmProm.name}` });
  38. const wrapper: React.FC = ({ children }) => <Router history={history}>{children}</Router>;
  39. const availableAMs = [grafanaAm, externalAmProm, externalAmMimir];
  40. const { result } = renderHook(() => useAlertManagerSourceName(availableAMs), { wrapper });
  41. const [alertManager] = result.current;
  42. expect(alertManager).toBe(externalAmProm.name);
  43. });
  44. it('Should return undefined if alert manager included in the query is not available', () => {
  45. const history = createMemoryHistory();
  46. history.push({ search: `alertmanager=Not available external AM` });
  47. const wrapper: React.FC = ({ children }) => <Router history={history}>{children}</Router>;
  48. const availableAMs = [grafanaAm, externalAmProm, externalAmMimir];
  49. const { result } = renderHook(() => useAlertManagerSourceName(availableAMs), { wrapper });
  50. const [alertManager] = result.current;
  51. expect(alertManager).toBe(undefined);
  52. });
  53. it('Should return alert manager from store if available and query is empty', () => {
  54. const wrapper: React.FC = ({ children }) => <MemoryRouter>{children}</MemoryRouter>;
  55. const availableAMs = [grafanaAm, externalAmProm, externalAmMimir];
  56. store.set(ALERTMANAGER_NAME_LOCAL_STORAGE_KEY, externalAmProm.name);
  57. const { result } = renderHook(() => useAlertManagerSourceName(availableAMs), { wrapper });
  58. const [alertManager] = result.current;
  59. expect(alertManager).toBe(externalAmProm.name);
  60. });
  61. it('Should prioritize the alert manager from query over store', () => {
  62. const history = createMemoryHistory();
  63. history.push({ search: `alertmanager=${externalAmProm.name}` });
  64. const wrapper: React.FC = ({ children }) => <Router history={history}>{children}</Router>;
  65. const availableAMs = [grafanaAm, externalAmProm, externalAmMimir];
  66. store.set(ALERTMANAGER_NAME_LOCAL_STORAGE_KEY, externalAmMimir.name);
  67. const { result } = renderHook(() => useAlertManagerSourceName(availableAMs), { wrapper });
  68. const [alertManager] = result.current;
  69. expect(alertManager).toBe(externalAmProm.name);
  70. });
  71. });