From 6e05b9f41c7f7757514f7d5462f03ccfec711abb Mon Sep 17 00:00:00 2001 From: Peter Holmberg Date: Thu, 21 Mar 2019 14:05:45 +0100 Subject: [PATCH] adding test --- .../grafana-ui/src/utils/displayValue.test.ts | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) 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 }); + }); +});