1
|
|
|
import { mock, instance, when, verify, deepEqual, anything } from 'ts-mockito'; |
2
|
|
|
import { ProductRepository } from 'src/Infrastructure/Product/Repository/ProductRepository'; |
3
|
|
|
import { IsProductAlreadyExist } from 'src/Domain/Product/Specification/IsProductAlreadyExist'; |
4
|
|
|
import { Product } from 'src/Domain/Product/Product.entity'; |
5
|
|
|
import { CreateProductCommandHandler } from 'src/Application/Product/Command/CreateProductCommandHandler'; |
6
|
|
|
import { CreateProductCommand } from 'src/Application/Product/Command/CreateProductCommand'; |
7
|
|
|
import { ProductAlreadyExistException } from 'src/Domain/Product/Exception/ProductAlreadyExistException'; |
8
|
|
|
|
9
|
|
|
describe('CreateProductCommandHandler', () => { |
10
|
|
|
let productRepository: ProductRepository; |
11
|
|
|
let isProductAlreadyExist: IsProductAlreadyExist; |
12
|
|
|
let createdProduct: Product; |
13
|
|
|
let handler: CreateProductCommandHandler; |
14
|
|
|
|
15
|
|
|
const command = new CreateProductCommand( |
16
|
|
|
'Mug', |
17
|
|
|
'Mug portrait enfant', |
18
|
|
|
9.99, |
19
|
|
|
1000 |
20
|
|
|
); |
21
|
|
|
|
22
|
|
|
beforeEach(() => { |
23
|
|
|
productRepository = mock(ProductRepository); |
24
|
|
|
isProductAlreadyExist = mock(IsProductAlreadyExist); |
25
|
|
|
createdProduct = mock(Product); |
26
|
|
|
|
27
|
|
|
handler = new CreateProductCommandHandler( |
28
|
|
|
instance(productRepository), |
29
|
|
|
instance(isProductAlreadyExist) |
30
|
|
|
); |
31
|
|
|
}); |
32
|
|
|
|
33
|
|
|
it('testProductCreatedSuccessfully', async () => { |
34
|
|
|
when(isProductAlreadyExist.isSatisfiedBy('Mug')).thenResolve(false); |
35
|
|
|
when(createdProduct.getId()).thenReturn( |
36
|
|
|
'2d5fb4da-12c2-11ea-8d71-362b9e155667' |
37
|
|
|
); |
38
|
|
|
when( |
39
|
|
|
productRepository.save( |
40
|
|
|
deepEqual( |
41
|
|
|
new Product( |
42
|
|
|
'Mug', |
43
|
|
|
'Mug portrait enfant', |
44
|
|
|
999, |
45
|
|
|
1000 |
46
|
|
|
) |
47
|
|
|
) |
48
|
|
|
) |
49
|
|
|
).thenResolve(instance(createdProduct)); |
50
|
|
|
|
51
|
|
|
expect(await handler.execute(command)).toBe( |
52
|
|
|
'2d5fb4da-12c2-11ea-8d71-362b9e155667' |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
verify(isProductAlreadyExist.isSatisfiedBy('Mug')).once(); |
56
|
|
|
verify( |
57
|
|
|
productRepository.save( |
58
|
|
|
deepEqual( |
59
|
|
|
new Product( |
60
|
|
|
'Mug', |
61
|
|
|
'Mug portrait enfant', |
62
|
|
|
999, |
63
|
|
|
1000 |
64
|
|
|
) |
65
|
|
|
) |
66
|
|
|
) |
67
|
|
|
).once(); |
68
|
|
|
verify(createdProduct.getId()).once(); |
69
|
|
|
}); |
70
|
|
|
|
71
|
|
|
it('testProductAlreadyExist', async () => { |
72
|
|
|
when(isProductAlreadyExist.isSatisfiedBy('Mug')).thenResolve(true); |
73
|
|
|
|
74
|
|
|
try { |
75
|
|
|
expect(await handler.execute(command)).toBeUndefined(); |
76
|
|
|
} catch (e) { |
77
|
|
|
expect(e).toBeInstanceOf(ProductAlreadyExistException); |
78
|
|
|
expect(e.message).toBe('products.errors.already_exist'); |
79
|
|
|
verify(isProductAlreadyExist.isSatisfiedBy('Mug')).once(); |
80
|
|
|
verify(productRepository.save(anything())).never(); |
81
|
|
|
verify(createdProduct.getId()).never(); |
82
|
|
|
} |
83
|
|
|
}); |
84
|
|
|
}); |
85
|
|
|
|