Passed
Pull Request — master (#258)
by Mathieu
02:04
created

server/src/Application/HumanResource/Savings/Query/GetUsersSavingsRecordsBalanceQueryHandler.spec.ts   A

Complexity

Total Complexity 1
Complexity/F 0

Size

Lines of Code 180
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 1
eloc 146
mnd 1
bc 1
fnc 0
dl 0
loc 180
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
import { LeaveRequestNotFoundException } from 'src/Domain/HumanResource/Leave/Exception/LeaveRequestNotFoundException';
2
import {
3
  LeaveRequest,
4
  Status,
5
  Type
6
} from 'src/Domain/HumanResource/Leave/LeaveRequest.entity';
7
import { User } from 'src/Domain/HumanResource/User/User.entity';
8
import { DateUtilsAdapter } from 'src/Infrastructure/Adapter/DateUtilsAdapter';
9
import { LeaveRequestRepository } from 'src/Infrastructure/HumanResource/Leave/Repository/LeaveRequestRepository';
10
import { instance, mock, verify, when } from 'ts-mockito';
11
import { UserSummaryView } from '../../User/View/UserSummaryView';
12
import { LeaveRequestDetailView } from '../View/LeaveRequestDetailView';
13
import { GetLeaveRequestByIdQuery } from './GetLeaveRequestByIdQuery';
14
import { GetUsersSavingsRecordsBalanceQuery } from './GetUsersSavingsRecordsBalanceQuery';
15
import { GetUsersSavingsRecordsBalanceQueryHandler } from './GetUsersSavingsRecordsBalanceQueryHandler';
16
17
describe('GetUsersSavingsRecordsBalanceQuery', () => {
18
  let leaveRequestRepository: LeaveRequestRepository;
19
  let dateUtils: DateUtilsAdapter;
20
  let queryHandler: GetUsersSavingsRecordsBalanceQuery;
21
22
  beforeEach(() => {
23
    leaveRequestRepository = mock(LeaveRequestRepository);
24
    dateUtils = mock(DateUtilsAdapter);
25
    queryHandler = new GetUsersSavingsRecordsBalanceQueryHandler(
26
      instance(leaveRequestRepository),
27
      instance(dateUtils)
28
    );
29
  });
30
31
  it('testGetLeaveRequestById', async () => {
32
    const expectedResult = new LeaveRequestDetailView(
33
      '204522d3-f077-4d21-b3ee-6e0d742fca44',
34
      Type.PAID,
35
      Status.ACCEPTED,
36
      '2020-05-05',
37
      false,
38
      '2020-05-15',
39
      true,
40
      7.5,
41
      'Country vacation',
42
      new UserSummaryView(
43
        '54bb15ad-56da-45f8-b594-3ca43f13d4c0',
44
        'Alan',
45
        'TURING'
46
      ),
47
      new UserSummaryView(
48
        '697f9cf3-ec97-492a-91db-8cc4c47daab0',
49
        'Ada',
50
        'LOVELACE'
51
      ),
52
      'Go go go'
53
    );
54
55
    const user = mock(User);
56
    when(user.getId()).thenReturn('54bb15ad-56da-45f8-b594-3ca43f13d4c0');
57
    when(user.getFirstName()).thenReturn('Alan');
58
    when(user.getLastName()).thenReturn('TURING');
59
60
    const moderator = mock(User);
61
    when(moderator.getId()).thenReturn('697f9cf3-ec97-492a-91db-8cc4c47daab0');
62
    when(moderator.getFirstName()).thenReturn('Ada');
63
    when(moderator.getLastName()).thenReturn('LOVELACE');
64
65
    const leave = mock(LeaveRequest);
66
    when(leave.getId()).thenReturn('204522d3-f077-4d21-b3ee-6e0d742fca44');
67
    when(leave.getType()).thenReturn(Type.PAID);
68
    when(leave.getStatus()).thenReturn(Status.ACCEPTED);
69
    when(leave.getStartDate()).thenReturn('2020-05-05');
70
    when(leave.isStartsAllDay()).thenReturn(false);
71
    when(leave.getEndDate()).thenReturn('2020-05-15');
72
    when(leave.isEndsAllDay()).thenReturn(true);
73
    when(leave.getComment()).thenReturn('Country vacation');
74
    when(leave.getUser()).thenReturn(instance(user));
75
    when(leave.getModerator()).thenReturn(instance(moderator));
76
    when(leave.getModerationComment()).thenReturn('Go go go');
77
78
    when(
79
      leaveRequestRepository.findOneById('204522d3-f077-4d21-b3ee-6e0d742fca44')
80
    ).thenResolve(instance(leave));
81
82
    when(
83
      dateUtils.getLeaveDuration('2020-05-05', false, '2020-05-15', true)
84
    ).thenReturn(7.5);
85
86
    expect(
87
      await queryHandler.execute(
88
        new GetLeaveRequestByIdQuery('204522d3-f077-4d21-b3ee-6e0d742fca44')
89
      )
90
    ).toMatchObject(expectedResult);
91
92
    verify(
93
      dateUtils.getLeaveDuration('2020-05-05', false, '2020-05-15', true)
94
    ).once();
95
    verify(
96
      leaveRequestRepository.findOneById('204522d3-f077-4d21-b3ee-6e0d742fca44')
97
    ).once();
98
  });
99
100
  it('testLeaveNotFound', async () => {
101
    when(
102
      leaveRequestRepository.findOneById('204522d3-f077-4d21-b3ee-6e0d742fca44')
103
    ).thenResolve(null);
104
105
    try {
106
      await queryHandler.execute(
107
        new GetLeaveRequestByIdQuery('204522d3-f077-4d21-b3ee-6e0d742fca44')
108
      );
109
    } catch (e) {
110
      expect(e).toBeInstanceOf(LeaveRequestNotFoundException);
111
      expect(e.message).toBe(
112
        'human_resources.leaves.requests.errors.not_found'
113
      );
114
    }
115
    verify(
116
      leaveRequestRepository.findOneById('204522d3-f077-4d21-b3ee-6e0d742fca44')
117
    ).once();
118
  });
119
120
  it('testLeaveWithoutModerator', async () => {
121
    const expectedResult = new LeaveRequestDetailView(
122
      'a3753b9c-b711-4e0e-a535-e473161bd612',
123
      Type.PAID,
124
      Status.ACCEPTED,
125
      '2020-05-05',
126
      false,
127
      '2020-05-15',
128
      true,
129
      7.5,
130
      'Country vacation',
131
      new UserSummaryView(
132
        '2402455a-4dc1-47c9-89a4-f9b859f02f5c',
133
        'John',
134
        'DOE'
135
      ),
136
      null,
137
      null
138
    );
139
140
    const user = mock(User);
141
    when(user.getId()).thenReturn('2402455a-4dc1-47c9-89a4-f9b859f02f5c');
142
    when(user.getFirstName()).thenReturn('John');
143
    when(user.getLastName()).thenReturn('DOE');
144
145
    const leave = mock(LeaveRequest);
146
    when(leave.getId()).thenReturn('a3753b9c-b711-4e0e-a535-e473161bd612');
147
    when(leave.getType()).thenReturn(Type.PAID);
148
    when(leave.getStatus()).thenReturn(Status.ACCEPTED);
149
    when(leave.getStartDate()).thenReturn('2020-05-05');
150
    when(leave.isStartsAllDay()).thenReturn(false);
151
    when(leave.getEndDate()).thenReturn('2020-05-15');
152
    when(leave.isEndsAllDay()).thenReturn(true);
153
    when(leave.getComment()).thenReturn('Country vacation');
154
    when(leave.getUser()).thenReturn(instance(user));
155
    when(leave.getModerator()).thenReturn(null);
156
    when(leave.getModerationComment()).thenReturn(null);
157
158
    when(
159
      leaveRequestRepository.findOneById('a3753b9c-b711-4e0e-a535-e473161bd612')
160
    ).thenResolve(instance(leave));
161
162
    when(
163
      dateUtils.getLeaveDuration('2020-05-05', false, '2020-05-15', true)
164
    ).thenReturn(7.5);
165
166
    expect(
167
      await queryHandler.execute(
168
        new GetLeaveRequestByIdQuery('a3753b9c-b711-4e0e-a535-e473161bd612')
169
      )
170
    ).toMatchObject(expectedResult);
171
172
    verify(
173
      dateUtils.getLeaveDuration('2020-05-05', false, '2020-05-15', true)
174
    ).once();
175
    verify(
176
      leaveRequestRepository.findOneById('a3753b9c-b711-4e0e-a535-e473161bd612')
177
    ).once();
178
  });
179
});
180