import { Event , DurationEvent , FlowEvent } from '../types/EventInterfaces' ; import { EventsPhase } from '../types/Phases' ; /**
 * These tests are 50% about testing that the types are implemented correctly,
 * and 50% documenting how to handle mapping between Event types and subtypes
 * with and without EventsPhas literal.
 *
 * The tests rely on the @ts-expect-error pragma, which will pass the type check
 * if the following line has a type error, and will error if the following line is fine.
 */ describe ( 'Event' , ( ) => { it ( 'should allow constructing event objects using EventsPhase enum values' , ( ) => { const event = { ts : 1 , ph : EventsPhase . DURATION_EVENTS_BEGIN } ; expect ( event ) . toEqual ( { ts : 1 , ph : 'B' } ) ; } ) ; it ( 'should not allow constructing event objects using wrong enum values' , ( ) => { const event = { ts : 1 , ph : EventsPhase . INSTANT_EVENTS } ; expect ( event ) . toEqual ( { ts : 1 , ph : 'I' } ) ; } ) ; it ( 'should not allow constructing event objects with phase literal at type level' , ( ) => { const event = { ts : 'ts' , ph : 's' } ; expect ( event ) . toEqual ( { ts : 'ts' , ph : 's' } ) ; } ) ; it ( 'should not allow coercing event objects with incorrect phase literal' , ( ) => { const event = { ts : 'ts' , ph : 'NOT_s' } as DurationEvent ; expect ( event ) . toEqual ( { ts : 'ts' , ph : 'NOT_s' } ) ; } ) ; it ( 'should allow polymorphic lists of different event types' , ( ) => { const flow = { ts : 1 , ph : EventsPhase . FLOW_EVENTS_END } ; const duration = { ts : 1 , ph : EventsPhase . DURATION_EVENTS_END } ; const events = [ flow , duration ] ; expect ( events ) . toEqual ( [ { ts : 1 , ph : 'f' } , { ts : 1 , ph : 'E' } , ] ) ; } ) ; it ( 'should not allow polymorphic lists where any value is not a valid event type' , ( ) => { const durationEnd = { ts : 1 , ph : EventsPhase . DURATION_EVENTS_END } ; const durationBegin = { ts : 1 , ph : EventsPhase . DURATION_EVENTS_BEGIN } ; const invalid = { ts : 'ts' , ph : 'invalid' } ; const events = [ durationEnd , durationBegin , invalid ] ; expect ( events ) . toEqual ( [ { ts : 1 , ph : 'E' } , { ts : 1 , ph : 'B' } , { ts : 'ts' , ph : 'invalid' } , ] ) ; } ) ; it ( 'should support type guards' , ( ) => { function isDurationEvent ( event ) { return ( event . ph === EventsPhase . DURATION_EVENTS_BEGIN || event . ph === EventsPhase . DURATION_EVENTS_END ) ; } function expectsDurationEvent ( event ) { return event . ph ; } const durationEventLike = { ts : 1 , ph : 'B' } ; expectsDurationEvent ( durationEventLike ) ; if ( isDurationEvent ( durationEventLike ) ) { expectsDurationEvent ( durationEventLike ) ; } else { expectsDurationEvent ( durationEventLike ) ; } } ) ; } ) ;