mergePanelAndDashData.test.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { TestScheduler } from 'rxjs/testing';
  2. import { AlertState, getDefaultTimeRange, LoadingState, PanelData, toDataFrame } from '@grafana/data';
  3. import { mergePanelAndDashData } from './mergePanelAndDashData';
  4. function getTestContext() {
  5. const timeRange = getDefaultTimeRange();
  6. const panelData: PanelData = {
  7. state: LoadingState.Done,
  8. series: [],
  9. annotations: [toDataFrame([{ id: 'panelData' }])],
  10. timeRange,
  11. };
  12. const scheduler: TestScheduler = new TestScheduler((actual, expected) => {
  13. expect(actual).toEqual(expected);
  14. });
  15. return { timeRange, scheduler, panelData };
  16. }
  17. describe('mergePanelAndDashboardData', () => {
  18. describe('when called and dashboard data contains annotations', () => {
  19. it('then the annotations should be combined', () => {
  20. const { panelData, timeRange, scheduler } = getTestContext();
  21. scheduler.run(({ cold, expectObservable }) => {
  22. const panelObservable = cold('a', { a: panelData });
  23. const dashObservable = cold('a', { a: { annotations: [{ id: 'dashData' }] } });
  24. const result = mergePanelAndDashData(panelObservable, dashObservable);
  25. expectObservable(result).toBe('a', {
  26. a: {
  27. state: LoadingState.Done,
  28. series: [],
  29. annotations: [toDataFrame([{ id: 'panelData' }]), toDataFrame([{ id: 'dashData' }])],
  30. timeRange,
  31. },
  32. });
  33. });
  34. scheduler.flush();
  35. });
  36. });
  37. describe('when called and dashboard data contains alert states', () => {
  38. it('then the alert states should be added', () => {
  39. const { panelData, timeRange, scheduler } = getTestContext();
  40. scheduler.run(({ cold, expectObservable }) => {
  41. const panelObservable = cold('a', { a: panelData });
  42. const dashObservable = cold('a', {
  43. a: {
  44. annotations: [],
  45. alertState: { id: 1, state: AlertState.OK, dashboardId: 1, panelId: 1, newStateDate: '' },
  46. },
  47. });
  48. const result = mergePanelAndDashData(panelObservable, dashObservable);
  49. expectObservable(result).toBe('a', {
  50. a: {
  51. state: LoadingState.Done,
  52. series: [],
  53. annotations: [toDataFrame([{ id: 'panelData' }]), toDataFrame([])],
  54. alertState: { id: 1, state: AlertState.OK, dashboardId: 1, panelId: 1, newStateDate: '' },
  55. timeRange,
  56. },
  57. });
  58. });
  59. scheduler.flush();
  60. });
  61. });
  62. describe('when called and dashboard data does not contain annotations or alertState', () => {
  63. it('then the panelData is unchanged', () => {
  64. const { panelData, timeRange, scheduler } = getTestContext();
  65. scheduler.run(({ cold, expectObservable }) => {
  66. const panelObservable = cold('a', { a: panelData });
  67. const dashObservable = cold('a', {
  68. a: {
  69. annotations: [],
  70. },
  71. });
  72. const result = mergePanelAndDashData(panelObservable, dashObservable);
  73. expectObservable(result).toBe('a', {
  74. a: {
  75. state: LoadingState.Done,
  76. series: [],
  77. annotations: [toDataFrame([{ id: 'panelData' }])],
  78. timeRange,
  79. },
  80. });
  81. });
  82. scheduler.flush();
  83. });
  84. });
  85. });