GaugeMigrations.test.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import { PanelModel } from '@grafana/data';
  2. import { gaugePanelMigrationHandler, gaugePanelChangedHandler } from './GaugeMigrations';
  3. describe('Gauge Panel Migrations', () => {
  4. it('from 6.1.1', () => {
  5. const panel = {
  6. datasource: '-- Grafana --',
  7. gridPos: {
  8. h: 9,
  9. w: 12,
  10. x: 0,
  11. y: 0,
  12. },
  13. id: 2,
  14. options: {
  15. maxValue: '50',
  16. minValue: '-50',
  17. orientation: 'auto',
  18. showThresholdLabels: true,
  19. showThresholdMarkers: true,
  20. thresholds: [
  21. {
  22. color: 'green',
  23. index: 0,
  24. value: -Infinity,
  25. },
  26. {
  27. color: '#EAB839',
  28. index: 1,
  29. value: -25,
  30. },
  31. {
  32. color: '#6ED0E0',
  33. index: 2,
  34. value: 0,
  35. },
  36. {
  37. color: 'red',
  38. index: 3,
  39. value: 25,
  40. },
  41. ],
  42. valueMappings: [
  43. {
  44. id: 1,
  45. operator: '',
  46. value: '',
  47. text: 'BIG',
  48. type: 2,
  49. from: '50',
  50. to: '1000',
  51. },
  52. ],
  53. valueOptions: {
  54. decimals: 3,
  55. prefix: 'XX',
  56. stat: 'last',
  57. suffix: 'YY',
  58. unit: 'accMS2',
  59. },
  60. },
  61. pluginVersion: '6.1.6',
  62. targets: [
  63. {
  64. refId: 'A',
  65. },
  66. {
  67. refId: 'B',
  68. },
  69. {
  70. refId: 'C',
  71. },
  72. ],
  73. timeFrom: null,
  74. timeShift: null,
  75. title: 'Panel Title',
  76. type: 'gauge',
  77. } as Omit<PanelModel, 'fieldConfig'>;
  78. const result = gaugePanelMigrationHandler(panel as PanelModel);
  79. expect(result).toMatchSnapshot();
  80. // Ignored due to the API change
  81. //@ts-ignore
  82. expect(result.reduceOptions.defaults).toBeUndefined();
  83. // Ignored due to the API change
  84. //@ts-ignore
  85. expect(result.reduceOptions.overrides).toBeUndefined();
  86. expect((panel as PanelModel).fieldConfig).toMatchInlineSnapshot(`
  87. Object {
  88. "defaults": Object {
  89. "color": Object {
  90. "mode": "thresholds",
  91. },
  92. "decimals": 3,
  93. "mappings": Array [
  94. Object {
  95. "from": "50",
  96. "id": 1,
  97. "operator": "",
  98. "text": "BIG",
  99. "to": "1000",
  100. "type": 2,
  101. "value": "",
  102. },
  103. ],
  104. "max": "50",
  105. "min": "-50",
  106. "thresholds": Object {
  107. "mode": "absolute",
  108. "steps": Array [
  109. Object {
  110. "color": "green",
  111. "index": 0,
  112. "value": -Infinity,
  113. },
  114. Object {
  115. "color": "#EAB839",
  116. "index": 1,
  117. "value": -25,
  118. },
  119. Object {
  120. "color": "#6ED0E0",
  121. "index": 2,
  122. "value": 0,
  123. },
  124. Object {
  125. "color": "red",
  126. "index": 3,
  127. "value": 25,
  128. },
  129. ],
  130. },
  131. "unit": "accMS2",
  132. },
  133. "overrides": Array [],
  134. }
  135. `);
  136. });
  137. it('change from angular singlestat to gauge', () => {
  138. const old: any = {
  139. angular: {
  140. format: 'ms',
  141. decimals: 7,
  142. gauge: {
  143. maxValue: 150,
  144. minValue: -10,
  145. show: true,
  146. thresholdLabels: true,
  147. thresholdMarkers: true,
  148. },
  149. },
  150. };
  151. const panel = {} as PanelModel;
  152. const newOptions = gaugePanelChangedHandler(panel, 'singlestat', old);
  153. expect(panel.fieldConfig.defaults.unit).toBe('ms');
  154. expect(panel.fieldConfig.defaults.min).toBe(-10);
  155. expect(panel.fieldConfig.defaults.max).toBe(150);
  156. expect(panel.fieldConfig.defaults.decimals).toBe(7);
  157. expect(newOptions.showThresholdMarkers).toBe(true);
  158. expect(newOptions.showThresholdLabels).toBe(true);
  159. });
  160. });