Passed
Push — master ( f58cd4...647f1d )
by Mathieu
01:52
created

api/src/Application/School/Command/Discount/RemoveDiscountCommandHandler.spec.ts   A

Complexity

Total Complexity 1
Complexity/F 0

Size

Lines of Code 56
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 1
eloc 46
mnd 1
bc 1
fnc 0
dl 0
loc 56
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
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