Passed
Pull Request — master (#215)
by
unknown
02:03
created

server/src/Application/Contact/Command/CreateContactCommandHandler.spec.ts   A

Complexity

Total Complexity 1
Complexity/F 0

Size

Lines of Code 88
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 68
dl 0
loc 88
rs 10
c 0
b 0
f 0
wmc 1
mnd 1
bc 1
fnc 0
bpm 0
cpm 0
noi 0
1
import { mock, instance, when, verify, deepEqual, anything } from 'ts-mockito';
2
import { ContactRepository } from 'src/Infrastructure/Contact/Repository/ContactRepository';
3
import { Contact } from 'src/Domain/Contact/Contact.entity';
4
import { EmptyContactException } from 'src/Domain/Contact/Exception/EmptyContactException';
5
import { CreateContactCommandHandler } from 'src/Application/Contact/Command/CreateContactCommandHandler';
6
import { CreateContactCommand } from './CreateContactCommand';
7
8
describe('CreateContactCommandHandler', () => {
9
  let contactRepository: ContactRepository;
10
  let createdContact: Contact;
11
  let handler: CreateContactCommandHandler;
12
13
  beforeEach(() => {
14
    contactRepository = mock(ContactRepository);
15
    createdContact = mock(Contact);
16
    handler = new CreateContactCommandHandler(instance(contactRepository));
17
  });
18
19
  it('testContactCreatedSuccessfully', async () => {
20
    when(createdContact.getId()).thenReturn(
21
      '2d5fb4da-12c2-11ea-8d71-362b9e155667'
22
    );
23
    when(
24
      contactRepository.save(
25
        deepEqual(
26
          new Contact(
27
            'Sarah',
28
            'Conor',
29
            'Aperture Science',
30
            '[email protected]',
31
            '0612345678',
32
            'Lorem ipsum'
33
          )
34
        )
35
      )
36
    ).thenResolve(instance(createdContact));
37
38
    expect(
39
      await handler.execute(
40
        new CreateContactCommand(
41
          'Sarah',
42
          'Conor',
43
          'Aperture Science',
44
          '[email protected]',
45
          '0612345678',
46
          'Lorem ipsum'
47
        )
48
      )
49
    ).toBe('2d5fb4da-12c2-11ea-8d71-362b9e155667');
50
51
    verify(
52
      contactRepository.save(
53
        deepEqual(
54
          new Contact(
55
            'Sarah',
56
            'Conor',
57
            'Aperture Science',
58
            '[email protected]',
59
            '0612345678',
60
            'Lorem ipsum'
61
          )
62
        )
63
      )
64
    ).once();
65
    verify(createdContact.getId()).once();
66
  });
67
68
  it('testEmptyContact', async () => {
69
    try {
70
      await handler.execute(
71
        new CreateContactCommand(
72
          null,
73
          null,
74
          null,
75
          '[email protected]',
76
          '0612345678',
77
          'Lorem ipsum'
78
        )
79
      );
80
    } catch (e) {
81
      expect(e).toBeInstanceOf(EmptyContactException);
82
      expect(e.message).toBe('crm.contacts.errors.empty');
83
      verify(contactRepository.save(anything())).never();
84
      verify(createdContact.getId()).never();
85
    }
86
  });
87
});
88