|
1
|
|
|
import { flushPromises } from '@test/utils/testUtils'; |
|
2
|
|
|
import { registeredBeans, registeredMethods } from '@/core'; |
|
3
|
|
|
import { Order, Setup } from '@/main'; |
|
4
|
|
|
import { Executor } from '@/core/executor'; |
|
5
|
|
|
|
|
6
|
|
|
jest.mock('@/core', () => ({ |
|
7
|
|
|
registeredBeans: new Map(), |
|
8
|
|
|
registeredMethods: new Map(), |
|
9
|
|
|
logger: { |
|
10
|
|
|
info: jest.fn(), |
|
11
|
|
|
debug: jest.fn(), |
|
12
|
|
|
error: jest.fn(), |
|
13
|
|
|
}, |
|
14
|
|
|
})); |
|
15
|
|
|
|
|
16
|
|
|
describe('Order.ts', () => { |
|
17
|
|
|
beforeEach(() => { |
|
18
|
|
|
jest.resetAllMocks(); |
|
19
|
|
|
registeredMethods.clear(); |
|
20
|
|
|
registeredBeans.clear(); |
|
21
|
|
|
Executor.stopLifecycle(); |
|
22
|
|
|
}); |
|
23
|
|
|
|
|
24
|
|
|
it('sets a hook order', async () => { |
|
25
|
|
|
// GIVEN |
|
26
|
|
|
const mock = jest.fn(); |
|
27
|
|
|
|
|
28
|
|
|
class Class { |
|
29
|
|
|
@Setup |
|
30
|
|
|
@Order(1) |
|
31
|
|
|
init() { |
|
32
|
|
|
mock(); |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
// WHEN |
|
37
|
|
|
const bean: any = new Class(); |
|
38
|
|
|
registeredMethods.set(bean.init, bean); |
|
39
|
|
|
registeredBeans.set('Class', bean); |
|
40
|
|
|
await flushPromises(); |
|
41
|
|
|
await Executor.execute(); |
|
42
|
|
|
|
|
43
|
|
|
// THEN |
|
44
|
|
|
expect(Executor.getTask(bean.init)?.order) |
|
45
|
|
|
.toBe(1); |
|
46
|
|
|
expect(mock).toHaveBeenCalled(); |
|
47
|
|
|
}); |
|
48
|
|
|
|
|
49
|
|
|
it('sets an async hook order', async () => { |
|
50
|
|
|
// GIVEN |
|
51
|
|
|
const mock = jest.fn(); |
|
52
|
|
|
|
|
53
|
|
|
class Class { |
|
54
|
|
|
@Setup |
|
55
|
|
|
@Order(1) |
|
56
|
|
|
async init() { |
|
57
|
|
|
mock(); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
// WHEN |
|
62
|
|
|
const bean: any = new Class(); |
|
63
|
|
|
registeredMethods.set(bean.init, bean); |
|
64
|
|
|
registeredBeans.set('Class', bean); |
|
65
|
|
|
await flushPromises(); |
|
66
|
|
|
await Executor.execute(); |
|
67
|
|
|
|
|
68
|
|
|
// THEN |
|
69
|
|
|
expect(Executor.getTask(bean.init)?.order) |
|
70
|
|
|
.toBe(1); |
|
71
|
|
|
expect(mock).toHaveBeenCalled(); |
|
72
|
|
|
}); |
|
73
|
|
|
|
|
74
|
|
|
it.each([ |
|
75
|
|
|
[1, 2, ['mock1', 'mock2']], |
|
76
|
|
|
[2, 1, ['mock2', 'mock1']], |
|
77
|
|
|
])('executes hooks in order %#', async (order1, order2, expected) => { |
|
78
|
|
|
// GIVEN |
|
79
|
|
|
const execution: string[] = []; |
|
80
|
|
|
const mock1 = jest.fn().mockImplementation(() => { |
|
81
|
|
|
execution.push('mock1'); |
|
82
|
|
|
}); |
|
83
|
|
|
const mock2 = jest.fn().mockImplementation(() => { |
|
84
|
|
|
execution.push('mock2'); |
|
85
|
|
|
}); |
|
86
|
|
|
|
|
87
|
|
|
class Class { |
|
88
|
|
|
@Setup |
|
89
|
|
|
@Order(order1) |
|
90
|
|
|
init1() { |
|
91
|
|
|
mock1(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
@Setup |
|
95
|
|
|
@Order(order2) |
|
96
|
|
|
init2() { |
|
97
|
|
|
mock2(); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
// WHEN |
|
102
|
|
|
const bean: any = new Class(); |
|
103
|
|
|
registeredMethods.set(bean.init1, bean); |
|
104
|
|
|
registeredMethods.set(bean.init2, bean); |
|
105
|
|
|
registeredBeans.set('Class', bean); |
|
106
|
|
|
await flushPromises(); |
|
107
|
|
|
await Executor.execute(); |
|
108
|
|
|
|
|
109
|
|
|
// THEN |
|
110
|
|
|
expect(mock1).toHaveBeenCalled(); |
|
111
|
|
|
expect(mock2).toHaveBeenCalled(); |
|
112
|
|
|
expect(execution).toEqual(expected); |
|
113
|
|
|
}); |
|
114
|
|
|
|
|
115
|
|
|
it.each([ |
|
116
|
|
|
[1, 2, ['mock1', 'mock2']], |
|
117
|
|
|
[2, 1, ['mock2', 'mock1']], |
|
118
|
|
|
])('executes async hooks in order %#', async (order1, order2, expected) => { |
|
119
|
|
|
// GIVEN |
|
120
|
|
|
const execution: string[] = []; |
|
121
|
|
|
const mock1 = jest.fn().mockImplementation(async () => { |
|
122
|
|
|
await flushPromises(); |
|
123
|
|
|
execution.push('mock1'); |
|
124
|
|
|
}); |
|
125
|
|
|
const mock2 = jest.fn().mockImplementation(async () => { |
|
126
|
|
|
await flushPromises(); |
|
127
|
|
|
execution.push('mock2'); |
|
128
|
|
|
}); |
|
129
|
|
|
|
|
130
|
|
|
class Class { |
|
131
|
|
|
@Setup |
|
132
|
|
|
@Order(order1) |
|
133
|
|
|
async init1() { |
|
134
|
|
|
await mock1(); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
@Setup |
|
138
|
|
|
@Order(order2) |
|
139
|
|
|
async init2() { |
|
140
|
|
|
await mock2(); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
// WHEN |
|
145
|
|
|
const bean: any = new Class(); |
|
146
|
|
|
registeredMethods.set(bean.init1, bean); |
|
147
|
|
|
registeredMethods.set(bean.init2, bean); |
|
148
|
|
|
registeredBeans.set('Class', bean); |
|
149
|
|
|
await flushPromises(); |
|
150
|
|
|
await Executor.execute(); |
|
151
|
|
|
|
|
152
|
|
|
// THEN |
|
153
|
|
|
expect(mock1).toHaveBeenCalled(); |
|
154
|
|
|
expect(mock2).toHaveBeenCalled(); |
|
155
|
|
|
expect(execution).toEqual(expected); |
|
156
|
|
|
}); |
|
157
|
|
|
|
|
158
|
|
|
it('sets a default order', async () => { |
|
159
|
|
|
// GIVEN |
|
160
|
|
|
const mock = jest.fn(); |
|
161
|
|
|
|
|
162
|
|
|
class Class { |
|
163
|
|
|
@Setup |
|
164
|
|
|
init() { |
|
165
|
|
|
mock(); |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
// WHEN |
|
170
|
|
|
const bean: any = new Class(); |
|
171
|
|
|
registeredMethods.set(bean.init, bean); |
|
172
|
|
|
registeredBeans.set('Class', bean); |
|
173
|
|
|
await flushPromises(); |
|
174
|
|
|
await Executor.execute(); |
|
175
|
|
|
|
|
176
|
|
|
// THEN |
|
177
|
|
|
expect(Executor.getTask(bean.init)?.order) |
|
178
|
|
|
.toBe(0); |
|
179
|
|
|
expect(mock).toHaveBeenCalled(); |
|
180
|
|
|
}); |
|
181
|
|
|
|
|
182
|
|
|
it('sets a negative order', async () => { |
|
183
|
|
|
// GIVEN |
|
184
|
|
|
const mock = jest.fn(); |
|
185
|
|
|
|
|
186
|
|
|
class Class { |
|
187
|
|
|
@Setup |
|
188
|
|
|
@Order(-1) |
|
189
|
|
|
init() { |
|
190
|
|
|
mock(); |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
// WHEN |
|
195
|
|
|
const bean: any = new Class(); |
|
196
|
|
|
registeredMethods.set(bean.init, bean); |
|
197
|
|
|
registeredBeans.set('Class', bean); |
|
198
|
|
|
await flushPromises(); |
|
199
|
|
|
await Executor.execute(); |
|
200
|
|
|
|
|
201
|
|
|
// THEN |
|
202
|
|
|
expect(Executor.getTask(bean.init)?.order) |
|
203
|
|
|
.toBe(-1); |
|
204
|
|
|
expect(mock).toHaveBeenCalled(); |
|
205
|
|
|
}); |
|
206
|
|
|
|
|
207
|
|
|
it('fails to set a order on a generic function', async () => { |
|
208
|
|
|
// GIVEN |
|
209
|
|
|
const mock = jest.fn(); |
|
210
|
|
|
|
|
211
|
|
|
class Class { |
|
212
|
|
|
@Order(1) |
|
213
|
|
|
init() { |
|
214
|
|
|
mock(); |
|
215
|
|
|
} |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
// WHEN |
|
219
|
|
|
const bean: any = new Class(); |
|
220
|
|
|
registeredMethods.set(bean.init, bean); |
|
221
|
|
|
registeredBeans.set('Class', bean); |
|
222
|
|
|
await flushPromises(); |
|
223
|
|
|
Executor.on('error', (error) => { |
|
224
|
|
|
expect(error).toEqual(new Error('Method not registered for execution')); |
|
225
|
|
|
}); |
|
226
|
|
|
Executor.startLifecycle(); |
|
227
|
|
|
await Executor.execution; |
|
228
|
|
|
await flushPromises(); |
|
229
|
|
|
|
|
230
|
|
|
// THEN |
|
231
|
|
|
expect(mock).not.toHaveBeenCalled(); |
|
232
|
|
|
expect.assertions(2); |
|
233
|
|
|
}); |
|
234
|
|
|
}); |
|
235
|
|
|
|