license import { supportPatchXHROnProperty } from '../test-util' ; declare const global ; describe ( 'TaskTrackingZone' , function ( ) { let _TaskTrackingZoneSpec = ( Zone as any ) [ 'TaskTrackingZoneSpec' ] ; let taskTrackingZoneSpec = null ; let taskTrackingZone ; beforeEach ( ( ) => { taskTrackingZoneSpec = new _TaskTrackingZoneSpec ( ) ; taskTrackingZone = Zone . current . fork ( taskTrackingZoneSpec ) ; } ) ; it ( 'should track tasks' , ( done ) => { taskTrackingZone . run ( ( ) => { taskTrackingZone . scheduleMicroTask ( 'test1' , ( ) => { } ) ; expect ( taskTrackingZoneSpec ! . microTasks . length ) . toBe ( 1 ) ; expect ( taskTrackingZoneSpec ! . microTasks [ 0 ] . source ) . toBe ( 'test1' ) ; setTimeout ( ( ) => { } ) ; expect ( taskTrackingZoneSpec ! . macroTasks . length ) . toBe ( 1 ) ; expect ( taskTrackingZoneSpec ! . macroTasks [ 0 ] . source ) . toBe ( 'setTimeout' ) ; taskTrackingZone . cancelTask ( taskTrackingZoneSpec ! . macroTasks [ 0 ] ) ; expect ( taskTrackingZoneSpec ! . macroTasks . length ) . toBe ( 0 ) ; setTimeout ( ( ) => { expect ( taskTrackingZoneSpec ! . macroTasks . length ) . toBe ( 0 ) ; expect ( taskTrackingZoneSpec ! . microTasks . length ) . toBe ( 0 ) ; if ( typeof global [ 'XMLHttpRequest' ] == 'undefined' ) return done ( ) ; const xhr = new XMLHttpRequest ( ) ; xhr . open ( 'get' , '/' , true ) ; xhr . onreadystatechange = ( ) => { if ( xhr . readyState == 4 ) { setTimeout ( ( ) => { expect ( taskTrackingZoneSpec ! . macroTasks . length ) . toBe ( 0 ) ; expect ( taskTrackingZoneSpec ! . microTasks . length ) . toBe ( 0 ) ; if ( supportPatchXHROnProperty ( ) ) { expect ( taskTrackingZoneSpec ! . eventTasks . length ) . not . toBe ( 0 ) ; } taskTrackingZoneSpec ! . clearEvents ( ) ; expect ( taskTrackingZoneSpec ! . eventTasks . length ) . toBe ( 0 ) ; done ( ) ; } ) ; } } ; xhr . send ( ) ; expect ( taskTrackingZoneSpec ! . macroTasks . length ) . toBe ( 1 ) ; expect ( taskTrackingZoneSpec ! . macroTasks [ 0 ] . source ) . toBe ( 'XMLHttpRequest.send' ) ; if ( supportPatchXHROnProperty ( ) ) { expect ( taskTrackingZoneSpec ! . eventTasks [ 0 ] . source ) . toMatch ( /\.addEventListener:readystatechange/ ) ; } } ) ; } ) ; } ) ; it ( 'should capture task creation stacktrace' , ( done ) => { taskTrackingZone . run ( ( ) => { setTimeout ( ( ) => { done ( ) ; } ) ; expect ( ( taskTrackingZoneSpec ! . macroTasks [ 0 ] as any ) [ 'creationLocation' ] ) . toBeTruthy ( ) ; } ) ; } ) ; } ) ;