1
1
import { MetricsAPI } from '../../impact-metrics/metric-client' ;
2
2
3
3
import test from 'ava' ;
4
+ import Client from '../../client' ;
5
+ import { MetricLabels } from '../../impact-metrics/metric-types' ;
6
+
7
+ const fakeVariantResolver = (
8
+ variantName = 'disabled' ,
9
+ feature_enabled = true ,
10
+ ) : Pick < Client , 'forceGetVariant' > => ( {
11
+ forceGetVariant : ( ) => ( {
12
+ name : variantName ,
13
+ feature_enabled,
14
+ enabled : variantName !== 'disabled' ,
15
+ featureEnabled : feature_enabled ,
16
+ } ) ,
17
+ } ) ;
4
18
5
19
test ( 'should not register a counter with empty name or help' , ( t ) => {
6
20
let counterRegistered = false ;
@@ -12,7 +26,7 @@ test('should not register a counter with empty name or help', (t) => {
12
26
} ;
13
27
14
28
const staticContext = { appName : 'my-app' , environment : 'dev' } ;
15
- const api = new MetricsAPI ( fakeRegistry as any , staticContext ) ;
29
+ const api = new MetricsAPI ( fakeRegistry as any , fakeVariantResolver ( ) , staticContext ) ;
16
30
17
31
api . defineCounter ( 'some_name' , '' ) ;
18
32
t . false ( counterRegistered , 'Counter should not be registered with empty help' ) ;
@@ -31,7 +45,7 @@ test('should register a counter with valid name and help', (t) => {
31
45
} ;
32
46
33
47
const staticContext = { appName : 'my-app' , environment : 'dev' } ;
34
- const api = new MetricsAPI ( fakeRegistry as any , staticContext ) ;
48
+ const api = new MetricsAPI ( fakeRegistry as any , fakeVariantResolver ( ) , staticContext ) ;
35
49
36
50
api . defineCounter ( 'valid_name' , 'Valid help text' ) ;
37
51
t . true ( counterRegistered , 'Counter should be registered with valid name and help' ) ;
@@ -47,7 +61,7 @@ test('should not register a gauge with empty name or help', (t) => {
47
61
} ;
48
62
49
63
const staticContext = { appName : 'my-app' , environment : 'dev' } ;
50
- const api = new MetricsAPI ( fakeRegistry as any , staticContext ) ;
64
+ const api = new MetricsAPI ( fakeRegistry as any , fakeVariantResolver ( ) , staticContext ) ;
51
65
52
66
api . defineGauge ( 'some_name' , '' ) ;
53
67
t . false ( gaugeRegistered , 'Gauge should not be registered with empty help' ) ;
@@ -66,18 +80,20 @@ test('should register a gauge with valid name and help', (t) => {
66
80
} ;
67
81
68
82
const staticContext = { appName : 'my-app' , environment : 'dev' } ;
69
- const api = new MetricsAPI ( fakeRegistry as any , staticContext ) ;
83
+ const api = new MetricsAPI ( fakeRegistry as any , fakeVariantResolver ( ) , staticContext ) ;
70
84
71
85
api . defineGauge ( 'valid_name' , 'Valid help text' ) ;
72
86
t . true ( gaugeRegistered , 'Gauge should be registered with valid name and help' ) ;
73
87
} ) ;
74
88
75
89
test ( 'should increment counter with valid parameters' , ( t ) => {
76
90
let counterIncremented = false ;
91
+ let recordedLabels : MetricLabels = { } ;
77
92
78
93
const fakeCounter = {
79
- inc : ( ) => {
94
+ inc : ( _value : number , labels : MetricLabels ) => {
80
95
counterIncremented = true ;
96
+ recordedLabels = labels ;
81
97
} ,
82
98
} ;
83
99
@@ -86,18 +102,21 @@ test('should increment counter with valid parameters', (t) => {
86
102
} ;
87
103
88
104
const staticContext = { appName : 'my-app' , environment : 'dev' } ;
89
- const api = new MetricsAPI ( fakeRegistry as any , staticContext ) ;
105
+ const api = new MetricsAPI ( fakeRegistry as any , fakeVariantResolver ( ) , staticContext ) ;
90
106
91
- api . incrementCounter ( 'valid_counter' , 5 , 'featureX' ) ;
107
+ api . incrementCounter ( 'valid_counter' , 5 , { flagNames : [ 'featureX' ] , context : staticContext } ) ;
92
108
t . true ( counterIncremented , 'Counter should be incremented with valid parameters' ) ;
109
+ t . deepEqual ( recordedLabels , { appName : 'my-app' , environment : 'dev' , featureX : 'enabled' } ) ;
93
110
} ) ;
94
111
95
112
test ( 'should set gauge with valid parameters' , ( t ) => {
96
113
let gaugeSet = false ;
114
+ let recordedLabels : MetricLabels = { } ;
97
115
98
116
const fakeGauge = {
99
- set : ( ) => {
117
+ set : ( _value : number , labels : MetricLabels ) => {
100
118
gaugeSet = true ;
119
+ recordedLabels = labels ;
101
120
} ,
102
121
} ;
103
122
@@ -106,10 +125,11 @@ test('should set gauge with valid parameters', (t) => {
106
125
} ;
107
126
108
127
const staticContext = { appName : 'my-app' , environment : 'dev' } ;
109
- const api = new MetricsAPI ( fakeRegistry as any , staticContext ) ;
128
+ const api = new MetricsAPI ( fakeRegistry as any , fakeVariantResolver ( 'variantY' ) , staticContext ) ;
110
129
111
- api . updateGauge ( 'valid_gauge' , 10 , 'featureY' ) ;
130
+ api . updateGauge ( 'valid_gauge' , 10 , { flagNames : [ 'featureY' ] , context : staticContext } ) ;
112
131
t . true ( gaugeSet , 'Gauge should be set with valid parameters' ) ;
132
+ t . deepEqual ( recordedLabels , { appName : 'my-app' , environment : 'dev' , featureY : 'variantY' } ) ;
113
133
} ) ;
114
134
115
135
test ( 'defining a counter automatically sets label names' , ( t ) => {
@@ -127,7 +147,7 @@ test('defining a counter automatically sets label names', (t) => {
127
147
} ;
128
148
129
149
const staticContext = { appName : 'my-app' , environment : 'dev' } ;
130
- const api = new MetricsAPI ( fakeRegistry as any , staticContext ) ;
150
+ const api = new MetricsAPI ( fakeRegistry as any , fakeVariantResolver ( ) , staticContext ) ;
131
151
132
152
api . defineCounter ( 'test_counter' , 'Test help text' ) ;
133
153
t . true ( counterRegistered , 'Counter should be registered' ) ;
@@ -148,7 +168,7 @@ test('defining a gauge automatically sets label names', (t) => {
148
168
} ;
149
169
150
170
const staticContext = { appName : 'my-app' , environment : 'dev' } ;
151
- const api = new MetricsAPI ( fakeRegistry as any , staticContext ) ;
171
+ const api = new MetricsAPI ( fakeRegistry as any , fakeVariantResolver ( 'variantX' ) , staticContext ) ;
152
172
153
173
api . defineGauge ( 'test_gauge' , 'Test help text' ) ;
154
174
t . true ( gaugeRegistered , 'Gauge should be registered' ) ;
0 commit comments