From 176cd00ea3a74c093faa9cda31b0a519f2282118 Mon Sep 17 00:00:00 2001 From: bogdannechyporenko Date: Tue, 15 Nov 2022 14:16:51 +0100 Subject: [PATCH] Added test cases Signed-off-by: bogdannechyporenko --- .../src/utils/formatters.test.ts | 16 ++++++++ .../cost-insights/src/utils/graphs.test.ts | 40 +++++++++++++++++++ plugins/cost-insights/src/utils/graphs.ts | 4 +- 3 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 plugins/cost-insights/src/utils/graphs.test.ts diff --git a/plugins/cost-insights/src/utils/formatters.test.ts b/plugins/cost-insights/src/utils/formatters.test.ts index 87a2b335d9..0bc95c2efe 100644 --- a/plugins/cost-insights/src/utils/formatters.test.ts +++ b/plugins/cost-insights/src/utils/formatters.test.ts @@ -49,6 +49,22 @@ describe('date formatters', () => { '$0.0000023', ]); }); + + it('Correctly formats values in euros to two significant digits', () => { + const values = [ + 0.00000040925, 0.21, 0.0000004, 0.4139877878, 0.00000234566, + ]; + const formattedValues = values.map(val => + lengthyCurrencyFormatter('EUR').format(val), + ); + expect(formattedValues).toEqual([ + '€0.00000041', + '€0.21', + '€0.00000040', + '€0.41', + '€0.0000023', + ]); + }); }); describe.each` diff --git a/plugins/cost-insights/src/utils/graphs.test.ts b/plugins/cost-insights/src/utils/graphs.test.ts new file mode 100644 index 0000000000..47e7cae752 --- /dev/null +++ b/plugins/cost-insights/src/utils/graphs.test.ts @@ -0,0 +1,40 @@ +/* + * Copyright 2020 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { formatGraphValue, tooltipItemOf } from './graphs'; +import { DataKey } from '../types'; + +describe('graphs', () => { + it('formatGraphValue', () => { + expect(formatGraphValue('SEK')(1000, 0)).toEqual('SEK 1,000'); + expect(formatGraphValue('EUR')(1000, 0)).toEqual('€1,000'); + expect(formatGraphValue('USD')(1000, 0)).toEqual('$1,000'); + }); + it('tooltipItemOf', () => { + expect( + tooltipItemOf('EUR', { + value: '1000', + color: 'red', + dataKey: DataKey.Current, + name: 'Kubernetes', + }), + ).toEqual({ + fill: 'red', + label: 'Kubernetes', + value: '€1,000', + }); + }); +}); diff --git a/plugins/cost-insights/src/utils/graphs.ts b/plugins/cost-insights/src/utils/graphs.ts index 2d63967c82..8f82d41054 100644 --- a/plugins/cost-insights/src/utils/graphs.ts +++ b/plugins/cost-insights/src/utils/graphs.ts @@ -45,8 +45,8 @@ export const tooltipItemOf = ( payload: Payload, ) => { const value = - typeof payload.value === 'number' - ? currencyFormatter(baseCurrency).format(payload.value) + payload.value && !isNaN(Number(payload.value)) + ? currencyFormatter(baseCurrency).format(Number(payload.value)) : payload.value; const fill = payload.color as string;