Passed
Push — master ( 3a2d3a...97549a )
by Mathieu
01:48
created

api/src/Application/School/Command/Shooting/UpdateShootingCommandHandler.spec.ts   A

Complexity

Total Complexity 1
Complexity/F 0

Size

Lines of Code 69
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 1
eloc 56
mnd 1
bc 1
fnc 0
dl 0
loc 69
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
import { mock, instance, when, verify, anything, deepEqual } from 'ts-mockito';
2
import { ShootingRepository } from 'src/Infrastructure/School/Repository/ShootingRepository';
3
import { Shooting } from 'src/Domain/School/Shooting.entity';
4
import { UpdateShootingCommandHandler } from './UpdateShootingCommandHandler';
5
import { UpdateShootingCommand } from './UpdateShootingCommand';
6
import { ShootingNotFoundException } from 'src/Domain/School/Exception/ShootingNotFoundException';
7
8
describe('UpdateShootingCommandHandler', () => {
9
  let shootingRepository: ShootingRepository;
10
  let updatedShooting: Shooting;
11
  let handler: UpdateShootingCommandHandler;
12
13
  const command = new UpdateShootingCommand(
14
    '17efcbee-bd2f-410e-9e99-51684b592bad',
15
    'Prise de vue début année',
16
    new Date('2021-04-18'),
17
    new Date('2021-09-01'),
18
  );
19
20
  beforeEach(() => {
21
    shootingRepository = mock(ShootingRepository);
22
    updatedShooting = mock(Shooting);
23
24
    handler = new UpdateShootingCommandHandler(
25
      instance(shootingRepository)
26
    );
27
  });
28
29
  it('testShootingUpdatedSuccessfully', async () => {
30
    when(shootingRepository.findOneById('17efcbee-bd2f-410e-9e99-51684b592bad'))
31
      .thenResolve(instance(updatedShooting));
32
    when(updatedShooting.getId()).thenReturn(
33
      '17efcbee-bd2f-410e-9e99-51684b592bad'
34
    );
35
    when(
36
      shootingRepository.save(instance(updatedShooting))
37
    ).thenResolve(instance(updatedShooting));
38
39
    expect(await handler.execute(command)).toBe(
40
      '17efcbee-bd2f-410e-9e99-51684b592bad'
41
    );
42
43
    verify(
44
      shootingRepository.save(instance(updatedShooting))
45
    ).once();
46
    verify(updatedShooting.getId()).once();
47
    verify(updatedShooting.update(
48
      'Prise de vue début année',
49
      deepEqual(new Date('2021-04-18')),
50
      deepEqual(new Date('2021-09-01')))
51
    ).once();
52
    verify(shootingRepository.findOneById('17efcbee-bd2f-410e-9e99-51684b592bad')).once();
53
  });
54
55
  it('testShootingNotFound', async () => {
56
    when(shootingRepository.findOneById('17efcbee-bd2f-410e-9e99-51684b592bad'))
57
      .thenResolve(null);
58
59
    try {
60
      expect(await handler.execute(command)).toBeUndefined();
61
    } catch (e) {
62
      expect(e).toBeInstanceOf(ShootingNotFoundException);
63
      expect(e.message).toBe('schools.shootings.errors.not_found');
64
      verify(shootingRepository.findOneById('17efcbee-bd2f-410e-9e99-51684b592bad')).once();
65
      verify(shootingRepository.save(anything())).never();
66
    }
67
  });
68
});
69