AnnotationsQueryRunner.test.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { Observable, of, throwError } from 'rxjs';
  2. import { getDefaultTimeRange } from '@grafana/data';
  3. import { silenceConsoleOutput } from '../../../../../test/core/utils/silenceConsoleOutput';
  4. import * as store from '../../../../store/store';
  5. import * as annotationsSrv from '../../../annotations/executeAnnotationQuery';
  6. import { AnnotationsQueryRunner } from './AnnotationsQueryRunner';
  7. import { toAsyncOfResult } from './testHelpers';
  8. import { AnnotationQueryRunnerOptions } from './types';
  9. function getDefaultOptions(): AnnotationQueryRunnerOptions {
  10. const annotation: any = {};
  11. const dashboard: any = {};
  12. const datasource: any = {
  13. annotationQuery: {},
  14. annotations: {},
  15. };
  16. const range = getDefaultTimeRange();
  17. return { annotation, datasource, dashboard, range };
  18. }
  19. function getTestContext(result: Observable<any> = toAsyncOfResult({ events: [{ id: '1' }] })) {
  20. jest.clearAllMocks();
  21. const dispatchMock = jest.spyOn(store, 'dispatch');
  22. const options = getDefaultOptions();
  23. const executeAnnotationQueryMock = jest.spyOn(annotationsSrv, 'executeAnnotationQuery').mockReturnValue(result);
  24. return { options, dispatchMock, executeAnnotationQueryMock };
  25. }
  26. describe('AnnotationsQueryRunner', () => {
  27. const runner = new AnnotationsQueryRunner();
  28. describe('when canWork is called with correct props', () => {
  29. it('then it should return true', () => {
  30. const datasource: any = {
  31. annotationQuery: jest.fn(),
  32. annotations: {},
  33. };
  34. expect(runner.canRun(datasource)).toBe(true);
  35. });
  36. });
  37. describe('when canWork is called without datasource', () => {
  38. it('then it should return false', () => {
  39. const datasource: any = undefined;
  40. expect(runner.canRun(datasource)).toBe(false);
  41. });
  42. });
  43. describe('when canWork is called with incorrect props', () => {
  44. it('then it should return false', () => {
  45. const datasource: any = {
  46. annotationQuery: jest.fn(),
  47. };
  48. expect(runner.canRun(datasource)).toBe(false);
  49. });
  50. });
  51. describe('when run is called with unsupported props', () => {
  52. it('then it should return the correct results', async () => {
  53. const datasource: any = {
  54. annotationQuery: jest.fn(),
  55. };
  56. const { options, executeAnnotationQueryMock } = getTestContext();
  57. await expect(runner.run({ ...options, datasource })).toEmitValuesWith((received) => {
  58. expect(received).toHaveLength(1);
  59. const results = received[0];
  60. expect(results).toEqual([]);
  61. expect(executeAnnotationQueryMock).toHaveBeenCalledTimes(0);
  62. });
  63. });
  64. });
  65. describe('when run is called and the request is successful', () => {
  66. it('then it should return the correct results', async () => {
  67. const { options, executeAnnotationQueryMock } = getTestContext();
  68. await expect(runner.run(options)).toEmitValuesWith((received) => {
  69. expect(received).toHaveLength(1);
  70. const results = received[0];
  71. expect(results).toEqual([{ id: '1' }]);
  72. expect(executeAnnotationQueryMock).toHaveBeenCalledTimes(1);
  73. });
  74. });
  75. describe('but result is missing events prop', () => {
  76. it('then it should return the correct results', async () => {
  77. const { options, executeAnnotationQueryMock } = getTestContext(of({ id: '1' }));
  78. await expect(runner.run(options)).toEmitValuesWith((received) => {
  79. expect(received).toHaveLength(1);
  80. const results = received[0];
  81. expect(results).toEqual([]);
  82. expect(executeAnnotationQueryMock).toHaveBeenCalledTimes(1);
  83. });
  84. });
  85. });
  86. });
  87. describe('when run is called and the request fails', () => {
  88. silenceConsoleOutput();
  89. it('then it should return the correct results', async () => {
  90. const { options, executeAnnotationQueryMock, dispatchMock } = getTestContext(throwError({ message: 'An error' }));
  91. await expect(runner.run(options)).toEmitValuesWith((received) => {
  92. expect(received).toHaveLength(1);
  93. const results = received[0];
  94. expect(results).toEqual([]);
  95. expect(executeAnnotationQueryMock).toHaveBeenCalledTimes(1);
  96. expect(dispatchMock).toHaveBeenCalledTimes(1);
  97. });
  98. });
  99. });
  100. describe('when run is called and the request is cancelled', () => {
  101. silenceConsoleOutput();
  102. it('then it should return the correct results', async () => {
  103. const { options, executeAnnotationQueryMock, dispatchMock } = getTestContext(throwError({ cancelled: true }));
  104. await expect(runner.run(options)).toEmitValuesWith((received) => {
  105. expect(received).toHaveLength(1);
  106. const results = received[0];
  107. expect(results).toEqual([]);
  108. expect(executeAnnotationQueryMock).toHaveBeenCalledTimes(1);
  109. expect(dispatchMock).not.toHaveBeenCalled();
  110. });
  111. });
  112. });
  113. });