SnapshotWorker.test.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { AnnotationEvent, getDefaultTimeRange } from '@grafana/data';
  2. import { SnapshotWorker } from './SnapshotWorker';
  3. import { DashboardQueryRunnerOptions } from './types';
  4. function getDefaultOptions(): DashboardQueryRunnerOptions {
  5. const dashboard: any = {};
  6. const range = getDefaultTimeRange();
  7. return { dashboard, range };
  8. }
  9. function getSnapshotData(annotation: any, timeEnd: number | undefined = undefined): AnnotationEvent[] {
  10. return [{ annotation, source: {}, timeEnd, time: 1 }];
  11. }
  12. function getAnnotation(timeEnd: number | undefined = undefined) {
  13. const annotation = {
  14. enable: true,
  15. hide: false,
  16. name: 'Test',
  17. iconColor: 'pink',
  18. };
  19. return {
  20. ...annotation,
  21. snapshotData: getSnapshotData(annotation, timeEnd),
  22. };
  23. }
  24. describe('SnapshotWorker', () => {
  25. const worker = new SnapshotWorker();
  26. describe('when canWork is called with correct props', () => {
  27. it('then it should return true', () => {
  28. const dashboard: any = { annotations: { list: [getAnnotation(), {}] } };
  29. const options = { ...getDefaultOptions(), dashboard };
  30. expect(worker.canWork(options)).toBe(true);
  31. });
  32. });
  33. describe('when canWork is called with incorrect props', () => {
  34. it('then it should return false', () => {
  35. const dashboard: any = { annotations: { list: [{}] } };
  36. const options = { ...getDefaultOptions(), dashboard };
  37. expect(worker.canWork(options)).toBe(false);
  38. });
  39. });
  40. describe('when run is called with incorrect props', () => {
  41. it('then it should return the correct results', async () => {
  42. const dashboard: any = { annotations: { list: [{}] } };
  43. const options = { ...getDefaultOptions(), dashboard };
  44. await expect(worker.work(options)).toEmitValues([{ alertStates: [], annotations: [] }]);
  45. });
  46. });
  47. describe('when run is called with correct props', () => {
  48. it('then it should return the correct results', async () => {
  49. const noRegionUndefined = getAnnotation();
  50. const noRegionEqualTime = getAnnotation(1);
  51. const region = getAnnotation(2);
  52. const noSnapshotData = { ...getAnnotation(), snapshotData: undefined };
  53. const dashboard: any = { annotations: { list: [noRegionUndefined, region, noSnapshotData, noRegionEqualTime] } };
  54. const options = { ...getDefaultOptions(), dashboard };
  55. await expect(worker.work(options)).toEmitValuesWith((received) => {
  56. expect(received).toHaveLength(1);
  57. const { alertStates, annotations } = received[0];
  58. expect(alertStates).toBeDefined();
  59. expect(annotations).toHaveLength(3);
  60. expect(annotations[0]).toEqual({
  61. annotation: { enable: true, hide: false, name: 'Test', iconColor: 'pink' },
  62. source: { enable: true, hide: false, name: 'Test', iconColor: 'pink' },
  63. timeEnd: undefined,
  64. time: 1,
  65. color: '#ffc0cb',
  66. type: 'Test',
  67. isRegion: false,
  68. });
  69. expect(annotations[1]).toEqual({
  70. annotation: { enable: true, hide: false, name: 'Test', iconColor: 'pink' },
  71. source: { enable: true, hide: false, name: 'Test', iconColor: 'pink' },
  72. timeEnd: 2,
  73. time: 1,
  74. color: '#ffc0cb',
  75. type: 'Test',
  76. isRegion: true,
  77. });
  78. expect(annotations[2]).toEqual({
  79. annotation: { enable: true, hide: false, name: 'Test', iconColor: 'pink' },
  80. source: { enable: true, hide: false, name: 'Test', iconColor: 'pink' },
  81. timeEnd: 1,
  82. time: 1,
  83. color: '#ffc0cb',
  84. type: 'Test',
  85. isRegion: false,
  86. });
  87. });
  88. });
  89. });
  90. });