src/Application/FairCalendar/Command/DeleteEventCommandHandler.spec.ts   A
last analyzed

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
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
rs 10
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/HumanResource/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
    420,
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('faircalendar.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('faircalendar.errors.event_doesnt_belong_to_user');
78
      verify(
79
        eventRepository.findOneById('50e624ef-3609-4053-a437-f74844a2d2de')
80
      ).once();
81
      verify(doesEventBelongToUser.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(doesEventBelongToUser.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