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
|
|
|
|