PanelModel.test.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. });
  95. describe('when changing from angular panel', () => {
  96. let tearDownPublished = false;
  97. beforeEach(() => {
  98. model.events.on('panel-teardown', () => {
  99. tearDownPublished = true;
  100. });
  101. model.changePlugin(getPanelPlugin({ id: 'graph' }));
  102. });
  103. it('should teardown / destroy panel so angular panels event subscriptions are removed', () => {
  104. expect(tearDownPublished).toBe(true);
  105. expect(model.events.getEventCount()).toBe(0);
  106. });
  107. });
  108. describe('when changing to react panel', () => {
  109. const onPanelTypeChanged = jest.fn();
  110. const reactPlugin = getPanelPlugin({ id: 'react' }).setPanelChangeHandler(onPanelTypeChanged as any);
  111. beforeEach(() => {
  112. model.changePlugin(reactPlugin);
  113. });
  114. it('should call react onPanelTypeChanged', () => {
  115. expect(onPanelTypeChanged.mock.calls.length).toBe(1);
  116. expect(onPanelTypeChanged.mock.calls[0][1]).toBe('table');
  117. expect(onPanelTypeChanged.mock.calls[0][2].thresholds).toBeDefined();
  118. });
  119. });
  120. describe('get panel options', () => {
  121. it('should apply defaults', () => {
  122. model.options = { existingProp: 10 };
  123. const options = model.getOptions({
  124. defaultProp: true,
  125. existingProp: 0,
  126. });
  127. expect(options.defaultProp).toBe(true);
  128. expect(options.existingProp).toBe(10);
  129. expect(model.options).toBe(options);
  130. });
  131. });
  132. });
  133. });