1
|
|
|
import { mock, instance, when, verify, deepEqual, anything } from 'ts-mockito'; |
2
|
|
|
import { CustomerRepository } from 'src/Infrastructure/Customer/Repository/CustomerRepository'; |
3
|
|
|
import { GenerateInvoiceCommandHandler } from './GenerateInvoiceCommandHandler'; |
4
|
|
|
import { GenerateInvoiceCommand } from './GenerateInvoiceCommand'; |
5
|
|
|
import { User } from 'src/Domain/HumanResource/User/User.entity'; |
6
|
|
|
import { CustomerNotFoundException } from 'src/Domain/Customer/Exception/CustomerNotFoundException'; |
7
|
|
|
import { InvoiceIdGenerator } from 'src/Domain/Accounting/Generators/InvoiceIdGenerator'; |
8
|
|
|
import { InvoiceStatus } from 'src/Domain/Accounting/Invoice.entity'; |
9
|
|
|
import { EventRepository } from 'src/Infrastructure/FairCalendar/Repository/EventRepository'; |
10
|
|
|
import { InvoiceRepository } from 'src/Infrastructure/Accounting/Repository/InvoiceRepository'; |
11
|
|
|
import { InvoiceItemRepository } from 'src/Infrastructure/Accounting/Repository/InvoiceItemRepository'; |
12
|
|
|
import { DateUtilsAdapter } from 'src/Infrastructure/Adapter/DateUtilsAdapter'; |
13
|
|
|
|
14
|
|
|
describe('GenerateInvoiceCommandHandler', () => { |
15
|
|
|
let customerRepository: CustomerRepository; |
16
|
|
|
let eventRepository: EventRepository; |
17
|
|
|
let invoiceRepository: InvoiceRepository; |
18
|
|
|
let invoiceItemRepository: InvoiceItemRepository; |
19
|
|
|
let invoiceIdGenerator: InvoiceIdGenerator; |
20
|
|
|
let dateUtilsAdapter: DateUtilsAdapter; |
21
|
|
|
let handler: GenerateInvoiceCommandHandler; |
22
|
|
|
|
23
|
|
|
const user = mock(User); |
24
|
|
|
const date = new Date('2020-11-23T17:43:14.299Z'); |
25
|
|
|
const command = new GenerateInvoiceCommand( |
26
|
|
|
'a491ccc9-df7c-4fc6-8e90-db816208f689', |
27
|
|
|
InvoiceStatus.DRAFT, |
28
|
|
|
30, |
29
|
|
|
date, |
30
|
|
|
instance(user) |
31
|
|
|
); |
32
|
|
|
|
33
|
|
|
beforeEach(() => { |
34
|
|
|
customerRepository = mock(CustomerRepository); |
35
|
|
|
invoiceIdGenerator = mock(InvoiceIdGenerator); |
36
|
|
|
eventRepository = mock(EventRepository); |
37
|
|
|
invoiceRepository = mock(InvoiceRepository); |
38
|
|
|
invoiceItemRepository = mock(InvoiceItemRepository); |
39
|
|
|
dateUtilsAdapter = mock(DateUtilsAdapter); |
40
|
|
|
|
41
|
|
|
handler = new GenerateInvoiceCommandHandler( |
42
|
|
|
instance(customerRepository), |
43
|
|
|
instance(eventRepository), |
44
|
|
|
instance(invoiceRepository), |
45
|
|
|
instance(invoiceItemRepository), |
46
|
|
|
instance(dateUtilsAdapter), |
47
|
|
|
instance(invoiceIdGenerator), |
48
|
|
|
); |
49
|
|
|
}); |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
it('testCustomerNotFound', async () => { |
53
|
|
|
when( |
54
|
|
|
customerRepository.findOneById('a491ccc9-df7c-4fc6-8e90-db816208f689') |
55
|
|
|
).thenResolve(null); |
56
|
|
|
|
57
|
|
|
try { |
58
|
|
|
await handler.execute(command); |
59
|
|
|
} catch (e) { |
60
|
|
|
expect(e).toBeInstanceOf(CustomerNotFoundException); |
61
|
|
|
expect(e.message).toBe('crm.customers.errors.not_found'); |
62
|
|
|
verify( |
63
|
|
|
customerRepository.findOneById('a491ccc9-df7c-4fc6-8e90-db816208f689') |
64
|
|
|
).once(); |
65
|
|
|
} |
66
|
|
|
}); |
67
|
|
|
}); |
68
|
|
|
|