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

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

Complexity

Total Complexity 1
Complexity/F 0

Size

Lines of Code 76
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 63
dl 0
loc 76
rs 10
c 0
b 0
f 0
wmc 1
mnd 1
bc 1
fnc 0
bpm 0
cpm 0
noi 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 type = Type.MEDICAL;
19
  const id = 'fakeId';
20
  const endDate = '2022-01-05';
21
  const startDate = '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('testUserNotFound', 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('testUserUpdated', async () => {
54
    const leaveRequest = new LeaveRequest(
55
      instance(user),
56
      Type.PAID,
57
      startDate,
58
      false,
59
      endDate,
60
      false
61
    );
62
63
    when(leaveRequestRepository.findOneById(id)).thenResolve(leaveRequest);
64
65
    await commandHandler.execute(command);
66
67
    verify(
68
      leaveRequestRepository.save(
69
        deepEqual(
70
          new LeaveRequest(instance(user), type, startDate, true, endDate, true)
71
        )
72
      )
73
    ).once();
74
  });
75
});
76