api/src/Application/School/Command/UpdateSchoolCommandHandler.spec.ts   A
last analyzed

Complexity

Total Complexity 2
Complexity/F 0

Size

Lines of Code 119
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 99
mnd 2
bc 2
fnc 0
dl 0
loc 119
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
import { mock, instance, when, verify, anything } from 'ts-mockito';
2
import { UpdateSchoolCommandHandler } from './UpdateSchoolCommandHandler';
3
import { UpdateSchoolCommand } from './UpdateSchoolCommand';
4
import { SchoolRepository } from 'src/Infrastructure/School/Repository/SchoolRepository';
5
import { IsSchoolAlreadyExist } from 'src/Domain/School/Specification/IsSchoolAlreadyExist';
6
import { School } from 'src/Domain/School/School.entity';
7
import { SchoolAlreadyExistException } from 'src/Domain/School/Exception/SchoolAlreadyExistException';
8
import { SchoolNotFoundException } from 'src/Domain/School/Exception/SchoolNotFoundException';
9
import { Status, Type } from 'src/Domain/School/AbstractSchool';
10
11
describe('UpdateSchoolCommandHandler', () => {
12
  let schoolRepository: SchoolRepository;
13
  let isSchoolAlreadyExist: IsSchoolAlreadyExist;
14
15
  let handler: UpdateSchoolCommandHandler;
16
17
  const school = mock(School);
18
  const command = new UpdateSchoolCommand(
19
    '8a9df044-94a7-4e6c-abd1-ecdd69d788d5',
20
    'LM120I',
21
    'Belliard',
22
    '127 Rue Belliard',
23
    '75018',
24
    'Paris',
25
    Status.PRIVATE,
26
    Type.ELEMENTARY,
27
    '[email protected]',
28
    '010101010101',
29
    200,
30
    10,
31
    'Observation'
32
  );
33
34
  beforeEach(() => {
35
    schoolRepository = mock(SchoolRepository);
36
    isSchoolAlreadyExist = mock(IsSchoolAlreadyExist);
37
    handler = new UpdateSchoolCommandHandler(
38
      instance(schoolRepository),
39
      instance(isSchoolAlreadyExist)
40
    );
41
  });
42
43
  it('testSchoolNotFound', async () => {
44
    when(
45
      schoolRepository.findOneById('8a9df044-94a7-4e6c-abd1-ecdd69d788d5')
46
    ).thenResolve(null);
47
48
    try {
49
      expect(await handler.execute(command)).toBeUndefined();
50
    } catch (e) {
51
      expect(e).toBeInstanceOf(SchoolNotFoundException);
52
      expect(e.message).toBe('schools.errors.not_found');
53
      verify(
54
        schoolRepository.findOneById('8a9df044-94a7-4e6c-abd1-ecdd69d788d5')
55
      ).once();
56
      verify(isSchoolAlreadyExist.isSatisfiedBy(anything())).never();
57
      verify(schoolRepository.save(anything())).never();
58
      verify(
59
        school.update(anything(), anything(), anything(), anything(), anything(), anything(), anything(), anything())
60
      ).never();
61
    }
62
  });
63
64
  it('testSchoolAlreadyExist', async () => {
65
    when(school.getReference()).thenReturn('abc');
66
    when(
67
      schoolRepository.findOneById('8a9df044-94a7-4e6c-abd1-ecdd69d788d5')
68
    ).thenResolve(instance(school));
69
    when(
70
      isSchoolAlreadyExist.isSatisfiedBy('LM120I')
71
    ).thenResolve(true);
72
73
    try {
74
      expect(await handler.execute(command)).toBeUndefined();
75
    } catch (e) {
76
      expect(e).toBeInstanceOf(SchoolAlreadyExistException);
77
      expect(e.message).toBe('schools.errors.already_exist');
78
      verify(
79
        isSchoolAlreadyExist.isSatisfiedBy('LM120I')
80
      ).once();
81
      verify(
82
        school.update(anything(), anything(), anything(), anything(), anything(), anything(), anything(), anything())
83
      ).never();
84
      verify(schoolRepository.save(anything())).never();
85
    }
86
  });
87
88
  it('testSuccessfullyUpdated', async () => {
89
    when(school.getId()).thenReturn('8a9df044-94a7-4e6c-abd1-ecdd69d788d5');
90
    when(school.getReference()).thenReturn('LM120I');
91
    when(
92
      schoolRepository.findOneById('8a9df044-94a7-4e6c-abd1-ecdd69d788d5')
93
    ).thenResolve(instance(school));
94
95
    expect(await handler.execute(command)).toBe(
96
      '8a9df044-94a7-4e6c-abd1-ecdd69d788d5'
97
    );
98
99
    verify(isSchoolAlreadyExist.isSatisfiedBy(anything())).never();
100
    verify(
101
      school.update(
102
        'LM120I',
103
        'Belliard',
104
        '127 Rue Belliard',
105
        '75018',
106
        'Paris',
107
        Status.PRIVATE,
108
        Type.ELEMENTARY,
109
        '[email protected]',
110
        '010101010101',
111
        200,
112
        10,
113
        'Observation'
114
      )
115
    ).calledBefore(schoolRepository.save(instance(school)));
116
    verify(schoolRepository.save(instance(school))).once();
117
  });
118
});
119