LegacyAnnotationQueryRunner.test.ts 4.2 KB

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