1
|
|
|
import { flushPromises } from '@test/utils/testUtils'; |
2
|
|
|
import { registeredBeans, registeredMethods } from '@/core'; |
3
|
|
|
import { Setup } from '@/hooks/decorators/Setup'; |
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('Setup.ts', () => { |
17
|
|
|
beforeEach(() => { |
18
|
|
|
jest.resetAllMocks(); |
19
|
|
|
registeredMethods.clear(); |
20
|
|
|
registeredBeans.clear(); |
21
|
|
|
Executor.stopLifecycle(); |
22
|
|
|
}); |
23
|
|
|
|
24
|
|
|
it('execute a setup function', async () => { |
25
|
|
|
// GIVEN |
26
|
|
|
const mock = jest.fn(); |
27
|
|
|
class Class { |
28
|
|
|
initialized: boolean = false; |
29
|
|
|
|
30
|
|
|
@Setup |
31
|
|
|
init() { |
32
|
|
|
this.initialized = true; |
33
|
|
|
mock(); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
const bean: any = new Class(); |
37
|
|
|
registeredMethods.set(bean.init, bean); |
38
|
|
|
registeredBeans.set('Class', bean); |
39
|
|
|
await flushPromises(); |
40
|
|
|
await Executor.execute(); |
41
|
|
|
|
42
|
|
|
// THEN |
43
|
|
|
expect(bean.initialized) |
44
|
|
|
.toBe(true); |
45
|
|
|
expect(mock).toHaveBeenCalled(); |
46
|
|
|
}); |
47
|
|
|
|
48
|
|
|
it('execute a setup async function', async () => { |
49
|
|
|
// GIVEN |
50
|
|
|
const mock = jest.fn(); |
51
|
|
|
class Class { |
52
|
|
|
initialized: boolean = false; |
53
|
|
|
|
54
|
|
|
@Setup |
55
|
|
|
async init() { |
56
|
|
|
this.initialized = true; |
57
|
|
|
mock(); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
const bean: any = new Class(); |
61
|
|
|
registeredMethods.set(bean.init, bean); |
62
|
|
|
registeredBeans.set('Class', bean); |
63
|
|
|
await flushPromises(); |
64
|
|
|
await Executor.execute(); |
65
|
|
|
|
66
|
|
|
// THEN |
67
|
|
|
expect(bean.initialized) |
68
|
|
|
.toBe(true); |
69
|
|
|
expect(mock).toHaveBeenCalled(); |
70
|
|
|
}); |
71
|
|
|
}); |
72
|
|
|
|