Passed
Pull Request — master (#148)
by Mathieu
03:17
created

server/src/Application/HumanResource/Leave/Command/CreateLeaveRequestCommandHandler.spec.ts   A

Complexity

Total Complexity 2
Complexity/F 0

Size

Lines of Code 189
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 149
mnd 2
bc 2
fnc 0
dl 0
loc 189
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
rs 10
1
import { mock, instance, when, verify, deepEqual, anything } from 'ts-mockito';
2
import { DoesLeaveRequestExistForPeriod } from 'src/Domain/HumanResource/Leave/Specification/DoesLeaveRequestExistForPeriod';
3
import { CreateLeaveRequestCommandHandler } from './CreateLeaveRequestCommandHandler';
4
import { CreateLeaveRequestCommand } from './CreateLeaveRequestCommand';
5
import { User } from 'src/Domain/HumanResource/User/User.entity';
6
import {
7
  Type,
8
  LeaveRequest
9
} from 'src/Domain/HumanResource/Leave/LeaveRequest.entity';
10
import { LeaveRequestAlreadyExistForThisPeriodException } from 'src/Domain/HumanResource/Leave/Exception/LeaveRequestAlreadyExistForThisPeriodException';
11
import { DoesEventsExistForPeriod } from 'src/Domain/FairCalendar/Specification/DoesEventsExistForPeriod';
12
import { EventsAlreadyExistForThisPeriodException } from 'src/Domain/FairCalendar/Exception/EventsAlreadyExistForThisPeriodException';
13
import { LeaveRequestRepository } from 'src/Infrastructure/HumanResource/Leave/Repository/LeaveRequestRepository';
14
15
describe('CreateLeaveRequestCommandHandler', () => {
16
  let leaveRequestRepository: LeaveRequestRepository;
17
  let doesLeaveRequestExistForPeriod: DoesLeaveRequestExistForPeriod;
18
  let doesEventsExistForPeriod: DoesEventsExistForPeriod;
19
  let handler: CreateLeaveRequestCommandHandler;
20
21
  const user = mock(User);
22
  const command = new CreateLeaveRequestCommand(
23
    instance(user),
24
    Type.PAID,
25
    '2019-01-04',
26
    true,
27
    '2019-01-06',
28
    true,
29
    'H&M wedding'
30
  );
31
32
  beforeEach(() => {
33
    leaveRequestRepository = mock(LeaveRequestRepository);
34
    doesLeaveRequestExistForPeriod = mock(DoesLeaveRequestExistForPeriod);
35
    doesEventsExistForPeriod = mock(DoesEventsExistForPeriod);
36
37
    handler = new CreateLeaveRequestCommandHandler(
38
      instance(leaveRequestRepository),
39
      instance(doesLeaveRequestExistForPeriod),
40
      instance(doesEventsExistForPeriod)
41
    );
42
  });
43
44
  it('testLeaveAlreadyExist', async () => {
45
    when(
46
      doesLeaveRequestExistForPeriod.isSatisfiedBy(
47
        instance(user),
48
        '2019-01-04',
49
        '2019-01-06'
50
      )
51
    ).thenResolve(true);
52
53
    try {
54
      await handler.execute(command);
55
    } catch (e) {
56
      expect(e).toBeInstanceOf(LeaveRequestAlreadyExistForThisPeriodException);
57
      expect(e.message).toBe(
58
        'human_resources.leaves.errors.already_exist_for_this_period'
59
      );
60
      verify(
61
        doesLeaveRequestExistForPeriod.isSatisfiedBy(
62
          instance(user),
63
          '2019-01-04',
64
          '2019-01-06'
65
        )
66
      ).once();
67
      verify(
68
        doesEventsExistForPeriod.isSatisfiedBy(
69
          instance(user),
70
          '2019-01-04',
71
          '2019-01-06'
72
        )
73
      ).never();
74
      verify(leaveRequestRepository.save(anything())).never();
75
    }
76
  });
77
78
  it('testEventsAlreadyExist', async () => {
79
    when(
80
      doesLeaveRequestExistForPeriod.isSatisfiedBy(
81
        instance(user),
82
        '2019-01-04',
83
        '2019-01-06'
84
      )
85
    ).thenResolve(false);
86
    when(
87
      doesEventsExistForPeriod.isSatisfiedBy(
88
        instance(user),
89
        '2019-01-04',
90
        '2019-01-06'
91
      )
92
    ).thenResolve(true);
93
94
    try {
95
      await handler.execute(command);
96
    } catch (e) {
97
      expect(e).toBeInstanceOf(EventsAlreadyExistForThisPeriodException);
98
      expect(e.message).toBe(
99
        'faircalendar.errors.events_already_exist_for_this_period'
100
      );
101
      verify(
102
        doesLeaveRequestExistForPeriod.isSatisfiedBy(
103
          instance(user),
104
          '2019-01-04',
105
          '2019-01-06'
106
        )
107
      ).once();
108
      verify(
109
        doesEventsExistForPeriod.isSatisfiedBy(
110
          instance(user),
111
          '2019-01-04',
112
          '2019-01-06'
113
        )
114
      ).once();
115
      verify(leaveRequestRepository.save(anything())).never();
116
    }
117
  });
118
119
  it('testCreateLeavesSuccessfully', async () => {
120
    const leave = mock(LeaveRequest);
121
    when(leave.getId()).thenReturn('cfdd06eb-cd71-44b9-82c6-46110b30ce05');
122
123
    when(
124
      doesLeaveRequestExistForPeriod.isSatisfiedBy(
125
        instance(user),
126
        '2019-01-04',
127
        '2019-01-06'
128
      )
129
    ).thenResolve(false);
130
    when(
131
      doesEventsExistForPeriod.isSatisfiedBy(
132
        instance(user),
133
        '2019-01-04',
134
        '2019-01-06'
135
      )
136
    ).thenResolve(false);
137
138
    when(
139
      leaveRequestRepository.save(
140
        deepEqual(
141
          new LeaveRequest(
142
            instance(user),
143
            Type.PAID,
144
            '2019-01-04',
145
            true,
146
            '2019-01-06',
147
            true,
148
            'H&M wedding'
149
          )
150
        )
151
      )
152
    ).thenResolve(instance(leaveRequest));
153
154
    expect(await handler.execute(command)).toBe(
155
      'cfdd06eb-cd71-44b9-82c6-46110b30ce05'
156
    );
157
158
    verify(
159
      doesLeaveRequestExistForPeriod.isSatisfiedBy(
160
        instance(user),
161
        '2019-01-04',
162
        '2019-01-06'
163
      )
164
    ).once();
165
    verify(
166
      doesEventsExistForPeriod.isSatisfiedBy(
167
        instance(user),
168
        '2019-01-04',
169
        '2019-01-06'
170
      )
171
    ).once();
172
    verify(
173
      leaveRequestRepository.save(
174
        deepEqual(
175
          new LeaveRequest(
176
            instance(user),
177
            Type.PAID,
178
            '2019-01-04',
179
            true,
180
            '2019-01-06',
181
            true,
182
            'H&M wedding'
183
          )
184
        )
185
      )
186
    ).once();
187
  });
188
});
189