migrations.test.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { FieldColorModeId, FieldConfigProperty, FieldMatcherID, PanelModel } from '@grafana/data';
  2. import { LegendDisplayMode } from '@grafana/schema';
  3. import { PieChartPanelChangedHandler } from './migrations';
  4. import { PieChartLabels } from './types';
  5. describe('PieChart -> PieChartV2 migrations', () => {
  6. it('only migrates old piechart', () => {
  7. const panel = {} as PanelModel;
  8. const options = PieChartPanelChangedHandler(panel, 'some-panel-id', {});
  9. expect(options).toEqual({});
  10. });
  11. it('correctly assigns color overrides', () => {
  12. const panel = { options: {} } as PanelModel;
  13. const oldPieChartOptions = {
  14. angular: {
  15. aliasColors: { x: '#fff' },
  16. },
  17. };
  18. PieChartPanelChangedHandler(panel, 'grafana-piechart-panel', oldPieChartOptions);
  19. expect(panel.fieldConfig.overrides).toContainEqual({
  20. matcher: {
  21. id: FieldMatcherID.byName,
  22. options: 'x',
  23. },
  24. properties: [
  25. {
  26. id: FieldConfigProperty.Color,
  27. value: {
  28. mode: FieldColorModeId.Fixed,
  29. fixedColor: '#fff',
  30. },
  31. },
  32. ],
  33. });
  34. });
  35. it('correctly sets sum calculation', () => {
  36. const panel = { options: {} } as PanelModel;
  37. const oldPieChartOptions = {
  38. angular: { valueName: 'total' },
  39. };
  40. const options = PieChartPanelChangedHandler(panel, 'grafana-piechart-panel', oldPieChartOptions);
  41. expect(options).toMatchObject({ reduceOptions: { calcs: ['sum'] } });
  42. });
  43. it('correctly sets labels when old PieChart has legend on graph', () => {
  44. const panel = { options: {} } as PanelModel;
  45. const oldPieChartOptions = {
  46. angular: {
  47. legendType: 'On graph',
  48. legend: { values: true },
  49. },
  50. };
  51. const options = PieChartPanelChangedHandler(panel, 'grafana-piechart-panel', oldPieChartOptions);
  52. expect(options).toMatchObject({ displayLabels: [PieChartLabels.Name, PieChartLabels.Value] });
  53. });
  54. it('hides the legend when no legend values are selected', () => {
  55. const panel = { options: {} } as PanelModel;
  56. const oldPieChartOptions = {
  57. angular: {
  58. legendType: 'On graph',
  59. legend: {},
  60. },
  61. };
  62. const options = PieChartPanelChangedHandler(panel, 'grafana-piechart-panel', oldPieChartOptions);
  63. expect(options).toMatchObject({ legend: { displayMode: LegendDisplayMode.Hidden } });
  64. });
  65. });