AlertStatesWorker.test.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { AlertState, AlertStateInfo, getDefaultTimeRange, TimeRange } from '@grafana/data';
  2. import { backendSrv } from 'app/core/services/backend_srv';
  3. import { silenceConsoleOutput } from '../../../../../test/core/utils/silenceConsoleOutput';
  4. import * as store from '../../../../store/store';
  5. import { AlertStatesWorker } from './AlertStatesWorker';
  6. import { DashboardQueryRunnerOptions } from './types';
  7. jest.mock('@grafana/runtime', () => ({
  8. ...(jest.requireActual('@grafana/runtime') as unknown as object),
  9. getBackendSrv: () => backendSrv,
  10. }));
  11. function getDefaultOptions(): DashboardQueryRunnerOptions {
  12. const dashboard: any = { id: 'an id', panels: [{ alert: {} }] };
  13. const range = getDefaultTimeRange();
  14. return { dashboard, range };
  15. }
  16. function getTestContext() {
  17. jest.clearAllMocks();
  18. const dispatchMock = jest.spyOn(store, 'dispatch');
  19. const options = getDefaultOptions();
  20. const getMock = jest.spyOn(backendSrv, 'get');
  21. return { getMock, options, dispatchMock };
  22. }
  23. describe('AlertStatesWorker', () => {
  24. const worker = new AlertStatesWorker();
  25. describe('when canWork is called with correct props', () => {
  26. it('then it should return true', () => {
  27. const options = getDefaultOptions();
  28. expect(worker.canWork(options)).toBe(true);
  29. });
  30. });
  31. describe('when canWork is called with no dashboard id', () => {
  32. it('then it should return false', () => {
  33. const dashboard: any = {};
  34. const options = { ...getDefaultOptions(), dashboard };
  35. expect(worker.canWork(options)).toBe(false);
  36. });
  37. });
  38. describe('when canWork is called with wrong range', () => {
  39. it('then it should return false', () => {
  40. const defaultRange = getDefaultTimeRange();
  41. const range: TimeRange = { ...defaultRange, raw: { ...defaultRange.raw, to: 'now-6h' } };
  42. const options = { ...getDefaultOptions(), range };
  43. expect(worker.canWork(options)).toBe(false);
  44. });
  45. });
  46. describe('when canWork is called for dashboard with no alert panels', () => {
  47. it('then it should return false', () => {
  48. const options = getDefaultOptions();
  49. options.dashboard.panels.forEach((panel) => delete panel.alert);
  50. expect(worker.canWork(options)).toBe(false);
  51. });
  52. });
  53. describe('when run is called with incorrect props', () => {
  54. it('then it should return the correct results', async () => {
  55. const { getMock, options } = getTestContext();
  56. const dashboard: any = {};
  57. await expect(worker.work({ ...options, dashboard })).toEmitValuesWith((received) => {
  58. expect(received).toHaveLength(1);
  59. const results = received[0];
  60. expect(results).toEqual({ alertStates: [], annotations: [] });
  61. expect(getMock).not.toHaveBeenCalled();
  62. });
  63. });
  64. });
  65. describe('when run is called with correct props and request is successful', () => {
  66. it('then it should return the correct results', async () => {
  67. const getResults: AlertStateInfo[] = [
  68. { id: 1, state: AlertState.Alerting, dashboardId: 1, panelId: 1 },
  69. { id: 2, state: AlertState.Alerting, dashboardId: 1, panelId: 2 },
  70. ];
  71. const { getMock, options } = getTestContext();
  72. getMock.mockResolvedValue(getResults);
  73. await expect(worker.work(options)).toEmitValuesWith((received) => {
  74. expect(received).toHaveLength(1);
  75. const results = received[0];
  76. expect(results).toEqual({ alertStates: getResults, annotations: [] });
  77. expect(getMock).toHaveBeenCalledTimes(1);
  78. });
  79. });
  80. });
  81. describe('when run is called with correct props and request fails', () => {
  82. silenceConsoleOutput();
  83. it('then it should return the correct results', async () => {
  84. const { getMock, options, dispatchMock } = getTestContext();
  85. getMock.mockRejectedValue({ message: 'An error' });
  86. await expect(worker.work(options)).toEmitValuesWith((received) => {
  87. expect(received).toHaveLength(1);
  88. const results = received[0];
  89. expect(results).toEqual({ alertStates: [], annotations: [] });
  90. expect(getMock).toHaveBeenCalledTimes(1);
  91. expect(dispatchMock).toHaveBeenCalledTimes(1);
  92. });
  93. });
  94. });
  95. describe('when run is called with correct props and request is cancelled', () => {
  96. silenceConsoleOutput();
  97. it('then it should return the correct results', async () => {
  98. const { getMock, options, dispatchMock } = getTestContext();
  99. getMock.mockRejectedValue({ cancelled: true });
  100. await expect(worker.work(options)).toEmitValuesWith((received) => {
  101. expect(received).toHaveLength(1);
  102. const results = received[0];
  103. expect(results).toEqual({ alertStates: [], annotations: [] });
  104. expect(getMock).toHaveBeenCalledTimes(1);
  105. expect(dispatchMock).not.toHaveBeenCalled();
  106. });
  107. });
  108. });
  109. });