From fd340b9a7cd1c91716ab4fc5eefd611bd2bd4dc1 Mon Sep 17 00:00:00 2001 From: Claudio Procida Date: Mon, 24 Jan 2022 10:50:22 +0100 Subject: [PATCH] chore: Adds test coverage and fixes for negative value formatters (#122272) * Adds tests for negative value formatters * Adds tests for negative duration formatters --- .../common/utils/formatters/duration.test.ts | 29 ++++++++++++++++++- .../utils/formatters/formatters.test.ts | 24 +++++++++++++++ .../common/utils/formatters/formatters.ts | 2 +- 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/x-pack/plugins/observability/common/utils/formatters/duration.test.ts b/x-pack/plugins/observability/common/utils/formatters/duration.test.ts index eaf073cfbb5b3..69f4792325a35 100644 --- a/x-pack/plugins/observability/common/utils/formatters/duration.test.ts +++ b/x-pack/plugins/observability/common/utils/formatters/duration.test.ts @@ -71,7 +71,7 @@ describe('duration formatters', () => { [1000, '1,000.0 tpm'], [1000000, '1,000,000.0 tpm'], ])( - 'displays the correct label when the number is integer and has zero decimals', + 'displays the correct label when the number is a positive integer and has zero decimals', (value, formattedValue) => { expect(asTransactionRate(value)).toBe(formattedValue); } @@ -89,12 +89,39 @@ describe('duration formatters', () => { expect(asTransactionRate(value)).toBe(formattedValue); } ); + + it.each([ + [-1, '< 0.1 tpm'], + [-10, '< 0.1 tpm'], + [-100, '< 0.1 tpm'], + [-1000, '< 0.1 tpm'], + [-1000000, '< 0.1 tpm'], + ])( + 'displays the correct label when the number is a negative integer and has zero decimals', + (value, formattedValue) => { + expect(asTransactionRate(value)).toBe(formattedValue); + } + ); + + it.each([ + [-1.23, '< 0.1 tpm'], + [-12.34, '< 0.1 tpm'], + [-123.45, '< 0.1 tpm'], + [-1234.56, '< 0.1 tpm'], + [-1234567.89, '< 0.1 tpm'], + ])( + 'displays the correct label when the number is negative and has decimal part', + (value, formattedValue) => { + expect(asTransactionRate(value)).toBe(formattedValue); + } + ); }); describe('asMilliseconds', () => { it('converts to formatted decimal milliseconds', () => { expect(asMillisecondDuration(0)).toEqual('0 ms'); }); + it('formats correctly with undefined values', () => { expect(asMillisecondDuration(undefined)).toEqual('N/A'); }); diff --git a/x-pack/plugins/observability/common/utils/formatters/formatters.test.ts b/x-pack/plugins/observability/common/utils/formatters/formatters.test.ts index c3b65ce7699d0..397eaae04b51a 100644 --- a/x-pack/plugins/observability/common/utils/formatters/formatters.test.ts +++ b/x-pack/plugins/observability/common/utils/formatters/formatters.test.ts @@ -108,5 +108,29 @@ describe('formatters', () => { ])('formats as decimal when number is below 10 ', (value, formattedValue) => { expect(asDecimalOrInteger(value)).toBe(formattedValue); }); + + it.each([ + [-0.123, '-0.1'], + [-1.234, '-1.2'], + [-9.876, '-9.9'], + ])( + 'formats as decimal when number is negative and below 10 in absolute value', + (value, formattedValue) => { + expect(asDecimalOrInteger(value)).toEqual(formattedValue); + } + ); + + it.each([ + [-12.34, '-12'], + [-123.45, '-123'], + [-1234.56, '-1,235'], + [-12345.67, '-12,346'], + [-12345678.9, '-12,345,679'], + ])( + 'formats as integer when number is negative and above or equals 10 in absolute value', + (value, formattedValue) => { + expect(asDecimalOrInteger(value)).toEqual(formattedValue); + } + ); }); }); diff --git a/x-pack/plugins/observability/common/utils/formatters/formatters.ts b/x-pack/plugins/observability/common/utils/formatters/formatters.ts index 9bdccc7e9edfe..05d8d2638ba7b 100644 --- a/x-pack/plugins/observability/common/utils/formatters/formatters.ts +++ b/x-pack/plugins/observability/common/utils/formatters/formatters.ts @@ -51,7 +51,7 @@ export type AsPercent = typeof asPercent; export function asDecimalOrInteger(value: number) { // exact 0 or above 10 should not have decimal - if (value === 0 || value >= 10) { + if (value === 0 || Math.abs(value) >= 10) { return asInteger(value); } return asDecimal(value);