ThresholdMapper.test.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import { hiddenReducerTypes, ThresholdMapper } from './ThresholdMapper';
  2. import alertDef from './alertDef';
  3. const visibleReducerTypes = alertDef.reducerTypes
  4. .filter(({ value }) => hiddenReducerTypes.indexOf(value) === -1)
  5. .map(({ value }) => value);
  6. describe('ThresholdMapper', () => {
  7. describe('with greater than evaluator', () => {
  8. it('can map query conditions to thresholds', () => {
  9. const panel: any = {
  10. type: 'graph',
  11. options: { alertThresholds: true },
  12. alert: {
  13. conditions: [
  14. {
  15. type: 'query',
  16. evaluator: { type: 'gt', params: [100] },
  17. },
  18. ],
  19. },
  20. };
  21. const updated = ThresholdMapper.alertToGraphThresholds(panel);
  22. expect(updated).toBe(true);
  23. expect(panel.thresholds[0].op).toBe('gt');
  24. expect(panel.thresholds[0].value).toBe(100);
  25. });
  26. });
  27. describe('with outside range evaluator', () => {
  28. it('can map query conditions to thresholds', () => {
  29. const panel: any = {
  30. type: 'graph',
  31. options: { alertThresholds: true },
  32. alert: {
  33. conditions: [
  34. {
  35. type: 'query',
  36. evaluator: { type: 'outside_range', params: [100, 200] },
  37. },
  38. ],
  39. },
  40. };
  41. const updated = ThresholdMapper.alertToGraphThresholds(panel);
  42. expect(updated).toBe(true);
  43. expect(panel.thresholds[0].op).toBe('lt');
  44. expect(panel.thresholds[0].value).toBe(100);
  45. expect(panel.thresholds[1].op).toBe('gt');
  46. expect(panel.thresholds[1].value).toBe(200);
  47. });
  48. });
  49. describe('with inside range evaluator', () => {
  50. it('can map query conditions to thresholds', () => {
  51. const panel: any = {
  52. type: 'graph',
  53. options: { alertThresholds: true },
  54. alert: {
  55. conditions: [
  56. {
  57. type: 'query',
  58. evaluator: { type: 'within_range', params: [100, 200] },
  59. },
  60. ],
  61. },
  62. };
  63. const updated = ThresholdMapper.alertToGraphThresholds(panel);
  64. expect(updated).toBe(true);
  65. expect(panel.thresholds[0].op).toBe('gt');
  66. expect(panel.thresholds[0].value).toBe(100);
  67. expect(panel.thresholds[1].op).toBe('lt');
  68. expect(panel.thresholds[1].value).toBe(200);
  69. });
  70. });
  71. visibleReducerTypes.forEach((type) => {
  72. describe(`with {${type}} reducer`, () => {
  73. it('visible should be true', () => {
  74. const panel = getPanel({ reducerType: type });
  75. const updated = ThresholdMapper.alertToGraphThresholds(panel);
  76. expect(updated).toBe(true);
  77. expect(panel.thresholds[0]).toEqual({
  78. value: 100,
  79. op: 'gt',
  80. fill: true,
  81. line: true,
  82. colorMode: 'critical',
  83. visible: true,
  84. });
  85. });
  86. });
  87. });
  88. hiddenReducerTypes.forEach((type) => {
  89. describe(`with {${type}} reducer`, () => {
  90. it('visible should be false', () => {
  91. const panel = getPanel({ reducerType: type });
  92. const updated = ThresholdMapper.alertToGraphThresholds(panel);
  93. expect(updated).toBe(true);
  94. expect(panel.thresholds[0]).toEqual({
  95. value: 100,
  96. op: 'gt',
  97. fill: true,
  98. line: true,
  99. colorMode: 'critical',
  100. visible: false,
  101. });
  102. });
  103. });
  104. });
  105. });
  106. function getPanel({ reducerType }: { reducerType?: string } = {}) {
  107. const panel: any = {
  108. type: 'graph',
  109. options: { alertThreshold: true },
  110. alert: {
  111. conditions: [
  112. {
  113. type: 'query',
  114. evaluator: { type: 'gt', params: [100] },
  115. reducer: { type: reducerType },
  116. },
  117. ],
  118. },
  119. };
  120. return panel;
  121. }