Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tikle-cap committed Nov 29, 2024
1 parent 6fb0f8e commit 173317c
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { OrganisationRepository } from '../../organisation/persistence/organisat
import { KeycloakConfig } from '../../../shared/config/keycloak.config.js';
import { KeycloakUserService } from '../../keycloak-administration/index.js';
import PersonTimeLimitService from '../services/person-time-limit-info.service.js';
import { TimeLimitOccasion } from '../domain/time-limit-occasion.enums.js';

describe('AuthenticationController', () => {
let module: TestingModule;
Expand All @@ -39,7 +40,7 @@ describe('AuthenticationController', () => {
let rolleRepoMock: DeepMocked<RolleRepo>;
const keycloakUserServiceMock: DeepMocked<KeycloakUserService> = createMock<KeycloakUserService>();
let keyCloakConfig: KeycloakConfig;

let personTimeLimitServiceMock: DeepMocked<PersonTimeLimitService>;
beforeAll(async () => {
module = await Test.createTestingModule({
imports: [
Expand Down Expand Up @@ -94,6 +95,7 @@ describe('AuthenticationController', () => {
dbiamPersonenkontextRepoMock = module.get(DBiamPersonenkontextRepo);
organisationRepoMock = module.get(OrganisationRepository);
rolleRepoMock = module.get(RolleRepo);
personTimeLimitServiceMock = module.get(PersonTimeLimitService);
});

afterEach(() => {
Expand Down Expand Up @@ -280,6 +282,13 @@ describe('AuthenticationController', () => {
value: person.updatedAt,
});

personTimeLimitServiceMock.getPersonTimeLimitInfo.mockResolvedValueOnce([
{
occasion: TimeLimitOccasion.KOPERS,
deadline: faker.date.future(),
},
]);

const requestMock: Request = setupRequest();
const result: UserinfoResponse = await authController.info(permissions, requestMock);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { DeepMocked, createMock } from '@golevelup/ts-jest';
import { TestingModule, Test } from '@nestjs/testing';
import { PersonRepository } from '../../person/persistence/person.repository.js';
import PersonTimeLimitService from './person-time-limit-info.service.js';
import { DBiamPersonenkontextService } from '../../personenkontext/domain/dbiam-personenkontext.service.js';
import { Person } from '../../person/domain/person.js';
import { DoFactory } from '../../../../test/utils/do-factory.js';
import { PersonTimeLimitInfo } from '../domain/person-time-limit-info.js';
import { TimeLimitOccasion } from '../domain/time-limit-occasion.enums.js';
import { Personenkontext } from '../../personenkontext/domain/personenkontext.js';

describe('PersonTimeLimitService', () => {
let module: TestingModule;
let sut: PersonTimeLimitService;
let personRepoMock: DeepMocked<PersonRepository>;
let dBiamPersonenkontextServiceMock: DeepMocked<DBiamPersonenkontextService>;

beforeAll(async () => {
module = await Test.createTestingModule({
providers: [
PersonTimeLimitService,
{
provide: PersonRepository,
useValue: createMock<PersonRepository>(),
},
{
provide: DBiamPersonenkontextService,
useValue: createMock<DBiamPersonenkontextService>(),
},
],
}).compile();
sut = module.get(PersonTimeLimitService);
personRepoMock = module.get(PersonRepository);
dBiamPersonenkontextServiceMock = module.get(DBiamPersonenkontextService);
});

afterAll(async () => {
await module.close();
});

beforeEach(() => {
jest.resetAllMocks();
});

it('should be defined', () => {
expect(sut).toBeDefined();
});

describe('getPersonTimeLimitInfo', () => {
it('should return PersonTimeLimitInfo array', async () => {
const person: Person<true> = DoFactory.createPerson(true);
person.personalnummer = undefined;
personRepoMock.findById.mockResolvedValue(person);

dBiamPersonenkontextServiceMock.isPersonalnummerRequiredForAnyPersonenkontextForPerson.mockResolvedValue(
true,
);
const pesonenkontext: Personenkontext<true> = DoFactory.createPersonenkontext(true);
dBiamPersonenkontextServiceMock.getKopersPersonenkontext.mockResolvedValue(pesonenkontext);

const result: PersonTimeLimitInfo[] = await sut.getPersonTimeLimitInfo(person.id);

const expectedDeadline: Date = new Date(pesonenkontext.createdAt);
expectedDeadline.setDate(expectedDeadline.getDate() + 56);

expect(result).toEqual<PersonTimeLimitInfo[]>([
{
occasion: TimeLimitOccasion.KOPERS,
deadline: expectedDeadline,
},
]);
});

it('should return empty array when person isnt found ', async () => {
personRepoMock.findById.mockResolvedValue(null);
const result: PersonTimeLimitInfo[] = await sut.getPersonTimeLimitInfo('');

expect(result).toEqual<PersonTimeLimitInfo[]>([]);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,60 @@ describe('DBiamPersonenkontextService', () => {
});
});
});

describe('getKopersPersonenkontext', () => {
describe('when a person has a personenkontext with a rolle with koperspflichtig merkmale', () => {
it('should return the personenkontext', async () => {
const personenkontexte: Personenkontext<true>[] = [
personenkontextFactory.construct('1', faker.date.past(), faker.date.recent(), '', '1', '1', '1'),
personenkontextFactory.construct('2', faker.date.past(), faker.date.recent(), '', '1', '2', '1'),
personenkontextFactory.construct('3', faker.date.past(), faker.date.recent(), '', '1', '1', '2'),
];
dbiamPersonenKontextRepoMock.findByPerson.mockResolvedValue(personenkontexte);

const mapRollen: Map<string, Rolle<true>> = new Map();
mapRollen.set(
'1',
DoFactory.createRolle(true, {
rollenart: RollenArt.LEHR,
merkmale: [RollenMerkmal.KOPERS_PFLICHT],
id: '1',
}),
);
mapRollen.set('2', DoFactory.createRolle(true, { rollenart: RollenArt.LEIT, merkmale: [], id: '2' }));
rolleRepoMock.findByIds.mockResolvedValueOnce(mapRollen);

const result: Personenkontext<true> | null = await sut.getKopersPersonenkontext('1');
expect(result).toEqual(personenkontexte[0]);
});
});

describe('when a person has no personenkontext with a rolle with koperspflichtig merkmale', () => {
it('should return null', async () => {
const personenkontexte: Personenkontext<true>[] = [
personenkontextFactory.construct('1', faker.date.past(), faker.date.recent(), '', '1', '1', '1'),
personenkontextFactory.construct('2', faker.date.past(), faker.date.recent(), '', '1', '2', '1'),
personenkontextFactory.construct('3', faker.date.past(), faker.date.recent(), '', '1', '1', '2'),
];
dbiamPersonenKontextRepoMock.findByPerson.mockResolvedValue(personenkontexte);

const mapRollen: Map<string, Rolle<true>> = new Map();

mapRollen.set(
'1',
DoFactory.createRolle(true, {
rollenart: RollenArt.LERN,
merkmale: [],
id: '1',
}),
);

mapRollen.set('2', DoFactory.createRolle(true, { rollenart: RollenArt.LEIT, merkmale: [], id: '2' }));
rolleRepoMock.findByIds.mockResolvedValueOnce(mapRollen);

const result: Personenkontext<true> | null = await sut.getKopersPersonenkontext('1');
expect(result).toBeNull();
});
});
});
});

0 comments on commit 173317c

Please sign in to comment.