@@ -27,8 +27,13 @@ describe('Client Metrics support', () => {
27
27
const metrics = new Metrics ( 'dummy-package' , '1.0' ) ;
28
28
29
29
const checkMetric = ( metricName : string , detailObject : MetricDetail ) => {
30
- expect ( window . AWSC . Clog . log ) . toHaveBeenCalledWith ( metricName , 1 , JSON . stringify ( detailObject ) ) ;
31
- expect ( window . AWSC . Clog . log ) . toHaveBeenCalledTimes ( 1 ) ;
30
+ expect ( window . panorama ) . toHaveBeenCalledWith ( 'trackCustomEvent' , {
31
+ eventName : metricName ,
32
+ eventDetail : JSON . stringify ( detailObject ) ,
33
+ eventValue : '1' ,
34
+ timestamp : expect . any ( Number ) ,
35
+ } ) ;
36
+ expect ( window . panorama ) . toHaveBeenCalledTimes ( 1 ) ;
32
37
} ;
33
38
34
39
beforeEach ( ( ) => {
@@ -49,31 +54,16 @@ describe('Client Metrics support', () => {
49
54
} ) ;
50
55
51
56
describe ( 'sendMetric' , ( ) => {
52
- test ( 'does nothing when window.AWSC is undefined' , ( ) => {
57
+ test ( 'does nothing of both AWSC and panorama APIs are not available' , ( ) => {
58
+ delete window . panorama ;
53
59
delete window . AWSC ;
54
- metrics . sendMetric ( 'name' , 0 ) ; // only proves no exception thrown
55
- } ) ;
56
-
57
- test ( 'does nothing when window.AWSC.Clog is undefined' , ( ) => {
58
- window . AWSC = { } ;
59
- metrics . sendMetric ( 'name' , 0 ) ; // only proves no exception thrown
60
+ expect ( ( ) => metrics . sendMetric ( 'name' , 0 ) ) . not . toThrow ( ) ;
60
61
} ) ;
61
62
62
- test ( 'uses panorama API as fallback when AWSC.Clog.log is unavailable' , ( ) => {
63
- delete window . AWSC ;
63
+ test ( 'uses AWSC.Clog.log API as fallback when panorama is unavailable' , ( ) => {
64
+ delete window . panorama ;
64
65
metrics . sendMetric ( 'name' , 0 ) ;
65
- expect ( window . panorama ) . toHaveBeenCalledWith ( 'trackCustomEvent' , {
66
- eventName : 'name' ,
67
- eventValue : '0' ,
68
- timestamp : expect . any ( Number ) ,
69
- } ) ;
70
- } ) ;
71
-
72
- test ( 'does nothing when window.AWSC.Clog.log is undefined' , ( ) => {
73
- window . AWSC = {
74
- Clog : undefined ,
75
- } ;
76
- metrics . sendMetric ( 'name' , 0 ) ; // only proves no exception thrown
66
+ expect ( window . AWSC . Clog . log ) . toHaveBeenCalledWith ( 'name' , 0 , undefined ) ;
77
67
} ) ;
78
68
79
69
describe ( 'within an iframe' , ( ) => {
@@ -82,51 +72,60 @@ describe('Client Metrics support', () => {
82
72
83
73
afterEach ( ( ) => {
84
74
Object . defineProperty ( window , 'parent' , originalWindowParent ) ;
85
- expect ( window . parent . AWSC ) . toBeUndefined ( ) ;
75
+ expect ( window . parent . panorama ) . toBeUndefined ( ) ;
86
76
} ) ;
87
77
88
78
const setupIframe = ( ) => {
89
79
Object . defineProperty ( window , 'parent' , { configurable : true , writable : true , value : { parent : { } } } ) ;
90
80
} ;
91
81
92
- test ( 'does nothing when AWSC is not defined in the parent iframe' , ( ) => {
93
- delete window . AWSC ;
82
+ test ( 'does nothing when window.panorama is not defined in the parent iframe' , ( ) => {
83
+ delete window . panorama ;
94
84
setupIframe ( ) ;
95
- expect ( window . parent . AWSC ) . toBeUndefined ( ) ;
85
+ expect ( window . parent . panorama ) . toBeUndefined ( ) ;
96
86
97
87
metrics . sendMetric ( 'name' , 0 ) ; // only proves no exception thrown
98
88
} ) ;
99
89
100
- test ( 'works if parent has AWSC ' , ( ) => {
90
+ test ( 'works if window. parent has panorama object ' , ( ) => {
101
91
setupIframe ( ) ;
102
- delete window . AWSC ;
103
- window . parent . AWSC = {
104
- Clog : {
105
- log : ( ) => { } ,
106
- } ,
107
- } ;
108
- jest . spyOn ( window . parent . AWSC . Clog , 'log' ) ;
109
- expect ( window . AWSC ) . toBeUndefined ( ) ;
110
- expect ( window . parent . AWSC ) . toBeDefined ( ) ;
92
+ delete window . panorama ;
93
+ window . parent . panorama = ( ) => { } ;
94
+ jest . spyOn ( window . parent , 'panorama' ) ;
95
+ expect ( window . panorama ) . toBeUndefined ( ) ;
96
+ expect ( window . parent . panorama ) . toBeDefined ( ) ;
111
97
112
98
metrics . sendMetric ( 'name' , 0 , undefined ) ;
113
- expect ( window . parent . AWSC . Clog . log ) . toHaveBeenCalledWith ( 'name' , 0 , undefined ) ;
99
+ expect ( window . parent . panorama ) . toHaveBeenCalledWith ( 'trackCustomEvent' , {
100
+ eventName : 'name' ,
101
+ eventValue : '0' ,
102
+ timestamp : expect . any ( Number ) ,
103
+ } ) ;
114
104
} ) ;
115
105
} ) ;
116
106
117
- describe ( 'when window.AWSC.Clog.log is defined' , ( ) => {
107
+ describe ( 'when window.panorama is defined' , ( ) => {
118
108
mockConsoleError ( ) ;
119
109
120
- test ( 'delegates to window.AWSC.Clog.log when defined' , ( ) => {
110
+ test ( 'delegates to window.panorama when defined' , ( ) => {
121
111
metrics . sendMetric ( 'name' , 0 , undefined ) ;
122
- expect ( window . AWSC . Clog . log ) . toHaveBeenCalledWith ( 'name' , 0 , undefined ) ;
112
+ expect ( window . panorama ) . toHaveBeenCalledWith ( 'trackCustomEvent' , {
113
+ eventName : 'name' ,
114
+ eventValue : '0' ,
115
+ timestamp : expect . any ( Number ) ,
116
+ } ) ;
123
117
} ) ;
124
118
125
119
describe ( 'Metric name validation' , ( ) => {
126
120
const tryValidMetric = ( metricName : string ) => {
127
- test ( `calls AWSC.Clog.log when valid metric name used (${ metricName } )` , ( ) => {
121
+ test ( `calls window.panorama when valid metric name used (${ metricName } )` , ( ) => {
128
122
metrics . sendMetric ( metricName , 1 , 'detail' ) ;
129
- expect ( window . AWSC . Clog . log ) . toHaveBeenCalledWith ( metricName , 1 , 'detail' ) ;
123
+ expect ( window . panorama ) . toHaveBeenCalledWith ( 'trackCustomEvent' , {
124
+ eventName : metricName ,
125
+ eventValue : '1' ,
126
+ eventDetail : 'detail' ,
127
+ timestamp : expect . any ( Number ) ,
128
+ } ) ;
130
129
} ) ;
131
130
} ;
132
131
@@ -135,6 +134,8 @@ describe('Client Metrics support', () => {
135
134
metrics . sendMetric ( metricName , 0 , 'detail' ) ;
136
135
expect ( console . error ) . toHaveBeenCalledWith ( `Invalid metric name: ${ metricName } ` ) ;
137
136
jest . mocked ( console . error ) . mockReset ( ) ;
137
+ expect ( window . panorama ) . not . toHaveBeenCalled ( ) ;
138
+ expect ( window . AWSC . Clog . log ) . not . toHaveBeenCalled ( ) ;
138
139
} ) ;
139
140
} ;
140
141
@@ -161,14 +162,21 @@ describe('Client Metrics support', () => {
161
162
test ( 'accepts details below the character limit' , ( ) => {
162
163
const validDetail = 'a' . repeat ( 4000 ) ;
163
164
metrics . sendMetric ( 'metricName' , 1 , validDetail ) ;
164
- expect ( window . AWSC . Clog . log ) . toHaveBeenCalledWith ( 'metricName' , 1 , validDetail ) ;
165
+ expect ( window . panorama ) . toHaveBeenCalledWith ( 'trackCustomEvent' , {
166
+ eventName : 'metricName' ,
167
+ eventValue : '1' ,
168
+ eventDetail : validDetail ,
169
+ timestamp : expect . any ( Number ) ,
170
+ } ) ;
165
171
} ) ;
166
172
167
173
test ( 'throws an error when detail is too long' , ( ) => {
168
174
const invalidDetail = 'a' . repeat ( 4001 ) ;
169
175
metrics . sendMetric ( 'metricName' , 0 , invalidDetail ) ;
170
176
expect ( console . error ) . toHaveBeenCalledWith ( `Detail for metric metricName is too long: ${ invalidDetail } ` ) ;
171
177
jest . mocked ( console . error ) . mockReset ( ) ;
178
+ expect ( window . panorama ) . not . toHaveBeenCalled ( ) ;
179
+ expect ( window . AWSC . Clog . log ) . not . toHaveBeenCalled ( ) ;
172
180
} ) ;
173
181
} ) ;
174
182
} ) ;
@@ -177,15 +185,31 @@ describe('Client Metrics support', () => {
177
185
describe ( 'sendMetricOnce' , ( ) => {
178
186
test ( 'logs a metric name only once' , ( ) => {
179
187
metrics . sendMetricOnce ( 'my-event' , 1 ) ;
180
- expect ( window . AWSC . Clog . log ) . toHaveBeenCalledWith ( 'my-event' , 1 , undefined ) ;
181
- expect ( window . AWSC . Clog . log ) . toHaveBeenCalledTimes ( 1 ) ;
188
+ expect ( window . panorama ) . toHaveBeenCalledWith ( 'trackCustomEvent' , {
189
+ eventName : 'my-event' ,
190
+ eventValue : '1' ,
191
+ timestamp : expect . any ( Number ) ,
192
+ } ) ;
193
+ expect ( window . panorama ) . toHaveBeenCalledTimes ( 1 ) ;
182
194
183
195
metrics . sendMetricOnce ( 'my-event' , 2 ) ;
184
- expect ( window . AWSC . Clog . log ) . toHaveBeenCalledTimes ( 1 ) ;
196
+ expect ( window . panorama ) . toHaveBeenCalledTimes ( 1 ) ;
197
+ } ) ;
185
198
186
- metrics . sendMetricOnce ( 'My-Event' , 3 ) ;
187
- expect ( window . AWSC . Clog . log ) . toHaveBeenCalledWith ( 'My-Event' , 3 , undefined ) ;
188
- expect ( window . AWSC . Clog . log ) . toHaveBeenCalledTimes ( 2 ) ;
199
+ test ( 'does not deduplicate metrics in different casing' , ( ) => {
200
+ metrics . sendMetricOnce ( 'my-event' , 1 ) ;
201
+ metrics . sendMetricOnce ( 'My-Event' , 2 ) ;
202
+ expect ( window . panorama ) . toHaveBeenCalledWith ( 'trackCustomEvent' , {
203
+ eventName : 'my-event' ,
204
+ eventValue : '1' ,
205
+ timestamp : expect . any ( Number ) ,
206
+ } ) ;
207
+ expect ( window . panorama ) . toHaveBeenCalledWith ( 'trackCustomEvent' , {
208
+ eventName : 'My-Event' ,
209
+ eventValue : '2' ,
210
+ timestamp : expect . any ( Number ) ,
211
+ } ) ;
212
+ expect ( window . panorama ) . toHaveBeenCalledTimes ( 2 ) ;
189
213
} ) ;
190
214
} ) ;
191
215
@@ -265,7 +289,7 @@ describe('Client Metrics support', () => {
265
289
266
290
metrics . sendMetricObjectOnce ( metricObj , 1 ) ;
267
291
metrics . sendMetricObjectOnce ( metricObj , 1 ) ;
268
- expect ( window . AWSC . Clog . log ) . toHaveBeenCalledTimes ( 1 ) ;
292
+ expect ( window . panorama ) . toHaveBeenCalledTimes ( 1 ) ;
269
293
} ) ;
270
294
test ( 'logs metric for each different version if same source and action' , ( ) => {
271
295
metrics . sendMetricObjectOnce (
@@ -284,7 +308,7 @@ describe('Client Metrics support', () => {
284
308
} ,
285
309
1
286
310
) ;
287
- expect ( window . AWSC . Clog . log ) . toHaveBeenCalledTimes ( 2 ) ;
311
+ expect ( window . panorama ) . toHaveBeenCalledTimes ( 2 ) ;
288
312
} ) ;
289
313
test ( 'logs a metric multiple times if same source but different actions' , ( ) => {
290
314
metrics . sendMetricObjectOnce (
@@ -303,7 +327,7 @@ describe('Client Metrics support', () => {
303
327
} ,
304
328
1
305
329
) ;
306
- expect ( window . AWSC . Clog . log ) . toHaveBeenCalledTimes ( 2 ) ;
330
+ expect ( window . panorama ) . toHaveBeenCalledTimes ( 2 ) ;
307
331
} ) ;
308
332
} ) ;
309
333
0 commit comments