PanelModel.test.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. let panelQueryRunner: any;
  80. beforeEach(() => {
  81. panelQueryRunner = model.getQueryRunner();
  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('getQueryRunner() should return same instance after plugin change', () => {
  97. const sameQueryRunner = model.getQueryRunner();
  98. expect(panelQueryRunner).toBe(sameQueryRunner);
  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', () => {
  115. const onPanelTypeChanged = jest.fn();
  116. const reactPlugin = getPanelPlugin({ id: 'react' }).setPanelChangeHandler(onPanelTypeChanged as any);
  117. beforeEach(() => {
  118. model.changePlugin(reactPlugin);
  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. });
  126. describe('get panel options', () => {
  127. it('should apply defaults', () => {
  128. model.options = { existingProp: 10 };
  129. const options = model.getOptions({
  130. defaultProp: true,
  131. existingProp: 0,
  132. });
  133. expect(options.defaultProp).toBe(true);
  134. expect(options.existingProp).toBe(10);
  135. expect(model.options).toBe(options);
  136. });
  137. });
  138. });
  139. });