1
|
|
|
import { mock, instance, when, verify, deepEqual, anything } from 'ts-mockito'; |
2
|
|
|
import { ShippingCostRepository } from 'src/Infrastructure/Order/Repository/ShippingCostRepository'; |
3
|
|
|
import { IsShippingCostAlreadyExist } from 'src/Domain/Order/Specification/IsShippingCostAlreadyExist'; |
4
|
|
|
import { ShippingCost } from 'src/Domain/Order/ShippingCost.entity'; |
5
|
|
|
import { ShippingCostAlreadyExistException } from 'src/Domain/Order/Exception/ShippingCostAlreadyExistException'; |
6
|
|
|
import { CreateShippingCostCommandHandler } from './CreateShippingCostCommandHandler'; |
7
|
|
|
import { CreateShippingCostCommand } from './CreateShippingCostCommand'; |
8
|
|
|
|
9
|
|
|
describe('CreateShippingCostCommandHandler', () => { |
10
|
|
|
let shippingcostRepository: ShippingCostRepository; |
11
|
|
|
let isShippingCostAlreadyExist: IsShippingCostAlreadyExist; |
12
|
|
|
let createdShippingCost: ShippingCost; |
13
|
|
|
let handler: CreateShippingCostCommandHandler; |
14
|
|
|
|
15
|
|
|
const command = new CreateShippingCostCommand( |
16
|
|
|
1000, |
17
|
|
|
9.99, |
18
|
|
|
); |
19
|
|
|
|
20
|
|
|
beforeEach(() => { |
21
|
|
|
shippingcostRepository = mock(ShippingCostRepository); |
22
|
|
|
isShippingCostAlreadyExist = mock(IsShippingCostAlreadyExist); |
23
|
|
|
createdShippingCost = mock(ShippingCost); |
24
|
|
|
|
25
|
|
|
handler = new CreateShippingCostCommandHandler( |
26
|
|
|
instance(shippingcostRepository), |
27
|
|
|
instance(isShippingCostAlreadyExist) |
28
|
|
|
); |
29
|
|
|
}); |
30
|
|
|
|
31
|
|
|
it('testShippingCostCreatedSuccessfully', async () => { |
32
|
|
|
when(isShippingCostAlreadyExist.isSatisfiedBy(1000)).thenResolve(false); |
33
|
|
|
when(createdShippingCost.getId()).thenReturn( |
34
|
|
|
'2d5fb4da-12c2-11ea-8d71-362b9e155667' |
35
|
|
|
); |
36
|
|
|
when( |
37
|
|
|
shippingcostRepository.save(deepEqual(new ShippingCost(1000,999))) |
38
|
|
|
).thenResolve(instance(createdShippingCost)); |
39
|
|
|
|
40
|
|
|
expect(await handler.execute(command)).toBe( |
41
|
|
|
'2d5fb4da-12c2-11ea-8d71-362b9e155667' |
42
|
|
|
); |
43
|
|
|
|
44
|
|
|
verify(isShippingCostAlreadyExist.isSatisfiedBy(1000)).once(); |
45
|
|
|
verify( |
46
|
|
|
shippingcostRepository.save(deepEqual(new ShippingCost(1000, 999))) |
47
|
|
|
).once(); |
48
|
|
|
verify(createdShippingCost.getId()).once(); |
49
|
|
|
}); |
50
|
|
|
|
51
|
|
|
it('testShippingCostAlreadyExist', async () => { |
52
|
|
|
when(isShippingCostAlreadyExist.isSatisfiedBy(1000)).thenResolve(true); |
53
|
|
|
|
54
|
|
|
try { |
55
|
|
|
expect(await handler.execute(command)).toBeUndefined(); |
56
|
|
|
} catch (e) { |
57
|
|
|
expect(e).toBeInstanceOf(ShippingCostAlreadyExistException); |
58
|
|
|
expect(e.message).toBe('shipping_costs.errors.already_exist'); |
59
|
|
|
verify(isShippingCostAlreadyExist.isSatisfiedBy(1000)).once(); |
60
|
|
|
verify(shippingcostRepository.save(anything())).never(); |
61
|
|
|
verify(createdShippingCost.getId()).never(); |
62
|
|
|
} |
63
|
|
|
}); |
64
|
|
|
}); |
65
|
|
|
|