@@ -470,4 +470,70 @@ describe('Working with dimensions', () => {
470
470
expect . not . objectContaining ( { Dimensions : [ [ 'test' ] ] } )
471
471
) ;
472
472
} ) ;
473
+
474
+ it . each ( [
475
+ { value : undefined , name : 'undefined' } ,
476
+ { value : null , name : 'null' } ,
477
+ {
478
+ value : '' ,
479
+ name : 'empty string' ,
480
+ } ,
481
+ ] ) ( 'skips invalid dimension values in addDimensions ($name)' , ( { value } ) => {
482
+ // Prepare
483
+ const metrics = new Metrics ( {
484
+ singleMetric : true ,
485
+ namespace : DEFAULT_NAMESPACE ,
486
+ } ) ;
487
+
488
+ // Act & Assess
489
+ metrics . addDimensions ( {
490
+ validDimension : 'valid' ,
491
+ invalidDimension : value as string ,
492
+ } ) ;
493
+ metrics . addMetric ( 'test' , MetricUnit . Count , 1 ) ;
494
+ metrics . publishStoredMetrics ( ) ;
495
+
496
+ expect ( console . warn ) . toHaveBeenCalledWith (
497
+ `The dimension invalidDimension doesn't meet the requirements and won't be added. Ensure the dimension name and value are non empty strings`
498
+ ) ;
499
+ expect ( console . log ) . toHaveEmittedEMFWith (
500
+ expect . objectContaining ( { validDimension : 'valid' } )
501
+ ) ;
502
+ expect ( console . log ) . toHaveEmittedEMFWith (
503
+ expect . not . objectContaining ( { invalidDimension : value } )
504
+ ) ;
505
+ } ) ;
506
+
507
+ it ( 'warns when addDimensions overwrites existing dimensions' , ( ) => {
508
+ // Prepare
509
+ const metrics = new Metrics ( {
510
+ namespace : DEFAULT_NAMESPACE ,
511
+ defaultDimensions : { environment : 'prod' } ,
512
+ } ) ;
513
+
514
+ // Act
515
+ metrics . addDimension ( 'region' , 'us-east-1' ) ;
516
+ metrics . addDimensions ( {
517
+ environment : 'staging' , // overwrites default dimension
518
+ region : 'us-west-2' , // overwrites regular dimension
519
+ newDim : 'value' ,
520
+ } ) ;
521
+ metrics . addMetric ( 'test' , MetricUnit . Count , 1 ) ;
522
+ metrics . publishStoredMetrics ( ) ;
523
+
524
+ // Assess
525
+ expect ( console . warn ) . toHaveBeenCalledWith (
526
+ 'Dimension "environment" has already been added. The previous value will be overwritten.'
527
+ ) ;
528
+ expect ( console . warn ) . toHaveBeenCalledWith (
529
+ 'Dimension "region" has already been added. The previous value will be overwritten.'
530
+ ) ;
531
+ expect ( console . log ) . toHaveEmittedEMFWith (
532
+ expect . objectContaining ( {
533
+ environment : 'staging' ,
534
+ region : 'us-west-2' ,
535
+ newDim : 'value' ,
536
+ } )
537
+ ) ;
538
+ } ) ;
473
539
} ) ;
0 commit comments