forked from ffMathy/FluffySpoon.JavaScript.Testing.Faking
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransformations.ts
More file actions
48 lines (36 loc) · 1.9 KB
/
Copy pathTransformations.ts
File metadata and controls
48 lines (36 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { AllArguments } from "./Arguments";
export type NoArgumentFunctionSubstitute<TReturnType> =
(() => (TReturnType & NoArgumentMockObjectMixin<TReturnType>))
export type FunctionSubstitute<TArguments extends any[], TReturnType> =
((...args: TArguments) => (TReturnType & MockObjectMixin<TArguments, TReturnType>)) &
((allArguments: AllArguments) => (TReturnType & MockObjectMixin<TArguments, TReturnType>))
export type PropertySubstitute<TReturnType> = (TReturnType & Partial<NoArgumentMockObjectMixin<TReturnType>>);
type BaseMockObjectMixin<TReturnType> = {
returns: (...args: TReturnType[]) => void;
}
type NoArgumentMockObjectMixin<TReturnType> = BaseMockObjectMixin<TReturnType> & {
mimicks: (func: () => TReturnType) => void;
}
type MockObjectMixin<TArguments extends any[], TReturnType> = BaseMockObjectMixin<TReturnType> & {
mimicks: (func: (...args: TArguments) => TReturnType) => void;
}
export type ObjectSubstitute<T extends Object, K extends Object = T> = ObjectSubstituteTransformation<T> & {
received(amount?: number): TerminatingObject<K>;
didNotReceive(amount?: number): TerminatingObject<K>;
mimick(instance: T): void;
}
type TerminatingObject<T> = {
[P in keyof T]:
T[P] extends () => infer R ? () => void :
T[P] extends (...args: infer F) => infer R ? (...args: F) => void :
T[P];
}
type ObjectSubstituteTransformation<T extends Object> = {
[P in keyof T]:
T[P] extends () => infer R ? NoArgumentFunctionSubstitute<R> :
T[P] extends (...args: infer F) => infer R ? FunctionSubstitute<F, R> :
PropertySubstitute<T[P]>;
}
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
export type OmitProxyMethods<T extends any> = Omit<T, 'mimick'|'received'|'didNotReceive'>;
export type DisabledSubstituteObject<T> = T extends ObjectSubstitute<OmitProxyMethods<infer K>, infer K> ? K : never;