1
|
|
|
import { mock, instance, when, verify, anything, deepEqual } from 'ts-mockito'; |
2
|
|
|
import { DeleteContactCommandHandler } from './DeleteContactCommandHandler'; |
3
|
|
|
import { DeleteContactCommand } from './DeleteContactCommand'; |
4
|
|
|
import { ContactNotFoundException } from 'src/Domain/Contact/Exception/ContactNotFoundException'; |
5
|
|
|
import { Contact } from 'src/Domain/Contact/Contact.entity'; |
6
|
|
|
import { ContactRepository } from 'src/Infrastructure/Contact/Repository/ContactRepository'; |
7
|
|
|
|
8
|
|
|
describe('DeleteContactCommandHandler', () => { |
9
|
|
|
let contactRepository: ContactRepository; |
10
|
|
|
let handler: DeleteContactCommandHandler; |
11
|
|
|
|
12
|
|
|
const contact = mock(Contact); |
13
|
|
|
const command = new DeleteContactCommand( |
14
|
|
|
'2d5fb4da-12c2-11ea-8d71-362b9e155667' |
15
|
|
|
); |
16
|
|
|
|
17
|
|
|
beforeEach(() => { |
18
|
|
|
contactRepository = mock(ContactRepository); |
19
|
|
|
|
20
|
|
|
handler = new DeleteContactCommandHandler(instance(contactRepository)); |
21
|
|
|
}); |
22
|
|
|
|
23
|
|
|
it('testContactNotFound', async () => { |
24
|
|
|
when( |
25
|
|
|
contactRepository.findOneById('2d5fb4da-12c2-11ea-8d71-362b9e155667') |
26
|
|
|
).thenResolve(null); |
27
|
|
|
|
28
|
|
|
try { |
29
|
|
|
expect(await handler.execute(command)).toBeUndefined(); |
30
|
|
|
} catch (e) { |
31
|
|
|
expect(e).toBeInstanceOf(ContactNotFoundException); |
32
|
|
|
expect(e.message).toBe('crm.contacts.errors.not_found'); |
33
|
|
|
verify( |
34
|
|
|
contactRepository.findOneById('2d5fb4da-12c2-11ea-8d71-362b9e155667') |
35
|
|
|
).once(); |
36
|
|
|
verify(contactRepository.remove(anything())).never(); |
37
|
|
|
} |
38
|
|
|
}); |
39
|
|
|
|
40
|
|
|
it('testContactSuccessfullyDeleted', async () => { |
41
|
|
|
when( |
42
|
|
|
contactRepository.findOneById('2d5fb4da-12c2-11ea-8d71-362b9e155667') |
43
|
|
|
).thenResolve(instance(contact)); |
44
|
|
|
|
45
|
|
|
expect(await handler.execute(command)).toBeUndefined(); |
46
|
|
|
|
47
|
|
|
verify( |
48
|
|
|
contactRepository.findOneById('2d5fb4da-12c2-11ea-8d71-362b9e155667') |
49
|
|
|
).once(); |
50
|
|
|
verify(contactRepository.remove(instance(contact))).once(); |
51
|
|
|
}); |
52
|
|
|
}); |
53
|
|
|
|