PanelModel.test.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. let persistedOptionsMock;
  9. const defaultOptionsMock = {
  10. fieldOptions: {
  11. thresholds: [
  12. {
  13. color: '#F2495C',
  14. index: 1,
  15. value: 50,
  16. },
  17. {
  18. color: '#73BF69',
  19. index: 0,
  20. value: null,
  21. },
  22. ],
  23. },
  24. showThresholds: true,
  25. };
  26. beforeEach(() => {
  27. persistedOptionsMock = {
  28. fieldOptions: {
  29. thresholds: [
  30. {
  31. color: '#F2495C',
  32. index: 1,
  33. value: 50,
  34. },
  35. {
  36. color: '#73BF69',
  37. index: 0,
  38. value: null,
  39. },
  40. ],
  41. },
  42. };
  43. modelJson = {
  44. type: 'table',
  45. showColumns: true,
  46. targets: [{ refId: 'A' }, { noRefId: true }],
  47. options: persistedOptionsMock,
  48. };
  49. model = new PanelModel(modelJson);
  50. const panelPlugin = getPanelPlugin(
  51. {
  52. id: 'table',
  53. },
  54. null, // react
  55. TablePanelCtrl // angular
  56. );
  57. panelPlugin.setDefaults(defaultOptionsMock);
  58. model.pluginLoaded(panelPlugin);
  59. });
  60. it('should apply defaults', () => {
  61. expect(model.gridPos.h).toBe(3);
  62. });
  63. it('should apply option defaults', () => {
  64. expect(model.getOptions().showThresholds).toBeTruthy();
  65. });
  66. it('should set model props on instance', () => {
  67. expect(model.showColumns).toBe(true);
  68. });
  69. it('should add missing refIds', () => {
  70. expect(model.targets[1].refId).toBe('B');
  71. });
  72. it("shouldn't break panel with non-array targets", () => {
  73. modelJson.targets = {
  74. 0: { refId: 'A' },
  75. foo: { bar: 'baz' },
  76. };
  77. model = new PanelModel(modelJson);
  78. expect(model.targets[0].refId).toBe('A');
  79. });
  80. it('getSaveModel should remove defaults', () => {
  81. const saveModel = model.getSaveModel();
  82. expect(saveModel.gridPos).toBe(undefined);
  83. });
  84. it('getSaveModel should remove nonPersistedProperties', () => {
  85. const saveModel = model.getSaveModel();
  86. expect(saveModel.events).toBe(undefined);
  87. });
  88. it('should restore -Infinity value for base threshold', () => {
  89. expect(model.options.fieldOptions.thresholds).toEqual([
  90. {
  91. color: '#F2495C',
  92. index: 1,
  93. value: 50,
  94. },
  95. {
  96. color: '#73BF69',
  97. index: 0,
  98. value: -Infinity,
  99. },
  100. ]);
  101. });
  102. describe('when changing panel type', () => {
  103. const newPanelPluginDefaults = {
  104. showThresholdLabels: false,
  105. };
  106. beforeEach(() => {
  107. const newPlugin = getPanelPlugin({ id: 'graph' });
  108. newPlugin.setDefaults(newPanelPluginDefaults);
  109. model.changePlugin(newPlugin);
  110. model.alert = { id: 2 };
  111. });
  112. it('should apply next panel option defaults', () => {
  113. expect(model.getOptions().showThresholdLabels).toBeFalsy();
  114. expect(model.getOptions().showThresholds).toBeUndefined();
  115. });
  116. it('should remove table properties but keep core props', () => {
  117. expect(model.showColumns).toBe(undefined);
  118. });
  119. it('should restore table properties when changing back', () => {
  120. model.changePlugin(getPanelPlugin({ id: 'table' }));
  121. expect(model.showColumns).toBe(true);
  122. });
  123. it('should remove alert rule when changing type that does not support it', () => {
  124. model.changePlugin(getPanelPlugin({ id: 'table' }));
  125. expect(model.alert).toBe(undefined);
  126. });
  127. it('panelQueryRunner should be cleared', () => {
  128. const panelQueryRunner = (model as any).queryRunner;
  129. expect(panelQueryRunner).toBeFalsy();
  130. });
  131. });
  132. describe('when changing from angular panel', () => {
  133. let tearDownPublished = false;
  134. beforeEach(() => {
  135. model.events.on('panel-teardown', () => {
  136. tearDownPublished = true;
  137. });
  138. model.changePlugin(getPanelPlugin({ id: 'graph' }));
  139. });
  140. it('should teardown / destroy panel so angular panels event subscriptions are removed', () => {
  141. expect(tearDownPublished).toBe(true);
  142. expect(model.events.getEventCount()).toBe(0);
  143. });
  144. });
  145. describe('when changing to react panel from angular panel', () => {
  146. let panelQueryRunner: any;
  147. const onPanelTypeChanged = jest.fn();
  148. const reactPlugin = getPanelPlugin({ id: 'react' }).setPanelChangeHandler(onPanelTypeChanged as any);
  149. beforeEach(() => {
  150. model.changePlugin(reactPlugin);
  151. panelQueryRunner = model.getQueryRunner();
  152. });
  153. it('should call react onPanelTypeChanged', () => {
  154. expect(onPanelTypeChanged.mock.calls.length).toBe(1);
  155. expect(onPanelTypeChanged.mock.calls[0][1]).toBe('table');
  156. expect(onPanelTypeChanged.mock.calls[0][2].fieldOptions.thresholds).toBeDefined();
  157. });
  158. it('getQueryRunner() should return same instance after changing to another react panel', () => {
  159. model.changePlugin(getPanelPlugin({ id: 'react2' }));
  160. const sameQueryRunner = model.getQueryRunner();
  161. expect(panelQueryRunner).toBe(sameQueryRunner);
  162. });
  163. });
  164. });
  165. });