Passed
Pull Request — master (#75)
by Mathieu
01:28
created

server/src/Application/FairCalendar/Command/DeleteEventCommandHandler.spec.ts   A

Complexity

Total Complexity 2
Complexity/F 0

Size

Lines of Code 103
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 84
mnd 2
bc 2
fnc 0
dl 0
loc 103
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
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 {IsEventBelongToUser} from 'src/Domain/FairCalendar/Specification/IsEventBelongToUser';
12
13
describe('DeleteEventCommandHandler', () => {
14
  let eventRepository: EventRepository;
15
  let isEventBelongToUser: IsEventBelongToUser;
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
    isEventBelongToUser = mock(IsEventBelongToUser);
39
    handler = new DeleteEventCommandHandler(
40
      instance(eventRepository),
41
      instance(isEventBelongToUser)
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(isEventBelongToUser.isSatisfiedBy(anything(), anything())).never();
59
      verify(eventRepository.delete(anything())).never();
60
    }
61
  });
62
63
  it('testNotEventOwner', async () => {
64
    when(
65
      eventRepository.findOneById('50e624ef-3609-4053-a437-f74844a2d2de')
66
    ).thenResolve(event);
67
    when(isEventBelongToUser.isSatisfiedBy(event, instance(user))).thenReturn(
68
      false
69
    );
70
71
    try {
72
      await handler.execute(command);
73
    } catch (e) {
74
      expect(e).toBeInstanceOf(EventDoesntBelongToUserException);
75
      expect(e.message).toBe(
76
        'fair_calendar.errors.event_doesnt_belong_to_user'
77
      );
78
      verify(
79
        eventRepository.findOneById('50e624ef-3609-4053-a437-f74844a2d2de')
80
      ).once();
81
      verify(isEventBelongToUser.isSatisfiedBy(event, instance(user))).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(isEventBelongToUser.isSatisfiedBy(event, instance(user))).thenReturn(
91
      true
92
    );
93
    when(user.getId()).thenReturn('e3fc9666-2932-4dc1-b2b9-d904388293fb');
94
95
    expect(await handler.execute(command));
96
97
    verify(
98
      eventRepository.findOneById('50e624ef-3609-4053-a437-f74844a2d2de')
99
    ).once();
100
    verify(eventRepository.delete(deepEqual(event))).once();
101
  });
102
});
103