@@ -115,33 +115,26 @@ describe('Working with dimensions', () => {
115
115
) ;
116
116
} ) ;
117
117
118
- it ( 'adds multiple dimension sets to the metric ' , ( ) => {
118
+ it ( 'adds empty dimension set when no dimensions are provided ' , ( ) => {
119
119
// Prepare
120
120
const metrics = new Metrics ( {
121
121
singleMetric : true ,
122
122
} ) ;
123
123
124
124
// Act
125
- metrics . addDimension ( 'environment' , 'test' ) ;
126
- metrics . addDimensions ( { dimension1 : '1' , dimension2 : '2' } ) ;
125
+ metrics . addDimensions ( { } ) ;
127
126
metrics . addMetric ( 'test' , MetricUnit . Count , 1 ) ;
128
127
129
128
// Assess
130
129
expect ( console . log ) . toHaveEmittedEMFWith (
131
130
expect . objectContaining ( {
132
131
service : 'hello-world' ,
133
- environment : 'test' ,
134
- dimension1 : '1' ,
135
- dimension2 : '2' ,
136
132
} )
137
133
) ;
138
- // With the new implementation , we expect two dimension sets
134
+ // With empty dimensions , we should only have the default dimension set
139
135
expect ( console . log ) . toHaveEmittedMetricWith (
140
136
expect . objectContaining ( {
141
- Dimensions : expect . arrayContaining ( [
142
- [ 'service' , 'environment' , 'dimension1' , 'dimension2' ] ,
143
- [ 'service' , 'dimension1' , 'dimension2' ] ,
144
- ] ) ,
137
+ Dimensions : [ [ 'service' ] ] ,
145
138
} )
146
139
) ;
147
140
} ) ;
@@ -424,3 +417,80 @@ describe('Working with dimensions', () => {
424
417
) ;
425
418
} ) ;
426
419
} ) ;
420
+ it ( 'adds multiple dimension sets to the metric' , ( ) => {
421
+ // Prepare
422
+ const metrics = new Metrics ( {
423
+ singleMetric : true ,
424
+ } ) ;
425
+
426
+ // Act
427
+ metrics . addDimension ( 'environment' , 'test' ) ;
428
+ metrics . addDimensions ( { dimension1 : '1' , dimension2 : '2' } ) ;
429
+ metrics . addMetric ( 'test' , MetricUnit . Count , 1 ) ;
430
+
431
+ // Assess
432
+ expect ( console . log ) . toHaveEmittedEMFWith (
433
+ expect . objectContaining ( {
434
+ service : 'hello-world' ,
435
+ environment : 'test' ,
436
+ dimension1 : '1' ,
437
+ dimension2 : '2' ,
438
+ } )
439
+ ) ;
440
+ // With the new implementation, we expect two dimension sets
441
+ expect ( console . log ) . toHaveEmittedMetricWith (
442
+ expect . objectContaining ( {
443
+ Dimensions : expect . arrayContaining ( [
444
+ [ 'service' , 'environment' , 'dimension1' , 'dimension2' ] ,
445
+ [ 'service' , 'dimension1' , 'dimension2' ] ,
446
+ ] ) ,
447
+ } )
448
+ ) ;
449
+ } ) ;
450
+ it ( 'skips adding dimension set when all values are invalid' , ( ) => {
451
+ // Prepare
452
+ const metrics = new Metrics ( {
453
+ singleMetric : true ,
454
+ } ) ;
455
+
456
+ // Act
457
+ metrics . addDimensions ( {
458
+ dimension1 : '' ,
459
+ dimension2 : null as unknown as string ,
460
+ } ) ;
461
+ metrics . addMetric ( 'test' , MetricUnit . Count , 1 ) ;
462
+
463
+ // Assess
464
+ expect ( console . log ) . toHaveEmittedEMFWith (
465
+ expect . objectContaining ( {
466
+ service : 'hello-world' ,
467
+ } )
468
+ ) ;
469
+ // With all invalid dimensions, we should only have the default dimension set
470
+ expect ( console . log ) . toHaveEmittedMetricWith (
471
+ expect . objectContaining ( {
472
+ Dimensions : [ [ 'service' ] ] ,
473
+ } )
474
+ ) ;
475
+ } ) ;
476
+ it ( 'throws when adding dimensions would exceed the maximum dimension count' , ( ) => {
477
+ // Prepare
478
+ const metrics = new Metrics ( {
479
+ singleMetric : true ,
480
+ } ) ;
481
+
482
+ // Mock getCurrentDimensionsCount to return a value close to the limit
483
+ const getCurrentDimensionsCountSpy = vi . spyOn (
484
+ metrics ,
485
+ 'getCurrentDimensionsCount'
486
+ ) ;
487
+ getCurrentDimensionsCountSpy . mockReturnValue ( MAX_DIMENSION_COUNT - 1 ) ;
488
+
489
+ // Act & Assert
490
+ expect ( ( ) => {
491
+ metrics . addDimensions ( { oneMore : 'tooMany' } ) ;
492
+ } ) . toThrow ( RangeError ) ;
493
+
494
+ // Restore the mock
495
+ getCurrentDimensionsCountSpy . mockRestore ( ) ;
496
+ } ) ;
0 commit comments