Passed
Pull Request — master (#159)
by Mathieu
01:35
created

server/src/Application/Settings/Query/GetCooperativeQueryHandler.spec.ts   A

Complexity

Total Complexity 1
Complexity/F 0

Size

Lines of Code 45
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 45
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 } from 'ts-mockito';
2
import { CooperativeRepository } from 'src/Infrastructure/Settings/Repository/CooperativeRepository';
3
import { Cooperative } from 'src/Domain/Settings/Cooperative.entity';
4
import { GetCooperativeQueryHandler } from './GetCooperativeQueryHandler';
5
import { CooperativeView } from '../View/CooperativeView';
6
import { GetCooperativeQuery } from './GetCooperativeQuery';
7
import { CooperativeNotFoundException } from 'src/Domain/Settings/Repository/CooperativeNotFoundException';
8
9
describe('GetCooperativeQueryHandlerHandler', () => {
10
  it('testCooperativeNotFound', async () => {
11
    const cooperativeRepository = mock(CooperativeRepository);
12
13
    when(cooperativeRepository.find()).thenResolve(null);
14
15
    const queryHandler = new GetCooperativeQueryHandler(
16
      instance(cooperativeRepository)
17
    );
18
19
    try {
20
      await queryHandler.execute(new GetCooperativeQuery());
21
    } catch (e) {
22
      expect(e).toBeInstanceOf(CooperativeNotFoundException);
23
      expect(e.message).toBe('settings.errors.cooperative_not_found');
24
      verify(cooperativeRepository.find()).once();
25
    }
26
  });
27
28
  it('testGetCooperative', async () => {
29
    const cooperativeRepository = mock(CooperativeRepository);
30
    const cooperative = mock(Cooperative);
31
32
    when(cooperative.getDayDuration()).thenReturn(420);
33
    when(cooperativeRepository.find()).thenResolve(instance(cooperative));
34
35
    const queryHandler = new GetCooperativeQueryHandler(
36
      instance(cooperativeRepository)
37
    );
38
39
    expect(await queryHandler.execute(new GetCooperativeQuery())).toMatchObject(
40
      new CooperativeView(420)
41
    );
42
    verify(cooperativeRepository.find()).once();
43
  });
44
});
45