1
|
|
|
import {mock, instance, when, verify, 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 {UpdateCustomerCommand} from './UpdateCustomerCommand'; |
6
|
|
|
import {CustomerNotFoundException} from 'src/Domain/Customer/Exception/CustomerNotFoundException'; |
7
|
|
|
import {CustomerAlreadyExistException} from 'src/Domain/Customer/Exception/CustomerAlreadyExistException'; |
8
|
|
|
import {UpdateCustomerCommandHandler} from './UpdateCustomerCommandHandler'; |
9
|
|
|
import {AddressRepository} from 'src/Infrastructure/Customer/Repository/AddressRepository'; |
10
|
|
|
import {Address} from 'src/Domain/Customer/Address.entity'; |
11
|
|
|
|
12
|
|
|
describe('UpdateCustomerCommandHandler', () => { |
13
|
|
|
let customerRepository: CustomerRepository; |
14
|
|
|
let addressRepository: AddressRepository; |
15
|
|
|
let isCustomerAlreadyExist: IsCustomerAlreadyExist; |
16
|
|
|
let updatedCustomer: Customer; |
17
|
|
|
let updatedAddress: Address; |
18
|
|
|
let handler: UpdateCustomerCommandHandler; |
19
|
|
|
|
20
|
|
|
const command = new UpdateCustomerCommand( |
21
|
|
|
'afda00b1-bf49-4102-9bc2-bce17f3acd48', |
22
|
|
|
'Customer', |
23
|
|
|
'2 rue Dieu', |
24
|
|
|
'Paris', |
25
|
|
|
'75010', |
26
|
|
|
'FR' |
27
|
|
|
); |
28
|
|
|
|
29
|
|
|
beforeEach(() => { |
30
|
|
|
customerRepository = mock(CustomerRepository); |
31
|
|
|
addressRepository = mock(AddressRepository); |
32
|
|
|
isCustomerAlreadyExist = mock(IsCustomerAlreadyExist); |
33
|
|
|
updatedCustomer = mock(Customer); |
34
|
|
|
updatedAddress = mock(Address); |
35
|
|
|
|
36
|
|
|
handler = new UpdateCustomerCommandHandler( |
37
|
|
|
instance(customerRepository), |
38
|
|
|
instance(addressRepository), |
39
|
|
|
instance(isCustomerAlreadyExist) |
40
|
|
|
); |
41
|
|
|
}); |
42
|
|
|
|
43
|
|
|
it('testUpdateSuccessfully', async () => { |
44
|
|
|
when( |
45
|
|
|
customerRepository.findOneById('afda00b1-bf49-4102-9bc2-bce17f3acd48') |
46
|
|
|
).thenResolve(instance(updatedCustomer)); |
47
|
|
|
|
48
|
|
|
when(updatedCustomer.getAddress()).thenReturn(instance(updatedAddress)); |
49
|
|
|
when(updatedCustomer.getName()).thenReturn('Old customer'); |
50
|
|
|
when(isCustomerAlreadyExist.isSatisfiedBy('Customer')).thenResolve(false); |
51
|
|
|
|
52
|
|
|
// Command return nothing |
53
|
|
|
expect(await handler.execute(command)).toBeUndefined(); |
54
|
|
|
|
55
|
|
|
verify(isCustomerAlreadyExist.isSatisfiedBy('Customer')).once(); |
56
|
|
|
verify(customerRepository.save(instance(updatedCustomer))).once(); |
57
|
|
|
verify(addressRepository.save(instance(updatedAddress))).once(); |
58
|
|
|
verify(updatedCustomer.getAddress()).once(); |
59
|
|
|
verify(updatedAddress.update('2 rue Dieu', 'Paris', '75010', 'FR')).once(); |
60
|
|
|
verify(updatedCustomer.updateName('Customer')).once(); |
61
|
|
|
verify( |
62
|
|
|
updatedAddress.update('2 rue Dieu', 'Paris', '75010', 'FR') |
63
|
|
|
).calledBefore(addressRepository.save(instance(updatedAddress))); |
64
|
|
|
verify(updatedCustomer.updateName('Customer')).calledBefore( |
65
|
|
|
customerRepository.save(instance(updatedCustomer)) |
66
|
|
|
); |
67
|
|
|
verify(updatedCustomer.getName()).once(); |
68
|
|
|
}); |
69
|
|
|
|
70
|
|
|
it('testCustomerNotFound', async () => { |
71
|
|
|
when( |
72
|
|
|
customerRepository.findOneById('afda00b1-bf49-4102-9bc2-bce17f3acd48') |
73
|
|
|
).thenResolve(null); |
74
|
|
|
|
75
|
|
|
try { |
76
|
|
|
await handler.execute(command); |
77
|
|
|
} catch (e) { |
78
|
|
|
expect(e).toBeInstanceOf(CustomerNotFoundException); |
79
|
|
|
expect(e.message).toBe('crm.customers.errors.not_found'); |
80
|
|
|
verify(isCustomerAlreadyExist.isSatisfiedBy(anything())).never(); |
81
|
|
|
verify(addressRepository.save(anything())).never(); |
82
|
|
|
verify( |
83
|
|
|
updatedAddress.update(anything(), anything(), anything(), anything()) |
84
|
|
|
).never(); |
85
|
|
|
verify(customerRepository.save(anything())).never(); |
86
|
|
|
verify(updatedCustomer.updateName(anything())).never(); |
87
|
|
|
verify(updatedCustomer.getName()).never(); |
88
|
|
|
} |
89
|
|
|
}); |
90
|
|
|
|
91
|
|
|
it('testCustomerAlreadyExist', async () => { |
92
|
|
|
when( |
93
|
|
|
customerRepository.findOneById('afda00b1-bf49-4102-9bc2-bce17f3acd48') |
94
|
|
|
).thenResolve(instance(updatedCustomer)); |
95
|
|
|
when(isCustomerAlreadyExist.isSatisfiedBy('Customer')).thenResolve(true); |
96
|
|
|
|
97
|
|
|
try { |
98
|
|
|
await handler.execute(command); |
99
|
|
|
} catch (e) { |
100
|
|
|
expect(e).toBeInstanceOf(CustomerAlreadyExistException); |
101
|
|
|
expect(e.message).toBe('crm.customers.errors.already_exist'); |
102
|
|
|
verify(isCustomerAlreadyExist.isSatisfiedBy('Customer')).once(); |
103
|
|
|
verify(customerRepository.save(anything())).never(); |
104
|
|
|
verify(updatedCustomer.updateName(anything())).never(); |
105
|
|
|
verify(addressRepository.save(anything())).never(); |
106
|
|
|
verify( |
107
|
|
|
updatedAddress.update(anything(), anything(), anything(), anything()) |
108
|
|
|
).never(); |
109
|
|
|
verify(updatedCustomer.getName()).once(); |
110
|
|
|
} |
111
|
|
|
}); |
112
|
|
|
}); |
113
|
|
|
|