import { Context , EventType , SegmentAPIIntegrations , TrackEventType , UserTraits , } from '../types' ; import { sendEvents } from '../api' ; import * as context from '../context' ; describe ( '#sendEvents' , ( ) => { beforeEach ( ( ) => { jest . spyOn ( context , 'getContext' ) . mockImplementationOnce ( async ( userTraits ? ) : Promise < Context > => { return { appName : 'Segment Example' , traits : userTraits , } as any ; } ) ; jest . spyOn ( Date . prototype , 'toISOString' ) . mockReturnValue ( '2001-01-01T00:00:00.000Z' ) ; } ) ; it ( 'sends an event' , async ( ) => { const mockResponse = Promise . resolve ( 'MANOS' ) ; global . fetch = jest . fn ( ( ) => Promise . resolve ( mockResponse ) ) ; const serializedEventProperties = { anonymousId : '3534a492-e975-4efa-a18b-3c70c562fec2' , event : 'Awesome event' , type : EventType . TrackEvent , properties : { } , timestamp : '2000-01-01T00:00:00.000Z' , messageId : '1d1744bf-5beb-41ac-ad7a-943eac33babc' , } ; const additionalEventProperties = { context : await context . getContext ( { name : 'Hello' } ) , integrations : { Firebase : false , } , } ; const event = { ... serializedEventProperties , ... additionalEventProperties , } ; await sendEvents ( { config : { writeKey : 'SEGMENT_KEY' , } , events : [ event ] , } ) ; expect ( fetch ) . toHaveBeenCalledWith ( 'https://api.segment.io/v1/batch' , { method : 'POST' , body : JSON . stringify ( { batch : [ { ... event , sentAt : '2001-01-01T00:00:00.000Z' } ] , } ) , headers : { 'Authorization' : 'Basic U0VHTUVOVF9LRVk6' , 'Content-Type' : 'application/json' , } , } ) ; } ) ; } ) ;