Passed
Push — main ( a24599...473189 )
by Lorenzo
01:33 queued 37s
created

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

Complexity

Total Complexity 2
Complexity/F 1

Size

Lines of Code 68
Function Count 2

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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

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
5
jest.mock('@/core', () => ({
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