grafana/public/app/features/dashboard/state/PanelModel.test.ts
Torkel Ödegaard abddb442a1
Changed how react panels store their options (#15468)
Changed how react panels store their options

* Added a ReactPanelPlugin as the interface that react panels export, this way react panels have clearer api, and gives us hooks to handle migrations and a way for panel to handle panel changes in the future
* Moved gauge value options into a sub oject and made editor more generic, will be moved out of gauge pane later and shared between singlestat, gauge, bargauge, honecomb
* Also remove nested options prop that was there due to bug
* Added missing Gauge props
* Fixed gauge issue that will require migration later and also value options editor did not handle null decimals or 0 decimals
* Fixed unit tests
* More fixes for react panels
2019-02-18 11:41:14 +01:00

74 lines
2.0 KiB
TypeScript

import _ from 'lodash';
import { PanelModel } from '../state/PanelModel';
describe('PanelModel', () => {
describe('when creating new panel model', () => {
let model;
beforeEach(() => {
model = new PanelModel({
type: 'table',
showColumns: true,
targets: [{ refId: 'A' }, { noRefId: true }],
});
});
it('should apply defaults', () => {
expect(model.gridPos.h).toBe(3);
});
it('should set model props on instance', () => {
expect(model.showColumns).toBe(true);
});
it('should add missing refIds', () => {
expect(model.targets[1].refId).toBe('B');
});
it('getSaveModel should remove defaults', () => {
const saveModel = model.getSaveModel();
expect(saveModel.gridPos).toBe(undefined);
});
it('getSaveModel should remove nonPersistedProperties', () => {
const saveModel = model.getSaveModel();
expect(saveModel.events).toBe(undefined);
});
describe('when changing panel type', () => {
beforeEach(() => {
model.changeType('graph', true);
model.alert = { id: 2 };
});
it('should remove table properties but keep core props', () => {
expect(model.showColumns).toBe(undefined);
});
it('should restore table properties when changing back', () => {
model.changeType('table', true);
expect(model.showColumns).toBe(true);
});
it('should remove alert rule when changing type that does not support it', () => {
model.changeType('table', true);
expect(model.alert).toBe(undefined);
});
});
describe('get panel options', () => {
it('should apply defaults', () => {
model.options = { existingProp: 10 };
const options = model.getOptions({
defaultProp: true,
existingProp: 0,
});
expect(options.defaultProp).toBe(true);
expect(options.existingProp).toBe(10);
expect(model.options).toBe(options);
});
});
});
});