displayValue.test.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import { getDisplayProcessor, getColorFromThreshold, DisplayProcessor, DisplayValue } from './displayValue';
  2. import { MappingType, ValueMapping } from '../types/panel';
  3. function assertSame(input: any, processors: DisplayProcessor[], match: DisplayValue) {
  4. processors.forEach(processor => {
  5. const value = processor(input);
  6. expect(value.text).toEqual(match.text);
  7. if (match.hasOwnProperty('numeric')) {
  8. expect(value.numeric).toEqual(match.numeric);
  9. }
  10. });
  11. }
  12. describe('Process simple display values', () => {
  13. // Don't test float values here since the decimal formatting changes
  14. const processors = [
  15. // Without options, this shortcuts to a much easier implementation
  16. getDisplayProcessor(),
  17. // Add a simple option that is not used (uses a different base class)
  18. getDisplayProcessor({ color: '#FFF' }),
  19. // Add a simple option that is not used (uses a different base class)
  20. getDisplayProcessor({ unit: 'locale' }),
  21. ];
  22. it('support null', () => {
  23. assertSame(null, processors, { text: '', numeric: NaN });
  24. });
  25. it('support undefined', () => {
  26. assertSame(undefined, processors, { text: '', numeric: NaN });
  27. });
  28. it('support NaN', () => {
  29. assertSame(NaN, processors, { text: 'NaN', numeric: NaN });
  30. });
  31. it('Integer', () => {
  32. assertSame(3, processors, { text: '3', numeric: 3 });
  33. });
  34. it('Text to number', () => {
  35. assertSame('3', processors, { text: '3', numeric: 3 });
  36. });
  37. it('Simple String', () => {
  38. assertSame('hello', processors, { text: 'hello', numeric: NaN });
  39. });
  40. it('empty array', () => {
  41. assertSame([], processors, { text: '', numeric: NaN });
  42. });
  43. it('array of text', () => {
  44. assertSame(['a', 'b', 'c'], processors, { text: 'a,b,c', numeric: NaN });
  45. });
  46. it('array of numbers', () => {
  47. assertSame([1, 2, 3], processors, { text: '1,2,3', numeric: NaN });
  48. });
  49. it('empty object', () => {
  50. assertSame({}, processors, { text: '[object Object]', numeric: NaN });
  51. });
  52. it('boolean true', () => {
  53. assertSame(true, processors, { text: 'true', numeric: 1 });
  54. });
  55. it('boolean false', () => {
  56. assertSame(false, processors, { text: 'false', numeric: 0 });
  57. });
  58. });
  59. describe('Processor with more configs', () => {
  60. it('support prefix & suffix', () => {
  61. const processor = getDisplayProcessor({
  62. prefix: 'AA_',
  63. suffix: '_ZZ',
  64. });
  65. expect(processor('XXX').text).toEqual('AA_XXX_ZZ');
  66. });
  67. });
  68. describe('Get color from threshold', () => {
  69. it('should get first threshold color when only one threshold', () => {
  70. const thresholds = [{ index: 0, value: -Infinity, color: '#7EB26D' }];
  71. expect(getColorFromThreshold(49, thresholds)).toEqual('#7EB26D');
  72. });
  73. it('should get the threshold color if value is same as a threshold', () => {
  74. const thresholds = [
  75. { index: 2, value: 75, color: '#6ED0E0' },
  76. { index: 1, value: 50, color: '#EAB839' },
  77. { index: 0, value: -Infinity, color: '#7EB26D' },
  78. ];
  79. expect(getColorFromThreshold(50, thresholds)).toEqual('#EAB839');
  80. });
  81. it('should get the nearest threshold color between thresholds', () => {
  82. const thresholds = [
  83. { index: 2, value: 75, color: '#6ED0E0' },
  84. { index: 1, value: 50, color: '#EAB839' },
  85. { index: 0, value: -Infinity, color: '#7EB26D' },
  86. ];
  87. expect(getColorFromThreshold(55, thresholds)).toEqual('#EAB839');
  88. });
  89. });
  90. describe('Format value', () => {
  91. it('should return if value isNaN', () => {
  92. const valueMappings: ValueMapping[] = [];
  93. const value = 'N/A';
  94. const instance = getDisplayProcessor({ mappings: valueMappings });
  95. const result = instance(value);
  96. expect(result.text).toEqual('N/A');
  97. });
  98. it('should return formatted value if there are no value mappings', () => {
  99. const valueMappings: ValueMapping[] = [];
  100. const value = '6';
  101. const instance = getDisplayProcessor({ mappings: valueMappings, decimals: 1 });
  102. const result = instance(value);
  103. expect(result.text).toEqual('6.0');
  104. });
  105. it('should return formatted value if there are no matching value mappings', () => {
  106. const valueMappings: ValueMapping[] = [
  107. { id: 0, operator: '', text: 'elva', type: MappingType.ValueToText, value: '11' },
  108. { id: 1, operator: '', text: '1-9', type: MappingType.RangeToText, from: '1', to: '9' },
  109. ];
  110. const value = '10';
  111. const instance = getDisplayProcessor({ mappings: valueMappings, decimals: 1 });
  112. const result = instance(value);
  113. expect(result.text).toEqual('10.0');
  114. });
  115. it('should return mapped value if there are matching value mappings', () => {
  116. const valueMappings: ValueMapping[] = [
  117. { id: 0, operator: '', text: '1-20', type: MappingType.RangeToText, from: '1', to: '20' },
  118. { id: 1, operator: '', text: 'elva', type: MappingType.ValueToText, value: '11' },
  119. ];
  120. const value = '11';
  121. const instance = getDisplayProcessor({ mappings: valueMappings, decimals: 1 });
  122. expect(instance(value).text).toEqual('1-20');
  123. });
  124. });