|
| 1 | +export type RecursivePartial<T> = Partial< |
| 2 | + { |
| 3 | + [key in keyof T]: T[key] extends (...a: Array<infer U>) => unknown |
| 4 | + ? (...a: Array<U>) => RecursivePartial<ReturnType<T[key]>> | ReturnType<T[key]> // tslint:disable-line |
| 5 | + : T[key] extends Array<unknown> ? Array<RecursivePartial<T[key][number]>> |
| 6 | + : RecursivePartial<T[key]> | T[key]; |
| 7 | + } |
| 8 | +>; |
| 9 | + |
| 10 | +/** |
| 11 | + * Mock object with generated stubs. |
| 12 | + * |
| 13 | + * There is no guarantee whether a property is actually a stub or not as it can be overridden at creation time. |
| 14 | + */ |
| 15 | +export type IMock<TObject extends MockableObject, TStub> = TObject & { [P in keyof TObject]: TStub }; |
| 16 | + |
| 17 | +/** |
| 18 | + * Generate new mock class using given {@param stubFunction} to generate stubs |
| 19 | + */ |
| 20 | +export function createMockImplementationWithStubFunction<TStub>(stubFunction: () => TStub): MockPrototype<TStub> { |
| 21 | + return class Mock { |
| 22 | + /** |
| 23 | + * Generate a new mock for the given class. |
| 24 | + * |
| 25 | + * @param clazz the class to generate a mock for. |
| 26 | + * @param overrides can be used to set properties. They will not be replaced with stubs. |
| 27 | + */ |
| 28 | + public static of<TObject extends MockableObject>( |
| 29 | + clazz: Clazz<TObject> = null, |
| 30 | + overrides: RecursivePartial<TObject> = {}, |
| 31 | + ): IMock<TObject, TStub> { |
| 32 | + return new Proxy<IMock<TObject, TStub>>(overrides as any, { |
| 33 | + get(target: IMock<TObject, TStub>, key: PropertyKey) { |
| 34 | + // Angular tries to create a Set of a provided value via the following code |
| 35 | + // |
| 36 | + // ```javascript |
| 37 | + // var QUOTED_KEYS = '$quoted$'; |
| 38 | + // var quotedSet = new Set(map && map[QUOTED_KEYS]); |
| 39 | + // ``` |
| 40 | + // |
| 41 | + // This fails when $quoted$ is a stub, so we explicitly return undefined here |
| 42 | + if (key === "$quoted$") { |
| 43 | + return undefined; |
| 44 | + } |
| 45 | + // TODO: allow configuration of Mock as a Promise |
| 46 | + if (key === "then") { |
| 47 | + return undefined; |
| 48 | + } |
| 49 | + const name: keyof TObject = key as any; |
| 50 | + if (target[name] === undefined) { |
| 51 | + target[name] = stubFunction() as any; |
| 52 | + } |
| 53 | + return target[name]; |
| 54 | + }, |
| 55 | + getPrototypeOf(): MockableObject | null { |
| 56 | + return clazz?.prototype ?? null; |
| 57 | + }, |
| 58 | + }); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Generate new mock without setting the prototype (for e.g. a Typescript interface) |
| 63 | + * |
| 64 | + * {@param overrides} can be used to set properties. They will not be replaced with stubs. |
| 65 | + */ |
| 66 | + public static with<TObject extends MockableObject>( |
| 67 | + overrides: RecursivePartial<TObject> = {}, |
| 68 | + ): IMock<TObject, TStub> { |
| 69 | + return Mock.of<TObject>(null, overrides); |
| 70 | + } |
| 71 | + }; |
| 72 | +} |
| 73 | + |
| 74 | +interface Clazz<T> extends Function { |
| 75 | + new(...args: any[]): T; |
| 76 | +} |
| 77 | + |
| 78 | +export interface MockPrototype<TStub> { |
| 79 | + /** |
| 80 | + * Generate a new mock for the given class. |
| 81 | + * |
| 82 | + * @param clazz the class to generate a mock for. |
| 83 | + * @param overrides can be used to set properties. They will not be replaced with stubs. |
| 84 | + */ |
| 85 | + of<TObject extends MockableObject>( |
| 86 | + clazz: Clazz<TObject>, |
| 87 | + overrides?: RecursivePartial<TObject>, |
| 88 | + ): IMock<TObject, TStub>; |
| 89 | + |
| 90 | + /** |
| 91 | + * Generate new mock without setting the prototype (for e.g. a Typescript interface) |
| 92 | + * |
| 93 | + * {@param overrides} can be used to set properties. They will not be replaced with stubs. |
| 94 | + */ |
| 95 | + with<TObject extends MockableObject>(overrides: RecursivePartial<TObject>): IMock<TObject, TStub>; |
| 96 | +} |
| 97 | + |
| 98 | +export type MockableObject = object; |
0 commit comments