|
1
|
|
|
import { mock, instance, when, verify } from 'ts-mockito'; |
|
2
|
|
|
import { GetShippingCostByIdQueryHandler } from './GetShippingCostByIdQueryHandler'; |
|
3
|
|
|
import { GetShippingCostByIdQuery } from './GetShippingCostByIdQuery'; |
|
4
|
|
|
import { ShippingCostRepository } from 'src/Infrastructure/Order/Repository/ShippingCostRepository'; |
|
5
|
|
|
import { ShippingCostView } from '../../View/ShippingCostView'; |
|
6
|
|
|
import { ShippingCost } from 'src/Domain/Order/ShippingCost.entity'; |
|
7
|
|
|
import { ShippingCostNotFoundException } from 'src/Domain/Order/Exception/ShippingCostNotFoundException'; |
|
8
|
|
|
|
|
9
|
|
|
describe('GetShippingCostByIdQueryHandler', () => { |
|
10
|
|
|
const query = new GetShippingCostByIdQuery('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2'); |
|
11
|
|
|
|
|
12
|
|
|
it('testGetShippingCost', async () => { |
|
13
|
|
|
const shippingcostRepository = mock(ShippingCostRepository); |
|
14
|
|
|
const queryHandler = new GetShippingCostByIdQueryHandler(instance(shippingcostRepository)); |
|
15
|
|
|
const expectedResult = new ShippingCostView( |
|
16
|
|
|
'eb9e1d9b-dce2-48a9-b64f-f0872f3157d2', |
|
17
|
|
|
1000, |
|
18
|
|
|
9.99 |
|
19
|
|
|
); |
|
20
|
|
|
|
|
21
|
|
|
const shippingcost = mock(ShippingCost); |
|
22
|
|
|
when(shippingcost.getId()).thenReturn('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2'); |
|
23
|
|
|
when(shippingcost.getGrams()).thenReturn(1000); |
|
24
|
|
|
when(shippingcost.getPriceFromCents()).thenReturn(9.99); |
|
25
|
|
|
when( |
|
26
|
|
|
shippingcostRepository.findOneById('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2') |
|
27
|
|
|
).thenResolve(instance(shippingcost)); |
|
28
|
|
|
|
|
29
|
|
|
expect(await queryHandler.execute(query)).toMatchObject(expectedResult); |
|
30
|
|
|
|
|
31
|
|
|
verify( |
|
32
|
|
|
shippingcostRepository.findOneById('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2') |
|
33
|
|
|
).once(); |
|
34
|
|
|
}); |
|
35
|
|
|
|
|
36
|
|
|
it('testGetShippingCostNotFound', async () => { |
|
37
|
|
|
const shippingcostRepository = mock(ShippingCostRepository); |
|
38
|
|
|
const queryHandler = new GetShippingCostByIdQueryHandler(instance(shippingcostRepository)); |
|
39
|
|
|
when( |
|
40
|
|
|
shippingcostRepository.findOneById('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2') |
|
41
|
|
|
).thenResolve(null); |
|
42
|
|
|
|
|
43
|
|
|
try { |
|
44
|
|
|
expect(await queryHandler.execute(query)).toBeUndefined(); |
|
45
|
|
|
} catch (e) { |
|
46
|
|
|
expect(e).toBeInstanceOf(ShippingCostNotFoundException); |
|
47
|
|
|
expect(e.message).toBe('shipping_costs.errors.not_found'); |
|
48
|
|
|
verify( |
|
49
|
|
|
shippingcostRepository.findOneById('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2') |
|
50
|
|
|
).once(); |
|
51
|
|
|
} |
|
52
|
|
|
}); |
|
53
|
|
|
}); |
|
54
|
|
|
|