src/Application/Customer/Query/GetCustomerByIdQueryHandler.spec.ts   A
last analyzed

Complexity

Total Complexity 1
Complexity/F 0

Size

Lines of Code 59
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 1
eloc 46
mnd 1
bc 1
fnc 0
dl 0
loc 59
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
rs 10
1
import { mock, instance, when, verify } from 'ts-mockito';
2
import { CustomerRepository } from 'src/Infrastructure/Customer/Repository/CustomerRepository';
3
import { Customer } from 'src/Domain/Customer/Customer.entity';
4
import { CustomerView } from 'src/Application/Customer/View/CustomerView';
5
import { GetCustomerByIdQueryHandler } from './GetCustomerByIdQueryHandler';
6
import { GetCustomerByIdQuery } from './GetCustomerByIdQuery';
7
import { CustomerNotFoundException } from 'src/Domain/Customer/Exception/CustomerNotFoundException';
8
9
describe('GetCustomerByIdQueryHandler', () => {
10
  const query = new GetCustomerByIdQuery(
11
    'eb9e1d9b-dce2-48a9-b64f-f0872f3157d2'
12
  );
13
14
  it('testGetCustomer', async () => {
15
    const customerRepository = mock(CustomerRepository);
16
    const queryHandler = new GetCustomerByIdQueryHandler(
17
      instance(customerRepository)
18
    );
19
    const expectedResult = new CustomerView(
20
      'eb9e1d9b-dce2-48a9-b64f-f0872f3157d2',
21
      'Radio France'
22
    );
23
    const customer = mock(Customer);
24
25
    when(customer.getId()).thenReturn('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2');
26
    when(customer.getName()).thenReturn('Radio France');
27
28
    when(
29
      customerRepository.findOneById('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2')
30
    ).thenResolve(instance(customer));
31
32
    expect(await queryHandler.execute(query)).toMatchObject(expectedResult);
33
34
    verify(
35
      customerRepository.findOneById('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2')
36
    ).once();
37
  });
38
39
  it('testGetCustomerNotFound', async () => {
40
    const customerRepository = mock(CustomerRepository);
41
    const queryHandler = new GetCustomerByIdQueryHandler(
42
      instance(customerRepository)
43
    );
44
    when(
45
      customerRepository.findOneById('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2')
46
    ).thenResolve(null);
47
48
    try {
49
      await queryHandler.execute(query);
50
    } catch (e) {
51
      expect(e).toBeInstanceOf(CustomerNotFoundException);
52
      expect(e.message).toBe('crm.customers.errors.not_found');
53
      verify(
54
        customerRepository.findOneById('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2')
55
      ).once();
56
    }
57
  });
58
});
59