PanelModel.test.ts 2.8 KB

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