|
1
|
|
|
import {mock, instance, when, verify, anything, deepEqual} from 'ts-mockito'; |
|
2
|
|
|
import {EventRepository} from 'src/Infrastructure/FairCalendar/Repository/EventRepository'; |
|
3
|
|
|
import {DeleteEventCommandHandler} from './DeleteEventCommandHandler'; |
|
4
|
|
|
import {DeleteEventCommand} from './DeleteEventCommand'; |
|
5
|
|
|
import {User} from 'src/Domain/User/User.entity'; |
|
6
|
|
|
import {Event, EventType} from 'src/Domain/FairCalendar/Event.entity'; |
|
7
|
|
|
import {EventNotFoundException} from 'src/Domain/FairCalendar/Exception/EventNotFoundException'; |
|
8
|
|
|
import {NotEventOwnerException} from 'src/Domain/FairCalendar/Exception/NotEventOwnerException'; |
|
9
|
|
|
import {Project} from 'src/Domain/Project/Project.entity'; |
|
10
|
|
|
import {Task} from 'src/Domain/Task/Task.entity'; |
|
11
|
|
|
|
|
12
|
|
|
describe('DeleteEventCommandHandler', () => { |
|
13
|
|
|
let eventRepository: EventRepository; |
|
14
|
|
|
let handler: DeleteEventCommandHandler; |
|
15
|
|
|
|
|
16
|
|
|
const user = mock(User); |
|
17
|
|
|
const otherUser = mock(User); |
|
18
|
|
|
const project = mock(Project); |
|
19
|
|
|
const task = mock(Task); |
|
20
|
|
|
const event = new Event( |
|
21
|
|
|
EventType.MISSION, |
|
22
|
|
|
instance(user), |
|
23
|
|
|
100, |
|
24
|
|
|
'2020-05-15', |
|
25
|
|
|
instance(project), |
|
26
|
|
|
instance(task), |
|
27
|
|
|
'summary' |
|
28
|
|
|
); |
|
29
|
|
|
|
|
30
|
|
|
const command = new DeleteEventCommand( |
|
31
|
|
|
'50e624ef-3609-4053-a437-f74844a2d2de', |
|
32
|
|
|
instance(user) |
|
33
|
|
|
); |
|
34
|
|
|
|
|
35
|
|
|
beforeEach(() => { |
|
36
|
|
|
eventRepository = mock(EventRepository); |
|
37
|
|
|
handler = new DeleteEventCommandHandler(instance(eventRepository)); |
|
38
|
|
|
}); |
|
39
|
|
|
|
|
40
|
|
|
it('testEventNotFound', async () => { |
|
41
|
|
|
when( |
|
42
|
|
|
eventRepository.findOneById('50e624ef-3609-4053-a437-f74844a2d2de') |
|
43
|
|
|
).thenResolve(null); |
|
44
|
|
|
|
|
45
|
|
|
try { |
|
46
|
|
|
await handler.execute(command); |
|
47
|
|
|
} catch (e) { |
|
48
|
|
|
expect(e).toBeInstanceOf(EventNotFoundException); |
|
49
|
|
|
expect(e.message).toBe('fair_calendar.errors.event_not_found'); |
|
50
|
|
|
verify( |
|
51
|
|
|
eventRepository.findOneById('50e624ef-3609-4053-a437-f74844a2d2de') |
|
52
|
|
|
).once(); |
|
53
|
|
|
verify(eventRepository.delete(anything())).never(); |
|
54
|
|
|
} |
|
55
|
|
|
}); |
|
56
|
|
|
|
|
57
|
|
|
it('testNotEventOwner', async () => { |
|
58
|
|
|
const otherEvent = new Event( |
|
59
|
|
|
EventType.MISSION, |
|
60
|
|
|
instance(otherUser), |
|
61
|
|
|
100, |
|
62
|
|
|
'2020-05-15', |
|
63
|
|
|
instance(project), |
|
64
|
|
|
instance(task), |
|
65
|
|
|
'summary' |
|
66
|
|
|
); |
|
67
|
|
|
|
|
68
|
|
|
when( |
|
69
|
|
|
eventRepository.findOneById('50e624ef-3609-4053-a437-f74844a2d2de') |
|
70
|
|
|
).thenResolve(otherEvent); |
|
71
|
|
|
when(otherUser.getId()).thenReturn('e3fc9666-2932-4dc1-b2b9-d904388293fb'); |
|
72
|
|
|
when(user.getId()).thenReturn('e3fc9666-2932-4dc1-b2b9-d904388293fc'); |
|
73
|
|
|
|
|
74
|
|
|
try { |
|
75
|
|
|
await handler.execute(command); |
|
76
|
|
|
} catch (e) { |
|
77
|
|
|
expect(e).toBeInstanceOf(NotEventOwnerException); |
|
78
|
|
|
expect(e.message).toBe('fair_calendar.errors.event_not_owner'); |
|
79
|
|
|
verify( |
|
80
|
|
|
eventRepository.findOneById('50e624ef-3609-4053-a437-f74844a2d2de') |
|
81
|
|
|
).once(); |
|
82
|
|
|
verify(eventRepository.delete(anything())).never(); |
|
83
|
|
|
} |
|
84
|
|
|
}); |
|
85
|
|
|
|
|
86
|
|
|
it('testDeleteEvent', async () => { |
|
87
|
|
|
when( |
|
88
|
|
|
eventRepository.findOneById('50e624ef-3609-4053-a437-f74844a2d2de') |
|
89
|
|
|
).thenResolve(event); |
|
90
|
|
|
when(user.getId()).thenReturn('e3fc9666-2932-4dc1-b2b9-d904388293fb'); |
|
91
|
|
|
|
|
92
|
|
|
expect(await handler.execute(command)); |
|
93
|
|
|
|
|
94
|
|
|
verify( |
|
95
|
|
|
eventRepository.findOneById('50e624ef-3609-4053-a437-f74844a2d2de') |
|
96
|
|
|
).once(); |
|
97
|
|
|
verify(eventRepository.delete(deepEqual(event))).once(); |
|
98
|
|
|
}); |
|
99
|
|
|
}); |
|
100
|
|
|
|