@@ -20,15 +20,12 @@ describe('snippet', function() {
20
20
t : '' ,
21
21
r : document . referrer
22
22
} ;
23
-
24
- before ( function ( ) {
25
- snippet = Function ( render . max ( {
26
- // https://app.segment.com/segment-libraries/sources/snippet/settings/keys
27
- apiKey : 'zCueSsEKipbrRgqbJarlTG8UJsAZWpkm'
28
- } ) ) ;
29
- } ) ;
30
23
31
- beforeEach ( function ( ) {
24
+ var setup = function ( options ) {
25
+ snippet = Function ( render . max ( Object . assign ( { } , {
26
+ // https://app.segment.com/segment-libraries/sources/snippet/settings/keys
27
+ apiKey : 'zCueSsEKipbrRgqbJarlTG8UJsAZWpkm' ,
28
+ } , options ) ) ) ;
32
29
sandbox = sinon . sandbox . create ( ) ;
33
30
origConsole = window . console ;
34
31
origError = window . console . error ;
@@ -42,7 +39,7 @@ describe('snippet', function() {
42
39
sandbox . spy ( window . console , 'error' ) ;
43
40
window . analytics = undefined ;
44
41
snippet ( ) ;
45
- } ) ;
42
+ } ;
46
43
47
44
afterEach ( function ( ) {
48
45
sandbox . restore ( ) ;
@@ -51,27 +48,45 @@ describe('snippet', function() {
51
48
} ) ;
52
49
53
50
it ( 'should define a global queue' , function ( ) {
51
+ setup ( )
54
52
assert ( window . analytics instanceof Array ) ;
55
53
} ) ;
56
54
55
+ it ( 'works with different globalAnalyticsKey' , function ( ) {
56
+ setup ( {
57
+ globalAnalyticsKey : 'segment_analytics'
58
+ } )
59
+
60
+ assert ( window . analytics === undefined )
61
+ assert ( window . segment_analytics instanceof Array )
62
+ assert ( typeof window . segment_analytics . track === 'function' )
63
+ assert ( typeof window . segment_analytics . identify === 'function' )
64
+ // check that the custom global key is exposed to the main runtime through a data attribute
65
+ assert ( document . querySelector ( 'script[data-global-segment-analytics-key]' ) . dataset . globalSegmentAnalyticsKey === 'segment_analytics' )
66
+ } )
67
+
57
68
it ( 'should load the script once' , function ( ) {
69
+ setup ( )
58
70
var scripts = document . scripts ;
59
71
var length = scripts . length ;
60
72
Function ( snippet ) ( ) ;
61
73
assert ( length === scripts . length ) ;
62
74
} ) ;
63
75
64
76
it ( 'should set SNIPPET_VERSION to module version' , function ( ) {
77
+ setup ( )
65
78
assert ( require ( '../package.json' ) . version === window . analytics . SNIPPET_VERSION ) ;
66
79
} ) ;
67
80
68
81
it ( 'should warn using console.error when the snippet is included > 1' , function ( ) {
82
+ setup ( )
69
83
snippet ( ) ;
70
84
var args = window . console . error . args ;
71
85
assert . equal ( 'Segment snippet included twice.' , args [ 0 ] [ 0 ] ) ;
72
86
} ) ;
73
87
74
88
it ( 'should ignore the snippet when the real analytics is already included' , function ( ) {
89
+ setup ( )
75
90
var ajs = { initialize : function ( ) { } } ;
76
91
window . analytics = ajs ;
77
92
snippet ( ) ;
@@ -81,20 +96,23 @@ describe('snippet', function() {
81
96
} ) ;
82
97
83
98
it ( 'should not call .page() again when included > 1' , function ( ) {
99
+ setup ( )
84
100
window . analytics = { invoked : true , page : sandbox . spy ( ) } ;
85
101
snippet ( ) ;
86
102
var args = window . analytics . page . args ;
87
103
assert . equal ( 0 , args . length ) ;
88
104
} ) ;
89
105
90
106
it ( 'should not error when window.console is unavailable' , function ( ) {
107
+ setup ( )
91
108
window . analytics . included = true ;
92
109
window . console = null ;
93
110
snippet ( ) ;
94
111
} ) ;
95
112
96
113
describe ( '.page' , function ( ) {
97
114
it ( 'should call .page by default' , function ( ) {
115
+ setup ( )
98
116
assert . strictEqual ( window . analytics [ 0 ] [ 0 ] , 'page' ) ;
99
117
} ) ;
100
118
} ) ;
@@ -103,6 +121,7 @@ describe('snippet', function() {
103
121
[ 'track' , 'screen' , 'alias' , 'group' , 'page' , 'identify' ] . forEach (
104
122
function ( method ) {
105
123
it ( method + ' should have a buffered page context' , function ( ) {
124
+ setup ( )
106
125
window . analytics [ method ] ( 'foo' ) ;
107
126
var lastCall = window . analytics [ window . analytics . length - 1 ] ;
108
127
assert . deepStrictEqual ( lastCall , [ method , 'foo' , bufferedPageContext ] ) ;
@@ -113,112 +132,138 @@ describe('snippet', function() {
113
132
114
133
describe ( '.methods' , function ( ) {
115
134
it ( 'should define analytics.js methods' , function ( ) {
135
+ setup ( )
116
136
assert ( window . analytics . methods instanceof Array ) ;
117
137
} ) ;
118
138
119
139
it ( '.identify' , function ( ) {
140
+ setup ( )
120
141
assert ( arrayContains ( window . analytics . methods , 'identify' ) ) ;
121
142
} ) ;
122
143
123
144
it ( '.track' , function ( ) {
145
+ setup ( )
124
146
assert ( arrayContains ( window . analytics . methods , 'track' ) ) ;
125
147
} ) ;
126
148
127
149
it ( '.trackLink' , function ( ) {
150
+ setup ( )
128
151
assert ( arrayContains ( window . analytics . methods , 'trackLink' ) ) ;
129
152
} ) ;
130
153
131
154
it ( '.trackForm' , function ( ) {
155
+ setup ( )
132
156
assert ( arrayContains ( window . analytics . methods , 'trackForm' ) ) ;
133
157
} ) ;
134
158
135
159
it ( '.trackClick' , function ( ) {
160
+ setup ( )
136
161
assert ( arrayContains ( window . analytics . methods , 'trackClick' ) ) ;
137
162
} ) ;
138
163
139
164
it ( '.trackSubmit' , function ( ) {
165
+ setup ( )
140
166
assert ( arrayContains ( window . analytics . methods , 'trackSubmit' ) ) ;
141
167
} ) ;
142
168
143
169
it ( '.page' , function ( ) {
170
+ setup ( )
144
171
assert ( arrayContains ( window . analytics . methods , 'page' ) ) ;
145
172
} ) ;
146
173
147
174
it ( '.pageview' , function ( ) {
175
+ setup ( )
148
176
assert ( arrayContains ( window . analytics . methods , 'pageview' ) ) ;
149
177
} ) ;
150
178
151
179
it ( '.alias' , function ( ) {
180
+ setup ( )
152
181
assert ( arrayContains ( window . analytics . methods , 'alias' ) ) ;
153
182
} ) ;
154
183
155
184
it ( '.ready' , function ( ) {
185
+ setup ( )
156
186
assert ( arrayContains ( window . analytics . methods , 'ready' ) ) ;
157
187
} ) ;
158
188
159
189
it ( '.group' , function ( ) {
190
+ setup ( )
160
191
assert ( arrayContains ( window . analytics . methods , 'group' ) ) ;
161
192
} ) ;
162
193
163
194
it ( '.on' , function ( ) {
195
+ setup ( )
164
196
assert ( arrayContains ( window . analytics . methods , 'on' ) ) ;
165
197
} ) ;
166
198
167
199
it ( '.once' , function ( ) {
200
+ setup ( )
168
201
assert ( arrayContains ( window . analytics . methods , 'once' ) ) ;
169
202
} ) ;
170
203
171
204
it ( '.off' , function ( ) {
205
+ setup ( )
172
206
assert ( arrayContains ( window . analytics . methods , 'off' ) ) ;
173
207
} ) ;
174
208
175
209
it ( '.addSourceMiddleware' , function ( ) {
210
+ setup ( )
176
211
assert ( arrayContains ( window . analytics . methods , 'addSourceMiddleware' ) ) ;
177
212
} ) ;
178
213
179
214
it ( '.addIntegrationMiddleware' , function ( ) {
215
+ setup ( )
180
216
assert ( arrayContains ( window . analytics . methods , 'addIntegrationMiddleware' ) ) ;
181
217
} ) ;
182
218
183
219
it ( '.setAnonymousId' , function ( ) {
220
+ setup ( )
184
221
assert ( arrayContains ( window . analytics . methods , 'setAnonymousId' ) ) ;
185
222
} ) ;
186
223
187
224
it ( '.addDestinationMiddleware' , function ( ) {
225
+ setup ( )
188
226
assert ( arrayContains ( window . analytics . methods , 'addDestinationMiddleware' ) ) ;
189
227
} ) ;
190
228
191
229
it ( '.screen' , function ( ) {
230
+ setup ( )
192
231
assert ( arrayContains ( window . analytics . methods , 'screen' ) ) ;
193
232
} ) ;
194
233
195
234
it ( '.register' , function ( ) {
235
+ setup ( )
196
236
assert ( arrayContains ( window . analytics . methods , 'register' ) ) ;
197
237
} ) ;
198
238
} ) ;
199
239
200
240
describe ( '.factory' , function ( ) {
201
241
it ( 'should define a factory' , function ( ) {
242
+ setup ( )
202
243
assert . strictEqual ( typeof window . analytics . factory , 'function' ) ;
203
244
} ) ;
204
245
205
246
it ( 'should return a queue stub' , function ( ) {
247
+ setup ( )
206
248
assert . strictEqual ( typeof window . analytics . factory ( 'test' ) , 'function' ) ;
207
249
} ) ;
208
250
209
251
it ( 'should push arguments onto the stub' , function ( ) {
252
+ setup ( )
210
253
var stub = window . analytics . factory ( 'test' ) ;
211
254
stub ( 1 , 2 , 3 ) ;
212
255
var args = window . analytics [ window . analytics . length - 1 ] ;
213
256
assert . deepEqual ( args , [ 'test' , 1 , 2 , 3 ] ) ;
214
257
} ) ;
215
258
216
259
it ( 'should return the analytics object' , function ( ) {
260
+ setup ( )
217
261
var stub = window . analytics . factory ( ) ;
218
262
assert ( window . analytics === stub ( ) ) ;
219
263
} ) ;
220
264
221
265
it ( 'should generate a stub for each method' , function ( ) {
266
+ setup ( )
222
267
for ( var i = 0 ; i < window . analytics . methods . length ; i ++ ) {
223
268
var method = window . analytics . methods [ i ] ;
224
269
assert . strictEqual ( typeof window . analytics [ method ] , 'function' ) ;
@@ -228,10 +273,12 @@ describe('snippet', function() {
228
273
229
274
describe ( '.load' , function ( ) {
230
275
it ( 'should define a load method' , function ( ) {
276
+ setup ( )
231
277
assert . strictEqual ( typeof window . analytics . load , 'function' ) ;
232
278
} ) ;
233
279
234
280
it ( 'should load analytics.js from the server' , function ( done ) {
281
+ setup ( )
235
282
var id = setInterval ( function ( ) {
236
283
if ( typeof window . analytics === 'object' ) {
237
284
clearInterval ( id ) ;
0 commit comments