Passed
Pull Request — master (#188)
by Mathieu
02:03
created

server/src/Application/Customer/Command/CreateCustomerCommandHandler.spec.ts   A

Complexity

Total Complexity 1
Complexity/F 0

Size

Lines of Code 85
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 71
dl 0
loc 85
rs 10
c 0
b 0
f 0
wmc 1
mnd 1
bc 1
fnc 0
bpm 0
cpm 0
noi 0
1
import {mock, instance, when, verify, deepEqual, anything} from 'ts-mockito';
2
import {CustomerRepository} from 'src/Infrastructure/Customer/Repository/CustomerRepository';
3
import {IsCustomerAlreadyExist} from 'src/Domain/Customer/Specification/IsCustomerAlreadyExist';
4
import {Customer} from 'src/Domain/Customer/Customer.entity';
5
import {CreateCustomerCommandHandler} from 'src/Application/Customer/Command/CreateCustomerCommandHandler';
6
import {CreateCustomerCommand} from 'src/Application/Customer/Command/CreateCustomerCommand';
7
import {CustomerAlreadyExistException} from 'src/Domain/Customer/Exception/CustomerAlreadyExistException';
8
import {AddressRepository} from 'src/Infrastructure/Customer/Repository/AddressRepository';
9
import {Address} from 'src/Domain/Customer/Address.entity';
10
11
describe('CreateCustomerCommandHandler', () => {
12
  let customerRepository: CustomerRepository;
13
  let addressRepository: AddressRepository;
14
  let isCustomerAlreadyExist: IsCustomerAlreadyExist;
15
  let createdCustomer: Customer;
16
  let createdAddress: Address;
17
  let handler: CreateCustomerCommandHandler;
18
19
  const command = new CreateCustomerCommand(
20
    'Customer',
21
    '2 rue Dieu',
22
    'Paris',
23
    '75010',
24
    'FR'
25
  );
26
27
  beforeEach(() => {
28
    customerRepository = mock(CustomerRepository);
29
    addressRepository = mock(AddressRepository);
30
    isCustomerAlreadyExist = mock(IsCustomerAlreadyExist);
31
    createdCustomer = mock(Customer);
32
    createdAddress = mock(Address);
33
34
    handler = new CreateCustomerCommandHandler(
35
      instance(customerRepository),
36
      instance(addressRepository),
37
      instance(isCustomerAlreadyExist)
38
    );
39
  });
40
41
  it('testCustomerCreatedSuccessfully', async () => {
42
    when(isCustomerAlreadyExist.isSatisfiedBy('Customer')).thenResolve(false);
43
    when(createdCustomer.getId()).thenReturn(
44
      '2d5fb4da-12c2-11ea-8d71-362b9e155667'
45
    );
46
    when(createdCustomer.getName()).thenReturn('Customer');
47
    when(
48
      addressRepository.save(
49
        deepEqual(new Address('2 rue Dieu', 'Paris', '75010', 'FR'))
50
      )
51
    ).thenResolve(instance(createdAddress));
52
    when(
53
      customerRepository.save(
54
        deepEqual(new Customer('Customer', instance(createdAddress)))
55
      )
56
    ).thenResolve(instance(createdCustomer));
57
58
    expect(await handler.execute(command)).toBe(
59
      '2d5fb4da-12c2-11ea-8d71-362b9e155667'
60
    );
61
62
    verify(isCustomerAlreadyExist.isSatisfiedBy('Customer')).once();
63
    verify(
64
      customerRepository.save(
65
        deepEqual(new Customer('Customer', instance(createdAddress)))
66
      )
67
    ).once();
68
    verify(createdCustomer.getId()).once();
69
  });
70
71
  it('testCustomerAlreadyExist', async () => {
72
    when(isCustomerAlreadyExist.isSatisfiedBy('Customer')).thenResolve(true);
73
74
    try {
75
      await handler.execute(command);
76
    } catch (e) {
77
      expect(e).toBeInstanceOf(CustomerAlreadyExistException);
78
      expect(e.message).toBe('crm.customers.errors.already_exist');
79
      verify(isCustomerAlreadyExist.isSatisfiedBy('Customer')).once();
80
      verify(customerRepository.save(anything())).never();
81
      verify(createdCustomer.getId()).never();
82
    }
83
  });
84
});
85