import { spy as sinonSpy } from "sinon" ; import { ISerializedRequest , ISerializedResponse } from "../../interfaces" ; import decorateSpy from "./decorate" ; import { ICallTracker , ServiceSpy } from "./types" ; /**
 * Container for tracking spy calls via `track` method,
 * calls are exposed via `spy` property.
 */ class CallTracker { param gen private static buildSpy ( gen ) { const bareSpy = sinonSpy ( gen ) ; return decorateSpy ( bareSpy ) ; } public readonly spy : ServiceSpy ; private returnValue ? : ISerializedResponse ; constructor ( ) { this . spy = CallTracker . buildSpy ( this . spyFn . bind ( this ) ) ; } public track ( { req , res , } ) { this . returnValue = res ; this . spy ( req ) ; this . returnValue = undefined ; } public reset ( ) { this . spy . resetHistory ( ) ; } private spyFn ( _ ) { if ( typeof this . returnValue === "undefined" ) { throw Error ( "Undefined return value" ) ; } return this . returnValue ; } } export * from "./types" ; export const createCallTracker = ( ) : ICallTracker => new CallTracker ( ) ;