@@ -3,7 +3,6 @@ import { range, uniq } from 'lodash'
33import { EventFactory } from '..'
44import { getDefaultPageContext } from '../../page'
55import { User } from '../../user'
6- import { SegmentEvent , Options } from '../interfaces'
76
87describe ( 'Event Factory' , ( ) => {
98 let user : User
@@ -59,6 +58,25 @@ describe('Event Factory', () => {
5958 const group = factory . group ( 'coolKidsId' , { coolkids : true } )
6059 expect ( group . groupId ) . toEqual ( 'coolKidsId' )
6160 } )
61+
62+ it ( 'allows userId / anonymousId to be specified as an option' , ( ) => {
63+ const group = factory . group ( 'my_group_id' , undefined , {
64+ userId : 'bar' ,
65+ anonymousId : 'foo' ,
66+ } )
67+ expect ( group . userId ) . toBe ( 'bar' )
68+ expect ( group . anonymousId ) . toBe ( 'foo' )
69+ } )
70+
71+ it ( 'uses userId / anonymousId from the user class (if specified)' , function ( ) {
72+ const user = new User ( )
73+ user . anonymousId = ( ) => '123'
74+ user . id = ( ) => 'abc'
75+ factory = new EventFactory ( user )
76+ const group = factory . group ( 'my_group_id' )
77+ expect ( group . userId ) . toBe ( 'abc' )
78+ expect ( group . anonymousId ) . toBe ( '123' )
79+ } )
6280 } )
6381
6482 describe ( 'page' , ( ) => {
@@ -394,32 +412,6 @@ describe('Event Factory', () => {
394412 } )
395413 } )
396414
397- describe ( 'normalize' , function ( ) {
398- const msg : SegmentEvent = { type : 'track' }
399- const opts : Options = ( msg . options = { } )
400-
401- describe ( 'message' , function ( ) {
402- it ( 'should merge original with normalized' , function ( ) {
403- msg . userId = 'user-id'
404- opts . integrations = { Segment : true }
405- const normalized = factory [ 'normalize' ] ( msg )
406-
407- expect ( normalized . messageId ?. length ) . toBeGreaterThanOrEqual ( 41 ) // 'ajs-next-md5(content + [UUID])'
408- delete normalized . messageId
409-
410- expect ( normalized . timestamp ) . toBeInstanceOf ( Date )
411- delete normalized . timestamp
412-
413- expect ( normalized ) . toStrictEqual ( {
414- integrations : { Segment : true } ,
415- type : 'track' ,
416- userId : 'user-id' ,
417- context : defaultContext ,
418- } )
419- } )
420- } )
421- } )
422-
423415 describe ( 'Page context augmentation' , ( ) => {
424416 // minimal tests -- more tests should be specifically around addPageContext
425417 factory = new EventFactory ( new User ( ) )
0 commit comments