1
1
/* eslint-env node, mocha */
2
+ /* global PerformanceObserver, Metric, chai */
2
3
3
4
const assert = chai . assert ;
4
5
6
+ /* eslint-disable */
7
+ function loadAnalytics ( ) {
8
+ ( function ( i , s , o , g , r , a , m ) { i [ 'GoogleAnalyticsObject' ] = r ; i [ r ] = i [ r ] || function ( ) {
9
+ ( i [ r ] . q = i [ r ] . q || [ ] ) . push ( arguments ) } , i [ r ] . l = 1 * new Date ( ) ; a = s . createElement ( o ) ,
10
+ m = s . getElementsByTagName ( o ) [ 0 ] ; a . async = 1 ; a . src = g ; m . parentNode . insertBefore ( a , m ) ;
11
+ } ) ( window , document , 'script' , 'https://www.google-analytics.com/analytics.js' , 'ga' ) ;
12
+ ga ( 'create' , 'UA-XXXXX-Y' , 'auto' ) ;
13
+ }
14
+ /* eslint-enable */
15
+
16
+ loadAnalytics ( ) ;
17
+
5
18
describe ( 'appmetrics.js' , function ( ) {
6
19
const METRIC_NAME = 'test_metric' ;
7
- let metric = new Metric ( METRIC_NAME ) ;
8
-
9
- function loadAnalytics ( ) {
10
- ( function ( i , s , o , g , r , a , m ) { i [ 'GoogleAnalyticsObject' ] = r ; i [ r ] = i [ r ] || function ( ) {
11
- ( i [ r ] . q = i [ r ] . q || [ ] ) . push ( arguments ) } , i [ r ] . l = 1 * new Date ( ) ; a = s . createElement ( o ) ,
12
- m = s . getElementsByTagName ( o ) [ 0 ] ; a . async = 1 ; a . src = g ; m . parentNode . insertBefore ( a , m ) ;
13
- } ) ( window , document , 'script' , 'https://www.google-analytics.com/analytics.js' , 'ga' ) ;
14
- ga ( 'create' , 'UA-XXXXX-Y' , 'auto' ) ;
15
- }
20
+ const metric = new Metric ( METRIC_NAME ) ;
16
21
17
22
function isAnalyticsRequest ( entry ) {
18
23
return entry . name . includes ( '/collect' ) && entry . name . includes ( 't=timing' ) ;
19
24
}
20
25
21
26
if ( ! window . PerformanceObserver ) {
22
- throw 'Cannot run tests in a browser PerformanceObserver' ;
27
+ throw Error ( 'Cannot run tests in a browser PerformanceObserver' ) ;
23
28
}
24
29
25
30
before ( function ( ) {
26
31
if ( ! location . origin . includes ( 'localhost' ) ) {
27
32
assert . fail ( false , true , 'Tests need to be run from a web server.' ) ;
28
33
}
29
- loadAnalytics ( ) ;
34
+ // loadAnalytics();
30
35
} ) ;
31
36
32
37
beforeEach ( function ( ) {
@@ -44,8 +49,8 @@ describe('appmetrics.js', function() {
44
49
assert . equal ( metric . duration , - 1 ) ;
45
50
} ) ;
46
51
it ( 'has correct feature detection' , function ( ) {
47
- assert . equal ( Metric . supportsPerfNow , performance . now ) ;
48
- assert . equal ( Metric . supportsPerfMark , performance . mark ) ;
52
+ assert . equal ( Metric . supportsPerfNow , true ) ;
53
+ assert . equal ( Metric . supportsPerfMark , true ) ;
49
54
} ) ;
50
55
it ( 'has private properties' , function ( ) {
51
56
assert . isUndefined ( metric . _start ) ;
@@ -54,12 +59,11 @@ describe('appmetrics.js', function() {
54
59
} ) ;
55
60
56
61
describe ( 'start()' , function ( ) {
57
-
58
62
it ( 'creates a mark' , function ( done ) {
59
- let observer = new PerformanceObserver ( list => {
63
+ const observer = new PerformanceObserver ( list => {
60
64
observer . disconnect ( ) ;
61
65
62
- let entries = list . getEntriesByName ( `mark_${ METRIC_NAME } _start` ) ;
66
+ const entries = list . getEntriesByName ( `mark_${ METRIC_NAME } _start` ) ;
63
67
assert . equal ( entries [ 0 ] . entryType , 'mark' , 'not a mark entry' ) ;
64
68
assert . equal ( entries . length , 1 ) ;
65
69
@@ -79,17 +83,15 @@ describe('appmetrics.js', function() {
79
83
} ) ;
80
84
81
85
describe ( 'end()' , function ( ) {
82
-
83
86
it ( 'creates a mark' , function ( done ) {
84
-
85
- let observer = new PerformanceObserver ( list => {
87
+ const observer = new PerformanceObserver ( list => {
86
88
observer . disconnect ( ) ;
87
89
88
- let markEntries = list . getEntriesByName ( `mark_${ METRIC_NAME } _end` ) ;
90
+ const markEntries = list . getEntriesByName ( `mark_${ METRIC_NAME } _end` ) ;
89
91
assert . equal ( markEntries . length , 1 ) ;
90
92
assert . equal ( markEntries [ 0 ] . entryType , 'mark' , 'not a mark entry' ) ;
91
93
92
- let measureEntries = list . getEntriesByName ( METRIC_NAME ) ;
94
+ const measureEntries = list . getEntriesByName ( METRIC_NAME ) ;
93
95
assert . equal ( measureEntries [ 0 ] . entryType , 'measure' , 'not a measurement entry' ) ;
94
96
assert . equal ( measureEntries . length , 1 ) ;
95
97
@@ -125,12 +127,11 @@ describe('appmetrics.js', function() {
125
127
} ) ;
126
128
127
129
describe ( 'sendToAnalytics()' , function ( ) {
128
-
129
130
it ( 'sends default request' , function ( done ) {
130
- let observer = new PerformanceObserver ( list => {
131
+ const observer = new PerformanceObserver ( list => {
131
132
observer . disconnect ( ) ;
132
133
133
- let entries = list . getEntries ( ) . filter ( entry => {
134
+ const entries = list . getEntries ( ) . filter ( entry => {
134
135
return isAnalyticsRequest ( entry ) &&
135
136
entry . name . includes ( metric . duration ) &&
136
137
entry . name . includes ( metric . name ) &&
@@ -146,10 +147,10 @@ describe('appmetrics.js', function() {
146
147
} ) ;
147
148
148
149
it ( 'can override duration and name' , function ( done ) {
149
- let observer = new PerformanceObserver ( list => {
150
+ const observer = new PerformanceObserver ( list => {
150
151
observer . disconnect ( ) ;
151
152
152
- let entries = list . getEntries ( ) . filter ( entry => {
153
+ const entries = list . getEntries ( ) . filter ( entry => {
153
154
return isAnalyticsRequest ( entry ) &&
154
155
entry . name . includes ( '1234567890' ) &&
155
156
entry . name . includes ( 'category_name' ) &&
@@ -165,12 +166,12 @@ describe('appmetrics.js', function() {
165
166
} ) ;
166
167
167
168
it ( 'can send a duration without measuring' , function ( done ) {
168
- let duration = Date . now ( ) ;
169
+ const duration = Date . now ( ) ;
169
170
170
- let observer = new PerformanceObserver ( list => {
171
+ const observer = new PerformanceObserver ( list => {
171
172
observer . disconnect ( ) ;
172
173
173
- let entries = list . getEntries ( ) . filter ( entry => {
174
+ const entries = list . getEntries ( ) . filter ( entry => {
174
175
return isAnalyticsRequest ( entry ) &&
175
176
entry . name . includes ( duration ) &&
176
177
entry . name . includes ( 'category_name' ) &&
@@ -182,24 +183,24 @@ describe('appmetrics.js', function() {
182
183
} ) ;
183
184
observer . observe ( { entryTypes : [ 'resource' ] } ) ;
184
185
185
- let metric = new Metric ( 'override_duration' ) ;
186
+ const metric = new Metric ( 'override_duration' ) ;
186
187
metric . sendToAnalytics ( 'category_name' , metric . name , duration ) ;
187
188
} ) ;
188
189
189
190
it ( 'no requests are to GA before a measurement' , function ( done ) {
190
191
// If the perf observer sees a request, the test should fail.
191
- let observer = new PerformanceObserver ( list => {
192
+ const observer = new PerformanceObserver ( list => {
192
193
observer . disconnect ( ) ;
193
- assert . fail ( false , true , 'Google Analytics request was sent before a measurement was made.' ) ;
194
+ assert . fail (
195
+ false , true , 'Google Analytics request was sent before a measurement was made.' ) ;
194
196
done ( ) ;
195
197
} ) ;
196
198
observer . observe ( { entryTypes : [ 'resource' ] } ) ;
197
199
198
- let metric = new Metric ( 'test_metric' ) ;
200
+ const metric = new Metric ( 'test_metric' ) ;
199
201
metric . sendToAnalytics ( 'should_not_be_sent' ) ;
200
202
201
203
setTimeout ( done , 500 ) ;
202
204
} ) ;
203
205
} ) ;
204
-
205
206
} ) ;
0 commit comments