panel.test.ts 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { advanceTo, clear } from 'jest-date-mock';
  2. import { ComponentClass } from 'react';
  3. import { dateTime, DateTime, PanelProps, TimeRange } from '@grafana/data';
  4. import { applyPanelTimeOverrides, calculateInnerPanelHeight } from 'app/features/dashboard/utils/panel';
  5. import { getPanelPlugin } from '../../plugins/__mocks__/pluginMocks';
  6. import { PanelModel } from '../state';
  7. const dashboardTimeRange: TimeRange = {
  8. from: dateTime([2019, 1, 11, 12, 0]),
  9. to: dateTime([2019, 1, 11, 18, 0]),
  10. raw: {
  11. from: 'now-6h',
  12. to: 'now',
  13. },
  14. };
  15. describe('applyPanelTimeOverrides', () => {
  16. const fakeCurrentDate = dateTime([2019, 1, 11, 14, 0, 0]).toDate();
  17. beforeAll(() => {
  18. advanceTo(fakeCurrentDate);
  19. });
  20. afterAll(() => {
  21. clear();
  22. });
  23. it('should apply relative time override', () => {
  24. const panelModel = {
  25. timeFrom: '2h',
  26. };
  27. // @ts-ignore: PanelModel type inconsistency
  28. const overrides = applyPanelTimeOverrides(panelModel, dashboardTimeRange);
  29. expect(overrides.timeRange.from.toISOString()).toBe(dateTime([2019, 1, 11, 12]).toISOString());
  30. expect(overrides.timeRange.to.toISOString()).toBe(fakeCurrentDate.toISOString());
  31. expect(overrides.timeRange.raw.from).toBe('now-2h');
  32. expect(overrides.timeRange.raw.to).toBe('now');
  33. });
  34. it('should apply time shift', () => {
  35. const panelModel = {
  36. timeShift: '2h',
  37. };
  38. const expectedFromDate = dateTime([2019, 1, 11, 10, 0, 0]).toDate();
  39. const expectedToDate = dateTime([2019, 1, 11, 16, 0, 0]).toDate();
  40. // @ts-ignore: PanelModel type inconsistency
  41. const overrides = applyPanelTimeOverrides(panelModel, dashboardTimeRange);
  42. expect(overrides.timeRange.from.toISOString()).toBe(expectedFromDate.toISOString());
  43. expect(overrides.timeRange.to.toISOString()).toBe(expectedToDate.toISOString());
  44. expect((overrides.timeRange.raw.from as DateTime).toISOString()).toEqual(expectedFromDate.toISOString());
  45. expect((overrides.timeRange.raw.to as DateTime).toISOString()).toEqual(expectedToDate.toISOString());
  46. });
  47. it('should apply both relative time and time shift', () => {
  48. const panelModel = {
  49. timeFrom: '2h',
  50. timeShift: '2h',
  51. };
  52. const expectedFromDate = dateTime([2019, 1, 11, 10, 0, 0]).toDate();
  53. const expectedToDate = dateTime([2019, 1, 11, 12, 0, 0]).toDate();
  54. // @ts-ignore: PanelModel type inconsistency
  55. const overrides = applyPanelTimeOverrides(panelModel, dashboardTimeRange);
  56. expect(overrides.timeRange.from.toISOString()).toBe(expectedFromDate.toISOString());
  57. expect(overrides.timeRange.to.toISOString()).toBe(expectedToDate.toISOString());
  58. expect((overrides.timeRange.raw.from as DateTime).toISOString()).toEqual(expectedFromDate.toISOString());
  59. expect((overrides.timeRange.raw.to as DateTime).toISOString()).toEqual(expectedToDate.toISOString());
  60. });
  61. it('Calculate panel height', () => {
  62. const panelModel = new PanelModel({});
  63. const height = calculateInnerPanelHeight(panelModel, 100);
  64. expect(height).toBe(82);
  65. });
  66. it('Calculate panel height with panel plugin zeroChromePadding', () => {
  67. const panelModel = new PanelModel({});
  68. panelModel.pluginLoaded(
  69. getPanelPlugin({ id: 'table' }, null as unknown as ComponentClass<PanelProps>, null).setNoPadding()
  70. );
  71. const height = calculateInnerPanelHeight(panelModel, 100);
  72. expect(height).toBe(98);
  73. });
  74. });