PanelModel.test.ts 2.6 KB

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