Passed
Pull Request — master (#216)
by
unknown
02:09 queued 19s
created

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

Complexity

Total Complexity 2
Complexity/F 0

Size

Lines of Code 124
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 106
mnd 2
bc 2
fnc 0
dl 0
loc 124
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 { ContactRepository } from 'src/Infrastructure/Contact/Repository/ContactRepository';
3
import { Contact } from 'src/Domain/Contact/Contact.entity';
4
import { UpdateContactCommand } from './UpdateContactCommand';
5
import { ContactNotFoundException } from 'src/Domain/Contact/Exception/ContactNotFoundException';
6
import { IsContactEmpty } from 'src/Domain/Contact/Specification/IsContactEmpty';
7
import { UpdateContactCommandHandler } from './UpdateContactCommandHandler';
8
import { EmptyContactException } from 'src/Domain/Contact/Exception/EmptyContactException';
9
10
describe('UpdateContactCommandHandler', () => {
11
  let contactRepository: ContactRepository;
12
  let updatedContact: Contact;
13
  let handler: UpdateContactCommandHandler;
14
15
  let command = new UpdateContactCommand(
16
    '2d5fb4da-12c2-11ea-8d71-362b9e155667',
17
    'Sarah',
18
    'Conor',
19
    'Aperture Science',
20
    '[email protected]',
21
    '0612345678',
22
    'Lorem ipsum'
23
  );
24
25
  beforeEach(() => {
26
    contactRepository = mock(ContactRepository);
27
    updatedContact = mock(Contact);
28
29
    handler = new UpdateContactCommandHandler(
30
      instance(contactRepository),
31
      new IsContactEmpty()
32
    );
33
  });
34
35
  it('testUpdateSuccessfully', async () => {
36
    when(
37
      contactRepository.findOneById('2d5fb4da-12c2-11ea-8d71-362b9e155667')
38
    ).thenResolve(instance(updatedContact));
39
40
    expect(await handler.execute(command)).toBeUndefined();
41
42
    verify(contactRepository.save(instance(updatedContact))).once();
43
    verify(
44
      updatedContact.update(
45
        'Sarah',
46
        'Conor',
47
        'Aperture Science',
48
        '[email protected]',
49
        '0612345678',
50
        'Lorem ipsum'
51
      )
52
    ).once();
53
    verify(
54
      updatedContact.update(
55
        'Sarah',
56
        'Conor',
57
        'Aperture Science',
58
        '[email protected]',
59
        '0612345678',
60
        'Lorem ipsum'
61
      )
62
    ).calledBefore(contactRepository.save(instance(updatedContact)));
63
  });
64
65
  it('testContactNotFound', async () => {
66
    when(
67
      contactRepository.findOneById('2d5fb4da-12c2-11ea-8d71-362b9e155667')
68
    ).thenResolve(null);
69
70
    try {
71
      await handler.execute(command);
72
    } catch (e) {
73
      expect(e).toBeInstanceOf(ContactNotFoundException);
74
      expect(e.message).toBe('crm.contacts.errors.not_found');
75
      verify(contactRepository.save(anything())).never();
76
      verify(
77
        updatedContact.update(
78
          anything(),
79
          anything(),
80
          anything(),
81
          anything(),
82
          anything(),
83
          anything()
84
        )
85
      ).never();
86
      verify(updatedContact.getFirstName()).never();
87
    }
88
  });
89
90
  it('testUpdateEmpty', async () => {
91
    when(
92
      contactRepository.findOneById('2d5fb4da-12c2-11ea-8d71-362b9e155667')
93
    ).thenResolve(instance(updatedContact));
94
95
    command = new UpdateContactCommand(
96
      '2d5fb4da-12c2-11ea-8d71-362b9e155667',
97
      '',
98
      '',
99
      '',
100
      '[email protected]',
101
      '0612345678',
102
      'Lorem ipsum'
103
    );
104
105
    try {
106
      await handler.execute(command);
107
    } catch (e) {
108
      expect(e).toBeInstanceOf(EmptyContactException);
109
      expect(e.message).toBe('crm.contacts.errors.empty');
110
      verify(contactRepository.save(anything())).never();
111
      verify(
112
        updatedContact.update(
113
          anything(),
114
          anything(),
115
          anything(),
116
          anything(),
117
          anything(),
118
          anything()
119
        )
120
      ).never();
121
    }
122
  });
123
});
124