1
|
|
|
import { mock, instance, when, verify, deepEqual, anything } from 'ts-mockito'; |
2
|
|
|
import { School } from 'src/Domain/School/School.entity'; |
3
|
|
|
import { SchoolNotFoundException } from 'src/Domain/School/Exception/SchoolNotFoundException'; |
4
|
|
|
import { CreateIngestionCommand } from './CreateIngestionCommand'; |
5
|
|
|
import { CreateIngestionCommandHandler } from './CreateShootingCommandHandler'; |
6
|
|
|
import { IIngestionRepository } from 'src/Domain/Ingestion/Repository/IIngestionRepository'; |
7
|
|
|
import { Ingestion } from 'src/Domain/Ingestion/Ingestion.entity'; |
8
|
|
|
import { ISchoolRepository } from 'src/Domain/School/Repository/ISchoolRepository'; |
9
|
|
|
|
10
|
|
|
describe('CreateIngestionCommandHandler', () => { |
11
|
|
|
let ingestionRepository: IIngestionRepository; |
12
|
|
|
let schoolRepository: ISchoolRepository; |
13
|
|
|
let handler: CreateIngestionCommandHandler; |
14
|
|
|
|
15
|
|
|
const school = mock(School); |
16
|
|
|
const createdIngestion = mock(Ingestion); |
17
|
|
|
const command = new CreateIngestionCommand( |
18
|
|
|
'8c4ef867-1d31-43fe-87bd-19c87370f5f8' |
19
|
|
|
); |
20
|
|
|
|
21
|
|
|
beforeEach(() => { |
22
|
|
|
ingestionRepository = mock<IIngestionRepository>(); |
23
|
|
|
schoolRepository = mock<ISchoolRepository>(); |
24
|
|
|
|
25
|
|
|
handler = new CreateIngestionCommandHandler( |
26
|
|
|
instance(schoolRepository), |
27
|
|
|
instance(ingestionRepository) |
28
|
|
|
); |
29
|
|
|
}); |
30
|
|
|
|
31
|
|
|
it('testShootingCreatedSuccessfully', async () => { |
32
|
|
|
when(schoolRepository.findOneById('8c4ef867-1d31-43fe-87bd-19c87370f5f8')) |
33
|
|
|
.thenResolve(instance(school)); |
34
|
|
|
when( |
35
|
|
|
ingestionRepository.save( |
36
|
|
|
deepEqual( |
37
|
|
|
new Ingestion( |
38
|
|
|
instance(school) |
39
|
|
|
) |
40
|
|
|
) |
41
|
|
|
) |
42
|
|
|
).thenResolve(instance(createdIngestion)); |
43
|
|
|
|
44
|
|
|
when(createdIngestion.getId()).thenReturn( |
45
|
|
|
'2d5fb4da-12c2-11ea-8d71-362b9e155667' |
46
|
|
|
); |
47
|
|
|
|
48
|
|
|
expect(await handler.execute(command)).toBe( |
49
|
|
|
'2d5fb4da-12c2-11ea-8d71-362b9e155667' |
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
verify( |
53
|
|
|
ingestionRepository.save( |
54
|
|
|
deepEqual( |
55
|
|
|
new Ingestion( |
56
|
|
|
instance(school) |
57
|
|
|
) |
58
|
|
|
) |
59
|
|
|
) |
60
|
|
|
).once(); |
61
|
|
|
verify(createdIngestion.getId()).once(); |
62
|
|
|
verify(schoolRepository.findOneById('8c4ef867-1d31-43fe-87bd-19c87370f5f8')).once(); |
63
|
|
|
}); |
64
|
|
|
|
65
|
|
|
it('testSchoolNotFound', async () => { |
66
|
|
|
when(schoolRepository.findOneById('8c4ef867-1d31-43fe-87bd-19c87370f5f8')) |
67
|
|
|
.thenResolve(null); |
68
|
|
|
|
69
|
|
|
try { |
70
|
|
|
expect(await handler.execute(command)).toBeUndefined(); |
71
|
|
|
} catch (e) { |
72
|
|
|
expect(e).toBeInstanceOf(SchoolNotFoundException); |
73
|
|
|
expect(e.message).toBe('schools.errors.not_found'); |
74
|
|
|
verify(schoolRepository.findOneById('8c4ef867-1d31-43fe-87bd-19c87370f5f8')).once(); |
75
|
|
|
verify(ingestionRepository.save(anything())).never(); |
76
|
|
|
} |
77
|
|
|
}); |
78
|
|
|
}); |
79
|
|
|
|