panelMerge.test.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { PanelModel } from '@grafana/data';
  2. import { DashboardModel } from '../state/DashboardModel';
  3. describe('Merge dashbaord panels', () => {
  4. describe('simple changes', () => {
  5. let dashboard: DashboardModel;
  6. let rawPanels: PanelModel[];
  7. beforeEach(() => {
  8. dashboard = new DashboardModel({
  9. title: 'simple title',
  10. panels: [
  11. {
  12. id: 1,
  13. type: 'timeseries',
  14. },
  15. {
  16. id: 2,
  17. type: 'timeseries',
  18. },
  19. {
  20. id: 3,
  21. type: 'table',
  22. fieldConfig: {
  23. defaults: {
  24. thresholds: {
  25. mode: 'absolute',
  26. steps: [
  27. { color: 'green', value: -Infinity }, // save model has this as null
  28. { color: 'red', value: 80 },
  29. ],
  30. },
  31. mappings: [],
  32. color: { mode: 'thresholds' },
  33. },
  34. overrides: [],
  35. },
  36. },
  37. ],
  38. });
  39. rawPanels = dashboard.getSaveModelClone().panels;
  40. });
  41. it('should load and support noop', () => {
  42. expect(dashboard.title).toBe('simple title');
  43. expect(dashboard.panels.length).toEqual(rawPanels.length);
  44. const info = dashboard.updatePanels(rawPanels);
  45. expect(info.changed).toBeFalsy();
  46. expect(info.actions).toMatchInlineSnapshot(`
  47. Object {
  48. "add": Array [],
  49. "noop": Array [
  50. 1,
  51. 2,
  52. 3,
  53. ],
  54. "remove": Array [],
  55. "replace": Array [],
  56. "update": Array [],
  57. }
  58. `);
  59. });
  60. it('should identify an add', () => {
  61. rawPanels.push({
  62. id: 7,
  63. type: 'canvas',
  64. } as any);
  65. const info = dashboard.updatePanels(rawPanels);
  66. expect(info.changed).toBeTruthy();
  67. expect(info.actions['add']).toEqual([7]);
  68. });
  69. it('should identify a remove', () => {
  70. rawPanels.shift();
  71. const info = dashboard.updatePanels(rawPanels);
  72. expect(info.changed).toBeTruthy();
  73. expect(info.actions['remove']).toEqual([1]);
  74. });
  75. it('should allow change in key order for nested elements', () => {
  76. (rawPanels[2] as any).fieldConfig = {
  77. defaults: {
  78. color: { mode: 'thresholds' },
  79. mappings: [],
  80. thresholds: {
  81. steps: [
  82. { color: 'green', value: null },
  83. { color: 'red', value: 80 },
  84. ],
  85. mode: 'absolute',
  86. },
  87. },
  88. overrides: [],
  89. };
  90. // Same config, different order
  91. const js0 = JSON.stringify(dashboard.panels[2].fieldConfig);
  92. const js1 = JSON.stringify(rawPanels[2].fieldConfig);
  93. expect(js1).not.toEqual(js0);
  94. expect(js1.length).toEqual(js0.length);
  95. // no real changes here
  96. const info = dashboard.updatePanels(rawPanels);
  97. expect(info.changed).toBeFalsy();
  98. });
  99. it('should replace a type change', () => {
  100. (rawPanels[1] as any).type = 'canvas';
  101. const info = dashboard.updatePanels(rawPanels);
  102. expect(info.changed).toBeTruthy();
  103. expect(info.actions).toMatchInlineSnapshot(`
  104. Object {
  105. "add": Array [],
  106. "noop": Array [
  107. 1,
  108. 3,
  109. ],
  110. "remove": Array [],
  111. "replace": Array [
  112. 2,
  113. ],
  114. "update": Array [],
  115. }
  116. `);
  117. });
  118. });
  119. });