1
|
|
|
import {mock, instance, when, verify, deepEqual, anything} from 'ts-mockito'; |
2
|
|
|
import {CustomerRepository} from 'src/Infrastructure/Customer/Repository/CustomerRepository'; |
3
|
|
|
import {IsCustomerAlreadyExist} from 'src/Domain/Customer/Specification/IsCustomerAlreadyExist'; |
4
|
|
|
import {Customer} from 'src/Domain/Customer/Customer.entity'; |
5
|
|
|
import {CreateCustomerCommandHandler} from 'src/Application/Customer/Command/CreateCustomerCommandHandler'; |
6
|
|
|
import {CreateCustomerCommand} from 'src/Application/Customer/Command/CreateCustomerCommand'; |
7
|
|
|
import {CustomerAlreadyExistException} from 'src/Domain/Customer/Exception/CustomerAlreadyExistException'; |
8
|
|
|
import {AddressRepository} from 'src/Infrastructure/Customer/Repository/AddressRepository'; |
9
|
|
|
import {Address} from 'src/Domain/Customer/Address.entity'; |
10
|
|
|
|
11
|
|
|
describe('CreateCustomerCommandHandler', () => { |
12
|
|
|
let customerRepository: CustomerRepository; |
13
|
|
|
let addressRepository: AddressRepository; |
14
|
|
|
let isCustomerAlreadyExist: IsCustomerAlreadyExist; |
15
|
|
|
let createdCustomer: Customer; |
16
|
|
|
let createdAddress: Address; |
17
|
|
|
let handler: CreateCustomerCommandHandler; |
18
|
|
|
|
19
|
|
|
const command = new CreateCustomerCommand( |
20
|
|
|
'Customer', |
21
|
|
|
'2 rue Dieu', |
22
|
|
|
'Paris', |
23
|
|
|
'75010', |
24
|
|
|
'FR' |
25
|
|
|
); |
26
|
|
|
|
27
|
|
|
beforeEach(() => { |
28
|
|
|
customerRepository = mock(CustomerRepository); |
29
|
|
|
addressRepository = mock(AddressRepository); |
30
|
|
|
isCustomerAlreadyExist = mock(IsCustomerAlreadyExist); |
31
|
|
|
createdCustomer = mock(Customer); |
32
|
|
|
createdAddress = mock(Address); |
33
|
|
|
|
34
|
|
|
handler = new CreateCustomerCommandHandler( |
35
|
|
|
instance(customerRepository), |
36
|
|
|
instance(addressRepository), |
37
|
|
|
instance(isCustomerAlreadyExist) |
38
|
|
|
); |
39
|
|
|
}); |
40
|
|
|
|
41
|
|
|
it('testCustomerCreatedSuccessfully', async () => { |
42
|
|
|
when(isCustomerAlreadyExist.isSatisfiedBy('Customer')).thenResolve(false); |
43
|
|
|
when(createdCustomer.getId()).thenReturn( |
44
|
|
|
'2d5fb4da-12c2-11ea-8d71-362b9e155667' |
45
|
|
|
); |
46
|
|
|
when(createdCustomer.getName()).thenReturn('Customer'); |
47
|
|
|
when( |
48
|
|
|
addressRepository.save( |
49
|
|
|
deepEqual(new Address('2 rue Dieu', 'Paris', '75010', 'FR')) |
50
|
|
|
) |
51
|
|
|
).thenResolve(instance(createdAddress)); |
52
|
|
|
when( |
53
|
|
|
customerRepository.save( |
54
|
|
|
deepEqual(new Customer('Customer', instance(createdAddress))) |
55
|
|
|
) |
56
|
|
|
).thenResolve(instance(createdCustomer)); |
57
|
|
|
|
58
|
|
|
expect(await handler.execute(command)).toBe( |
59
|
|
|
'2d5fb4da-12c2-11ea-8d71-362b9e155667' |
60
|
|
|
); |
61
|
|
|
|
62
|
|
|
verify(isCustomerAlreadyExist.isSatisfiedBy('Customer')).once(); |
63
|
|
|
verify( |
64
|
|
|
customerRepository.save( |
65
|
|
|
deepEqual(new Customer('Customer', instance(createdAddress))) |
66
|
|
|
) |
67
|
|
|
).once(); |
68
|
|
|
verify(createdCustomer.getId()).once(); |
69
|
|
|
}); |
70
|
|
|
|
71
|
|
|
it('testCustomerAlreadyExist', async () => { |
72
|
|
|
when(isCustomerAlreadyExist.isSatisfiedBy('Customer')).thenResolve(true); |
73
|
|
|
|
74
|
|
|
try { |
75
|
|
|
await handler.execute(command); |
76
|
|
|
} catch (e) { |
77
|
|
|
expect(e).toBeInstanceOf(CustomerAlreadyExistException); |
78
|
|
|
expect(e.message).toBe('crm.customers.errors.already_exist'); |
79
|
|
|
verify(isCustomerAlreadyExist.isSatisfiedBy('Customer')).once(); |
80
|
|
|
verify(customerRepository.save(anything())).never(); |
81
|
|
|
verify(createdCustomer.getId()).never(); |
82
|
|
|
} |
83
|
|
|
}); |
84
|
|
|
}); |
85
|
|
|
|