-
Notifications
You must be signed in to change notification settings - Fork 0
/
MockMockLibrary.ts
166 lines (128 loc) · 4.13 KB
/
MockMockLibrary.ts
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
type PropertySetupOperations<T> = {
returns(t: T): void;
ofType<R extends T>(t: new(...args) => R): PropertySetup<R>;
setup(sf: (st: ObjectSetup<T>) => void): void
}
type PropertySetup<T> =
ObjectSetup<T> &
(T extends (...args: any) => any ? (((...args: Parameters<T>) => PropertySetup<ReturnType<T>>)) : ObjectSetup<T>) &
PropertySetupOperations<T>;
type ObjectSetup<O> = {
[field in keyof O]: PropertySetup<O[field]>;
};
function CollapseValueHolders(vh) {
let heldVal = vh?._v;
if(heldVal instanceof MockBlueprint) {
return heldVal.compile();
} else {
return heldVal;
}
}
class MockBlueprint extends Function {
constructor() {
super();
}
compile(): any {}
};
class MockValueBlueprint extends MockBlueprint {
public v: any;
constructor() {
super();
}
compile() {
if(this.v instanceof MockBlueprint) {
return this.v.compile();
} else {
return this.v;
}
}
blueprintValue<T extends MockBlueprint>(Ttor: new () => T): T {
if(!this.v) {
this.v = new Ttor();
}
if(! (this.v instanceof Ttor)) {
throw new Error(`Expecting a blueprint of type ${Ttor.name}`)
}
return this.v;
}
}
class MockObjectBlueprint extends MockBlueprint {
public v: { [index: string | symbol]: MockValueBlueprint } = {};
public ofType: new (...args) => any;
constructor() {
super();
}
compile() {
const ret = {};
Object.entries(this.v).forEach(([k, v]) => {
ret[k] = v.compile();
});
return this.ofType ?
this.createTypedProxy(ret, this.ofType) :
ret;
}
private createTypedProxy(data: any, type: new (...args: any) => any) {
return new Proxy(data, {
get(target, p) { return target[p]; },
apply(target, thisArg, args) { return target.apply(thisArg, args); },
getPrototypeOf(target) { return type.prototype; }
});
}
}
class MockFunctionBlueprint extends MockBlueprint {
public dispatchTable: { [serializedArgs: string]: MockValueBlueprint } = {};
constructor() {
super();
}
compile() {
return (...args: any) => {
return this.dispatchTable[JSON.stringify(args)].compile();
}
};
}
type PropertySetupOperationHandlersInterface = {
[oper in keyof PropertySetupOperations<any>]:
(proxyBuilder, valueHolder: MockValueBlueprint, ...args: Parameters<PropertySetupOperations<any>[oper]>) =>
ReturnType<PropertySetupOperations<any>[oper]>;
}
const PropertySetupOperationHandlers: PropertySetupOperationHandlersInterface = {
returns(proxyBuilder, valueHolder: MockValueBlueprint, retVal) {
valueHolder.v = retVal;
},
ofType(proxyBuilder, valueHolder: MockValueBlueprint, ctor) {
const objectBlueprint = valueHolder.blueprintValue(MockObjectBlueprint);
objectBlueprint.ofType = ctor;
return proxyBuilder;
},
setup(proxyBuilder, valueHolder: MockValueBlueprint, setupFunc) {
setupFunc(proxyBuilder);
},
};
function CreateSetupProxy(valueHolder: MockValueBlueprint) {
const proxyBuilder = new Proxy(valueHolder, {
get(target, propertyName) {
if(PropertySetupOperationHandlers[propertyName]) {
return (...args: any) => (PropertySetupOperationHandlers[propertyName] as (...any) => any).apply(null, [proxyBuilder, target, ...args]);
} else {
const objectBlueprint = target.blueprintValue(MockObjectBlueprint);
if(!objectBlueprint.v[propertyName]) {
objectBlueprint.v[propertyName] = new MockValueBlueprint();
}
return CreateSetupProxy(objectBlueprint.v[propertyName]);
}
},
apply(target, thisArg, args) {
const invokeResultHolder = new MockValueBlueprint();
const fnBlueprint = target.blueprintValue(MockFunctionBlueprint);
fnBlueprint.dispatchTable[JSON.stringify(args)] = invokeResultHolder;
return CreateSetupProxy(invokeResultHolder);
}
});
return proxyBuilder;
}
export function mock<T>(setupFunction: (d: ObjectSetup<T>) => void): T {
const mockData = new MockValueBlueprint();
const objSetup = CreateSetupProxy(mockData);
setupFunction(objSetup);
return mockData.compile();
}