Skip to content

Instantly share code, notes, and snippets.

@Devcon4
Created July 16, 2018 23:30
Show Gist options
  • Save Devcon4/bcf2ed9d544bda6ec313f20175f52cc5 to your computer and use it in GitHub Desktop.
Save Devcon4/bcf2ed9d544bda6ec313f20175f52cc5 to your computer and use it in GitHub Desktop.
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