PanelModel.test.ts 2.5 KB

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