1
|
|
|
import {mock, instance, when, verify} from 'ts-mockito'; |
2
|
|
|
import {GetPaySlipByIdQueryHandler} from './GetPaySlipByIdQueryHandler'; |
3
|
|
|
import {GetPaySlipByIdQuery} from './GetPaySlipByIdQuery'; |
4
|
|
|
import {PaySlipView} from '../View/PaySlipView'; |
5
|
|
|
import {UserSummaryView} from '../../User/View/UserSummaryView'; |
6
|
|
|
import {FileView} from 'src/Application/File/View/FileView'; |
7
|
|
|
import {PaySlipRepository} from 'src/Infrastructure/HumanResource/PaySlip/Repository/PaySlipRepository'; |
8
|
|
|
import {File} from 'src/Domain/File/File.entity'; |
9
|
|
|
import {User} from 'src/Domain/HumanResource/User/User.entity'; |
10
|
|
|
import {PaySlip} from 'src/Domain/HumanResource/PaySlip/PaySlip.entity'; |
11
|
|
|
import {PaySlipNotFoundException} from 'src/Domain/HumanResource/PaySlip/Exception/PaySlipNotFoundException'; |
12
|
|
|
|
13
|
|
|
describe('GetPaySlipByIdQueryHandler', () => { |
14
|
|
|
const query = new GetPaySlipByIdQuery('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2'); |
15
|
|
|
|
16
|
|
|
it('testGetPaySlip', async () => { |
17
|
|
|
const payslipRepository = mock(PaySlipRepository); |
18
|
|
|
const queryHandler = new GetPaySlipByIdQueryHandler( |
19
|
|
|
instance(payslipRepository) |
20
|
|
|
); |
21
|
|
|
const expectedResult = new PaySlipView( |
22
|
|
|
'eb9e1d9b-dce2-48a9-b64f-f0872f3157d2', |
23
|
|
|
'2020-05-03', |
24
|
|
|
new UserSummaryView( |
25
|
|
|
'c453e506-dd9f-500f-927e-dcc54cc035d6', |
26
|
|
|
'Mathieu', |
27
|
|
|
'MARCHOIS' |
28
|
|
|
), |
29
|
|
|
new FileView('f4646506-dd8f-490f-927e-d5c54cc035d6', 'file1.pdf', 200) |
30
|
|
|
); |
31
|
|
|
const user = mock(User); |
32
|
|
|
when(user.getId()).thenReturn('c453e506-dd9f-500f-927e-dcc54cc035d6'); |
33
|
|
|
when(user.getFirstName()).thenReturn('Mathieu'); |
34
|
|
|
when(user.getLastName()).thenReturn('MARCHOIS'); |
35
|
|
|
|
36
|
|
|
const file1 = mock(File); |
37
|
|
|
when(file1.getId()).thenReturn('f4646506-dd8f-490f-927e-d5c54cc035d6'); |
38
|
|
|
when(file1.getSize()).thenReturn(200); |
39
|
|
|
when(file1.getName()).thenReturn('xbn_file1.pdf'); |
40
|
|
|
when(file1.getOriginalName()).thenReturn('file1.pdf'); |
41
|
|
|
|
42
|
|
|
const paySlip1 = mock(PaySlip); |
43
|
|
|
when(paySlip1.getId()).thenReturn('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2'); |
44
|
|
|
when(paySlip1.getDate()).thenReturn('2020-05-03'); |
45
|
|
|
when(paySlip1.getUser()).thenReturn(instance(user)); |
46
|
|
|
when(paySlip1.getFile()).thenReturn(instance(file1)); |
47
|
|
|
|
48
|
|
|
when( |
49
|
|
|
payslipRepository.findOneById('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2') |
50
|
|
|
).thenResolve(instance(paySlip1)); |
51
|
|
|
|
52
|
|
|
expect(await queryHandler.execute(query)).toMatchObject(expectedResult); |
53
|
|
|
|
54
|
|
|
verify( |
55
|
|
|
payslipRepository.findOneById('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2') |
56
|
|
|
).once(); |
57
|
|
|
}); |
58
|
|
|
|
59
|
|
|
it('testGetPaySlipNotFound', async () => { |
60
|
|
|
const payslipRepository = mock(PaySlipRepository); |
61
|
|
|
const queryHandler = new GetPaySlipByIdQueryHandler( |
62
|
|
|
instance(payslipRepository) |
63
|
|
|
); |
64
|
|
|
when( |
65
|
|
|
payslipRepository.findOneById('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2') |
66
|
|
|
).thenResolve(null); |
67
|
|
|
|
68
|
|
|
try { |
69
|
|
|
await queryHandler.execute(query); |
70
|
|
|
} catch (e) { |
71
|
|
|
expect(e).toBeInstanceOf(PaySlipNotFoundException); |
72
|
|
|
expect(e.message).toBe('human_resource.errors.pay_slip_not_found'); |
73
|
|
|
verify( |
74
|
|
|
payslipRepository.findOneById('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2') |
75
|
|
|
).once(); |
76
|
|
|
} |
77
|
|
|
}); |
78
|
|
|
}); |
79
|
|
|
|