-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EW-617: Add tests for personenkontext creation.
- Loading branch information
1 parent
64c88fa
commit 371a764
Showing
13 changed files
with
419 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { DeepMocked, createMock } from '@golevelup/ts-jest'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { DoFactory, MapperTestModule } from '../../../../test/utils/index.js'; | ||
import { PersonenkontextDo } from '../domain/personenkontext.do.js'; | ||
import { PersonenkontextService } from '../domain/personenkontext.service.js'; | ||
import { CreatePersonenkontextDto } from './create-personenkontext.dto.js'; | ||
import { PersonApiMapperProfile } from './person-api.mapper.profile.js'; | ||
import { PersonenkontextUc } from './personenkontext.uc.js'; | ||
import { EntityCouldNotBeCreated } from '../../../shared/error/entity-could-not-be-created.error.js'; | ||
import { CreatedPersonenkontextDto } from './created-personenkontext.dto.js'; | ||
|
||
describe('PersonenkontextUc', () => { | ||
let module: TestingModule; | ||
let personenkontextUc: PersonenkontextUc; | ||
let personenkontextServiceMock: DeepMocked<PersonenkontextService>; | ||
|
||
beforeAll(async () => { | ||
module = await Test.createTestingModule({ | ||
imports: [MapperTestModule], | ||
providers: [ | ||
PersonenkontextUc, | ||
PersonApiMapperProfile, | ||
{ | ||
provide: PersonenkontextService, | ||
useValue: createMock<PersonenkontextService>(), | ||
}, | ||
], | ||
}).compile(); | ||
personenkontextUc = module.get(PersonenkontextUc); | ||
personenkontextServiceMock = module.get(PersonenkontextService); | ||
}); | ||
|
||
afterAll(async () => { | ||
await module.close(); | ||
}); | ||
|
||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(personenkontextUc).toBeDefined(); | ||
}); | ||
|
||
describe('createPersonenkontext', () => { | ||
describe('when creation of personenkontext is successful', () => { | ||
it('should not throw', async () => { | ||
const personenkontextDo: PersonenkontextDo<true> = DoFactory.createPersonenkontext(true); | ||
personenkontextServiceMock.createPersonenkontext.mockResolvedValue({ | ||
ok: true, | ||
value: personenkontextDo, | ||
}); | ||
|
||
const createPersonPromise: Promise<CreatedPersonenkontextDto> = personenkontextUc.createPersonenkontext( | ||
{} as CreatePersonenkontextDto, | ||
); | ||
|
||
await expect(createPersonPromise).resolves.not.toThrow(); | ||
}); | ||
}); | ||
describe('when creation of personenkontext is not successful', () => { | ||
it('should throw Error', async () => { | ||
const error: EntityCouldNotBeCreated = new EntityCouldNotBeCreated('Personenkontext'); | ||
personenkontextServiceMock.createPersonenkontext.mockResolvedValue({ | ||
ok: false, | ||
error: error, | ||
}); | ||
|
||
const createPersonPromise: Promise<CreatedPersonenkontextDto> = personenkontextUc.createPersonenkontext( | ||
{} as CreatePersonenkontextDto, | ||
); | ||
|
||
await expect(createPersonPromise).rejects.toThrow(error); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { Dictionary, Mapper } from '@automapper/core'; | ||
import { getMapperToken } from '@automapper/nestjs'; | ||
import { faker } from '@faker-js/faker'; | ||
import { DeepMocked, createMock } from '@golevelup/ts-jest'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { DoFactory } from '../../../../test/utils/do-factory.js'; | ||
import { EntityCouldNotBeCreated } from '../../../shared/error/entity-could-not-be-created.error.js'; | ||
import { PersonenkontextRepo } from '../persistence/personenkontext.repo.js'; | ||
import { PersonenkontextDo } from './personenkontext.do.js'; | ||
import { PersonenkontextService } from './personenkontext.service.js'; | ||
|
||
describe('PersonenkontextService', () => { | ||
let module: TestingModule; | ||
let personenkontextService: PersonenkontextService; | ||
let personenkontextRepoMock: DeepMocked<PersonenkontextRepo>; | ||
let mapperMock: DeepMocked<Mapper>; | ||
|
||
beforeAll(async () => { | ||
module = await Test.createTestingModule({ | ||
providers: [ | ||
PersonenkontextService, | ||
{ | ||
provide: PersonenkontextRepo, | ||
useValue: createMock<PersonenkontextRepo>(), | ||
}, | ||
{ | ||
provide: getMapperToken(), | ||
useValue: createMock<Mapper>(), | ||
}, | ||
], | ||
}).compile(); | ||
personenkontextService = module.get(PersonenkontextService); | ||
personenkontextRepoMock = module.get(PersonenkontextRepo); | ||
mapperMock = module.get(getMapperToken()); | ||
}); | ||
|
||
afterAll(async () => { | ||
await module.close(); | ||
}); | ||
|
||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(personenkontextService).toBeDefined(); | ||
}); | ||
|
||
describe('createPersonenkontext', () => { | ||
describe('when personenkontext is saved successfully', () => { | ||
it('should create an Personenkontext', async () => { | ||
const personenkontextDo: PersonenkontextDo<false> = DoFactory.createPersonenkontext(false); | ||
personenkontextRepoMock.save.mockResolvedValue(personenkontextDo as unknown as PersonenkontextDo<true>); | ||
mapperMock.map.mockReturnValue(personenkontextDo as unknown as Dictionary<unknown>); | ||
const result: Result<PersonenkontextDo<true>> = await personenkontextService.createPersonenkontext( | ||
personenkontextDo, | ||
); | ||
expect(result).toEqual<Result<PersonenkontextDo<true>>>({ | ||
ok: true, | ||
value: personenkontextDo as unknown as PersonenkontextDo<true>, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('when personenkontext is not saved successfully', () => { | ||
it('should return a domain error', async () => { | ||
const personenkontextDo: PersonenkontextDo<false> = DoFactory.createPersonenkontext(false); | ||
personenkontextDo.id = faker.string.uuid(); | ||
const result: Result<PersonenkontextDo<true>> = await personenkontextService.createPersonenkontext( | ||
personenkontextDo, | ||
); | ||
expect(result).toEqual<Result<PersonenkontextDo<true>>>({ | ||
ok: false, | ||
error: new EntityCouldNotBeCreated(`Personenkontext`), | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.