|
1
|
|
|
import { mock, instance, when, verify } from 'ts-mockito'; |
|
2
|
|
|
import { ProjectRepository } from 'src/Infrastructure/Project/Repository/ProjectRepository'; |
|
3
|
|
|
import { InvoiceUnits, Project } from 'src/Domain/Project/Project.entity'; |
|
4
|
|
|
import { ProjectView } from 'src/Application/Project/View/ProjectView'; |
|
5
|
|
|
import { GetProjectByIdQueryHandler } from './GetProjectByIdQueryHandler'; |
|
6
|
|
|
import { GetProjectByIdQuery } from './GetProjectByIdQuery'; |
|
7
|
|
|
import { ProjectNotFoundException } from 'src/Domain/Project/Exception/ProjectNotFoundException'; |
|
8
|
|
|
import { CustomerView } from 'src/Application/Customer/View/CustomerView'; |
|
9
|
|
|
import { Customer } from 'src/Domain/Customer/Customer.entity'; |
|
10
|
|
|
|
|
11
|
|
|
describe('GetProjectByIdQueryHandler', () => { |
|
12
|
|
|
const query = new GetProjectByIdQuery('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2'); |
|
13
|
|
|
|
|
14
|
|
|
it('testGetProject', async () => { |
|
15
|
|
|
const projectRepository = mock(ProjectRepository); |
|
16
|
|
|
const queryHandler = new GetProjectByIdQueryHandler( |
|
17
|
|
|
instance(projectRepository) |
|
18
|
|
|
); |
|
19
|
|
|
|
|
20
|
|
|
const project = mock(Project); |
|
21
|
|
|
const customer = mock(Customer); |
|
22
|
|
|
when(customer.getId()).thenReturn('aeb50974-0dcd-4ef4-af43-d656250e43bc'); |
|
23
|
|
|
when(customer.getName()).thenReturn('Customer'); |
|
24
|
|
|
|
|
25
|
|
|
when(project.getId()).thenReturn('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2'); |
|
26
|
|
|
when(project.getName()).thenReturn('Project'); |
|
27
|
|
|
when(project.getDayDuration()).thenReturn(420); |
|
28
|
|
|
when(project.getInvoiceUnit()).thenReturn(InvoiceUnits.DAY); |
|
29
|
|
|
when(project.getCustomer()).thenReturn(instance(customer)); |
|
30
|
|
|
when( |
|
31
|
|
|
projectRepository.findOneById('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2') |
|
32
|
|
|
).thenResolve(instance(project)); |
|
33
|
|
|
|
|
34
|
|
|
expect(await queryHandler.execute(query)).toMatchObject( |
|
35
|
|
|
new ProjectView( |
|
36
|
|
|
'eb9e1d9b-dce2-48a9-b64f-f0872f3157d2', |
|
37
|
|
|
'Project', |
|
38
|
|
|
420, |
|
39
|
|
|
InvoiceUnits.DAY, |
|
40
|
|
|
new CustomerView('aeb50974-0dcd-4ef4-af43-d656250e43bc', 'Customer') |
|
41
|
|
|
) |
|
42
|
|
|
); |
|
43
|
|
|
|
|
44
|
|
|
verify( |
|
45
|
|
|
projectRepository.findOneById('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2') |
|
46
|
|
|
).once(); |
|
47
|
|
|
verify(project.getId()).once(); |
|
48
|
|
|
verify(project.getName()).once(); |
|
49
|
|
|
verify(project.getDayDuration()).once(); |
|
50
|
|
|
verify(project.getInvoiceUnit()).once(); |
|
51
|
|
|
verify(project.getCustomer()).once(); |
|
52
|
|
|
verify(customer.getId()).once(); |
|
53
|
|
|
verify(customer.getName()).once(); |
|
54
|
|
|
}); |
|
55
|
|
|
|
|
56
|
|
|
it('testGetProjectNotFound', async () => { |
|
57
|
|
|
const projectRepository = mock(ProjectRepository); |
|
58
|
|
|
const queryHandler = new GetProjectByIdQueryHandler( |
|
59
|
|
|
instance(projectRepository) |
|
60
|
|
|
); |
|
61
|
|
|
when( |
|
62
|
|
|
projectRepository.findOneById('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2') |
|
63
|
|
|
).thenResolve(null); |
|
64
|
|
|
|
|
65
|
|
|
try { |
|
66
|
|
|
await queryHandler.execute(query); |
|
67
|
|
|
} catch (e) { |
|
68
|
|
|
expect(e).toBeInstanceOf(ProjectNotFoundException); |
|
69
|
|
|
expect(e.message).toBe('crm.projects.errors.not_found'); |
|
70
|
|
|
verify( |
|
71
|
|
|
projectRepository.findOneById('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2') |
|
72
|
|
|
).once(); |
|
73
|
|
|
} |
|
74
|
|
|
}); |
|
75
|
|
|
}); |
|
76
|
|
|
|