PanelModel.test.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import { PanelModel } from './PanelModel';
  2. import { getPanelPlugin } from '../../plugins/__mocks__/pluginMocks';
  3. import { ReactPanelPlugin } from '@grafana/ui/src/types/panel';
  4. describe('PanelModel', () => {
  5. describe('when creating new panel model', () => {
  6. let model;
  7. let modelJson;
  8. beforeEach(() => {
  9. modelJson = {
  10. type: 'table',
  11. showColumns: true,
  12. targets: [{ refId: 'A' }, { noRefId: true }],
  13. options: {
  14. thresholds: [
  15. {
  16. color: '#F2495C',
  17. index: 1,
  18. value: 50,
  19. },
  20. {
  21. color: '#73BF69',
  22. index: 0,
  23. value: null,
  24. },
  25. ],
  26. },
  27. };
  28. model = new PanelModel(modelJson);
  29. });
  30. it('should apply defaults', () => {
  31. expect(model.gridPos.h).toBe(3);
  32. });
  33. it('should set model props on instance', () => {
  34. expect(model.showColumns).toBe(true);
  35. });
  36. it('should add missing refIds', () => {
  37. expect(model.targets[1].refId).toBe('B');
  38. });
  39. it("shouldn't break panel with non-array targets", () => {
  40. modelJson.targets = {
  41. 0: { refId: 'A' },
  42. foo: { bar: 'baz' },
  43. };
  44. model = new PanelModel(modelJson);
  45. expect(model.targets[0].refId).toBe('A');
  46. });
  47. it('getSaveModel should remove defaults', () => {
  48. const saveModel = model.getSaveModel();
  49. expect(saveModel.gridPos).toBe(undefined);
  50. });
  51. it('getSaveModel should remove nonPersistedProperties', () => {
  52. const saveModel = model.getSaveModel();
  53. expect(saveModel.events).toBe(undefined);
  54. });
  55. it('should restore -Infinity value for base threshold', () => {
  56. expect(model.options.thresholds).toEqual([
  57. {
  58. color: '#F2495C',
  59. index: 1,
  60. value: 50,
  61. },
  62. {
  63. color: '#73BF69',
  64. index: 0,
  65. value: -Infinity,
  66. },
  67. ]);
  68. });
  69. describe('when changing panel type', () => {
  70. beforeEach(() => {
  71. model.changePlugin(getPanelPlugin({ id: 'graph', exports: {} }));
  72. model.alert = { id: 2 };
  73. });
  74. it('should remove table properties but keep core props', () => {
  75. expect(model.showColumns).toBe(undefined);
  76. });
  77. it('should restore table properties when changing back', () => {
  78. model.changePlugin(getPanelPlugin({ id: 'table', exports: {} }));
  79. expect(model.showColumns).toBe(true);
  80. });
  81. it('should remove alert rule when changing type that does not support it', () => {
  82. model.changePlugin(getPanelPlugin({ id: 'table', exports: {} }));
  83. expect(model.alert).toBe(undefined);
  84. });
  85. });
  86. describe('when changing from angular panel', () => {
  87. let tearDownPublished = false;
  88. beforeEach(() => {
  89. model.events.on('panel-teardown', () => {
  90. tearDownPublished = true;
  91. });
  92. model.changePlugin(getPanelPlugin({ id: 'graph', exports: {} }));
  93. });
  94. it('should teardown / destroy panel so angular panels event subscriptions are removed', () => {
  95. expect(tearDownPublished).toBe(true);
  96. expect(model.events.getEventCount()).toBe(0);
  97. });
  98. });
  99. describe('when changing to react panel', () => {
  100. const onPanelTypeChanged = jest.fn();
  101. const reactPanel = new ReactPanelPlugin({} as any).setPanelChangeHandler(onPanelTypeChanged as any);
  102. beforeEach(() => {
  103. model.changePlugin(
  104. getPanelPlugin({
  105. id: 'react',
  106. exports: {
  107. reactPanel,
  108. },
  109. })
  110. );
  111. });
  112. it('should call react onPanelTypeChanged', () => {
  113. expect(onPanelTypeChanged.mock.calls.length).toBe(1);
  114. expect(onPanelTypeChanged.mock.calls[0][1]).toBe('table');
  115. expect(onPanelTypeChanged.mock.calls[0][2].thresholds).toBeDefined();
  116. });
  117. });
  118. describe('get panel options', () => {
  119. it('should apply defaults', () => {
  120. model.options = { existingProp: 10 };
  121. const options = model.getOptions({
  122. defaultProp: true,
  123. existingProp: 0,
  124. });
  125. expect(options.defaultProp).toBe(true);
  126. expect(options.existingProp).toBe(10);
  127. expect(model.options).toBe(options);
  128. });
  129. });
  130. });
  131. });