From 9160f608b4b1f60e1c8b3495263eff40ca7be5ca Mon Sep 17 00:00:00 2001 From: Jev Forsberg <46619047+baldm0mma@users.noreply.github.com> Date: Wed, 1 Mar 2023 13:54:59 -0700 Subject: [PATCH] TextFormatting/chore: update and add test coverage to the value formatting (#63975) baldm0mma/chore/updateFormattingTests/ update and add tests to formatting module --- .../valueFormats/arithmeticFormatters.test.ts | 117 +++++++++++++----- .../src/valueFormats/arithmeticFormatters.ts | 10 +- 2 files changed, 91 insertions(+), 36 deletions(-) diff --git a/packages/grafana-data/src/valueFormats/arithmeticFormatters.test.ts b/packages/grafana-data/src/valueFormats/arithmeticFormatters.test.ts index af23b4957b8..9b5de04116e 100644 --- a/packages/grafana-data/src/valueFormats/arithmeticFormatters.test.ts +++ b/packages/grafana-data/src/valueFormats/arithmeticFormatters.test.ts @@ -1,41 +1,96 @@ -import { toHex, toHex0x } from './arithmeticFormatters'; +import { sci, toHex, toHex0x, toPercent, toPercentUnit } from './arithmeticFormatters'; import { formattedValueToString } from './valueFormats'; -describe('hex', () => { - it('positive integer', () => { - const str = toHex(100, 0); - expect(formattedValueToString(str)).toBe('64'); - }); - it('negative integer', () => { - const str = toHex(-100, 0); - expect(formattedValueToString(str)).toBe('-64'); - }); - it('positive float', () => { - const str = toHex(50.52, 1); - expect(formattedValueToString(str)).toBe('32.8'); - }); - it('negative float', () => { - const str = toHex(-50.333, 2); - expect(formattedValueToString(str)).toBe('-32.547AE147AE14'); +describe('scientific formatting', () => { + describe('sci', () => { + it('follows the sad path as expected', () => { + const str = sci(null, 0); + expect(formattedValueToString(str)).toBe(''); + }); + it('renders the correct postive scientific notation as expected', () => { + const str = sci(1000, 0); + expect(formattedValueToString(str)).toBe('1e+3'); + }); + it('renders the correct nagative scientific notation as expected', () => { + const str = sci(-1000, 0); + expect(formattedValueToString(str)).toBe('-1e+3'); + }); + it('renders the correct decimal scientific notation as expected', () => { + const str = sci(1000, 2); + expect(formattedValueToString(str)).toBe('1.00e+3'); + }); }); }); -describe('hex 0x', () => { - it('positive integer', () => { - const str = toHex0x(7999, 0); - expect(formattedValueToString(str)).toBe('0x1F3F'); - }); - it('negative integer', () => { - const str = toHex0x(-584, 0); - expect(formattedValueToString(str)).toBe('-0x248'); +describe('hexadecimal formatting', () => { + describe('toHex', () => { + it('follows the sad path as expected', () => { + const str = toHex(null, 0); + expect(formattedValueToString(str)).toBe(''); + }); + it('renders a positive integer', () => { + const str = toHex(100, 0); + expect(formattedValueToString(str)).toBe('64'); + }); + it('negative integer', () => { + const str = toHex(-100, 0); + expect(formattedValueToString(str)).toBe('-64'); + }); + it('positive float', () => { + const str = toHex(50.52, 1); + expect(formattedValueToString(str)).toBe('32.8'); + }); + it('negative float', () => { + const str = toHex(-50.333, 2); + expect(formattedValueToString(str)).toBe('-32.547AE147AE14'); + }); }); + describe('toHex0x', () => { + it('follows the sad path as expected', () => { + const str = toHex0x(null, 0); + expect(formattedValueToString(str)).toBe(''); + }); + it('positive integer', () => { + const str = toHex0x(7999, 0); + expect(formattedValueToString(str)).toBe('0x1F3F'); + }); + it('negative integer', () => { + const str = toHex0x(-584, 0); + expect(formattedValueToString(str)).toBe('-0x248'); + }); - it('positive float', () => { - const str = toHex0x(74.443, 3); - expect(formattedValueToString(str)).toBe('0x4A.716872B020C4'); + it('positive float', () => { + const str = toHex0x(74.443, 3); + expect(formattedValueToString(str)).toBe('0x4A.716872B020C4'); + }); + it('negative float', () => { + const str = toHex0x(-65.458, 1); + expect(formattedValueToString(str)).toBe('-0x41.8'); + }); }); - it('negative float', () => { - const str = toHex0x(-65.458, 1); - expect(formattedValueToString(str)).toBe('-0x41.8'); +}); + +describe('percentage formatting', () => { + const size = 33.33333; + const decimals = 2; + describe('toPercent', () => { + it('follows the sad path as expected', () => { + const str = toPercent(null, decimals); + expect(formattedValueToString(str)).toBe(''); + }); + it('renders a percent as expected', () => { + const str = toPercent(size, 2); + expect(formattedValueToString(str)).toBe('33.33%'); + }); + }); + describe('toPercentUnit', () => { + it('follows the sad path as expected', () => { + const str = toPercentUnit(null, decimals); + expect(formattedValueToString(str)).toBe(''); + }); + it('renders a percent unit as expected', () => { + const str = toPercentUnit(size, 2); + expect(formattedValueToString(str)).toBe('3333.33%'); + }); }); }); diff --git a/packages/grafana-data/src/valueFormats/arithmeticFormatters.ts b/packages/grafana-data/src/valueFormats/arithmeticFormatters.ts index a28e2ee7405..33e6856d140 100644 --- a/packages/grafana-data/src/valueFormats/arithmeticFormatters.ts +++ b/packages/grafana-data/src/valueFormats/arithmeticFormatters.ts @@ -2,21 +2,21 @@ import { DecimalCount } from '../types/displayValue'; import { toFixed, FormattedValue } from './valueFormats'; -export function toPercent(size: number, decimals: DecimalCount): FormattedValue { +export function toPercent(size: number | null, decimals: DecimalCount): FormattedValue { if (size === null) { return { text: '' }; } return { text: toFixed(size, decimals), suffix: '%' }; } -export function toPercentUnit(size: number, decimals: DecimalCount): FormattedValue { +export function toPercentUnit(size: number | null, decimals: DecimalCount): FormattedValue { if (size === null) { return { text: '' }; } return { text: toFixed(100 * size, decimals), suffix: '%' }; } -export function toHex0x(value: number, decimals: DecimalCount): FormattedValue { +export function toHex0x(value: number | null, decimals: DecimalCount): FormattedValue { if (value == null) { return { text: '' }; } @@ -29,7 +29,7 @@ export function toHex0x(value: number, decimals: DecimalCount): FormattedValue { return asHex; } -export function toHex(value: number, decimals: DecimalCount): FormattedValue { +export function toHex(value: number | null, decimals: DecimalCount): FormattedValue { if (value == null) { return { text: '' }; } @@ -38,7 +38,7 @@ export function toHex(value: number, decimals: DecimalCount): FormattedValue { }; } -export function sci(value: number, decimals: DecimalCount): FormattedValue { +export function sci(value: number | null, decimals: DecimalCount): FormattedValue { if (value == null) { return { text: '' }; }