Passed
Pull Request — master (#213)
by
unknown
01:45
created

server/src/Application/HumanResource/Leave/Command/UpdateLeaveRequestCommand.Handler.spec.ts   A

Complexity

Total Complexity 1
Complexity/F 0

Size

Lines of Code 79
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 1
eloc 66
mnd 1
bc 1
fnc 0
dl 0
loc 79
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
  Type
5
} from 'src/Domain/HumanResource/Leave/LeaveRequest.entity';
6
import { ILeaveRequestRepository } from 'src/Domain/HumanResource/Leave/Repository/ILeaveRequestRepository';
7
import { User } from 'src/Domain/HumanResource/User/User.entity';
8
import { LeaveRequestRepository } from 'src/Infrastructure/HumanResource/Leave/Repository/LeaveRequestRepository';
9
import { anything, deepEqual, instance, mock, verify, when } from 'ts-mockito';
10
import { UpdateLeaveRequestCommand } from './UpdateLeaveRequestCommand';
11
import { UpdateLeaveRequestCommandHandler } from './UpdateLeaveRequestCommandHandler';
12
13
describe('UpdateLeaveRequestCommandHandler', () => {
14
  let leaveRequestRepository: ILeaveRequestRepository;
15
  let user: User;
16
  let commandHandler: UpdateLeaveRequestCommandHandler;
17
18
  const id = 'fakeId';
19
  const type = Type.MEDICAL;
20
  const startDate = '2022-01-05';
21
  const endDate = '2022-01-05';
22
23
  const command = new UpdateLeaveRequestCommand(
24
    id,
25
    type,
26
    startDate,
27
    true,
28
    endDate,
29
    true
30
  );
31
32
  beforeEach(() => {
33
    leaveRequestRepository = mock(LeaveRequestRepository);
34
    user = mock(User);
35
    commandHandler = new UpdateLeaveRequestCommandHandler(
36
      instance(leaveRequestRepository)
37
    );
38
  });
39
40
  it('testLeaveRequestNotFound', async () => {
41
    when(leaveRequestRepository.findOneById(id)).thenResolve(null);
42
43
    try {
44
      await commandHandler.execute(command);
45
    } catch (e) {
46
      expect(e).toBeInstanceOf(LeaveRequestNotFoundException);
47
48
      verify(leaveRequestRepository.findOneById(anything())).once();
49
      verify(leaveRequestRepository.save(anything())).never();
50
    }
51
  });
52
53
  it('testLeaveRequestUpdated', async () => {
54
    const startsAllDay = true;
55
    const endsAllDay = true;
56
57
    const leaveRequest = new LeaveRequest(
58
      instance(user),
59
      Type.PAID,
60
      startDate,
61
      false,
62
      endDate,
63
      false
64
    );
65
66
    when(leaveRequestRepository.findOneById(id)).thenResolve(leaveRequest);
67
68
    await commandHandler.execute(command);
69
70
    verify(
71
      leaveRequestRepository.save(
72
        deepEqual(
73
          new LeaveRequest(instance(user), type, startDate, startsAllDay, endDate, endsAllDay)
74
        )
75
      )
76
    ).once();
77
  });
78
});
79