Total Complexity | 2 |
Complexity/F | 1 |
Lines of Code | 68 |
Function Count | 2 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | import { flushPromises } from '@test/utils/testUtils'; |
||
2 | import { registeredBeans, registeredMethods } from '@/decorators'; |
||
3 | import { Setup } from '@/decorators/Setup'; |
||
4 | |||
5 | jest.mock('@/decorators', () => ({ |
||
6 | registeredBeans: new Map(), |
||
7 | registeredMethods: new Map(), |
||
8 | logger: { |
||
9 | info: jest.fn(), |
||
10 | debug: jest.fn(), |
||
11 | error: jest.fn(), |
||
12 | }, |
||
13 | })); |
||
14 | |||
15 | describe('Setup.ts', () => { |
||
16 | beforeEach(() => { |
||
17 | jest.resetAllMocks(); |
||
18 | registeredMethods.clear(); |
||
19 | registeredBeans.clear(); |
||
20 | }); |
||
21 | |||
22 | it('execute a setup function', async () => { |
||
23 | // GIVEN |
||
24 | const mock = jest.fn(); |
||
25 | class Class { |
||
26 | initialized: boolean = false; |
||
27 | |||
28 | @Setup |
||
29 | init() { |
||
30 | this.initialized = true; |
||
31 | mock(); |
||
32 | } |
||
33 | } |
||
34 | const bean: any = new Class(); |
||
35 | registeredMethods.set(bean.init, bean); |
||
36 | registeredBeans.set('Class', bean); |
||
37 | await flushPromises(); |
||
38 | |||
39 | // THEN |
||
40 | expect(bean.initialized) |
||
41 | .toBe(true); |
||
42 | expect(mock).toHaveBeenCalled(); |
||
43 | }); |
||
44 | |||
45 | it('execute a setup async function', async () => { |
||
46 | // GIVEN |
||
47 | const mock = jest.fn(); |
||
48 | class Class { |
||
49 | initialized: boolean = false; |
||
50 | |||
51 | @Setup |
||
52 | async init() { |
||
53 | this.initialized = true; |
||
54 | mock(); |
||
55 | } |
||
56 | } |
||
57 | const bean: any = new Class(); |
||
58 | registeredMethods.set(bean.init, bean); |
||
59 | registeredBeans.set('Class', bean); |
||
60 | await flushPromises(); |
||
61 | |||
62 | // THEN |
||
63 | expect(bean.initialized) |
||
64 | .toBe(true); |
||
65 | expect(mock).toHaveBeenCalled(); |
||
66 | }); |
||
67 | }); |
||
68 |