license const playerState = { UNSTARTED : - 1 , ENDED : 0 , PLAYING : 1 , PAUSED : 2 , BUFFERING : 3 , CUED : 5 , } ; const modestBranding = { Full : 0 , Modest : 1 , } ; interface FakeYtNamespace { playerCtorSpy : jasmine . Spy ; playerSpy : jasmine . SpyObj < YT . Player > ; events : Required < YT . Events > ; namespace : typeof YT ; } export function createFakeYtNamespace ( ) { const playerSpy = jasmine . createSpyObj ( 'Player' , [ 'getPlayerState' , 'destroy' , 'cueVideoById' , 'loadVideoById' , 'pauseVideo' , 'stopVideo' , 'seekTo' , 'isMuted' , 'mute' , 'unMute' , 'getVolume' , 'getPlaybackRate' , 'getAvailablePlaybackRates' , 'getVideoLoadedFraction' , 'getPlayerState' , 'getCurrentTime' , 'getPlaybackQuality' , 'getAvailableQualityLevels' , 'getDuration' , 'getVideoUrl' , 'getVideoEmbedCode' , 'playVideo' , 'setSize' , 'setVolume' , 'setPlaybackQuality' , 'setPlaybackRate' , 'addEventListener' , 'removeEventListener' , ] ) ; let playerConfig ; const boundListeners = new Map < keyof YT . Events , Set < ( event : any ) => void > > ( ) ; const playerCtorSpy = jasmine . createSpy ( 'Player Constructor' ) ; playerCtorSpy . and . callFake ( function ( _el , config ) { playerConfig = config ; return playerSpy ; } ) ; playerSpy . addEventListener . and . callFake ( ( name , listener ) => { if ( ! boundListeners . has ( name ) ) { boundListeners . set ( name , new Set ( ) ) ; } boundListeners . get ( name ) ! . add ( listener ) ; } ) ; playerSpy . removeEventListener . and . callFake ( ( name , listener ) => { if ( boundListeners . has ( name ) ) { boundListeners . get ( name ) ! . delete ( listener ) ; } } ) ; function eventHandlerFactory ( name ) { return ( arg = { } ) => { if ( ! playerConfig ) { throw new Error ( `Player not initialized before ${ name } called` ) ; } if ( boundListeners . has ( name ) ) { boundListeners . get ( name ) ! . forEach ( callback => callback ( arg ) ) ; } } ; } const events = { onReady : eventHandlerFactory ( 'onReady' ) , onStateChange : eventHandlerFactory ( 'onStateChange' ) , onPlaybackQualityChange : eventHandlerFactory ( 'onPlaybackQualityChange' ) , onPlaybackRateChange : eventHandlerFactory ( 'onPlaybackRateChange' ) , onError : eventHandlerFactory ( 'onError' ) , onApiChange : eventHandlerFactory ( 'onApiChange' ) , } ; return { playerCtorSpy , playerSpy , events , namespace : { 'Player' : playerCtorSpy as unknown as typeof YT . Player , 'PlayerState' : playerState , 'ModestBranding' : modestBranding , } as typeof YT , } ; }