Passed
Push — master ( 2cdb9e...45e047 )
by Mathieu
01:31
created

api/src/Application/School/Query/Shooting/GetShootingByIdQueryHandler.spec.ts   A

Complexity

Total Complexity 1
Complexity/F 0

Size

Lines of Code 58
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 1
eloc 49
mnd 1
bc 1
fnc 0
dl 0
loc 58
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
import { mock, instance, when, verify } from 'ts-mockito';
2
import { Shooting, ShootingStatus } from 'src/Domain/School/Shooting.entity';
3
import { ShootingRepository } from 'src/Infrastructure/School/Repository/ShootingRepository';
4
import { ShootingView } from '../../View/ShootingView';
5
import { GetShootingByIdQuery } from './GetSchootingByIdQuery';
6
import { GetShootingByIdQueryHandler } from './GetShootingByIdQueryHandler';
7
import { ShootingNotFoundException } from 'src/Domain/School/Exception/ShootingNotFoundException';
8
9
describe('GetShootingByIdQueryHandler', () => {
10
  const query = new GetShootingByIdQuery('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2');
11
12
  it('testGetShooting', async () => {
13
    const shootingRepository = mock(ShootingRepository);
14
    const queryHandler = new GetShootingByIdQueryHandler(instance(shootingRepository));
15
    const expectedResult = new ShootingView(
16
      'eb9e1d9b-dce2-48a9-b64f-f0872f3157d2',
17
      'Prise de vue fin année',
18
      ShootingStatus.DISABLED,
19
      new Date('2021-04-18'),
20
      new Date('2021-09-01')
21
    );
22
23
    const shooting = mock(Shooting);
24
    when(shooting.getId()).thenReturn('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2');
25
    when(shooting.getName()).thenReturn('Prise de vue fin année');
26
    when(shooting.getClosingDate()).thenReturn(new Date('2021-09-01'));
27
    when(shooting.getShootingDate()).thenReturn(new Date('2021-04-18'));
28
    when(shooting.getStatus()).thenReturn(ShootingStatus.DISABLED);
29
    when(
30
      shootingRepository.findOneById('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2')
31
    ).thenResolve(instance(shooting));
32
33
    expect(await queryHandler.execute(query)).toMatchObject(expectedResult);
34
35
    verify(
36
      shootingRepository.findOneById('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2')
37
    ).once();
38
  });
39
40
  it('testGetShootingNotFound', async () => {
41
    const shootingRepository = mock(ShootingRepository);
42
    const queryHandler = new GetShootingByIdQueryHandler(instance(shootingRepository));
43
    when(
44
      shootingRepository.findOneById('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2')
45
    ).thenResolve(null);
46
47
    try {
48
      expect(await queryHandler.execute(query)).toBeUndefined();
49
    } catch (e) {
50
      expect(e).toBeInstanceOf(ShootingNotFoundException);
51
      expect(e.message).toBe('schools.shootings.errors.not_found');
52
      verify(
53
        shootingRepository.findOneById('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2')
54
      ).once();
55
    }
56
  });
57
});
58