PanelModel.test.ts 4.8 KB

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