PanelModel.test.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { PanelModel } from './PanelModel';
  2. import { getPanelPlugin } from '../../plugins/__mocks__/pluginMocks';
  3. describe('PanelModel', () => {
  4. describe('when creating new panel model', () => {
  5. let model;
  6. let modelJson;
  7. beforeEach(() => {
  8. modelJson = {
  9. type: 'table',
  10. showColumns: true,
  11. targets: [{ refId: 'A' }, { noRefId: true }],
  12. options: {
  13. thresholds: [
  14. {
  15. color: '#F2495C',
  16. index: 1,
  17. value: 50,
  18. },
  19. {
  20. color: '#73BF69',
  21. index: 0,
  22. value: null,
  23. },
  24. ],
  25. },
  26. };
  27. model = new PanelModel(modelJson);
  28. });
  29. it('should apply defaults', () => {
  30. expect(model.gridPos.h).toBe(3);
  31. });
  32. it('should set model props on instance', () => {
  33. expect(model.showColumns).toBe(true);
  34. });
  35. it('should add missing refIds', () => {
  36. expect(model.targets[1].refId).toBe('B');
  37. });
  38. it("shouldn't break panel with non-array targets", () => {
  39. modelJson.targets = {
  40. 0: { refId: 'A' },
  41. foo: { bar: 'baz' },
  42. };
  43. model = new PanelModel(modelJson);
  44. expect(model.targets[0].refId).toBe('A');
  45. });
  46. it('getSaveModel should remove defaults', () => {
  47. const saveModel = model.getSaveModel();
  48. expect(saveModel.gridPos).toBe(undefined);
  49. });
  50. it('getSaveModel should remove nonPersistedProperties', () => {
  51. const saveModel = model.getSaveModel();
  52. expect(saveModel.events).toBe(undefined);
  53. });
  54. it('should restore -Infinity value for base threshold', () => {
  55. expect(model.options.thresholds).toEqual([
  56. {
  57. color: '#F2495C',
  58. index: 1,
  59. value: 50,
  60. },
  61. {
  62. color: '#73BF69',
  63. index: 0,
  64. value: -Infinity,
  65. },
  66. ]);
  67. });
  68. describe('when changing panel type', () => {
  69. beforeEach(() => {
  70. model.changePlugin(getPanelPlugin({ id: 'graph', exports: {} }));
  71. model.alert = { id: 2 };
  72. });
  73. it('should remove table properties but keep core props', () => {
  74. expect(model.showColumns).toBe(undefined);
  75. });
  76. it('should restore table properties when changing back', () => {
  77. model.changePlugin(getPanelPlugin({ id: 'table', exports: {} }));
  78. expect(model.showColumns).toBe(true);
  79. });
  80. it('should remove alert rule when changing type that does not support it', () => {
  81. model.changePlugin(getPanelPlugin({ id: 'table', exports: {} }));
  82. expect(model.alert).toBe(undefined);
  83. });
  84. });
  85. describe('get panel options', () => {
  86. it('should apply defaults', () => {
  87. model.options = { existingProp: 10 };
  88. const options = model.getOptions({
  89. defaultProp: true,
  90. existingProp: 0,
  91. });
  92. expect(options.defaultProp).toBe(true);
  93. expect(options.existingProp).toBe(10);
  94. expect(model.options).toBe(options);
  95. });
  96. });
  97. });
  98. });