-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathGetUserElementsQueryHandler.spec.ts
More file actions
204 lines (187 loc) · 7.21 KB
/
Copy pathGetUserElementsQueryHandler.spec.ts
File metadata and controls
204 lines (187 loc) · 7.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import { mock, instance, when, verify, deepEqual } from 'ts-mockito';
import { UserRepository } from 'src/Infrastructure/HumanResource/User/Repository/UserRepository';
import { GetUsersElementsQuery } from './GetUsersElementsQuery';
import { GetUsersElementsQueryHandler } from './GetUsersElementsQueryHandler';
import { MealTicketsPerMonthView } from '../../MealTicket/Views/MealTicketsPerMonthView';
import { User } from 'src/Domain/HumanResource/User/User.entity';
import {
ContractType,
UserAdministrative,
WorkingTimeType
} from 'src/Domain/HumanResource/User/UserAdministrative.entity';
import { UserElementsView } from '../View/UserElementsView';
import { MealTicketRemovalRepository } from 'src/Infrastructure/HumanResource/MealTicket/Repository/MealTicketRemovalRepository';
import { GetMealTicketsPerMonthQueryHandler } from '../../MealTicket/Query/GetMealTicketsPerMonthQueryHandler';
import { GetMealTicketsPerMonthQuery } from '../../MealTicket/Query/GetMealTicketsPerMonthQuery';
import { DateUtilsAdapter } from 'src/Infrastructure/Adapter/DateUtilsAdapter';
import { IDateUtils } from 'src/Application/IDateUtils';
import { GetLeavesByMonthQueryHandler } from '../../Leave/Query/GetLeavesByMonthQueryHandler';
import { GetLeavesByMonthQuery } from '../../Leave/Query/GetLeavesByMonthQuery';
import { UserLeavesView } from '../View/UserLeavesView';
import { LeavesCollection } from 'src/Domain/HumanResource/Leave/LeavesCollection';
import {
LeaveRequest,
Type
} from 'src/Domain/HumanResource/Leave/LeaveRequest.entity';
import { MonthDate } from 'src/Application/Common/MonthDate';
import { LeaveRequestSlotView } from '../../Leave/View/LeaveRequestSlotView';
describe('GetUserElementsQueryHandler', () => {
let mealTicketRemovalRepository: MealTicketRemovalRepository;
let userRepository: UserRepository;
let leavesByMonthQueryHandler: GetLeavesByMonthQueryHandler;
let mealTicketsQueryHandler: GetMealTicketsPerMonthQueryHandler;
let dateUtilsAdapter: IDateUtils;
let queryHandler: GetUsersElementsQueryHandler;
const date = new Date();
const query = new GetUsersElementsQuery(date);
beforeEach(() => {
mealTicketRemovalRepository = mock(MealTicketRemovalRepository);
userRepository = mock(UserRepository);
leavesByMonthQueryHandler = mock(GetLeavesByMonthQueryHandler);
mealTicketsQueryHandler = mock(GetMealTicketsPerMonthQueryHandler);
dateUtilsAdapter = mock(DateUtilsAdapter);
queryHandler = new GetUsersElementsQueryHandler(
instance(userRepository),
instance(leavesByMonthQueryHandler),
instance(mealTicketsQueryHandler),
instance(dateUtilsAdapter)
);
});
it('testGetUserElements', async () => {
const user = mock(User);
const earnings = 2000000;
const rawTransportFee = 7500;
const rawSustainableMobilityFee = 7000;
const rawSportsPassFee = 3000;
const userAdministrative = mock(UserAdministrative);
when(userAdministrative.getContract()).thenReturn(ContractType.CDI);
when(userAdministrative.isExecutivePosition()).thenReturn(true);
when(userAdministrative.getJoiningDate()).thenReturn(date.toISOString());
when(userAdministrative.getAnnualEarnings()).thenReturn(earnings);
when(userAdministrative.getAnnualEarnings()).thenReturn(earnings);
when(userAdministrative.getWorkingTime()).thenReturn(
WorkingTimeType.FULL_TIME
);
when(userAdministrative.getTransportFee()).thenReturn(rawTransportFee);
when(userAdministrative.getSustainableMobilityFee()).thenReturn(
rawSustainableMobilityFee
);
when(userAdministrative.getSportsPassFee()).thenReturn(rawSportsPassFee);
when(userAdministrative.haveHealthInsurance()).thenReturn(true);
when(user.getId()).thenReturn('3b8a1954-2ade-44a2-a03c-338985c327ef');
when(user.getFirstName()).thenReturn('John');
when(user.getLastName()).thenReturn('Doe');
when(user.getUserAdministrative()).thenReturn(instance(userAdministrative));
when(userRepository.findUsersWithPayslipInfo()).thenResolve([
instance(user)
]);
when(dateUtilsAdapter.getMonth(date)).thenReturn(new MonthDate(2022, 5));
const createLeaveRequestMock = (
startDate: string,
endDate: string
): LeaveRequest => {
const leaveRequest = mock(LeaveRequest);
when(leaveRequest.getUser()).thenReturn(instance(user));
when(leaveRequest.getType()).thenReturn(Type.PAID);
when(leaveRequest.getStartDate()).thenReturn(startDate);
when(leaveRequest.isStartsAllDay()).thenReturn(false);
when(leaveRequest.getEndDate()).thenReturn(endDate);
when(leaveRequest.isEndsAllDay()).thenReturn(false);
return leaveRequest;
};
const startDate1 = '2022-05-09';
const endDate1 = '2022-05-11';
const leaveRequest1 = createLeaveRequestMock(startDate1, endDate1);
const leaveRequestSlot1 = new LeaveRequestSlotView(
startDate1,
false,
endDate1,
false
);
const startDate2 = '2022-04-25';
const endDate2 = '2022-05-02';
const leaveRequest2 = createLeaveRequestMock(startDate2, endDate2);
const leaveRequestSlot2 = new LeaveRequestSlotView(
'2022-05-01T00:00:00.000Z',
true,
endDate2,
false
);
const startDate3 = '2022-05-29';
const endDate3 = '2022-06-05';
const leaveRequest3 = createLeaveRequestMock(startDate3, endDate3);
const leaveRequestSlot3 = new LeaveRequestSlotView(
startDate3,
false,
'2022-05-31T00:00:00.000Z',
true
);
when(
leavesByMonthQueryHandler.execute(
deepEqual(new GetLeavesByMonthQuery(date))
)
).thenResolve(
new LeavesCollection([
instance(leaveRequest1),
instance(leaveRequest2),
instance(leaveRequest3)
])
);
when(
mealTicketsQueryHandler.execute(
deepEqual(new GetMealTicketsPerMonthQuery(date))
)
).thenResolve([
new MealTicketsPerMonthView(
'3b8a1954-2ade-44a2-a03c-338985c327ef',
'John',
'Doe',
5,
0
)
]);
const monthlyEarnings = earnings / 1200;
const yearlyEarning = earnings * 0.01;
const transportFee = rawTransportFee * 0.01;
const sustainableMobilityFee = rawSustainableMobilityFee * 0.01;
const sportsPassFee = rawSportsPassFee * 0.01;
expect(await queryHandler.execute(query)).toMatchObject([
new UserElementsView(
'John',
'Doe',
ContractType.CDI,
true,
date.toISOString(),
yearlyEarning,
monthlyEarnings,
WorkingTimeType.FULL_TIME,
transportFee,
sustainableMobilityFee,
sportsPassFee,
5,
true,
new UserLeavesView(0, [
leaveRequestSlot1,
leaveRequestSlot2,
leaveRequestSlot3
]),
new UserLeavesView(0, []),
new UserLeavesView(0, []),
new UserLeavesView(0, []),
new UserLeavesView(0, []),
new UserLeavesView(0, [])
)
]);
verify(userRepository.findUsersWithPayslipInfo()).once();
verify(
leavesByMonthQueryHandler.execute(
deepEqual(new GetLeavesByMonthQuery(date))
)
).once();
verify(
mealTicketsQueryHandler.execute(
deepEqual(new GetMealTicketsPerMonthQuery(date))
)
).once();
});
});