-
Notifications
You must be signed in to change notification settings - Fork 568
/
mockService.ts
227 lines (201 loc) 路 5.59 KB
/
mockService.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import {
IMidwayApplication,
IMidwayContainer,
IMidwayContext,
ISimulation,
ScopeEnum,
} from '../interface';
import {
Destroy,
Init,
listModule,
Provide,
Scope,
MOCK_KEY,
} from '../decorator';
@Provide()
@Scope(ScopeEnum.Singleton)
export class MidwayMockService {
protected mocks = [];
protected contextMocks: Array<{
app: IMidwayApplication;
key: string | ((ctx: IMidwayContext) => void);
value: any;
}> = [];
protected cache = new Map();
protected simulatorList: Array<ISimulation> = [];
constructor(readonly applicationContext: IMidwayContainer) {}
@Init()
async init() {
if (MidwayMockService.prepareMocks.length > 0) {
for (const item of MidwayMockService.prepareMocks) {
this.mockProperty(item.obj, item.key, item.value);
}
MidwayMockService.prepareMocks = [];
}
}
static prepareMocks = [];
static mockClassProperty(
clzz: new (...args) => any,
propertyName: string,
value: any
) {
this.mockProperty(clzz.prototype, propertyName, value);
}
static mockProperty(obj: new (...args) => any, key: string, value: any) {
this.prepareMocks.push({
obj,
key,
value,
});
}
public mockClassProperty(
clzz: new (...args) => any,
propertyName: string,
value: any
) {
return this.mockProperty(clzz.prototype, propertyName, value);
}
public mockProperty(obj: any, key: string, value) {
// eslint-disable-next-line no-prototype-builtins
const hasOwnProperty = obj.hasOwnProperty(key);
this.mocks.push({
obj,
key,
descriptor: Object.getOwnPropertyDescriptor(obj, key),
// Make sure the key exists on object not the prototype
hasOwnProperty,
});
// Delete the origin key, redefine it below
if (hasOwnProperty) {
delete obj[key];
}
// Set a flag that checks if it is mocked
let flag = this.cache.get(obj);
if (!flag) {
flag = new Set();
this.cache.set(obj, flag);
}
flag.add(key);
const descriptor = this.overridePropertyDescriptor(value);
Object.defineProperty(obj, key, descriptor);
}
public mockContext(
app: IMidwayApplication,
key: string | ((ctx: IMidwayContext) => void),
value?: PropertyDescriptor | any
) {
this.contextMocks.push({
app,
key,
value,
});
}
@Destroy()
restore() {
for (let i = this.mocks.length - 1; i >= 0; i--) {
const m = this.mocks[i];
if (!m.hasOwnProperty) {
// Delete the mock key, use key on the prototype
delete m.obj[m.key];
} else {
// Redefine the origin key instead of the mock key
Object.defineProperty(m.obj, m.key, m.descriptor);
}
}
this.mocks = [];
this.contextMocks = [];
this.cache.clear();
this.simulatorList = [];
MidwayMockService.prepareMocks = [];
}
isMocked(obj, key) {
const flag = this.cache.get(obj);
return flag ? flag.has(key) : false;
}
applyContextMocks(app: IMidwayApplication, ctx: IMidwayContext) {
if (this.contextMocks.length > 0) {
for (const mockItem of this.contextMocks) {
if (mockItem.app === app) {
const descriptor = this.overridePropertyDescriptor(mockItem.value);
if (typeof mockItem.key === 'string') {
Object.defineProperty(ctx, mockItem.key, descriptor);
} else {
mockItem.key(ctx);
}
}
}
}
}
getContextMocksSize() {
return this.contextMocks.length;
}
private overridePropertyDescriptor(value) {
const descriptor = {
configurable: true,
enumerable: true,
} as PropertyDescriptor;
if (value && (value.get || value.set)) {
// Default to undefined
descriptor.get = value.get;
descriptor.set = value.set;
} else {
// Without getter/setter mode
descriptor.value = value;
descriptor.writable = true;
}
return descriptor;
}
public async initSimulation() {
const simulationModule: Array<new (...args) => ISimulation> =
listModule(MOCK_KEY);
for (const module of simulationModule) {
const instance = await this.applicationContext.getAsync(module);
if (await instance.enableCondition()) {
this.simulatorList.push(instance);
}
}
}
public async runSimulatorSetup() {
for (const simulator of this.simulatorList) {
await simulator.setup?.();
}
}
public async runSimulatorTearDown() {
// reverse loop and not change origin simulatorList
for (let i = this.simulatorList.length - 1; i >= 0; i--) {
const simulator = this.simulatorList[i];
await simulator.tearDown?.();
}
}
public async runSimulatorAppSetup(app: IMidwayApplication) {
for (const simulator of this.simulatorList) {
await simulator.appSetup?.(app);
}
}
public async runSimulatorAppTearDown(app: IMidwayApplication) {
// reverse loop and not change origin simulatorList
for (let i = this.simulatorList.length - 1; i >= 0; i--) {
const simulator = this.simulatorList[i];
await simulator.appTearDown?.(app);
}
}
public async runSimulatorContextSetup(
ctx: IMidwayContext,
app: IMidwayApplication
) {
for (const simulator of this.simulatorList) {
await simulator.contextSetup?.(ctx, app);
}
}
public async runSimulatorContextTearDown(
ctx: IMidwayContext,
app: IMidwayApplication
) {
// reverse loop and not change origin simulatorList
for (let i = this.simulatorList.length - 1; i >= 0; i--) {
const simulator = this.simulatorList[i];
await simulator?.contextTearDown?.(ctx, app);
}
}
}