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
|
|
|
|