testHelpers.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { asyncScheduler, Observable, of, scheduled } from 'rxjs';
  2. import { AnnotationEvent, getDefaultTimeRange } from '@grafana/data';
  3. import { DashboardQueryRunnerOptions } from './types';
  4. // function that creates an async of result Observable
  5. export function toAsyncOfResult(result: any): Observable<any> {
  6. return scheduled(of(result), asyncScheduler);
  7. }
  8. export const LEGACY_DS_NAME = 'Legacy';
  9. export const NEXT_GEN_DS_NAME = 'NextGen';
  10. function getSnapshotData(annotation: any): AnnotationEvent[] {
  11. return [{ annotation, source: {}, timeEnd: 2, time: 1 }];
  12. }
  13. function getAnnotation({
  14. enable = true,
  15. useSnapshotData = false,
  16. datasource = LEGACY_DS_NAME,
  17. }: { enable?: boolean; useSnapshotData?: boolean; datasource?: string } = {}) {
  18. const annotation = {
  19. id: useSnapshotData ? 'Snapshotted' : undefined,
  20. enable,
  21. hide: false,
  22. name: 'Test',
  23. iconColor: 'pink',
  24. datasource,
  25. };
  26. return {
  27. ...annotation,
  28. snapshotData: useSnapshotData ? getSnapshotData(annotation) : undefined,
  29. };
  30. }
  31. export function getDefaultOptions(): DashboardQueryRunnerOptions {
  32. const legacy = getAnnotation({ datasource: LEGACY_DS_NAME });
  33. const nextGen = getAnnotation({ datasource: NEXT_GEN_DS_NAME });
  34. const dashboard: any = {
  35. id: 1,
  36. annotations: {
  37. list: [
  38. legacy,
  39. nextGen,
  40. getAnnotation({ enable: false }),
  41. getAnnotation({ useSnapshotData: true }),
  42. getAnnotation({ enable: false, useSnapshotData: true }),
  43. ],
  44. },
  45. events: {
  46. subscribe: jest.fn().mockReturnValue({ unsubscribe: jest.fn() }),
  47. publish: jest.fn(),
  48. },
  49. panels: [{ alert: {} } as any],
  50. };
  51. const range = getDefaultTimeRange();
  52. return { dashboard, range };
  53. }