PanelModel.test.ts 4.4 KB

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