Passed
Push — master ( 1029c0...f58cd4 )
by Mathieu
01:51 queued 22s
created

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

Complexity

Total Complexity 2
Complexity/F 0

Size

Lines of Code 116
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 90
mnd 2
bc 2
fnc 0
dl 0
loc 116
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
import { mock, instance, when, verify, deepEqual, anything } from 'ts-mockito';
2
import { DiscountRepository } from 'src/Infrastructure/School/Repository/DiscountRepository';
3
import { Discount, DiscountType } from 'src/Domain/School/Discount.entity';
4
import { CreateDiscountCommandHandler } from './CreateDiscountCommandHandler';
5
import { SchoolRepository } from 'src/Infrastructure/School/Repository/SchoolRepository';
6
import { CreateDiscountCommand } from './CreateDiscountCommand';
7
import { School } from 'src/Domain/School/School.entity';
8
import { SchoolNotFoundException } from 'src/Domain/School/Exception/SchoolNotFoundException';
9
import { IsDiscountAlreadyExist } from 'src/Domain/School/Specification/Discount/IsDiscountAlreadyExist';
10
import { DiscountAlreadyExistException } from 'src/Domain/School/Exception/DiscountAlreadyExistException';
11
12
describe('CreateDiscountCommandHandler', () => {
13
  let schoolRepository: SchoolRepository;
14
  let isDiscountAlreadyExist: IsDiscountAlreadyExist;
15
  let discountRepository: DiscountRepository;
16
  let createdDiscount: Discount;
17
  let handler: CreateDiscountCommandHandler;
18
19
  const school = mock(School);
20
  const command = new CreateDiscountCommand(
21
    DiscountType.PERCENT,
22
    100,
23
    10,
24
    '553e2b3c-eb11-42b1-8f76-903add071ca7',
25
  );
26
27
  beforeEach(() => {
28
    schoolRepository = mock(SchoolRepository);
29
    discountRepository = mock(DiscountRepository);
30
    isDiscountAlreadyExist = mock(IsDiscountAlreadyExist);
31
    createdDiscount = mock(Discount);
32
33
    handler = new CreateDiscountCommandHandler(
34
      instance(schoolRepository),
35
      instance(discountRepository),
36
      instance(isDiscountAlreadyExist)
37
    );
38
  });
39
40
  it('testDiscountCreatedSuccessfully', async () => {
41
    when(schoolRepository.findOneById('553e2b3c-eb11-42b1-8f76-903add071ca7'))
42
      .thenResolve(instance(school));
43
    when(createdDiscount.getId()).thenReturn(
44
      '2d5fb4da-12c2-11ea-8d71-362b9e155667'
45
    );
46
    when(isDiscountAlreadyExist.isSatisfiedBy(10000, instance(school)))
47
      .thenResolve(false);
48
    when(
49
      discountRepository.save(
50
        deepEqual(
51
          new Discount(
52
            DiscountType.PERCENT,
53
            10000,
54
            1000,
55
            instance(school),
56
          )
57
        )
58
      )
59
    ).thenResolve(instance(createdDiscount));
60
61
    expect(await handler.execute(command)).toBe(
62
      '2d5fb4da-12c2-11ea-8d71-362b9e155667'
63
    );
64
65
    verify(
66
      discountRepository.save(
67
        deepEqual(
68
          new Discount(
69
            DiscountType.PERCENT,
70
            10000,
71
            1000,
72
            instance(school),
73
          )
74
        )
75
      )
76
    ).once();
77
    verify(isDiscountAlreadyExist.isSatisfiedBy(10000, instance(school))).once();
78
    verify(createdDiscount.getId()).once();
79
    verify(schoolRepository.findOneById('553e2b3c-eb11-42b1-8f76-903add071ca7')).once();
80
  });
81
82
  it('testDiscountAlreadyExistSchoolNotFound', async () => {
83
    when(schoolRepository.findOneById('553e2b3c-eb11-42b1-8f76-903add071ca7'))
84
      .thenResolve(instance(school));
85
    when(isDiscountAlreadyExist.isSatisfiedBy(10000, instance(school)))
86
      .thenResolve(true);
87
88
    try {
89
      expect(await handler.execute(command)).toBeUndefined();
90
    } catch (e) {
91
      expect(e).toBeInstanceOf(DiscountAlreadyExistException);
92
      expect(e.message).toBe('schools.discounts.errors.already_exist');
93
      verify(schoolRepository.findOneById('553e2b3c-eb11-42b1-8f76-903add071ca7')).once();
94
      verify(discountRepository.save(anything())).never();
95
      verify(isDiscountAlreadyExist.isSatisfiedBy(10000, instance(school))).once();
96
      verify(createdDiscount.getId()).never();
97
    }
98
  });
99
100
  it('testSchoolNotFound', async () => {
101
    when(schoolRepository.findOneById('553e2b3c-eb11-42b1-8f76-903add071ca7'))
102
      .thenResolve(null);
103
104
    try {
105
      expect(await handler.execute(command)).toBeUndefined();
106
    } catch (e) {
107
      expect(e).toBeInstanceOf(SchoolNotFoundException);
108
      expect(e.message).toBe('schools.errors.not_found');
109
      verify(schoolRepository.findOneById('553e2b3c-eb11-42b1-8f76-903add071ca7')).once();
110
      verify(discountRepository.save(anything())).never();
111
      verify(isDiscountAlreadyExist.isSatisfiedBy(anything(), anything())).never();
112
      verify(createdDiscount.getId()).never();
113
    }
114
  });
115
});
116