22 * Tests for formatters utility
33 */
44
5- import { formatNumber , formatDate , type DateFormat } from '@/lib/formatters' ;
5+ import { formatNumber , formatDate , formatKPIValue , type DateFormat } from '@/lib/formatters' ;
66
77describe ( 'formatNumber' , ( ) => {
88 it . each ( [
@@ -13,8 +13,7 @@ describe('formatNumber', () => {
1313 [ 1000000 , 'international' , '1,000,000' ] ,
1414 [ 1000000 , 'indian' , '10,00,000' ] ,
1515 [ 1000000 , 'european' , '1.000.000' ] ,
16- [ 85.5 , 'percentage' , '85.5%' ] ,
17- [ 1000 , 'currency' , '$1,000' ] ,
16+ [ 0.855 , 'percentage' , '85.5%' ] ,
1817 [ - 1234567 , 'international' , '-1,234,567' ] ,
1918 [ 0 , 'international' , '0' ] ,
2019 [ 0 , 'indian' , '0' ] ,
@@ -27,7 +26,7 @@ describe('formatNumber', () => {
2726 [ 1234567 , { format : 'international' , decimalPlaces : 2 } , '1,234,567.00' ] ,
2827 [ 1234567 , { format : 'indian' , decimalPlaces : 2 } , '12,34,567.00' ] ,
2928 [ 1234567 , { format : 'european' , decimalPlaces : 2 } , '1.234.567,00' ] ,
30- [ 85.567 , { format : 'percentage' , decimalPlaces : 1 } , '85.6%' ] ,
29+ [ 0.85567 , { format : 'percentage' , decimalPlaces : 1 } , '85.6%' ] ,
3130 [ 0.005 , { format : 'default' , decimalPlaces : 2 } , '0.01' ] ,
3231 [ 0 , { format : 'international' , decimalPlaces : 2 } , '0.00' ] ,
3332 ] ) ( 'formatNumber(%s, %j) => %s (with decimal places)' , ( value , options , expected ) => {
@@ -71,6 +70,42 @@ describe('formatNumber', () => {
7170 } ) ;
7271} ) ;
7372
73+ describe ( 'formatKPIValue' , ( ) => {
74+ it ( 'returns "No data" for null/undefined/NaN — no prefix/suffix wrap' , ( ) => {
75+ expect ( formatKPIValue ( null , { numberPrefix : '₹' , numberSuffix : '%' } ) ) . toBe ( 'No data' ) ;
76+ expect ( formatKPIValue ( undefined , { numberFormat : 'indian' } ) ) . toBe ( 'No data' ) ;
77+ expect ( formatKPIValue ( NaN , { numberPrefix : '$' } ) ) . toBe ( 'No data' ) ;
78+ } ) ;
79+
80+ it ( 'shows the raw number as-is when no customizations are set' , ( ) => {
81+ // No grouping, no compression — pure String(value)
82+ expect ( formatKPIValue ( 1_234_567 ) ) . toBe ( '1234567' ) ;
83+ expect ( formatKPIValue ( 85.5 ) ) . toBe ( '85.5' ) ;
84+ } ) ;
85+
86+ it ( 'shows raw number when customizations object has no numberFormat' , ( ) => {
87+ // Edge: prefix/suffix without format → still no formatting applied
88+ expect ( formatKPIValue ( 1500 , { numberPrefix : '₹' } ) ) . toBe ( '1500' ) ;
89+ } ) ;
90+
91+ it . each ( [
92+ [ 123456 , { numberFormat : 'indian' , decimalPlaces : 0 , numberPrefix : '₹' } , '₹1,23,456' ] ,
93+ [ 0.85 , { numberFormat : 'percentage' , decimalPlaces : 0 } , '85%' ] ,
94+ [
95+ 1500 ,
96+ { numberFormat : 'default' , decimalPlaces : 0 , numberPrefix : '' , numberSuffix : ' people' } ,
97+ '1500 people' ,
98+ ] ,
99+ [
100+ 2_500_000 ,
101+ { numberFormat : 'adaptive_international' , decimalPlaces : 2 , numberPrefix : '$' } ,
102+ '$2.50M' ,
103+ ] ,
104+ ] ) ( 'formatKPIValue(%s, %j) => %s' , ( value , customizations , expected ) => {
105+ expect ( formatKPIValue ( value as number , customizations as any ) ) . toBe ( expected ) ;
106+ } ) ;
107+ } ) ;
108+
74109describe ( 'formatDate' , ( ) => {
75110 // Edge cases
76111 it . each ( [
0 commit comments