|
1
|
|
|
import { mock, instance, when, verify, anything } from 'ts-mockito'; |
|
2
|
|
|
import { ShippingCostRepository } from 'src/Infrastructure/Order/Repository/ShippingCostRepository'; |
|
3
|
|
|
import { ShippingCost } from 'src/Domain/Order/ShippingCost.entity'; |
|
4
|
|
|
import { RemoveShippingCostCommandHandler } from './RemoveShippingCostCommandHandler'; |
|
5
|
|
|
import { RemoveShippingCostCommand } from './RemoveShippingCostCommand'; |
|
6
|
|
|
import { ShippingCostNotFoundException } from 'src/Domain/Order/Exception/ShippingCostNotFoundException'; |
|
7
|
|
|
|
|
8
|
|
|
describe('RemoveShippingCostCommandHandler', () => { |
|
9
|
|
|
let shippingcostRepository: ShippingCostRepository; |
|
10
|
|
|
let removedShippingCost: ShippingCost; |
|
11
|
|
|
let handler: RemoveShippingCostCommandHandler; |
|
12
|
|
|
|
|
13
|
|
|
const command = new RemoveShippingCostCommand('17efcbee-bd2f-410e-9e99-51684b592bad'); |
|
14
|
|
|
|
|
15
|
|
|
beforeEach(() => { |
|
16
|
|
|
shippingcostRepository = mock(ShippingCostRepository); |
|
17
|
|
|
removedShippingCost = mock(ShippingCost); |
|
18
|
|
|
|
|
19
|
|
|
handler = new RemoveShippingCostCommandHandler( |
|
20
|
|
|
instance(shippingcostRepository) |
|
21
|
|
|
); |
|
22
|
|
|
}); |
|
23
|
|
|
|
|
24
|
|
|
it('testShippingCostRemovedSuccessfully', async () => { |
|
25
|
|
|
when(shippingcostRepository.findOneById('17efcbee-bd2f-410e-9e99-51684b592bad')) |
|
26
|
|
|
.thenResolve(instance(removedShippingCost)); |
|
27
|
|
|
|
|
28
|
|
|
await handler.execute(command); |
|
29
|
|
|
|
|
30
|
|
|
verify( |
|
31
|
|
|
shippingcostRepository.remove(instance(removedShippingCost)) |
|
32
|
|
|
).once(); |
|
33
|
|
|
verify(shippingcostRepository.findOneById('17efcbee-bd2f-410e-9e99-51684b592bad')).once(); |
|
34
|
|
|
}); |
|
35
|
|
|
|
|
36
|
|
|
it('testShippingCostNotFound', async () => { |
|
37
|
|
|
when(shippingcostRepository.findOneById('17efcbee-bd2f-410e-9e99-51684b592bad')) |
|
38
|
|
|
.thenResolve(null); |
|
39
|
|
|
|
|
40
|
|
|
try { |
|
41
|
|
|
expect(await handler.execute(command)).toBeUndefined(); |
|
42
|
|
|
} catch (e) { |
|
43
|
|
|
expect(e).toBeInstanceOf(ShippingCostNotFoundException); |
|
44
|
|
|
expect(e.message).toBe('shipping_costs.errors.not_found'); |
|
45
|
|
|
verify(shippingcostRepository.findOneById('17efcbee-bd2f-410e-9e99-51684b592bad')).once(); |
|
46
|
|
|
verify(shippingcostRepository.remove(anything())).never(); |
|
47
|
|
|
} |
|
48
|
|
|
}); |
|
49
|
|
|
}); |
|
50
|
|
|
|