PanelModel.test.ts 4.4 KB

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