diff --git a/packages/grafana-ui/src/utils/displayValue.test.ts b/packages/grafana-ui/src/utils/displayValue.test.ts index 8e7a4a421a2..c52aee6f87b 100644 --- a/packages/grafana-ui/src/utils/displayValue.test.ts +++ b/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'; function assertSame(input: any, processors: DisplayProcessor[], match: DisplayValue) { @@ -155,3 +161,18 @@ describe('Format value', () => { 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 }); + }); +});