Skip to content

Commit 5b34ffc

Browse files
Ajouts de tests unitaires
1 parent 5635e95 commit 5b34ffc

3 files changed

Lines changed: 62 additions & 36 deletions

File tree

src/Application/HumanResource/Leave/Query/GetLeaveRequestByIdQueryHandler.spec.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,9 @@ describe('GetLeaveRequestByIdQueryHandler', () => {
236236
when(viewer.getId()).thenReturn(viewerId);
237237

238238
const menstrualLeave = mock(LeaveRequest);
239-
when(menstrualLeave.getId()).thenReturn('204522d3-f077-4d21-b3ee-6e0d742fca44');
239+
when(menstrualLeave.getId()).thenReturn(
240+
'204522d3-f077-4d21-b3ee-6e0d742fca44'
241+
);
240242
when(menstrualLeave.getType()).thenReturn(Type.MENSTRUAL);
241243
when(menstrualLeave.getUserId()).thenReturn(ownerId);
242244
when(menstrualLeave.getStatus()).thenReturn(Status.ACCEPTED);
@@ -249,7 +251,10 @@ describe('GetLeaveRequestByIdQueryHandler', () => {
249251
when(menstrualLeave.getModerator()).thenReturn(null);
250252

251253
when(
252-
canLeaveRequestBeCancelled.isSatisfiedBy(instance(viewer), instance(menstrualLeave))
254+
canLeaveRequestBeCancelled.isSatisfiedBy(
255+
instance(viewer),
256+
instance(menstrualLeave)
257+
)
253258
).thenReturn(false);
254259

255260
when(

src/Application/HumanResource/Leave/Query/GetLeaveRequestsQueryHandler.spec.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,9 @@ describe('GetLeaveRequestsQueryHandler', () => {
225225
when(loggedUser.getId()).thenReturn(viewerId);
226226

227227
const menstrualLeave = mock(LeaveRequest);
228-
when(menstrualLeave.getId()).thenReturn('d54f15d6-1a1d-47e8-8672-9f46018f9960');
228+
when(menstrualLeave.getId()).thenReturn(
229+
'd54f15d6-1a1d-47e8-8672-9f46018f9960'
230+
);
229231
when(menstrualLeave.getType()).thenReturn(Type.MENSTRUAL);
230232
when(menstrualLeave.getUserId()).thenReturn(ownerId);
231233
when(menstrualLeave.getStatus()).thenReturn(Status.ACCEPTED);
@@ -247,9 +249,9 @@ describe('GetLeaveRequestsQueryHandler', () => {
247249
1
248250
]);
249251

250-
when(
251-
userRepository.findOneById(viewerId)
252-
).thenResolve(instance(loggedUser));
252+
when(userRepository.findOneById(viewerId)).thenResolve(
253+
instance(loggedUser)
254+
);
253255

254256
when(
255257
dateUtils.getLeaveDuration('2020-05-08', true, '2020-05-15', true)
@@ -263,9 +265,7 @@ describe('GetLeaveRequestsQueryHandler', () => {
263265
);
264266

265267
expect(
266-
await queryHandler.execute(
267-
new GetLeaveRequestsQuery(viewerId, 1, null)
268-
)
268+
await queryHandler.execute(new GetLeaveRequestsQuery(viewerId, 1, null))
269269
).toMatchObject(expectedResult);
270270
});
271271
});

src/Domain/HumanResource/Leave/Specification/IsMenstrualLeaveMonthlyQuotaExceeded.spec.ts

Lines changed: 48 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ describe('IsMenstrualLeaveMonthlyQuotaExceeded', () => {
2222
when(user.getId()).thenReturn('user-123');
2323
});
2424

25-
it('testQuotaNotExceededWhenNoExistingRequests', async () => {
26-
// No existing menstrual leave requests
25+
it('testQuotaNotExceededWithHalfDay', async () => {
2726
when(
2827
leaveRequestRepository.findMenstrualLeaveRequestsByUserAndMonth(
2928
'user-123',
@@ -32,24 +31,22 @@ describe('IsMenstrualLeaveMonthlyQuotaExceeded', () => {
3231
)
3332
).thenResolve([]);
3433

35-
// New request is 1 day (under quota of 2)
3634
when(
37-
dateUtils.getLeaveDuration('2026-04-10', true, '2026-04-10', true)
38-
).thenReturn(1);
35+
dateUtils.getLeaveDuration('2026-04-10', false, '2026-04-10', true)
36+
).thenReturn(0.5);
3937

4038
const result = await specification.isSatisfiedBy(
4139
instance(user),
4240
'2026-04-10',
43-
true,
41+
false,
4442
'2026-04-10',
4543
true
4644
);
4745

48-
expect(result).toBe(false); // Not exceeded
46+
expect(result).toBe(false);
4947
});
5048

5149
it('testQuotaNotExceededAt2Days', async () => {
52-
// 1 existing day of menstrual leave
5350
const existingLeave = mock(LeaveRequest);
5451
when(existingLeave.getStartDate()).thenReturn('2026-04-05');
5552
when(existingLeave.isStartsAllDay()).thenReturn(true);
@@ -64,12 +61,10 @@ describe('IsMenstrualLeaveMonthlyQuotaExceeded', () => {
6461
)
6562
).thenResolve([instance(existingLeave)]);
6663

67-
// Existing request is 1 day
6864
when(
6965
dateUtils.getLeaveDuration('2026-04-05', true, '2026-04-05', true)
7066
).thenReturn(1);
7167

72-
// New request is 1 day (total = 2, exactly at quota)
7368
when(
7469
dateUtils.getLeaveDuration('2026-04-10', true, '2026-04-10', true)
7570
).thenReturn(1);
@@ -82,11 +77,10 @@ describe('IsMenstrualLeaveMonthlyQuotaExceeded', () => {
8277
true
8378
);
8479

85-
expect(result).toBe(false); // Not exceeded (2 = quota)
80+
expect(result).toBe(false);
8681
});
8782

88-
it('testQuotaExceededWhenAddingMoreThan2Days', async () => {
89-
// 1 existing day of menstrual leave
83+
it('testQuotaExceededWith3Days', async () => {
9084
const existingLeave = mock(LeaveRequest);
9185
when(existingLeave.getStartDate()).thenReturn('2026-04-05');
9286
when(existingLeave.isStartsAllDay()).thenReturn(true);
@@ -101,12 +95,10 @@ describe('IsMenstrualLeaveMonthlyQuotaExceeded', () => {
10195
)
10296
).thenResolve([instance(existingLeave)]);
10397

104-
// Existing request is 1 day
10598
when(
10699
dateUtils.getLeaveDuration('2026-04-05', true, '2026-04-05', true)
107100
).thenReturn(1);
108101

109-
// New request is 2 days (total = 3, exceeds quota of 2)
110102
when(
111103
dateUtils.getLeaveDuration('2026-04-10', true, '2026-04-11', true)
112104
).thenReturn(2);
@@ -119,11 +111,10 @@ describe('IsMenstrualLeaveMonthlyQuotaExceeded', () => {
119111
true
120112
);
121113

122-
expect(result).toBe(true); // Exceeded
114+
expect(result).toBe(true);
123115
});
124116

125117
it('testQuotaExceededWhenAlready2Days', async () => {
126-
// 2 existing days of menstrual leave
127118
const existingLeave = mock(LeaveRequest);
128119
when(existingLeave.getStartDate()).thenReturn('2026-04-05');
129120
when(existingLeave.isStartsAllDay()).thenReturn(true);
@@ -138,12 +129,10 @@ describe('IsMenstrualLeaveMonthlyQuotaExceeded', () => {
138129
)
139130
).thenResolve([instance(existingLeave)]);
140131

141-
// Existing request is 2 days
142132
when(
143133
dateUtils.getLeaveDuration('2026-04-05', true, '2026-04-06', true)
144134
).thenReturn(2);
145135

146-
// New request is 1 day (total = 3, exceeds quota of 2)
147136
when(
148137
dateUtils.getLeaveDuration('2026-04-10', true, '2026-04-10', true)
149138
).thenReturn(1);
@@ -156,12 +145,10 @@ describe('IsMenstrualLeaveMonthlyQuotaExceeded', () => {
156145
true
157146
);
158147

159-
expect(result).toBe(true); // Exceeded
148+
expect(result).toBe(true);
160149
});
161150

162-
it('testQuotaChecksMultipleMonths', async () => {
163-
// Request spans April and May
164-
// Mock April having 1 existing day
151+
it('testMultipleMonthsApril', async () => {
165152
when(
166153
leaveRequestRepository.findMenstrualLeaveRequestsByUserAndMonth(
167154
'user-123',
@@ -171,16 +158,51 @@ describe('IsMenstrualLeaveMonthlyQuotaExceeded', () => {
171158
).thenResolve([]);
172159

173160
when(
174-
dateUtils.getLeaveDuration('2026-04-01', true, '2026-04-01', true)
161+
dateUtils.getLeaveDuration('2026-04-10', true, '2026-04-10', true)
162+
).thenReturn(1);
163+
164+
when(
165+
leaveRequestRepository.findMenstrualLeaveRequestsByUserAndMonth(
166+
'user-123',
167+
2026,
168+
5
169+
)
170+
).thenResolve([]);
171+
172+
when(
173+
dateUtils.getLeaveDuration('2026-05-01', true, '2026-05-01', true)
175174
).thenReturn(0);
176175

177-
// Mock May having 2 existing days (at quota)
176+
const result = await specification.isSatisfiedBy(
177+
instance(user),
178+
'2026-04-10',
179+
true,
180+
'2026-05-01',
181+
true
182+
);
183+
184+
expect(result).toBe(false);
185+
});
186+
187+
it('testMultipleMonthsExceeded', async () => {
178188
const mayLeave = mock(LeaveRequest);
179189
when(mayLeave.getStartDate()).thenReturn('2026-05-05');
180190
when(mayLeave.isStartsAllDay()).thenReturn(true);
181191
when(mayLeave.getEndDate()).thenReturn('2026-05-06');
182192
when(mayLeave.isEndsAllDay()).thenReturn(true);
183193

194+
when(
195+
leaveRequestRepository.findMenstrualLeaveRequestsByUserAndMonth(
196+
'user-123',
197+
2026,
198+
4
199+
)
200+
).thenResolve([]);
201+
202+
when(
203+
dateUtils.getLeaveDuration('2026-04-10', true, '2026-04-10', true)
204+
).thenReturn(0);
205+
184206
when(
185207
leaveRequestRepository.findMenstrualLeaveRequestsByUserAndMonth(
186208
'user-123',
@@ -193,7 +215,6 @@ describe('IsMenstrualLeaveMonthlyQuotaExceeded', () => {
193215
dateUtils.getLeaveDuration('2026-05-05', true, '2026-05-06', true)
194216
).thenReturn(2);
195217

196-
// New request is 1 day in May (total May = 3, exceeds quota)
197218
when(
198219
dateUtils.getLeaveDuration('2026-05-10', true, '2026-05-10', true)
199220
).thenReturn(1);
@@ -206,6 +227,6 @@ describe('IsMenstrualLeaveMonthlyQuotaExceeded', () => {
206227
true
207228
);
208229

209-
expect(result).toBe(true); // Exceeded for May
230+
expect(result).toBe(true);
210231
});
211232
});

0 commit comments

Comments
 (0)