PanelModel.test.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. describe('when changing panel type', () => {
  89. const newPanelPluginDefaults = {
  90. showThresholdLabels: false,
  91. };
  92. beforeEach(() => {
  93. const newPlugin = getPanelPlugin({ id: 'graph' });
  94. newPlugin.setDefaults(newPanelPluginDefaults);
  95. model.changePlugin(newPlugin);
  96. model.alert = { id: 2 };
  97. });
  98. it('should apply next panel option defaults', () => {
  99. expect(model.getOptions().showThresholdLabels).toBeFalsy();
  100. expect(model.getOptions().showThresholds).toBeUndefined();
  101. });
  102. it('should remove table properties but keep core props', () => {
  103. expect(model.showColumns).toBe(undefined);
  104. });
  105. it('should restore table properties when changing back', () => {
  106. model.changePlugin(getPanelPlugin({ id: 'table' }));
  107. expect(model.showColumns).toBe(true);
  108. });
  109. it('should remove alert rule when changing type that does not support it', () => {
  110. model.changePlugin(getPanelPlugin({ id: 'table' }));
  111. expect(model.alert).toBe(undefined);
  112. });
  113. it('panelQueryRunner should be cleared', () => {
  114. const panelQueryRunner = (model as any).queryRunner;
  115. expect(panelQueryRunner).toBeFalsy();
  116. });
  117. });
  118. describe('when changing from angular panel', () => {
  119. let tearDownPublished = false;
  120. beforeEach(() => {
  121. model.events.on('panel-teardown', () => {
  122. tearDownPublished = true;
  123. });
  124. model.changePlugin(getPanelPlugin({ id: 'graph' }));
  125. });
  126. it('should teardown / destroy panel so angular panels event subscriptions are removed', () => {
  127. expect(tearDownPublished).toBe(true);
  128. expect(model.events.getEventCount()).toBe(0);
  129. });
  130. });
  131. describe('when changing to react panel from angular panel', () => {
  132. let panelQueryRunner: any;
  133. const onPanelTypeChanged = jest.fn();
  134. const reactPlugin = getPanelPlugin({ id: 'react' }).setPanelChangeHandler(onPanelTypeChanged as any);
  135. beforeEach(() => {
  136. model.changePlugin(reactPlugin);
  137. panelQueryRunner = model.getQueryRunner();
  138. });
  139. it('should call react onPanelTypeChanged', () => {
  140. expect(onPanelTypeChanged.mock.calls.length).toBe(1);
  141. expect(onPanelTypeChanged.mock.calls[0][1]).toBe('table');
  142. expect(onPanelTypeChanged.mock.calls[0][2].fieldOptions).toBeDefined();
  143. });
  144. it('getQueryRunner() should return same instance after changing to another react panel', () => {
  145. model.changePlugin(getPanelPlugin({ id: 'react2' }));
  146. const sameQueryRunner = model.getQueryRunner();
  147. expect(panelQueryRunner).toBe(sameQueryRunner);
  148. });
  149. });
  150. });
  151. });