interface Frame { id : number ; fn : FrameRequestCallback ; } interface MockRaf { lastFrameId : number ; pendingFrames : Frame [ ] ; flush : ( ) => void ; restore : ( ) => void ; requestAnimationFrame : ( fn : FrameRequestCallback ) => number ; cancelAnimationFrame : ( id : number ) => void ; } export function createMockRaf ( ) { const origRaf = window . requestAnimationFrame ; const origCancel = window . cancelAnimationFrame ; const mockRaf = { lastFrameId : 0 , pendingFrames : [ ] , flush ( ) { const framesToRun = this . pendingFrames . slice ( ) ; while ( framesToRun . length ) { const frame = framesToRun . shift ( ) ; if ( ! frame ) return ; const { id , fn } = frame ; fn ( id ) ; cancelAnimationFrame ( id ) ; } } , restore ( ) { this . lastFrameId = 0 ; this . pendingFrames = [ ] ; window . requestAnimationFrame = origRaf ; window . cancelAnimationFrame = origCancel ; } , requestAnimationFrame ( fn ) { const frameId = ++ this . lastFrameId ; this . pendingFrames . push ( { id : frameId , fn } ) ; return frameId ; } , cancelAnimationFrame ( id ) { for ( let i = 0 , frame ; ( frame = this . pendingFrames [ i ] ) ; i ++ ) { if ( frame . id === id ) { this . pendingFrames . splice ( i , 1 ) ; return ; } } } , } ; window . requestAnimationFrame = ( fn ) => mockRaf . requestAnimationFrame ( fn ) ; window . cancelAnimationFrame = ( id ) => mockRaf . cancelAnimationFrame ( id ) ; return mockRaf ; }