Passed
Pull Request — master (#148)
by Mathieu
01:37
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 {CreateLeaveCommandHandler} from './CreateLeaveRequestCommandHandler';
4
import {LeaveRepository} from 'src/Infrastructure/HumanResource/Leave/Repository/LeaveRepository';
5
import {CreateLeaveCommand} from './CreateLeaveRequestCommand';
6
import {User} from 'src/Domain/HumanResource/User/User.entity';
7
import {
8
  LeaveType,
9
  Leave
10
} from 'src/Domain/HumanResource/Leave/Leave.entity';
11
import {LeaveAlreadyExistForThisPeriodException} from 'src/Domain/HumanResource/Leave/Exception/LeaveAlreadyExistForThisPeriodException';
12
import {DoesEventsExistForPeriod} from 'src/Domain/FairCalendar/Specification/DoesEventsExistForPeriod';
13
import {EventsAlreadyExistForThisPeriodException} from 'src/Domain/FairCalendar/Exception/EventsAlreadyExistForThisPeriodException';
14
15
describe('CreateLeaveCommandHandler', () => {
16
  let leaveRepository: LeaveRepository;
17
  let DoesLeaveRequestExistForPeriod: DoesLeaveRequestExistForPeriod;
18
  let doesEventsExistForPeriod: DoesEventsExistForPeriod;
19
  let handler: CreateLeaveCommandHandler;
20
21
  const user = mock(User);
22
  const command = new CreateLeaveCommand(
23
    instance(user),
24
    LeaveType.PAID,
25
    '2019-01-04',
26
    true,
27
    '2019-01-06',
28
    true,
29
    'H&M wedding'
30
  );
31
32
  beforeEach(() => {
33
    leaveRepository = mock(LeaveRepository);
34
    DoesLeaveRequestExistForPeriod = mock(DoesLeaveRequestExistForPeriod);
35
    doesEventsExistForPeriod = mock(DoesEventsExistForPeriod);
36
37
    handler = new CreateLeaveCommandHandler(
38
      instance(leaveRepository),
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(LeaveAlreadyExistForThisPeriodException);
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(leaveRepository.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(leaveRepository.save(anything())).never();
116
    }
117
  });
118
119
  it('testCreateLeavesSuccessfully', async () => {
120
    const leave = mock(Leave);
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
      leaveRepository.save(
140
        deepEqual(
141
          new Leave(
142
            instance(user),
143
            LeaveType.PAID,
144
            '2019-01-04',
145
            true,
146
            '2019-01-06',
147
            true,
148
            'H&M wedding'
149
          )
150
        )
151
      )
152
    ).thenResolve(instance(leave));
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
      leaveRepository.save(
174
        deepEqual(
175
          new Leave(
176
            instance(user),
177
            LeaveType.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