|
1
|
|
|
import { mock, instance, when, verify, anything } from 'ts-mockito'; |
|
2
|
|
|
import { Discount } from 'src/Domain/School/Discount.entity'; |
|
3
|
|
|
import { DiscountNotFoundException } from 'src/Domain/School/Exception/DiscountNotFoundException'; |
|
4
|
|
|
import { DiscountRepository } from 'src/Infrastructure/School/Repository/DiscountRepository'; |
|
5
|
|
|
import { RemoveDiscountCommand } from './RemoveDiscountCommand'; |
|
6
|
|
|
import { RemoveDiscountCommandHandler } from './RemoveDiscountCommandHandler'; |
|
7
|
|
|
|
|
8
|
|
|
describe('RemoveDiscountCommandHandler', () => { |
|
9
|
|
|
let discountRepository: DiscountRepository; |
|
10
|
|
|
let removedDiscount: Discount; |
|
11
|
|
|
let handler: RemoveDiscountCommandHandler; |
|
12
|
|
|
|
|
13
|
|
|
const command = new RemoveDiscountCommand('17efcbee-bd2f-410e-9e99-51684b592bad'); |
|
14
|
|
|
|
|
15
|
|
|
beforeEach(() => { |
|
16
|
|
|
discountRepository = mock(DiscountRepository); |
|
17
|
|
|
removedDiscount = mock(Discount); |
|
18
|
|
|
|
|
19
|
|
|
handler = new RemoveDiscountCommandHandler( |
|
20
|
|
|
instance(discountRepository) |
|
21
|
|
|
); |
|
22
|
|
|
}); |
|
23
|
|
|
|
|
24
|
|
|
it('testDiscountRemovedSuccessfully', async () => { |
|
25
|
|
|
when(discountRepository.findOneById('17efcbee-bd2f-410e-9e99-51684b592bad')) |
|
26
|
|
|
.thenResolve(instance(removedDiscount)); |
|
27
|
|
|
when(removedDiscount.getId()).thenReturn( |
|
28
|
|
|
'17efcbee-bd2f-410e-9e99-51684b592bad' |
|
29
|
|
|
); |
|
30
|
|
|
when( |
|
31
|
|
|
discountRepository.save(instance(removedDiscount)) |
|
32
|
|
|
).thenResolve(instance(removedDiscount)); |
|
33
|
|
|
|
|
34
|
|
|
await handler.execute(command); |
|
35
|
|
|
|
|
36
|
|
|
verify( |
|
37
|
|
|
discountRepository.remove(instance(removedDiscount)) |
|
38
|
|
|
).once(); |
|
39
|
|
|
verify(discountRepository.findOneById('17efcbee-bd2f-410e-9e99-51684b592bad')).once(); |
|
40
|
|
|
}); |
|
41
|
|
|
|
|
42
|
|
|
it('testDiscountNotFound', async () => { |
|
43
|
|
|
when(discountRepository.findOneById('17efcbee-bd2f-410e-9e99-51684b592bad')) |
|
44
|
|
|
.thenResolve(null); |
|
45
|
|
|
|
|
46
|
|
|
try { |
|
47
|
|
|
expect(await handler.execute(command)).toBeUndefined(); |
|
48
|
|
|
} catch (e) { |
|
49
|
|
|
expect(e).toBeInstanceOf(DiscountNotFoundException); |
|
50
|
|
|
expect(e.message).toBe('schools.discounts.errors.not_found'); |
|
51
|
|
|
verify(discountRepository.findOneById('17efcbee-bd2f-410e-9e99-51684b592bad')).once(); |
|
52
|
|
|
verify(discountRepository.remove(anything())).never(); |
|
53
|
|
|
} |
|
54
|
|
|
}); |
|
55
|
|
|
}); |
|
56
|
|
|
|