Passed
Push — main ( 2cd17f...238ec8 )
by Lorenzo
01:26 queued 16s
created

src/core/decorators/Order.spec.ts   A

Complexity

Total Complexity 7
Complexity/F 1.75

Size

Lines of Code 126
Function Count 4

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 94
mnd 3
bc 3
fnc 4
dl 0
loc 126
bpm 0.75
cpm 1.75
noi 0
c 0
b 0
f 0
rs 10

1 Function

Rating   Name   Duplication   Size   Complexity  
A Class.init 0 4 2
1
import { flushPromises } from '@test/utils/testUtils';
2
import { registeredBeans, registeredMethods } from '@/core';
3
import { Order, Setup } from '@/main';
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('Order.ts', () => {
17
  beforeEach(() => {
18
    jest.resetAllMocks();
19
    registeredMethods.clear();
20
    registeredBeans.clear();
21
    Executor.stopLifecycle();
22
  });
23
24
  it('sets a hook order', async () => {
25
    // GIVEN
26
    const mock = jest.fn();
27
28
    class Class {
29
      @Setup
30
      @Order(1)
31
      init() {
32
        mock();
33
      }
34
    }
35
36
    // WHEN
37
    const bean: any = new Class();
38
    registeredMethods.set(bean.init, bean);
39
    registeredBeans.set('Class', bean);
40
    await flushPromises();
41
    await Executor.execute();
42
43
    // THEN
44
    expect(Executor.getTask(bean.init)?.order)
45
      .toBe(1);
46
    expect(mock).toHaveBeenCalled();
47
  });
48
49
  it('sets a default order', async () => {
50
    // GIVEN
51
    const mock = jest.fn();
52
53
    class Class {
54
      @Setup
55
      init() {
56
        mock();
57
      }
58
    }
59
60
    // WHEN
61
    const bean: any = new Class();
62
    registeredMethods.set(bean.init, bean);
63
    registeredBeans.set('Class', bean);
64
    await flushPromises();
65
    await Executor.execute();
66
67
    // THEN
68
    expect(Executor.getTask(bean.init)?.order)
69
      .toBe(0);
70
    expect(mock).toHaveBeenCalled();
71
  });
72
73
  it('sets a negative order', async () => {
74
    // GIVEN
75
    const mock = jest.fn();
76
77
    class Class {
78
      @Setup
79
      @Order(-1)
80
      init() {
81
        mock();
82
      }
83
    }
84
85
    // WHEN
86
    const bean: any = new Class();
87
    registeredMethods.set(bean.init, bean);
88
    registeredBeans.set('Class', bean);
89
    await flushPromises();
90
    await Executor.execute();
91
92
    // THEN
93
    expect(Executor.getTask(bean.init)?.order)
94
      .toBe(-1);
95
    expect(mock).toHaveBeenCalled();
96
  });
97
98
  it('fails to set a order on a generic function', async () => {
99
    // GIVEN
100
    const mock = jest.fn();
101
102
    class Class {
103
      @Order(1)
104
      init() {
105
        mock();
106
      }
107
    }
108
109
    // WHEN
110
    const bean: any = new Class();
111
    registeredMethods.set(bean.init, bean);
112
    registeredBeans.set('Class', bean);
113
    await flushPromises();
114
    Executor.on('error', (error) => {
115
      expect(error).toEqual(new Error('Method not registered for execution'));
116
    });
117
    Executor.startLifecycle();
118
    await Executor.execution;
119
    await flushPromises();
120
121
    // THEN
122
    expect(mock).not.toHaveBeenCalled();
123
    expect.assertions(2);
124
  });
125
});
126