Passed
Pull Request — master (#235)
by Mathieu
01:46
created

server/src/Domain/HumanResource/MealTicket/Converter/LeaveRequestToMealTicketRemovalConverter.spec.ts   A

Complexity

Total Complexity 1
Complexity/F 0

Size

Lines of Code 192
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 1
eloc 160
mnd 1
bc 1
fnc 0
dl 0
loc 192
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
import { instance, mock, when, verify, deepEqual, anything } from 'ts-mockito';
2
import { LeaveRequestToLeavesConverter } from './LeaveRequestToLeavesConverter';
3
import { DateUtilsAdapter } from 'src/Infrastructure/Adapter/DateUtilsAdapter';
4
import { User } from 'src/Domain/HumanResource/User/User.entity';
5
import { LeaveRepository } from 'src/Infrastructure/HumanResource/Leave/Repository/LeaveRepository';
6
import {
7
  LeaveRequest,
8
  Type
9
} from 'src/Domain/HumanResource/Leave/LeaveRequest.entity';
10
import { Leave } from 'src/Domain/HumanResource/Leave/Leave.entity';
11
import { CooperativeRepository } from 'src/Infrastructure/Settings/Repository/CooperativeRepository';
12
import { CooperativeNotFoundException } from 'src/Domain/Settings/Repository/CooperativeNotFoundException';
13
import { Cooperative } from 'src/Domain/Settings/Cooperative.entity';
14
15
describe('LeaveRequestToLeavesConverter', () => {
16
  let leaveRepository: LeaveRepository;
17
  let cooperativeRepository: CooperativeRepository;
18
  let dateUtilsAdapter: DateUtilsAdapter;
19
  let leaveRequestToLeavesConverter: LeaveRequestToLeavesConverter;
20
21
  const user = mock(User);
22
  const cooperative = mock(Cooperative);
23
24
  beforeEach(() => {
25
    leaveRepository = mock(LeaveRepository);
26
    cooperativeRepository = mock(CooperativeRepository);
27
    dateUtilsAdapter = mock(DateUtilsAdapter);
28
    leaveRequestToLeavesConverter = new LeaveRequestToLeavesConverter(
29
      instance(leaveRepository),
30
      instance(cooperativeRepository),
31
      instance(dateUtilsAdapter)
32
    );
33
  });
34
35
  it('testConvertLeaveToLeavesWithFullEnds', async () => {
36
    when(cooperative.getDayDuration()).thenReturn(420);
37
38
    const leaveRequest = mock(LeaveRequest);
39
    when(leaveRequest.getType()).thenReturn(Type.MEDICAL);
40
    when(leaveRequest.getStartDate()).thenReturn('2020-12-24');
41
    when(leaveRequest.isStartsAllDay()).thenReturn(false);
42
    when(leaveRequest.getEndDate()).thenReturn('2021-01-04');
43
    when(leaveRequest.isEndsAllDay()).thenReturn(true);
44
    when(leaveRequest.getUser()).thenReturn(instance(user));
45
46
    when(cooperativeRepository.find()).thenResolve(instance(cooperative));
47
    when(
48
      dateUtilsAdapter.getWorkedDaysDuringAPeriod(
49
        deepEqual(new Date('2020-12-24')),
50
        deepEqual(new Date('2021-01-04'))
51
      )
52
    ).thenReturn([
53
      new Date('2020-12-24'),
54
      new Date('2020-12-28'),
55
      new Date('2020-12-29'),
56
      new Date('2020-12-30'),
57
      new Date('2020-12-31'),
58
      new Date('2021-01-04')
59
    ]);
60
61
    await leaveRequestToLeavesConverter.convert(instance(leaveRequest));
62
63
    verify(
64
      dateUtilsAdapter.getWorkedDaysDuringAPeriod(
65
        deepEqual(new Date('2020-12-24')),
66
        deepEqual(new Date('2021-01-04'))
67
      )
68
    ).once();
69
    verify(
70
      leaveRepository.save(
71
        deepEqual([
72
          new Leave(instance(leaveRequest), 210, '2020-12-24T00:00:00.000Z'),
73
          new Leave(instance(leaveRequest), 420, '2020-12-28T00:00:00.000Z'),
74
          new Leave(instance(leaveRequest), 420, '2020-12-29T00:00:00.000Z'),
75
          new Leave(instance(leaveRequest), 420, '2020-12-30T00:00:00.000Z'),
76
          new Leave(instance(leaveRequest), 420, '2020-12-31T00:00:00.000Z'),
77
          new Leave(instance(leaveRequest), 420, '2021-01-04T00:00:00.000Z')
78
        ])
79
      )
80
    ).once();
81
    verify(cooperativeRepository.find()).once();
82
  });
83
84
  it('testConvertLeaveToLeavesWithFullStarts', async () => {
85
    when(cooperative.getDayDuration()).thenReturn(420);
86
87
    const leaveRequest = mock(LeaveRequest);
88
    when(leaveRequest.getType()).thenReturn(Type.PAID);
89
    when(leaveRequest.getStartDate()).thenReturn('2020-12-24');
90
    when(leaveRequest.isStartsAllDay()).thenReturn(true);
91
    when(leaveRequest.getEndDate()).thenReturn('2021-01-04');
92
    when(leaveRequest.isEndsAllDay()).thenReturn(false);
93
    when(leaveRequest.getUser()).thenReturn(instance(user));
94
    when(cooperativeRepository.find()).thenResolve(instance(cooperative));
95
    when(
96
      dateUtilsAdapter.getWorkedDaysDuringAPeriod(
97
        deepEqual(new Date('2020-12-24')),
98
        deepEqual(new Date('2021-01-04'))
99
      )
100
    ).thenReturn([
101
      new Date('2020-12-24'),
102
      new Date('2020-12-28'),
103
      new Date('2020-12-29'),
104
      new Date('2020-12-30'),
105
      new Date('2020-12-31'),
106
      new Date('2021-01-04')
107
    ]);
108
109
    await leaveRequestToLeavesConverter.convert(instance(leaveRequest));
110
111
    verify(
112
      dateUtilsAdapter.getWorkedDaysDuringAPeriod(
113
        deepEqual(new Date('2020-12-24')),
114
        deepEqual(new Date('2021-01-04'))
115
      )
116
    ).once();
117
    verify(cooperativeRepository.find()).once();
118
    verify(
119
      leaveRepository.save(
120
        deepEqual([
121
          new Leave(instance(leaveRequest), 420, '2020-12-24T00:00:00.000Z'),
122
          new Leave(instance(leaveRequest), 420, '2020-12-28T00:00:00.000Z'),
123
          new Leave(instance(leaveRequest), 420, '2020-12-29T00:00:00.000Z'),
124
          new Leave(instance(leaveRequest), 420, '2020-12-30T00:00:00.000Z'),
125
          new Leave(instance(leaveRequest), 420, '2020-12-31T00:00:00.000Z'),
126
          new Leave(instance(leaveRequest), 210, '2021-01-04T00:00:00.000Z')
127
        ])
128
      )
129
    ).once();
130
  });
131
132
  it('testEmptyDates', async () => {
133
    const leaveRequest = mock(LeaveRequest);
134
    when(leaveRequest.getType()).thenReturn(Type.PAID);
135
    when(leaveRequest.getStartDate()).thenReturn('2020-12-24');
136
    when(leaveRequest.isStartsAllDay()).thenReturn(true);
137
    when(leaveRequest.getEndDate()).thenReturn('2021-01-04');
138
    when(leaveRequest.isEndsAllDay()).thenReturn(false);
139
    when(leaveRequest.getUser()).thenReturn(instance(user));
140
    when(
141
      dateUtilsAdapter.getWorkedDaysDuringAPeriod(
142
        deepEqual(new Date('2020-12-24')),
143
        deepEqual(new Date('2021-01-04'))
144
      )
145
    ).thenReturn([]);
146
147
    await leaveRequestToLeavesConverter.convert(instance(leaveRequest));
148
149
    verify(
150
      dateUtilsAdapter.getWorkedDaysDuringAPeriod(
151
        deepEqual(new Date('2020-12-24')),
152
        deepEqual(new Date('2021-01-04'))
153
      )
154
    ).once();
155
    verify(cooperativeRepository.find()).never();
156
    verify(leaveRepository.save(anything())).never();
157
  });
158
159
  it('testCooperativeNotFound', async () => {
160
    const leaveRequest = mock(LeaveRequest);
161
    when(leaveRequest.getType()).thenReturn(Type.PAID);
162
    when(leaveRequest.getStartDate()).thenReturn('2020-12-24');
163
    when(leaveRequest.isStartsAllDay()).thenReturn(true);
164
    when(leaveRequest.getEndDate()).thenReturn('2021-01-04');
165
    when(leaveRequest.isEndsAllDay()).thenReturn(false);
166
    when(leaveRequest.getUser()).thenReturn(instance(user));
167
168
    when(cooperativeRepository.find()).thenResolve(null);
169
    when(
170
      dateUtilsAdapter.getWorkedDaysDuringAPeriod(
171
        deepEqual(new Date('2020-12-24')),
172
        deepEqual(new Date('2021-01-04'))
173
      )
174
    ).thenReturn([]);
175
176
    try {
177
      await leaveRequestToLeavesConverter.convert(instance(leaveRequest));
178
    } catch (e) {
179
      expect(e).toBeInstanceOf(CooperativeNotFoundException);
180
      expect(e.message).toBe('settings.errors.cooperative_not_found');
181
      verify(cooperativeRepository.find()).once();
182
      verify(
183
        dateUtilsAdapter.getWorkedDaysDuringAPeriod(
184
          deepEqual(new Date('2020-12-24')),
185
          deepEqual(new Date('2021-01-04'))
186
        )
187
      ).once();
188
      verify(leaveRepository.save(anything())).never();
189
    }
190
  });
191
});
192