|
1
|
|
|
import { mock, instance, when, verify } from 'ts-mockito'; |
|
2
|
|
|
import { GetEventByIdQueryHandler } from './GetEventByIdQueryHandler'; |
|
3
|
|
|
import { GetEventByIdQuery } from './GetEventByIdQuery'; |
|
4
|
|
|
import { Status, Type } from 'src/Domain/School/AbstractSchool'; |
|
5
|
|
|
import { EventRepository } from 'src/Infrastructure/Calendar/Repository/EventRepository'; |
|
6
|
|
|
import { EventDetailView } from '../View/EventDetailView'; |
|
7
|
|
|
import { SchoolSummaryView } from 'src/Application/School/View/SchoolSummaryView'; |
|
8
|
|
|
import { Event } from 'src/Domain/Calendar/Event.entity'; |
|
9
|
|
|
import { User } from 'src/Domain/User/User.entity'; |
|
10
|
|
|
import { School } from 'src/Domain/School/School.entity'; |
|
11
|
|
|
import { EventNotFoundException } from 'src/Domain/Calendar/Exception/EventNotFoundException'; |
|
12
|
|
|
import { UserSummaryView } from 'src/Application/User/View/UserSummaryView'; |
|
13
|
|
|
|
|
14
|
|
|
describe('GetEventByIdQueryHandler', () => { |
|
15
|
|
|
const query = new GetEventByIdQuery('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2'); |
|
16
|
|
|
let eventRepository: EventRepository; |
|
17
|
|
|
let queryHandler: GetEventByIdQueryHandler; |
|
18
|
|
|
let event: Event; |
|
19
|
|
|
|
|
20
|
|
|
beforeEach(() => { |
|
21
|
|
|
event = mock(Event); |
|
22
|
|
|
eventRepository = mock(EventRepository); |
|
23
|
|
|
queryHandler = new GetEventByIdQueryHandler(instance(eventRepository)); |
|
24
|
|
|
}); |
|
25
|
|
|
|
|
26
|
|
|
it('testGetEvent', async () => { |
|
27
|
|
|
const expectedResult = new EventDetailView( |
|
28
|
|
|
'eb9e1d9b-dce2-48a9-b64f-f0872f3157d2', |
|
29
|
|
|
new SchoolSummaryView( |
|
30
|
|
|
'd54f15d6-1a1d-47e8-8672-9f46018f9960', |
|
31
|
|
|
'Ecole maternelle Belliard', |
|
32
|
|
|
'LM290', |
|
33
|
|
|
'10 rue Belliard', |
|
34
|
|
|
'Paris', |
|
35
|
|
|
'75018' |
|
36
|
|
|
), |
|
37
|
|
|
new UserSummaryView( |
|
38
|
|
|
'484f0ddd-9a44-4f6d-9d4e-cdeded631e39', |
|
39
|
|
|
'Mathieu', |
|
40
|
|
|
'MARCHOIS', |
|
41
|
|
|
'[email protected]' |
|
42
|
|
|
), |
|
43
|
|
|
new Date('2021-04-01'), |
|
44
|
|
|
'Prise de vue' |
|
45
|
|
|
); |
|
46
|
|
|
|
|
47
|
|
|
const photographer = mock(User); |
|
48
|
|
|
when(photographer.getId()).thenReturn('484f0ddd-9a44-4f6d-9d4e-cdeded631e39'); |
|
49
|
|
|
when(photographer.getFirstName()).thenReturn('Mathieu'); |
|
50
|
|
|
when(photographer.getLastName()).thenReturn('MARCHOIS'); |
|
51
|
|
|
when(photographer.getEmail()).thenReturn('[email protected]'); |
|
52
|
|
|
|
|
53
|
|
|
const school = mock(School); |
|
54
|
|
|
when(school.getId()).thenReturn('d54f15d6-1a1d-47e8-8672-9f46018f9960'); |
|
55
|
|
|
when(school.getName()).thenReturn('Ecole maternelle Belliard'); |
|
56
|
|
|
when(school.getAddress()).thenReturn('10 rue Belliard'); |
|
57
|
|
|
when(school.getZipCode()).thenReturn('75018'); |
|
58
|
|
|
when(school.getCity()).thenReturn('Paris'); |
|
59
|
|
|
when(school.getReference()).thenReturn('LM290'); |
|
60
|
|
|
|
|
61
|
|
|
when(event.getId()).thenReturn('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2'); |
|
62
|
|
|
when(event.getDate()).thenReturn(new Date('2021-04-01')); |
|
63
|
|
|
when(event.getSummary()).thenReturn('Prise de vue'); |
|
64
|
|
|
when(event.getSchool()).thenReturn(instance(school)); |
|
65
|
|
|
when(event.getPhotographer()).thenReturn(instance(photographer)); |
|
66
|
|
|
|
|
67
|
|
|
when( |
|
68
|
|
|
eventRepository.findOneById('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2') |
|
69
|
|
|
).thenResolve(instance(event)); |
|
70
|
|
|
expect(await queryHandler.execute(query)).toMatchObject(expectedResult); |
|
71
|
|
|
|
|
72
|
|
|
verify( |
|
73
|
|
|
eventRepository.findOneById('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2') |
|
74
|
|
|
).once(); |
|
75
|
|
|
}); |
|
76
|
|
|
|
|
77
|
|
|
it('testGetEventNotFound', async () => { |
|
78
|
|
|
when( |
|
79
|
|
|
eventRepository.findOneById('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2') |
|
80
|
|
|
).thenResolve(null); |
|
81
|
|
|
|
|
82
|
|
|
try { |
|
83
|
|
|
expect(await queryHandler.execute(query)).toBeUndefined(); |
|
84
|
|
|
} catch (e) { |
|
85
|
|
|
expect(e).toBeInstanceOf(EventNotFoundException); |
|
86
|
|
|
expect(e.message).toBe('calendar.errors.event_not_found'); |
|
87
|
|
|
verify( |
|
88
|
|
|
eventRepository.findOneById('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2') |
|
89
|
|
|
).once(); |
|
90
|
|
|
} |
|
91
|
|
|
}); |
|
92
|
|
|
}); |
|
93
|
|
|
|