api/src/Application/School/Query/Voucher/GetVoucherByCodeQueryHandler.spec.ts   A
last analyzed

Complexity

Total Complexity 1
Complexity/F 0

Size

Lines of Code 60
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 1
eloc 51
mnd 1
bc 1
fnc 0
dl 0
loc 60
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
import { VoucherNotFoundException } from 'src/Domain/School/Exception/VoucherNotFoundException';
2
import { School } from 'src/Domain/School/School.entity';
3
import { Voucher } from 'src/Domain/School/Voucher.entity';
4
import { VoucherRepository } from 'src/Infrastructure/School/Repository/VoucherRepository';
5
import { mock, instance, when, verify } from 'ts-mockito';
6
import { VoucherView } from '../../View/VoucherView';
7
import { GetVoucherByCodeQuery } from './GetVoucherByCodeQuery';
8
import { GetVoucherByCodeQueryHandler } from './GetVoucherByCodeQueryHandler';
9
10
describe('GetVoucherByCodeQueryHandler', () => {
11
  const query = new GetVoucherByCodeQuery('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2');
12
13
  it('testGetVoucher', async () => {
14
    const voucherRepository = mock(VoucherRepository);
15
    const queryHandler = new GetVoucherByCodeQueryHandler(instance(voucherRepository));
16
    const expectedResult = new VoucherView(
17
      'eb9e1d9b-dce2-48a9-B64F-f0872f3157d2',
18
      'xLKJS',
19
      '[email protected]',
20
      'ee18bf86-c72a-4ed9-a6d1-212f4286759e'
21
    );
22
23
    const school = mock(School);
24
    when(school.getId()).thenReturn('ee18bf86-c72a-4ed9-a6d1-212f4286759e');
25
26
    const voucher = mock(Voucher);
27
    when(voucher.getId()).thenReturn('eb9e1d9b-dce2-48a9-B64F-f0872f3157d2');
28
    when(voucher.getCode()).thenReturn('xLKJS');
29
    when(voucher.getEmail()).thenReturn('[email protected]');
30
    when(voucher.getSchool()).thenReturn(instance(school));
31
    when(
32
      voucherRepository.findOneByCode('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2')
33
    ).thenResolve(instance(voucher));
34
35
    expect(await queryHandler.execute(query)).toMatchObject(expectedResult);
36
37
    verify(
38
      voucherRepository.findOneByCode('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2')
39
    ).once();
40
  });
41
42
  it('testGetVoucherNotFound', async () => {
43
    const voucherRepository = mock(VoucherRepository);
44
    const queryHandler = new GetVoucherByCodeQueryHandler(instance(voucherRepository));
45
    when(
46
      voucherRepository.findOneByCode('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2')
47
    ).thenResolve(null);
48
49
    try {
50
      expect(await queryHandler.execute(query)).toBeUndefined();
51
    } catch (e) {
52
      expect(e).toBeInstanceOf(VoucherNotFoundException);
53
      expect(e.message).toBe('schools.errors.voucher_not_found');
54
      verify(
55
        voucherRepository.findOneByCode('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2')
56
      ).once();
57
    }
58
  });
59
});
60