src/hooks/decorators/Setup.spec.ts   A
last analyzed

Complexity

Total Complexity 2
Complexity/F 1

Size

Lines of Code 72
Function Count 2

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 59
mnd 0
bc 0
fnc 2
dl 0
loc 72
rs 10
bpm 0
cpm 1
noi 0
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A Class.init 0 5 1
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