PanelModel.test.ts 4.9 KB

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