Skip to content

Commit

Permalink
feat(modules/profiles): implement ProfileService with create method
Browse files Browse the repository at this point in the history
  • Loading branch information
Mnigos committed Oct 17, 2023
1 parent 5070eeb commit a6b28d2
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/modules/profiles/index.ts
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'
9 changes: 6 additions & 3 deletions src/modules/profiles/profiles.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ import { TypeOrmModule } from '@nestjs/typeorm'

import { Profile } from './profile.entity'
import { ProfilesRepository } from './profiles.repository'
import { ProfilesService } from './profiles.service'

import { ImagesModule } from '@modules/images'

@Module({
imports: [TypeOrmModule.forFeature([Profile])],
providers: [ProfilesRepository],
exports: [ProfilesRepository],
imports: [TypeOrmModule.forFeature([Profile]), ImagesModule],
providers: [ProfilesRepository, ProfilesService],
exports: [ProfilesRepository, ProfilesService],
})
export class ProfilesModule {}
64 changes: 64 additions & 0 deletions src/modules/profiles/profiles.service.spec.ts
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()
})
})
29 changes: 29 additions & 0 deletions src/modules/profiles/profiles.service.ts
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,
})
}
}

0 comments on commit a6b28d2

Please sign in to comment.