Created
July 16, 2018 23:30
-
-
Save Devcon4/bcf2ed9d544bda6ec313f20175f52cc5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type funcPropNames<T> = { | |
[prop in keyof T]: T[prop] extends Function ? prop : never; | |
}[keyof T]; | |
| |
type FunctionProps<T> = Pick<T, funcPropNames<T>>; | |
| |
export type Spied<T> = { | |
[k in keyof T]: k extends FunctionProps<T> ? jasmine.Spy : T; | |
}; | |
| |
let spyOnClass = <T extends { new (...args: any[]): any }, U>(type: T, constructorArgs?: U) => { | |
let ctorArgs = Object.keys(constructorArgs).map(k => constructorArgs[k]); | |
let ctor = new type(...ctorArgs); | |
| |
let spyOnFunctions = (obj: Object) => { | |
if (obj instanceof Object) { | |
Object.keys(obj).forEach(p => { | |
if(!!obj) { | |
if (obj[p] instanceof Function) { | |
spyOn(obj, p as any); | |
} else { | |
obj[p] = spyOnFunctions(obj[p]); | |
} | |
} | |
}); | |
} | |
console.log(obj); | |
return obj; | |
} | |
| |
return spyOnFunctions(ctor) as Spied<T>; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment