|
1
|
|
|
import {mock, instance, when, verify} from 'ts-mockito'; |
|
2
|
|
|
import {GetDailyRateByIdQueryHandler} from './GetDailyRateByIdQueryHandler'; |
|
3
|
|
|
import {GetDailyRateByIdQuery} from './GetDailyRateByIdQuery'; |
|
4
|
|
|
import {Task} from 'src/Domain/Task/Task.entity'; |
|
5
|
|
|
import {TaskView} from 'src/Application/Task/View/TaskView'; |
|
6
|
|
|
import {DailyRateRepository} from 'src/Infrastructure/Billing/Repository/DailyRateRepository'; |
|
7
|
|
|
import {DailyRateView} from '../../View/DailyRate/DailyRateView'; |
|
8
|
|
|
import {UserView} from 'src/Application/User/View/UserView'; |
|
9
|
|
|
import {CustomerView} from 'src/Application/Customer/View/CustomerView'; |
|
10
|
|
|
import {User} from 'src/Domain/User/User.entity'; |
|
11
|
|
|
import {Customer} from 'src/Domain/Customer/Customer.entity'; |
|
12
|
|
|
import {DailyRate} from 'src/Domain/Billing/DailyRate.entity'; |
|
13
|
|
|
import {DailyRateNotFoundException} from 'src/Domain/Billing/Exception/DailyRateNotFoundException'; |
|
14
|
|
|
|
|
15
|
|
|
describe('GetDailyRateByIdQueryHandler', () => { |
|
16
|
|
|
const query = new GetDailyRateByIdQuery( |
|
17
|
|
|
'eb9e1d9b-dce2-48a9-b64f-f0872f3157d2' |
|
18
|
|
|
); |
|
19
|
|
|
|
|
20
|
|
|
it('testGetDailyRate', async () => { |
|
21
|
|
|
const dailyRateRepository = mock(DailyRateRepository); |
|
22
|
|
|
const queryHandler = new GetDailyRateByIdQueryHandler( |
|
23
|
|
|
instance(dailyRateRepository) |
|
24
|
|
|
); |
|
25
|
|
|
|
|
26
|
|
|
const expectedResult = new DailyRateView( |
|
27
|
|
|
'eb9e1d9b-dce2-48a9-b64f-f0872f3157d2', |
|
28
|
|
|
620.6, |
|
29
|
|
|
new UserView( |
|
30
|
|
|
'deffa668-b9af-4a52-94dd-61a35401b917', |
|
31
|
|
|
'Mathieu', |
|
32
|
|
|
'MARCHOIS', |
|
33
|
|
|
'[email protected]' |
|
34
|
|
|
), |
|
35
|
|
|
new TaskView('d54f15d6-1a1d-47e8-8672-9f46018f9960', 'Development'), |
|
36
|
|
|
new CustomerView('c6434c49-216b-41b3-a30a-79a3eb1198ec', 'Radio France') |
|
37
|
|
|
); |
|
38
|
|
|
|
|
39
|
|
|
const user = mock(User); |
|
40
|
|
|
when(user.getId()).thenReturn('deffa668-b9af-4a52-94dd-61a35401b917'); |
|
41
|
|
|
when(user.getFirstName()).thenReturn('Mathieu'); |
|
42
|
|
|
when(user.getLastName()).thenReturn('MARCHOIS'); |
|
43
|
|
|
when(user.getEmail()).thenReturn('[email protected]'); |
|
44
|
|
|
|
|
45
|
|
|
const customer = mock(Customer); |
|
46
|
|
|
when(customer.getId()).thenReturn('c6434c49-216b-41b3-a30a-79a3eb1198ec'); |
|
47
|
|
|
when(customer.getName()).thenReturn('Radio France'); |
|
48
|
|
|
|
|
49
|
|
|
const task = mock(Task); |
|
50
|
|
|
when(task.getId()).thenReturn('d54f15d6-1a1d-47e8-8672-9f46018f9960'); |
|
51
|
|
|
when(task.getName()).thenReturn('Development'); |
|
52
|
|
|
|
|
53
|
|
|
const dailyRate1 = mock(DailyRate); |
|
54
|
|
|
when(dailyRate1.getId()).thenReturn('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2'); |
|
55
|
|
|
when(dailyRate1.getAmount()).thenReturn(62060); |
|
56
|
|
|
when(dailyRate1.getUser()).thenReturn(instance(user)); |
|
57
|
|
|
when(dailyRate1.getCustomer()).thenReturn(instance(customer)); |
|
58
|
|
|
when(dailyRate1.getTask()).thenReturn(instance(task)); |
|
59
|
|
|
|
|
60
|
|
|
when( |
|
61
|
|
|
dailyRateRepository.findOneById('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2') |
|
62
|
|
|
).thenResolve(instance(dailyRate1)); |
|
63
|
|
|
|
|
64
|
|
|
expect(await queryHandler.execute(query)).toMatchObject(expectedResult); |
|
65
|
|
|
|
|
66
|
|
|
verify( |
|
67
|
|
|
dailyRateRepository.findOneById('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2') |
|
68
|
|
|
).once(); |
|
69
|
|
|
}); |
|
70
|
|
|
|
|
71
|
|
|
it('testGetDailyRateNotFound', async () => { |
|
72
|
|
|
const dailyrateRepository = mock(DailyRateRepository); |
|
73
|
|
|
const queryHandler = new GetDailyRateByIdQueryHandler( |
|
74
|
|
|
instance(dailyrateRepository) |
|
75
|
|
|
); |
|
76
|
|
|
when( |
|
77
|
|
|
dailyrateRepository.findOneById('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2') |
|
78
|
|
|
).thenResolve(null); |
|
79
|
|
|
|
|
80
|
|
|
try { |
|
81
|
|
|
await queryHandler.execute(query); |
|
82
|
|
|
} catch (e) { |
|
83
|
|
|
expect(e).toBeInstanceOf(DailyRateNotFoundException); |
|
84
|
|
|
expect(e.message).toBe('billing.errors.daily_rate_not_found'); |
|
85
|
|
|
verify( |
|
86
|
|
|
dailyrateRepository.findOneById('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2') |
|
87
|
|
|
).once(); |
|
88
|
|
|
} |
|
89
|
|
|
}); |
|
90
|
|
|
}); |
|
91
|
|
|
|