Passed
Push — develop ( 753300...72aeea )
by Lorenzo
01:40 queued 13s
created

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

Complexity

Total Complexity 2
Complexity/F 1

Size

Lines of Code 74
Function Count 2

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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

1 Function

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