Last active
August 7, 2018 16:41
-
-
Save Devcon4/056d8f2db15de76989225f400f222085 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
export type Spied<T> = { | |
[K in keyof T]: T[K] extends Function ? jasmine.Spy : T[K] extends object ? Spied<T[K]> : T[K]; | |
}; | |
export function spyOnClass<T, U1 extends unknown[]>(type: new (...tArg1: U1) => T, ...arg1: U1) { | |
let spyOnFunctions = function <W>(obj: W): Spied<W> { | |
Object.keys(obj).forEach(p => { | |
if (!!obj && !arg1.some(a => obj[p] === a)) { | |
if (obj[p] instanceof Function) { | |
spyOn(obj, p as keyof W); | |
} | |
else if (!!obj[p] && typeof obj[p] === 'object') { | |
obj[p] = spyOnFunctions(obj[p]); | |
} | |
} | |
}); | |
return obj as Spied<W>; | |
}; | |
let ctor = new type(...arg1 as U1); | |
return spyOnFunctions(ctor); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment