Completed
Push — master ( 7c7ab7...45cb8f )
by Mathieu
19s queued 11s
created

server/src/Application/HumanResource/User/Query/GetUserByIdQueryHandler.spec.ts   A

Complexity

Total Complexity 1
Complexity/F 0

Size

Lines of Code 66
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 1
eloc 56
mnd 1
bc 1
fnc 0
dl 0
loc 66
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
rs 10
1
import {mock, instance, when, verify} from 'ts-mockito';
2
import {UserRepository} from 'src/Infrastructure/HumanResource/User/Repository/UserRepository';
3
import {User, UserRole} from 'src/Domain/HumanResource/User/User.entity';
4
import {UserView} from 'src/Application/HumanResource/User/View/UserView';
5
import {GetUserByIdQueryHandler} from './GetUserByIdQueryHandler';
6
import {GetUserByIdQuery} from './GetUserByIdQuery';
7
import {UserNotFoundException} from 'src/Domain/HumanResource/User/Exception/UserNotFoundException';
8
9
describe('GetUserByIdQueryHandler', () => {
10
  const query = new GetUserByIdQuery('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2');
11
12
  it('testGetUser', async () => {
13
    const userRepository = mock(UserRepository);
14
    const queryHandler = new GetUserByIdQueryHandler(instance(userRepository));
15
16
    const user = mock(User);
17
18
    when(user.getId()).thenReturn('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2');
19
    when(user.getFirstName()).thenReturn('Mathieu');
20
    when(user.getLastName()).thenReturn('MARCHOIS');
21
    when(user.getEmail()).thenReturn('[email protected]');
22
    when(user.getEntryDate()).thenReturn('2019-09-19');
23
    when(user.getRole()).thenReturn(UserRole.COOPERATOR);
24
    when(
25
      userRepository.findOneById('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2')
26
    ).thenResolve(instance(user));
27
28
    expect(await queryHandler.execute(query)).toMatchObject(
29
      new UserView(
30
        'eb9e1d9b-dce2-48a9-b64f-f0872f3157d2',
31
        'Mathieu',
32
        'MARCHOIS',
33
        '[email protected]',
34
        UserRole.COOPERATOR,
35
        '2019-09-19'
36
      )
37
    );
38
39
    verify(
40
      userRepository.findOneById('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2')
41
    ).once();
42
    verify(user.getId()).once();
43
    verify(user.getFirstName()).once();
44
    verify(user.getLastName()).once();
45
    verify(user.getEmail()).once();
46
  });
47
48
  it('testGetUserNotFound', async () => {
49
    const userRepository = mock(UserRepository);
50
    const queryHandler = new GetUserByIdQueryHandler(instance(userRepository));
51
    when(
52
      userRepository.findOneById('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2')
53
    ).thenResolve(null);
54
55
    try {
56
      await queryHandler.execute(query);
57
    } catch (e) {
58
      expect(e).toBeInstanceOf(UserNotFoundException);
59
      expect(e.message).toBe('user.errors.not_found');
60
      verify(
61
        userRepository.findOneById('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2')
62
      ).once();
63
    }
64
  });
65
});
66