@@ -20,15 +20,12 @@ describe('snippet', function() {
2020 t : '' ,
2121 r : document . referrer
2222 } ;
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- } ) ;
3023
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 ) ) ) ;
3229 sandbox = sinon . sandbox . create ( ) ;
3330 origConsole = window . console ;
3431 origError = window . console . error ;
@@ -42,7 +39,7 @@ describe('snippet', function() {
4239 sandbox . spy ( window . console , 'error' ) ;
4340 window . analytics = undefined ;
4441 snippet ( ) ;
45- } ) ;
42+ } ;
4643
4744 afterEach ( function ( ) {
4845 sandbox . restore ( ) ;
@@ -51,27 +48,45 @@ describe('snippet', function() {
5148 } ) ;
5249
5350 it ( 'should define a global queue' , function ( ) {
51+ setup ( )
5452 assert ( window . analytics instanceof Array ) ;
5553 } ) ;
5654
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+
5768 it ( 'should load the script once' , function ( ) {
69+ setup ( )
5870 var scripts = document . scripts ;
5971 var length = scripts . length ;
6072 Function ( snippet ) ( ) ;
6173 assert ( length === scripts . length ) ;
6274 } ) ;
6375
6476 it ( 'should set SNIPPET_VERSION to module version' , function ( ) {
77+ setup ( )
6578 assert ( require ( '../package.json' ) . version === window . analytics . SNIPPET_VERSION ) ;
6679 } ) ;
6780
6881 it ( 'should warn using console.error when the snippet is included > 1' , function ( ) {
82+ setup ( )
6983 snippet ( ) ;
7084 var args = window . console . error . args ;
7185 assert . equal ( 'Segment snippet included twice.' , args [ 0 ] [ 0 ] ) ;
7286 } ) ;
7387
7488 it ( 'should ignore the snippet when the real analytics is already included' , function ( ) {
89+ setup ( )
7590 var ajs = { initialize : function ( ) { } } ;
7691 window . analytics = ajs ;
7792 snippet ( ) ;
@@ -81,20 +96,23 @@ describe('snippet', function() {
8196 } ) ;
8297
8398 it ( 'should not call .page() again when included > 1' , function ( ) {
99+ setup ( )
84100 window . analytics = { invoked : true , page : sandbox . spy ( ) } ;
85101 snippet ( ) ;
86102 var args = window . analytics . page . args ;
87103 assert . equal ( 0 , args . length ) ;
88104 } ) ;
89105
90106 it ( 'should not error when window.console is unavailable' , function ( ) {
107+ setup ( )
91108 window . analytics . included = true ;
92109 window . console = null ;
93110 snippet ( ) ;
94111 } ) ;
95112
96113 describe ( '.page' , function ( ) {
97114 it ( 'should call .page by default' , function ( ) {
115+ setup ( )
98116 assert . strictEqual ( window . analytics [ 0 ] [ 0 ] , 'page' ) ;
99117 } ) ;
100118 } ) ;
@@ -103,6 +121,7 @@ describe('snippet', function() {
103121 [ 'track' , 'screen' , 'alias' , 'group' , 'page' , 'identify' ] . forEach (
104122 function ( method ) {
105123 it ( method + ' should have a buffered page context' , function ( ) {
124+ setup ( )
106125 window . analytics [ method ] ( 'foo' ) ;
107126 var lastCall = window . analytics [ window . analytics . length - 1 ] ;
108127 assert . deepStrictEqual ( lastCall , [ method , 'foo' , bufferedPageContext ] ) ;
@@ -113,112 +132,138 @@ describe('snippet', function() {
113132
114133 describe ( '.methods' , function ( ) {
115134 it ( 'should define analytics.js methods' , function ( ) {
135+ setup ( )
116136 assert ( window . analytics . methods instanceof Array ) ;
117137 } ) ;
118138
119139 it ( '.identify' , function ( ) {
140+ setup ( )
120141 assert ( arrayContains ( window . analytics . methods , 'identify' ) ) ;
121142 } ) ;
122143
123144 it ( '.track' , function ( ) {
145+ setup ( )
124146 assert ( arrayContains ( window . analytics . methods , 'track' ) ) ;
125147 } ) ;
126148
127149 it ( '.trackLink' , function ( ) {
150+ setup ( )
128151 assert ( arrayContains ( window . analytics . methods , 'trackLink' ) ) ;
129152 } ) ;
130153
131154 it ( '.trackForm' , function ( ) {
155+ setup ( )
132156 assert ( arrayContains ( window . analytics . methods , 'trackForm' ) ) ;
133157 } ) ;
134158
135159 it ( '.trackClick' , function ( ) {
160+ setup ( )
136161 assert ( arrayContains ( window . analytics . methods , 'trackClick' ) ) ;
137162 } ) ;
138163
139164 it ( '.trackSubmit' , function ( ) {
165+ setup ( )
140166 assert ( arrayContains ( window . analytics . methods , 'trackSubmit' ) ) ;
141167 } ) ;
142168
143169 it ( '.page' , function ( ) {
170+ setup ( )
144171 assert ( arrayContains ( window . analytics . methods , 'page' ) ) ;
145172 } ) ;
146173
147174 it ( '.pageview' , function ( ) {
175+ setup ( )
148176 assert ( arrayContains ( window . analytics . methods , 'pageview' ) ) ;
149177 } ) ;
150178
151179 it ( '.alias' , function ( ) {
180+ setup ( )
152181 assert ( arrayContains ( window . analytics . methods , 'alias' ) ) ;
153182 } ) ;
154183
155184 it ( '.ready' , function ( ) {
185+ setup ( )
156186 assert ( arrayContains ( window . analytics . methods , 'ready' ) ) ;
157187 } ) ;
158188
159189 it ( '.group' , function ( ) {
190+ setup ( )
160191 assert ( arrayContains ( window . analytics . methods , 'group' ) ) ;
161192 } ) ;
162193
163194 it ( '.on' , function ( ) {
195+ setup ( )
164196 assert ( arrayContains ( window . analytics . methods , 'on' ) ) ;
165197 } ) ;
166198
167199 it ( '.once' , function ( ) {
200+ setup ( )
168201 assert ( arrayContains ( window . analytics . methods , 'once' ) ) ;
169202 } ) ;
170203
171204 it ( '.off' , function ( ) {
205+ setup ( )
172206 assert ( arrayContains ( window . analytics . methods , 'off' ) ) ;
173207 } ) ;
174208
175209 it ( '.addSourceMiddleware' , function ( ) {
210+ setup ( )
176211 assert ( arrayContains ( window . analytics . methods , 'addSourceMiddleware' ) ) ;
177212 } ) ;
178213
179214 it ( '.addIntegrationMiddleware' , function ( ) {
215+ setup ( )
180216 assert ( arrayContains ( window . analytics . methods , 'addIntegrationMiddleware' ) ) ;
181217 } ) ;
182218
183219 it ( '.setAnonymousId' , function ( ) {
220+ setup ( )
184221 assert ( arrayContains ( window . analytics . methods , 'setAnonymousId' ) ) ;
185222 } ) ;
186223
187224 it ( '.addDestinationMiddleware' , function ( ) {
225+ setup ( )
188226 assert ( arrayContains ( window . analytics . methods , 'addDestinationMiddleware' ) ) ;
189227 } ) ;
190228
191229 it ( '.screen' , function ( ) {
230+ setup ( )
192231 assert ( arrayContains ( window . analytics . methods , 'screen' ) ) ;
193232 } ) ;
194233
195234 it ( '.register' , function ( ) {
235+ setup ( )
196236 assert ( arrayContains ( window . analytics . methods , 'register' ) ) ;
197237 } ) ;
198238 } ) ;
199239
200240 describe ( '.factory' , function ( ) {
201241 it ( 'should define a factory' , function ( ) {
242+ setup ( )
202243 assert . strictEqual ( typeof window . analytics . factory , 'function' ) ;
203244 } ) ;
204245
205246 it ( 'should return a queue stub' , function ( ) {
247+ setup ( )
206248 assert . strictEqual ( typeof window . analytics . factory ( 'test' ) , 'function' ) ;
207249 } ) ;
208250
209251 it ( 'should push arguments onto the stub' , function ( ) {
252+ setup ( )
210253 var stub = window . analytics . factory ( 'test' ) ;
211254 stub ( 1 , 2 , 3 ) ;
212255 var args = window . analytics [ window . analytics . length - 1 ] ;
213256 assert . deepEqual ( args , [ 'test' , 1 , 2 , 3 ] ) ;
214257 } ) ;
215258
216259 it ( 'should return the analytics object' , function ( ) {
260+ setup ( )
217261 var stub = window . analytics . factory ( ) ;
218262 assert ( window . analytics === stub ( ) ) ;
219263 } ) ;
220264
221265 it ( 'should generate a stub for each method' , function ( ) {
266+ setup ( )
222267 for ( var i = 0 ; i < window . analytics . methods . length ; i ++ ) {
223268 var method = window . analytics . methods [ i ] ;
224269 assert . strictEqual ( typeof window . analytics [ method ] , 'function' ) ;
@@ -228,10 +273,12 @@ describe('snippet', function() {
228273
229274 describe ( '.load' , function ( ) {
230275 it ( 'should define a load method' , function ( ) {
276+ setup ( )
231277 assert . strictEqual ( typeof window . analytics . load , 'function' ) ;
232278 } ) ;
233279
234280 it ( 'should load analytics.js from the server' , function ( done ) {
281+ setup ( )
235282 var id = setInterval ( function ( ) {
236283 if ( typeof window . analytics === 'object' ) {
237284 clearInterval ( id ) ;
0 commit comments