From 49dfb9a5d3d29483693c7ecfa26070a6e7bb68a1 Mon Sep 17 00:00:00 2001 From: Sparkle <1284531+baurine@users.noreply.github.com> Date: Thu, 28 Nov 2019 00:03:22 +0800 Subject: [PATCH] Units: remove unreachable code (#20684) * Refactor: Remove the code which can't reach * Test: Add more unit tests for toFixed and scaledUnits methods - To make them easier to understand --- .../src/valueFormats/valueFormats.test.ts | 41 +++++++++++++++++++ .../src/valueFormats/valueFormats.ts | 5 +-- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/packages/grafana-data/src/valueFormats/valueFormats.test.ts b/packages/grafana-data/src/valueFormats/valueFormats.test.ts index 3e1f836a378..0853bedef57 100644 --- a/packages/grafana-data/src/valueFormats/valueFormats.test.ts +++ b/packages/grafana-data/src/valueFormats/valueFormats.test.ts @@ -1,6 +1,47 @@ import { toFixed, getValueFormat, scaledUnits } from './valueFormats'; describe('valueFormats', () => { + describe('normal cases', () => { + it('toFixed should handle number correctly if decimal is null', () => { + expect(toFixed(100)).toBe('100'); + + expect(toFixed(100.4)).toBe('100'); + expect(toFixed(100.5)).toBe('101'); + }); + + it('toFixed should handle number correctly if decimal is not null', () => { + expect(toFixed(100, 1)).toBe('100.0'); + + expect(toFixed(100.37, 1)).toBe('100.4'); + expect(toFixed(100.63, 1)).toBe('100.6'); + + expect(toFixed(100.4, 2)).toBe('100.40'); + expect(toFixed(100.5, 2)).toBe('100.50'); + }); + + it('scaledUnit should handle number correctly if scaledDecimals is not null', () => { + const units = ['', 'K', 'M', 'B', 'T']; + const scaler = scaledUnits(1000, units); + + expect(scaler(98765, 0, 0)).toBe('98.765K'); + expect(scaler(98765, 0, -1)).toBe('98.77K'); + + expect(scaler(9876543, 0, 0)).toBe('9.876543M'); + expect(scaler(9876543, 0, -1)).toBe('9.87654M'); + }); + + it('scaledUnit should handle number correctly if scaledDecimals is null', () => { + const units = ['', 'K', 'M', 'B', 'T']; + const scaler = scaledUnits(1000, units); + + expect(scaler(98765, 1, null)).toBe('98.8K'); + expect(scaler(98765, 2, null)).toBe('98.77K'); + + expect(scaler(9876543, 2, null)).toBe('9.88M'); + expect(scaler(9876543, 3, null)).toBe('9.877M'); + }); + }); + describe('format edge cases', () => { const negInf = Number.NEGATIVE_INFINITY.toLocaleString(); const posInf = Number.POSITIVE_INFINITY.toLocaleString(); diff --git a/packages/grafana-data/src/valueFormats/valueFormats.ts b/packages/grafana-data/src/valueFormats/valueFormats.ts index 0e99b6021c1..dcfd499a715 100644 --- a/packages/grafana-data/src/valueFormats/valueFormats.ts +++ b/packages/grafana-data/src/valueFormats/valueFormats.ts @@ -66,11 +66,8 @@ export function toFixedScaled( ) { if (scaledDecimals === null || scaledDecimals === undefined) { return toFixed(value, decimals) + ext; - } else { - return toFixed(value, scaledDecimals + additionalDecimals) + ext; } - - return toFixed(value, decimals) + ext; + return toFixed(value, scaledDecimals + additionalDecimals) + ext; } export function toFixedUnit(unit: string): ValueFormatter {