@@ -431,6 +431,45 @@ describe('Class: Metrics', () => {
431
431
`The number of metric dimensions must be lower than ${ MAX_DIMENSION_COUNT } `
432
432
) ;
433
433
} ) ;
434
+
435
+ describe ( 'invalid values should not be added as dimensions' , ( ) => {
436
+ const testCases = [
437
+ { value : undefined as unknown as string , description : 'undefined' } ,
438
+ { value : null as unknown as string , description : 'null' } ,
439
+ { value : '' , description : 'empty string' } ,
440
+ ] ;
441
+
442
+ for ( const { value, description } of testCases ) {
443
+ it ( `it should not add dimension with ${ description } value and log a warning` , ( ) => {
444
+ // Prepare
445
+ const customLogger = {
446
+ warn : jest . fn ( ) ,
447
+ debug : jest . fn ( ) ,
448
+ error : jest . fn ( ) ,
449
+ info : jest . fn ( ) ,
450
+ } ;
451
+ const metrics : Metrics = new Metrics ( {
452
+ namespace : TEST_NAMESPACE ,
453
+ logger : customLogger ,
454
+ } ) ;
455
+ const consoleWarnSpy = jest . spyOn ( customLogger , 'warn' ) ;
456
+ const testDimensionName = 'test-dimension' ;
457
+
458
+ // Act
459
+ metrics . addDimension ( testDimensionName , value ) ;
460
+
461
+ // Assess
462
+ expect ( consoleWarnSpy ) . toHaveBeenCalledWith (
463
+ `The dimension ${ testDimensionName } doesn't meet the requirements and won't be added. Ensure the dimension name and value are non empty strings`
464
+ ) ;
465
+ expect ( metrics ) . toEqual (
466
+ expect . objectContaining ( {
467
+ dimensions : { } ,
468
+ } )
469
+ ) ;
470
+ } ) ;
471
+ }
472
+ } ) ;
434
473
} ) ;
435
474
436
475
describe ( 'Method: addDimensions' , ( ) => {
@@ -520,6 +559,53 @@ describe('Class: Metrics', () => {
520
559
`Unable to add 1 dimensions: the number of metric dimensions must be lower than ${ MAX_DIMENSION_COUNT } `
521
560
) ;
522
561
} ) ;
562
+
563
+ describe ( 'invalid values should not be added as dimensions' , ( ) => {
564
+ const testCases = [
565
+ { value : undefined as unknown as string , description : 'undefined' } ,
566
+ { value : null as unknown as string , description : 'null' } ,
567
+ { value : '' , description : 'empty string' } ,
568
+ ] ;
569
+
570
+ for ( const { value, description } of testCases ) {
571
+ it ( `it should not add dimension with ${ description } value and log a warning` , ( ) => {
572
+ // Prepare
573
+ const customLogger = {
574
+ warn : jest . fn ( ) ,
575
+ debug : jest . fn ( ) ,
576
+ error : jest . fn ( ) ,
577
+ info : jest . fn ( ) ,
578
+ } ;
579
+ const metrics : Metrics = new Metrics ( {
580
+ namespace : TEST_NAMESPACE ,
581
+ logger : customLogger ,
582
+ } ) ;
583
+ const consoleWarnSpy = jest . spyOn ( customLogger , 'warn' ) ;
584
+ const dimensionsToBeAdded : LooseObject = {
585
+ 'test-dimension-1' : 'test-value-1' ,
586
+ 'test-dimension-2' : 'test-value-2' ,
587
+ } ;
588
+ const testDimensionName = 'test-dimension' ;
589
+
590
+ // Act
591
+ metrics . addDimensions ( dimensionsToBeAdded ) ;
592
+ metrics . addDimensions ( { [ testDimensionName ] : value } ) ;
593
+
594
+ // Assess
595
+ expect ( consoleWarnSpy ) . toHaveBeenCalledWith (
596
+ `The dimension ${ testDimensionName } doesn't meet the requirements and won't be added. Ensure the dimension name and value are non empty strings`
597
+ ) ;
598
+ expect ( metrics ) . toEqual (
599
+ expect . objectContaining ( {
600
+ dimensions : {
601
+ 'test-dimension-1' : 'test-value-1' ,
602
+ 'test-dimension-2' : 'test-value-2' ,
603
+ } ,
604
+ } )
605
+ ) ;
606
+ } ) ;
607
+ }
608
+ } ) ;
523
609
} ) ;
524
610
525
611
describe ( 'Method: addMetadata' , ( ) => {
0 commit comments