-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(modules/profiles): implement
ProfileService
with create
method
- Loading branch information
Showing
4 changed files
with
100 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './profile.entity' | ||
export * from './profiles.repository' | ||
export * from './profiles.module' | ||
export * from './profiles.service' |
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,64 @@ | ||
import { beforeEach, describe, expect, test, vi } from 'vitest' | ||
import { Test, TestingModule } from '@nestjs/testing' | ||
import { mock } from 'vitest-mock-extended' | ||
|
||
import { ProfilesService } from './profiles.service' | ||
import { ProfilesRepository } from './profiles.repository' | ||
import { CreateProfile } from './dtos' | ||
import { Profile } from './profile.entity' | ||
|
||
import { Image, ImagesRepository } from '@modules/images' | ||
|
||
describe('ProfilesService', () => { | ||
let profileService: ProfilesService | ||
let profileRepository: ProfilesRepository | ||
let imagesRepository: ImagesRepository | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
ProfilesService, | ||
{ | ||
provide: ProfilesRepository, | ||
useValue: { | ||
createProfile: vi.fn(), | ||
}, | ||
}, | ||
{ | ||
provide: ImagesRepository, | ||
useValue: { | ||
createImage: vi.fn(), | ||
}, | ||
}, | ||
], | ||
}).compile() | ||
|
||
profileService = module.get<ProfilesService>(ProfilesService) | ||
profileRepository = module.get<ProfilesRepository>(ProfilesRepository) | ||
imagesRepository = module.get<ImagesRepository>(ImagesRepository) | ||
}) | ||
|
||
test('should be defined', () => { | ||
expect(profileService).toBeDefined() | ||
}) | ||
|
||
test('should create profile', async () => { | ||
const imageMock = mock<Image>() | ||
const createProfileMock = mock<CreateProfile>({ | ||
images: [imageMock], | ||
}) | ||
const profileMock = mock<Profile>() | ||
|
||
const createProfileSpy = vi | ||
.spyOn(profileRepository, 'createProfile') | ||
.mockResolvedValue(profileMock) | ||
const createImageSpy = vi | ||
.spyOn(imagesRepository, 'createImage') | ||
.mockResolvedValue(imageMock) | ||
|
||
expect(await profileService.create(createProfileMock)).toEqual(profileMock) | ||
|
||
expect(createProfileSpy).toHaveBeenCalled() | ||
expect(createImageSpy).toHaveBeenCalled() | ||
}) | ||
}) |
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,29 @@ | ||
import { Injectable } from '@nestjs/common' | ||
|
||
import { ProfilesRepository } from './profiles.repository' | ||
import { CreateProfile } from './dtos' | ||
|
||
import { ImagesRepository } from '@modules/images' | ||
|
||
@Injectable() | ||
export class ProfilesService { | ||
constructor( | ||
private readonly profilesRepository: ProfilesRepository, | ||
private readonly imagesRepository: ImagesRepository | ||
) {} | ||
|
||
create({ images, ...newProfile }: CreateProfile) { | ||
let imageEntities = [] | ||
|
||
images.map(async image => { | ||
const newImage = await this.imagesRepository.createImage(image) | ||
|
||
imageEntities = [newImage] | ||
}) | ||
|
||
return this.profilesRepository.createProfile({ | ||
...newProfile, | ||
images: imageEntities, | ||
}) | ||
} | ||
} |