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

Complexity

Total Complexity 1
Complexity/F 0

Size

Lines of Code 49
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 1
eloc 40
mnd 1
bc 1
fnc 0
dl 0
loc 49
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
    await handler.execute(command);
28
29
    verify(
30
      discountRepository.remove(instance(removedDiscount))
31
    ).once();
32
    verify(discountRepository.findOneById('17efcbee-bd2f-410e-9e99-51684b592bad')).once();
33
  });
34
35
  it('testDiscountNotFound', async () => {
36
    when(discountRepository.findOneById('17efcbee-bd2f-410e-9e99-51684b592bad'))
37
      .thenResolve(null);
38
39
    try {
40
      expect(await handler.execute(command)).toBeUndefined();
41
    } catch (e) {
42
      expect(e).toBeInstanceOf(DiscountNotFoundException);
43
      expect(e.message).toBe('schools.discounts.errors.not_found');
44
      verify(discountRepository.findOneById('17efcbee-bd2f-410e-9e99-51684b592bad')).once();
45
      verify(discountRepository.remove(anything())).never();
46
    }
47
  });
48
});
49