@@ -51,13 +51,24 @@ describe('before loading', () => {
51
51
window . localStorage . clear ( )
52
52
}
53
53
} )
54
+
54
55
describe ( '#normalize' , ( ) => {
55
56
let object : SegmentEvent
57
+ let defaultCtx : any
58
+ const withSearchParams = ( search ?: string ) => {
59
+ object . context = { page : { search } }
60
+ }
56
61
57
62
beforeEach ( ( ) => {
58
63
cookie . remove ( 's:context.referrer' )
64
+ defaultCtx = {
65
+ page : {
66
+ search : '' ,
67
+ } ,
68
+ }
59
69
object = {
60
70
type : 'track' ,
71
+ context : defaultCtx ,
61
72
}
62
73
} )
63
74
@@ -94,17 +105,15 @@ describe('before loading', () => {
94
105
} )
95
106
96
107
it ( 'should always add .anonymousId even if .userId is given' , ( ) => {
97
- const object : SegmentEvent = { userId : 'baz' , type : 'track' }
108
+ object . userId = 'baz'
98
109
normalize ( analytics , object , options , { } )
99
110
assert ( object . anonymousId ?. length === 36 )
100
111
} )
101
112
102
113
it ( 'should accept anonymousId being set in an event' , async ( ) => {
103
- const object : SegmentEvent = {
104
- userId : 'baz' ,
105
- type : 'track' ,
106
- anonymousId : '👻' ,
107
- }
114
+ object . userId = 'baz'
115
+ object . anonymousId = '👻'
116
+
108
117
normalize ( analytics , object , options , { } )
109
118
expect ( object . anonymousId ) . toEqual ( '👻' )
110
119
} )
@@ -115,15 +124,16 @@ describe('before loading', () => {
115
124
} )
116
125
117
126
it ( 'should not rewrite context if provided' , ( ) => {
118
- const ctx = { }
127
+ const ctx = defaultCtx
119
128
const obj = { ...object , context : ctx }
120
129
normalize ( analytics , obj , options , { } )
121
130
expect ( obj . context ) . toEqual ( ctx )
122
131
} )
123
132
124
- it ( 'should copy . options to . context' , ( ) => {
133
+ it ( 'should overwrite options with context if context does not exist ' , ( ) => {
125
134
const opts = { }
126
135
const obj = { ...object , options : opts }
136
+ delete obj . context
127
137
normalize ( analytics , obj , options , { } )
128
138
assert ( obj . context === opts )
129
139
assert ( obj . options == null )
@@ -172,6 +182,7 @@ describe('before loading', () => {
172
182
173
183
it ( 'should not replace .locale if provided' , ( ) => {
174
184
const ctx = {
185
+ ...defaultCtx ,
175
186
locale : 'foobar' ,
176
187
}
177
188
const obj = { ...object , context : ctx }
@@ -180,10 +191,9 @@ describe('before loading', () => {
180
191
} )
181
192
182
193
it ( 'should add .campaign' , ( ) => {
183
- jsdom . reconfigure ( {
184
- url : 'http://localhost?utm_source=source&utm_medium=medium&utm_term=term&utm_content=content&utm_campaign=name' ,
185
- } )
186
-
194
+ withSearchParams (
195
+ 'utm_source=source&utm_medium=medium&utm_term=term&utm_content=content&utm_campaign=name'
196
+ )
187
197
normalize ( analytics , object , options , { } )
188
198
189
199
assert ( object )
@@ -197,10 +207,7 @@ describe('before loading', () => {
197
207
} )
198
208
199
209
it ( 'should decode query params' , ( ) => {
200
- jsdom . reconfigure ( {
201
- url : 'http://localhost?utm_source=%5BFoo%5D' ,
202
- } )
203
-
210
+ withSearchParams ( '?utm_source=%5BFoo%5D' )
204
211
normalize ( analytics , object , options , { } )
205
212
206
213
assert ( object )
@@ -210,9 +217,7 @@ describe('before loading', () => {
210
217
} )
211
218
212
219
it ( 'should guard against undefined utm params' , ( ) => {
213
- jsdom . reconfigure ( {
214
- url : 'http://localhost?utm_source' ,
215
- } )
220
+ withSearchParams ( '?utm_source' )
216
221
217
222
normalize ( analytics , object , options , { } )
218
223
@@ -223,10 +228,7 @@ describe('before loading', () => {
223
228
} )
224
229
225
230
it ( 'should guard against empty utm params' , ( ) => {
226
- jsdom . reconfigure ( {
227
- url : 'http://localhost?utm_source=' ,
228
- } )
229
-
231
+ withSearchParams ( '?utm_source=' )
230
232
normalize ( analytics , object , options , { } )
231
233
232
234
assert ( object )
@@ -236,10 +238,7 @@ describe('before loading', () => {
236
238
} )
237
239
238
240
it ( 'only parses utm params suffixed with _' , ( ) => {
239
- jsdom . reconfigure ( {
240
- url : 'http://localhost?utm' ,
241
- } )
242
-
241
+ withSearchParams ( '?utm' )
243
242
normalize ( analytics , object , options , { } )
244
243
245
244
assert ( object )
@@ -248,9 +247,7 @@ describe('before loading', () => {
248
247
} )
249
248
250
249
it ( 'should guard against short utm params' , ( ) => {
251
- jsdom . reconfigure ( {
252
- url : 'http://localhost?utm_' ,
253
- } )
250
+ withSearchParams ( '?utm_' )
254
251
255
252
normalize ( analytics , object , options , { } )
256
253
@@ -260,13 +257,14 @@ describe('before loading', () => {
260
257
} )
261
258
262
259
it ( 'should allow override of .campaign' , ( ) => {
263
- jsdom . reconfigure ( {
264
- url : 'http://localhost ?utm_source=source&utm_medium=medium&utm_term=term&utm_content=content&utm_campaign=name',
265
- } )
260
+ withSearchParams (
261
+ ' ?utm_source=source&utm_medium=medium&utm_term=term&utm_content=content&utm_campaign=name'
262
+ )
266
263
267
264
const obj = {
268
265
...object ,
269
266
context : {
267
+ ...defaultCtx ,
270
268
campaign : {
271
269
source : 'overrideSource' ,
272
270
medium : 'overrideMedium' ,
@@ -288,9 +286,7 @@ describe('before loading', () => {
288
286
} )
289
287
290
288
it ( 'should add .referrer.id and .referrer.type (cookies)' , ( ) => {
291
- jsdom . reconfigure ( {
292
- url : 'http://localhost?utm_source=source&urid=medium' ,
293
- } )
289
+ withSearchParams ( '?utm_source=source&urid=medium' )
294
290
295
291
normalize ( analytics , object , options , { } )
296
292
assert ( object )
@@ -307,9 +303,7 @@ describe('before loading', () => {
307
303
} )
308
304
309
305
it ( 'should add .referrer.id and .referrer.type (cookieless)' , ( ) => {
310
- jsdom . reconfigure ( {
311
- url : 'http://localhost?utm_source=source&urid=medium' ,
312
- } )
306
+ withSearchParams ( 'utm_source=source&urid=medium' )
313
307
const setCookieSpy = jest . spyOn ( cookie , 'set' )
314
308
analytics = new Analytics (
315
309
{ writeKey : options . apiKey } ,
@@ -329,10 +323,6 @@ describe('before loading', () => {
329
323
it ( 'should add .referrer.id and .referrer.type from cookie' , ( ) => {
330
324
cookie . set ( 's:context.referrer' , '{"id":"baz","type":"millennial-media"}' )
331
325
332
- jsdom . reconfigure ( {
333
- url : 'http://localhost?utm_source=source' ,
334
- } )
335
-
336
326
normalize ( analytics , object , options , { } )
337
327
338
328
assert ( object )
@@ -348,10 +338,6 @@ describe('before loading', () => {
348
338
'{"id":"medium","type":"millennial-media"}'
349
339
)
350
340
351
- jsdom . reconfigure ( {
352
- url : 'http://localhost' ,
353
- } )
354
-
355
341
normalize ( analytics , object , options , { } )
356
342
assert ( object )
357
343
assert ( object . context )
0 commit comments