license import { after , before , } from 'mocha-sugar-free' ; import { SinonStub , stub , } from 'sinon' ; import * as createMockRAF from 'mock-raf' ; export interface MockRAF { now ( ) : number ; raf : typeof requestAnimationFrame ; cancel : typeof cancelAnimationFrame ; step ( options ? ) : void ; } export type MockedRAFClosure = ( mockRAF : MockRAF ) => void ; /**
 * Replaces `window.requestAnimationFrame`, `performance.now`, and `Date.now`
 * with mocks for the duration of a mocha testing suite.
 *
 * The two `now` methods will only be incremented when `mockRAF.step` is called.
 */ export default function useMockedRAF ( closure ) { return ( ) => { const mockRAF = createMockRAF ( ) ; const initialTime = Date . now ( ) ; function mockNow ( ) { return initialTime + mockRAF . now ( ) ; } before ( ( ) => { stub ( window , 'requestAnimationFrame' ) . callsFake ( mockRAF . raf ) ; stub ( window , 'cancelAnimationFrame' ) . callsFake ( mockRAF . cancel ) ; stub ( Date , 'now' ) . callsFake ( mockNow ) ; stub ( performance , 'now' ) . callsFake ( mockRAF . now ) ; } ) ; after ( ( ) => { ( window . requestAnimationFrame as SinonStub ) . restore ( ) ; ( window . cancelAnimationFrame as SinonStub ) . restore ( ) ; ( Date . now as SinonStub ) . restore ( ) ; ( performance . now as SinonStub ) . restore ( ) ; } ) ; return closure ( mockRAF ) ; } ; } ;