@@ -8,47 +8,60 @@ describe('TinyTag', () => {
88 document . cookie = '' ;
99 global . fetch = jest . fn ( ( ) => Promise . resolve ( { ok : true } ) ) ;
1010 tt = new TinyTag ( ) ;
11+ jest . spyOn ( tt . queue , 'push' ) ; // Spy on queue.push to check events
1112 tt . init ( { writeKey : 'testkey' , dataPlaneUrl : 'http://example.com' } ) ;
1213 } ) ;
1314
15+ afterEach ( ( ) => {
16+ jest . restoreAllMocks ( ) ;
17+ } ) ;
18+
1419 test ( 'page queues an event with context' , ( ) => {
1520 tt . page ( 'Home' ) ;
16- expect ( tt . queue . queue [ 0 ] ) . toMatchObject ( {
17- type : 'page' ,
18- name : 'Home' ,
19- context : expect . objectContaining ( {
20- userAgent : navigator . userAgent
21+ expect ( tt . queue . push ) . toHaveBeenCalledWith (
22+ expect . objectContaining ( {
23+ type : 'page' ,
24+ name : 'Home' ,
25+ context : expect . objectContaining ( {
26+ userAgent : navigator . userAgent
27+ } )
2128 } )
22- } ) ;
29+ ) ;
2330 } ) ;
2431
2532 test ( 'track queues an event' , ( ) => {
2633 tt . track ( 'Button Clicked' , { button : 'test' } ) ;
27- expect ( tt . queue . queue [ 0 ] ) . toMatchObject ( {
28- type : 'track' ,
29- event : 'Button Clicked' ,
30- properties : { button : 'test' }
31- } ) ;
34+ expect ( tt . queue . push ) . toHaveBeenCalledWith (
35+ expect . objectContaining ( {
36+ type : 'track' ,
37+ event : 'Button Clicked' ,
38+ properties : { button : 'test' }
39+ } )
40+ ) ;
3241 } ) ;
3342
3443 test ( 'identify sets userId and merges traits' , ( ) => {
3544 setCookie ( 'rl_trait' , JSON . stringify ( { email :
'[email protected] ' } ) ) ; 3645 tt . identify ( 'user123' , { name : 'New User' } ) ;
37- expect ( tt . queue . queue [ 0 ] ) . toMatchObject ( {
38- type : 'identify' ,
39- userId : 'user123' ,
40- traits :
{ email :
'[email protected] ' , name :
'New User' } 41- } ) ;
46+ expect ( tt . queue . push ) . toHaveBeenCalledWith (
47+ expect . objectContaining ( {
48+ type : 'identify' ,
49+ userId : 'user123' ,
50+ traits :
{ email :
'[email protected] ' , name :
'New User' } 51+ } )
52+ ) ;
4253 expect ( document . cookie ) . toContain ( 'userId=user123' ) ;
4354 } ) ;
4455
4556 test ( 'group queues an event' , ( ) => {
4657 tt . group ( 'group456' , { name : 'Test Group' } ) ;
47- expect ( tt . queue . queue [ 0 ] ) . toMatchObject ( {
48- type : 'group' ,
49- groupId : 'group456' ,
50- traits : { name : 'Test Group' }
51- } ) ;
58+ expect ( tt . queue . push ) . toHaveBeenCalledWith (
59+ expect . objectContaining ( {
60+ type : 'group' ,
61+ groupId : 'group456' ,
62+ traits : { name : 'Test Group' }
63+ } )
64+ ) ;
5265 } ) ;
5366
5467 test ( 'getAnonymousId uses Segment cookie if present' , ( ) => {
0 commit comments