grafana/public/app/features/dashboard/utils/panel.test.ts
Diana Payton a3d1cf03a9
Edit dashboard UI text (#32520)
* Update DashboardPermissions.tsx

* Update DashboardRow.tsx

* Update AutoRefreshIntervals.tsx

* Update JsonEditorSettings.tsx

* ui updates

* Update FolderPickerCtrl.ts

* Update LinkSettingsEdit.tsx

* Update LinkSettingsHeader.tsx

* Update LinkSettingsList.tsx

* Update actions.ts

* Update AngularPanelOptions.tsx

* Update getFieldOverrideElements.tsx

* Update OptionsPaneOptions.tsx

* Update types.ts

* Update usePanelLatestData.ts

* Update VisualizationButton.tsx

* Update RowOptionsModal.tsx

* Update SaveDashboardAsForm.tsx

* Update SaveDashboardForm.tsx

* Update SaveProvisionedDashboardForm.tsx

* Update SaveDashboardErrorProxy.tsx

* text edits

* Update ShareSnapshot.tsx

* Update PanelTypeCard.tsx

* Update TransformationsEditor.tsx

* Update panel.test.ts

* text edits

* Update DashboardSrv.ts

* Update VersionHistoryButtons.tsx

* Update DiffViewer.tsx

* Update VersionHistoryTable.tsx

* Update DashboardGrid.tsx

* Update DashboardPanel.tsx

* Update PanelPluginError.tsx

* Update DashboardMigrator.ts
2021-04-01 17:42:57 +02:00

94 lines
3.3 KiB
TypeScript

import { dateTime, DateTime, PanelProps, TimeRange } from '@grafana/data';
import { applyPanelTimeOverrides, calculateInnerPanelHeight } from 'app/features/dashboard/utils/panel';
import { advanceTo, clear } from 'jest-date-mock';
import { PanelModel } from '../state';
import { getPanelPlugin } from '../../plugins/__mocks__/pluginMocks';
import { ComponentClass } from 'react';
const dashboardTimeRange: TimeRange = {
from: dateTime([2019, 1, 11, 12, 0]),
to: dateTime([2019, 1, 11, 18, 0]),
raw: {
from: 'now-6h',
to: 'now',
},
};
describe('applyPanelTimeOverrides', () => {
const fakeCurrentDate = dateTime([2019, 1, 11, 14, 0, 0]).toDate();
beforeAll(() => {
advanceTo(fakeCurrentDate);
});
afterAll(() => {
clear();
});
it('should apply relative time override', () => {
const panelModel = {
timeFrom: '2h',
};
// @ts-ignore: PanelModel type inconsistency
const overrides = applyPanelTimeOverrides(panelModel, dashboardTimeRange);
expect(overrides.timeRange.from.toISOString()).toBe(dateTime([2019, 1, 11, 12]).toISOString());
expect(overrides.timeRange.to.toISOString()).toBe(fakeCurrentDate.toISOString());
expect(overrides.timeRange.raw.from).toBe('now-2h');
expect(overrides.timeRange.raw.to).toBe('now');
});
it('should apply time shift', () => {
const panelModel = {
timeShift: '2h',
};
const expectedFromDate = dateTime([2019, 1, 11, 10, 0, 0]).toDate();
const expectedToDate = dateTime([2019, 1, 11, 16, 0, 0]).toDate();
// @ts-ignore: PanelModel type inconsistency
const overrides = applyPanelTimeOverrides(panelModel, dashboardTimeRange);
expect(overrides.timeRange.from.toISOString()).toBe(expectedFromDate.toISOString());
expect(overrides.timeRange.to.toISOString()).toBe(expectedToDate.toISOString());
expect((overrides.timeRange.raw.from as DateTime).toISOString()).toEqual(expectedFromDate.toISOString());
expect((overrides.timeRange.raw.to as DateTime).toISOString()).toEqual(expectedToDate.toISOString());
});
it('should apply both relative time and time shift', () => {
const panelModel = {
timeFrom: '2h',
timeShift: '2h',
};
const expectedFromDate = dateTime([2019, 1, 11, 10, 0, 0]).toDate();
const expectedToDate = dateTime([2019, 1, 11, 12, 0, 0]).toDate();
// @ts-ignore: PanelModel type inconsistency
const overrides = applyPanelTimeOverrides(panelModel, dashboardTimeRange);
expect(overrides.timeRange.from.toISOString()).toBe(expectedFromDate.toISOString());
expect(overrides.timeRange.to.toISOString()).toBe(expectedToDate.toISOString());
expect((overrides.timeRange.raw.from as DateTime).toISOString()).toEqual(expectedFromDate.toISOString());
expect((overrides.timeRange.raw.to as DateTime).toISOString()).toEqual(expectedToDate.toISOString());
});
it('Calculate panel height', () => {
const panelModel = new PanelModel({});
const height = calculateInnerPanelHeight(panelModel, 100);
expect(height).toBe(82);
});
it('Calculate panel height with panel plugin zeroChromePadding', () => {
const panelModel = new PanelModel({});
panelModel.pluginLoaded(
getPanelPlugin({ id: 'table' }, (null as unknown) as ComponentClass<PanelProps>, null).setNoPadding()
);
const height = calculateInnerPanelHeight(panelModel, 100);
expect(height).toBe(98);
});
});