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 a default order', async () => { |
50
|
|
|
// GIVEN |
51
|
|
|
const mock = jest.fn(); |
52
|
|
|
|
53
|
|
|
class Class { |
54
|
|
|
@Setup |
55
|
|
|
init() { |
56
|
|
|
mock(); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// WHEN |
61
|
|
|
const bean: any = new Class(); |
62
|
|
|
registeredMethods.set(bean.init, bean); |
63
|
|
|
registeredBeans.set('Class', bean); |
64
|
|
|
await flushPromises(); |
65
|
|
|
await Executor.execute(); |
66
|
|
|
|
67
|
|
|
// THEN |
68
|
|
|
expect(Executor.getTask(bean.init)?.order) |
69
|
|
|
.toBe(0); |
70
|
|
|
expect(mock).toHaveBeenCalled(); |
71
|
|
|
}); |
72
|
|
|
|
73
|
|
|
it('sets a negative order', async () => { |
74
|
|
|
// GIVEN |
75
|
|
|
const mock = jest.fn(); |
76
|
|
|
|
77
|
|
|
class Class { |
78
|
|
|
@Setup |
79
|
|
|
@Order(-1) |
80
|
|
|
init() { |
81
|
|
|
mock(); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// WHEN |
86
|
|
|
const bean: any = new Class(); |
87
|
|
|
registeredMethods.set(bean.init, bean); |
88
|
|
|
registeredBeans.set('Class', bean); |
89
|
|
|
await flushPromises(); |
90
|
|
|
await Executor.execute(); |
91
|
|
|
|
92
|
|
|
// THEN |
93
|
|
|
expect(Executor.getTask(bean.init)?.order) |
94
|
|
|
.toBe(-1); |
95
|
|
|
expect(mock).toHaveBeenCalled(); |
96
|
|
|
}); |
97
|
|
|
|
98
|
|
|
it('fails to set a order on a generic function', async () => { |
99
|
|
|
// GIVEN |
100
|
|
|
const mock = jest.fn(); |
101
|
|
|
|
102
|
|
|
class Class { |
103
|
|
|
@Order(1) |
104
|
|
|
init() { |
105
|
|
|
mock(); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
// WHEN |
110
|
|
|
const bean: any = new Class(); |
111
|
|
|
registeredMethods.set(bean.init, bean); |
112
|
|
|
registeredBeans.set('Class', bean); |
113
|
|
|
await flushPromises(); |
114
|
|
|
Executor.on('error', (error) => { |
115
|
|
|
expect(error).toEqual(new Error('Method not registered for execution')); |
116
|
|
|
}); |
117
|
|
|
Executor.startLifecycle(); |
118
|
|
|
await Executor.execution; |
119
|
|
|
await flushPromises(); |
120
|
|
|
|
121
|
|
|
// THEN |
122
|
|
|
expect(mock).not.toHaveBeenCalled(); |
123
|
|
|
expect.assertions(2); |
124
|
|
|
}); |
125
|
|
|
}); |
126
|
|
|
|