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

server/src/Application/FairCalendar/Query/GetEventByIdQueryHandler.spec.ts   A

Complexity

Total Complexity 1
Complexity/F 0

Size

Lines of Code 79
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 1
eloc 66
mnd 1
bc 1
fnc 0
dl 0
loc 79
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
import {mock, instance, when, verify} from 'ts-mockito';
2
import {EventRepository} from 'src/Infrastructure/FairCalendar/Repository/EventRepository';
3
import {Event} from 'src/Domain/FairCalendar/Event.entity';
4
import {EventView} from 'src/Application/FairCalendar/View/EventView';
5
import {GetEventByIdQueryHandler} from './GetEventByIdQueryHandler';
6
import {GetEventByIdQuery} from './GetEventByIdQuery';
7
import {EventNotFoundException} from 'src/Domain/FairCalendar/Exception/EventNotFoundException';
8
import {Task} from 'src/Domain/Task/Task.entity';
9
import {Project} from 'src/Domain/Project/Project.entity';
10
import {ProjectView} from 'src/Application/Project/View/ProjectView';
11
import {TaskView} from 'src/Application/Task/View/TaskView';
12
13
describe('GetEventByIdQueryHandler', () => {
14
  const query = new GetEventByIdQuery('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2');
15
16
  it('testGetEvent', async () => {
17
    const eventRepository = mock(EventRepository);
18
    const queryHandler = new GetEventByIdQueryHandler(
19
      instance(eventRepository)
20
    );
21
    const expectedResult = new EventView(
22
      'eb9e1d9b-dce2-48a9-b64f-f0872f3157d2',
23
      'mission',
24
      75,
25
      '2019-12-12',
26
      'Summary',
27
      new ProjectView('bf4a645c-9754-4943-baec-783361c6d814', 'RadioFrance'),
28
      new TaskView('7fb77f06-2d0b-4758-886a-42bba5445fcd', 'Development')
29
    );
30
31
    const task = mock(Task);
32
    const project = mock(Project);
33
34
    when(project.getId()).thenReturn('bf4a645c-9754-4943-baec-783361c6d814');
35
    when(project.getName()).thenReturn('RadioFrance');
36
    when(task.getId()).thenReturn('7fb77f06-2d0b-4758-886a-42bba5445fcd');
37
    when(task.getName()).thenReturn('Development');
38
39
    const event1 = mock(Event);
40
    when(event1.getId()).thenReturn('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2');
41
    when(event1.getType()).thenReturn('mission');
42
    when(event1.getTime()).thenReturn(75);
43
    when(event1.getDate()).thenReturn('2019-12-12');
44
    when(event1.getSummary()).thenReturn('Summary');
45
    when(event1.getTask()).thenReturn(instance(task));
46
    when(event1.getProject()).thenReturn(instance(project));
47
48
    when(
49
      eventRepository.findOneById('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2')
50
    ).thenResolve(instance(event1));
51
52
    expect(await queryHandler.execute(query)).toMatchObject(expectedResult);
53
54
    verify(
55
      eventRepository.findOneById('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2')
56
    ).once();
57
  });
58
59
  it('testGetEventNotFound', async () => {
60
    const eventRepository = mock(EventRepository);
61
    const queryHandler = new GetEventByIdQueryHandler(
62
      instance(eventRepository)
63
    );
64
    when(
65
      eventRepository.findOneById('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2')
66
    ).thenResolve(null);
67
68
    try {
69
      await queryHandler.execute(query);
70
    } catch (e) {
71
      expect(e).toBeInstanceOf(EventNotFoundException);
72
      expect(e.message).toBe('fair_calendar.errors.event_not_found');
73
      verify(
74
        eventRepository.findOneById('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2')
75
      ).once();
76
    }
77
  });
78
});
79