1
|
|
|
import { mock, instance, when, verify } from 'ts-mockito'; |
2
|
|
|
import { ContactRepository } from 'src/Infrastructure/Contact/Repository/ContactRepository'; |
3
|
|
|
import { Contact } from 'src/Domain/Contact/Contact.entity'; |
4
|
|
|
import { ContactView } from 'src/Application/Contact/View/ContactView'; |
5
|
|
|
import { GetContactByIdQueryHandler } from './GetContactByIdQueryHandler'; |
6
|
|
|
import { GetContactByIdQuery } from './GetContactByIdQuery'; |
7
|
|
|
import { ContactNotFoundException } from 'src/Domain/Contact/Exception/ContactNotFoundException'; |
8
|
|
|
|
9
|
|
|
describe('GetContactByIdQueryHandler', () => { |
10
|
|
|
const query = new GetContactByIdQuery('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2'); |
11
|
|
|
|
12
|
|
|
it('testGetContact', async () => { |
13
|
|
|
const contactRepository = mock(ContactRepository); |
14
|
|
|
const handler = new GetContactByIdQueryHandler(instance(contactRepository)); |
15
|
|
|
|
16
|
|
|
const contact = mock(Contact); |
17
|
|
|
when(contact.getId()).thenReturn('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2'); |
18
|
|
|
when(contact.getFirstName()).thenReturn('Sarah'); |
19
|
|
|
when(contact.getLastName()).thenReturn('Conor'); |
20
|
|
|
when(contact.getCompany()).thenReturn('Aperture Science'); |
21
|
|
|
when(contact.getEmail()).thenReturn('[email protected]'); |
22
|
|
|
when(contact.getPhoneNumber()).thenReturn('0687654321'); |
23
|
|
|
when(contact.getNotes()).thenReturn('Encountered crossing portals'); |
24
|
|
|
|
25
|
|
|
when( |
26
|
|
|
contactRepository.findOneById('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2') |
27
|
|
|
).thenResolve(instance(contact)); |
28
|
|
|
|
29
|
|
|
const expectedResult = new ContactView( |
30
|
|
|
'eb9e1d9b-dce2-48a9-b64f-f0872f3157d2', |
31
|
|
|
'Sarah', |
32
|
|
|
'Conor', |
33
|
|
|
'Aperture Science', |
34
|
|
|
'[email protected]', |
35
|
|
|
'0687654321', |
36
|
|
|
'Encountered crossing portals' |
37
|
|
|
); |
38
|
|
|
|
39
|
|
|
expect(await handler.execute(query)).toMatchObject(expectedResult); |
40
|
|
|
|
41
|
|
|
verify( |
42
|
|
|
contactRepository.findOneById('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2') |
43
|
|
|
).once(); |
44
|
|
|
}); |
45
|
|
|
|
46
|
|
|
it('testGetContactNotFound', async () => { |
47
|
|
|
const contactRepository = mock(ContactRepository); |
48
|
|
|
const handler = new GetContactByIdQueryHandler(instance(contactRepository)); |
49
|
|
|
|
50
|
|
|
when( |
51
|
|
|
contactRepository.findOneById('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2') |
52
|
|
|
).thenResolve(null); |
53
|
|
|
|
54
|
|
|
try { |
55
|
|
|
await handler.execute(query); |
56
|
|
|
} catch (e) { |
57
|
|
|
expect(e).toBeInstanceOf(ContactNotFoundException); |
58
|
|
|
expect(e.message).toBe('crm.contacts.errors.not_found'); |
59
|
|
|
verify( |
60
|
|
|
contactRepository.findOneById('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2') |
61
|
|
|
).once(); |
62
|
|
|
} |
63
|
|
|
}); |
64
|
|
|
}); |
65
|
|
|
|