Jelajahi Sumber

adding test

Peter Holmberg 6 tahun lalu
induk
melakukan
6e05b9f41c
1 mengubah file dengan 22 tambahan dan 1 penghapusan
  1. 22 1
      packages/grafana-ui/src/utils/displayValue.test.ts

+ 22 - 1
packages/grafana-ui/src/utils/displayValue.test.ts

@@ -1,4 +1,10 @@
-import { getDisplayProcessor, getColorFromThreshold, DisplayProcessor, DisplayValue } from './displayValue';
+import {
+  getDisplayProcessor,
+  getColorFromThreshold,
+  DisplayProcessor,
+  DisplayValue,
+  getDecimalsForValue,
+} from './displayValue';
 import { MappingType, ValueMapping } from '../types';
 import { MappingType, ValueMapping } from '../types';
 
 
 function assertSame(input: any, processors: DisplayProcessor[], match: DisplayValue) {
 function assertSame(input: any, processors: DisplayProcessor[], match: DisplayValue) {
@@ -155,3 +161,18 @@ describe('Format value', () => {
     expect(instance(value).text).toEqual('1-20');
     expect(instance(value).text).toEqual('1-20');
   });
   });
 });
 });
+
+describe('getDecimalsForValue()', () => {
+  it('should calculate reasonable decimals precision for given value', () => {
+    expect(getDecimalsForValue(1.01)).toEqual({ decimals: 1, scaledDecimals: 4 });
+    expect(getDecimalsForValue(9.01)).toEqual({ decimals: 0, scaledDecimals: 2 });
+    expect(getDecimalsForValue(1.1)).toEqual({ decimals: 1, scaledDecimals: 4 });
+    expect(getDecimalsForValue(2)).toEqual({ decimals: 0, scaledDecimals: 2 });
+    expect(getDecimalsForValue(20)).toEqual({ decimals: 0, scaledDecimals: 1 });
+    expect(getDecimalsForValue(200)).toEqual({ decimals: 0, scaledDecimals: 0 });
+    expect(getDecimalsForValue(2000)).toEqual({ decimals: 0, scaledDecimals: 0 });
+    expect(getDecimalsForValue(20000)).toEqual({ decimals: 0, scaledDecimals: -2 });
+    expect(getDecimalsForValue(200000)).toEqual({ decimals: 0, scaledDecimals: -3 });
+    expect(getDecimalsForValue(200000000)).toEqual({ decimals: 0, scaledDecimals: -6 });
+  });
+});