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