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 { UpdateContactCommandHandler } from './UpdateContactCommandHandler'; |
7
|
|
|
|
8
|
|
|
describe('UpdateContactCommandHandler', () => { |
9
|
|
|
let contactRepository: ContactRepository; |
10
|
|
|
let updatedContact: Contact; |
11
|
|
|
let handler: UpdateContactCommandHandler; |
12
|
|
|
|
13
|
|
|
const command = new UpdateContactCommand( |
14
|
|
|
'2d5fb4da-12c2-11ea-8d71-362b9e155667', |
15
|
|
|
'Sarah', |
16
|
|
|
'Conor', |
17
|
|
|
'Aperture Science', |
18
|
|
|
'[email protected]', |
19
|
|
|
'0612345678', |
20
|
|
|
'Lorem ipsum' |
21
|
|
|
); |
22
|
|
|
|
23
|
|
|
beforeEach(() => { |
24
|
|
|
contactRepository = mock(ContactRepository); |
25
|
|
|
updatedContact = mock(Contact); |
26
|
|
|
|
27
|
|
|
handler = new UpdateContactCommandHandler(instance(contactRepository)); |
28
|
|
|
}); |
29
|
|
|
|
30
|
|
|
it('testUpdateSuccessfully', async () => { |
31
|
|
|
when( |
32
|
|
|
contactRepository.findOneById('2d5fb4da-12c2-11ea-8d71-362b9e155667') |
33
|
|
|
).thenResolve(instance(updatedContact)); |
34
|
|
|
|
35
|
|
|
expect(await handler.execute(command)).toBeUndefined(); |
36
|
|
|
|
37
|
|
|
verify(contactRepository.save(instance(updatedContact))).once(); |
38
|
|
|
verify( |
39
|
|
|
updatedContact.update( |
40
|
|
|
'Sarah', |
41
|
|
|
'Conor', |
42
|
|
|
'Aperture Science', |
43
|
|
|
'[email protected]', |
44
|
|
|
'0612345678', |
45
|
|
|
'Lorem ipsum' |
46
|
|
|
) |
47
|
|
|
).once(); |
48
|
|
|
verify( |
49
|
|
|
updatedContact.update( |
50
|
|
|
'Sarah', |
51
|
|
|
'Conor', |
52
|
|
|
'Aperture Science', |
53
|
|
|
'[email protected]', |
54
|
|
|
'0612345678', |
55
|
|
|
'Lorem ipsum' |
56
|
|
|
) |
57
|
|
|
).calledBefore(contactRepository.save(instance(updatedContact))); |
58
|
|
|
}); |
59
|
|
|
|
60
|
|
|
it('testContactNotFound', async () => { |
61
|
|
|
when( |
62
|
|
|
contactRepository.findOneById('2d5fb4da-12c2-11ea-8d71-362b9e155667') |
63
|
|
|
).thenResolve(null); |
64
|
|
|
|
65
|
|
|
try { |
66
|
|
|
await handler.execute(command); |
67
|
|
|
} catch (e) { |
68
|
|
|
expect(e).toBeInstanceOf(ContactNotFoundException); |
69
|
|
|
expect(e.message).toBe('crm.contacts.errors.not_found'); |
70
|
|
|
verify(contactRepository.save(anything())).never(); |
71
|
|
|
verify( |
72
|
|
|
updatedContact.update( |
73
|
|
|
anything(), |
74
|
|
|
anything(), |
75
|
|
|
anything(), |
76
|
|
|
anything(), |
77
|
|
|
anything(), |
78
|
|
|
anything() |
79
|
|
|
) |
80
|
|
|
).never(); |
81
|
|
|
verify(updatedContact.getFirstName()).never(); |
82
|
|
|
} |
83
|
|
|
}); |
84
|
|
|
}); |
85
|
|
|
|