diff --git a/.DS_Store b/.DS_Store index 883af07..68449ce 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/package.json b/package.json index fa4d9f4..80a3492 100644 --- a/package.json +++ b/package.json @@ -80,7 +80,8 @@ "main", "module", "dto", - "interface" + "interface", + "countries" ], "coverageDirectory": "../coverage", "testEnvironment": "node" diff --git a/src/app.module.ts b/src/app.module.ts index 6b31e8b..ffb8593 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -6,7 +6,6 @@ import { ClubsModule } from './clubs/clubs.module'; import { CoreModule } from './core/core.module'; import { ConfigModule } from '@nestjs/config'; import { PrismaModule } from './prisma/prisma.module'; -import { CountriesService } from './countries/countries.service'; @Module({ imports: [ @@ -19,6 +18,6 @@ import { CountriesService } from './countries/countries.service'; PrismaModule, ], controllers: [AppController], - providers: [AppService, CountriesService], + providers: [AppService], }) export class AppModule {} diff --git a/src/clubs/clubs.controller.spec.ts b/src/clubs/clubs.controller.spec.ts index 5729ed4..502aa56 100644 --- a/src/clubs/clubs.controller.spec.ts +++ b/src/clubs/clubs.controller.spec.ts @@ -1,5 +1,25 @@ import { Test, TestingModule } from '@nestjs/testing'; import { ClubsController } from './clubs.controller'; +import { CryptoService } from '../core/crypto/crypto.service'; +import { JwtService } from '@nestjs/jwt'; +import { ConfigService } from '@nestjs/config'; +import { ClubsService } from './clubs.service'; +import { FilesService } from '../core/files/files.service'; +import { UpdateClubDto } from './entities/club.dto'; + +const mockGuard = {}; + +const mockClubService = { + getClubs: jest.fn().mockResolvedValue([]), + getOneClub: jest.fn().mockResolvedValue([]), + createClub: jest.fn().mockResolvedValue([]), + updateClub: jest.fn().mockResolvedValue([]), + deleteClub: jest.fn().mockResolvedValue([]), +}; + +const mockFileService = { + uploadImage: jest.fn().mockResolvedValue([]), +}; describe('ClubsController', () => { let controller: ClubsController; @@ -7,7 +27,19 @@ describe('ClubsController', () => { beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ controllers: [ClubsController], - }).compile(); + providers: [ + { provide: ClubsService, useValue: mockClubService }, + { provide: FilesService, useValue: mockFileService }, + CryptoService, + JwtService, + ConfigService, + FilesService, + ], + imports: [], + }) + .overrideGuard(mockGuard) + .useValue({ canActivate: jest.fn(() => true) }) + .compile(); controller = module.get(ClubsController); }); @@ -15,4 +47,41 @@ describe('ClubsController', () => { it('should be defined', () => { expect(controller).toBeDefined(); }); + + describe('findAll', () => { + it('should return an array of clubs', async () => { + const clubs = await controller.findAll(); + expect(clubs).toEqual([]); + }); + }); + + describe('findOne', () => { + it('should return a club', async () => { + const club = await controller.findOne('1'); + expect(club).toEqual([]); + }); + }); + + describe('update', () => { + describe('should update a club', () => { + it('without logo', async () => { + const club = await controller.update('1', {} as UpdateClubDto, null); + expect(club).toEqual([]); + }); + }); + }); + + describe('create', () => { + it('should create a club', async () => { + const club = await controller.create({} as UpdateClubDto, null); + expect(club).toEqual([]); + }); + }); + + describe('delete', () => { + it('should delete a club', async () => { + const club = await controller.delete('1'); + expect(club).toEqual([]); + }); + }); }); diff --git a/src/clubs/clubs.controller.ts b/src/clubs/clubs.controller.ts index 4a0e36c..6d57fa3 100644 --- a/src/clubs/clubs.controller.ts +++ b/src/clubs/clubs.controller.ts @@ -14,12 +14,12 @@ import { UsePipes, ValidationPipe, } from '@nestjs/common'; -import { FilesService } from 'src/core/files/files.service'; +import { FilesService } from '../core/files/files.service'; import { ClubsService } from './clubs.service'; import { FileInterceptor } from '@nestjs/platform-express'; import { ImgData } from 'src/types/image.data'; import { UpdateClubDto } from './entities/club.dto'; -import { LoggedGuard } from 'src/core/auth/logged.guard'; +import { LoggedGuard } from '../core/auth/logged.guard'; @UsePipes(new ValidationPipe({ whitelist: true, forbidNonWhitelisted: true })) @Controller('clubs') @@ -93,25 +93,6 @@ export class ClubsController { ) { const { name } = data; let logo: ImgData | null = null; - if (file) { - const cloudinaryResponse = await this.filesService.uploadImage( - name, - file, - ); - logo = { - publicId: cloudinaryResponse.public_id, - folder: cloudinaryResponse.folder, - fieldName: file.fieldname, - originalName: file.originalname, - secureUrl: cloudinaryResponse.secure_url, - resourceType: cloudinaryResponse.resource_type, - mimetype: file.mimetype, - format: cloudinaryResponse.format, - width: cloudinaryResponse.width, - height: cloudinaryResponse.height, - bytes: cloudinaryResponse.bytes, - }; - } if (file) { const cloudinaryResponse = await this.filesService.uploadImage( diff --git a/src/clubs/clubs.module.ts b/src/clubs/clubs.module.ts index ed8f4eb..6f8ddad 100644 --- a/src/clubs/clubs.module.ts +++ b/src/clubs/clubs.module.ts @@ -3,10 +3,12 @@ import { CoreModule } from 'src/core/core.module'; import { PrismaModule } from 'src/prisma/prisma.module'; import { ClubsService } from './clubs.service'; import { ClubsController } from './clubs.controller'; - +import { LoggedGuard } from '../core/auth/logged.guard'; +import { CryptoService } from '../core/crypto/crypto.service'; +import { JwtService } from '@nestjs/jwt'; @Module({ imports: [PrismaModule, CoreModule], - providers: [ClubsService], + providers: [ClubsService, LoggedGuard, CryptoService, JwtService], controllers: [ClubsController], }) export class ClubsModule {} diff --git a/src/clubs/clubs.service.spec.ts b/src/clubs/clubs.service.spec.ts index 6a5b3c1..26e9e74 100644 --- a/src/clubs/clubs.service.spec.ts +++ b/src/clubs/clubs.service.spec.ts @@ -1,13 +1,39 @@ import { Test, TestingModule } from '@nestjs/testing'; import { ClubsService } from './clubs.service'; +import { CreateClubDto } from './entities/club.dto'; +import { PrismaService } from '../prisma/prisma.service'; +import { LoggedGuard } from '../core/auth/logged.guard'; +import { NotFoundException } from '@nestjs/common'; + +const mockPrismaService = { + club: { + findMany: jest.fn(() => []), + findUnique: jest.fn(() => ({})), + create: jest.fn(() => ({})), + update: jest.fn(() => ({})), + delete: jest.fn(() => ({})), + }, + logo: { + create: jest.fn(() => ({})), + delete: jest.fn(() => ({})), + }, + $transaction: jest.fn(() => ({})), +}; describe('ClubsService', () => { let service: ClubsService; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ - providers: [ClubsService], - }).compile(); + imports: [], + providers: [ + ClubsService, + { provide: PrismaService, useValue: mockPrismaService }, + ], + }) + .overrideGuard(LoggedGuard) + .useValue({ canActivate: jest.fn(() => true) }) + .compile(); service = module.get(ClubsService); }); @@ -15,4 +41,40 @@ describe('ClubsService', () => { it('should be defined', () => { expect(service).toBeDefined(); }); + + describe('getClubs', () => { + it('should return an array of clubs', async () => { + const clubs = await service.getClubs(); + expect(clubs).toEqual([]); + }); + }); + + describe('getOneClub', () => { + it('should return a club', async () => { + const club = await service.getOneClub('1'); + expect(club).toEqual({}); + }); + }); + + describe('createClub', () => { + it('should create a club', async () => { + const club = await service.createClub({} as CreateClubDto, null); + expect(club).toEqual({}); + }); + }); + + describe('updateClub', () => { + it('should update a club', async () => { + const club = await service.updateClub('1', {} as CreateClubDto, null); + expect(club).toEqual({}); + }); + }); + + describe('deleteClub', () => { + it('should delete a club', async () => { + await service.deleteClub('1'); + expect(mockPrismaService.club.delete).toHaveBeenCalled(); + }); + it('should return an error if the club does not exist', async () => {}); + }); }); diff --git a/src/clubs/clubs.service.ts b/src/clubs/clubs.service.ts index 486dfa5..90e44e7 100644 --- a/src/clubs/clubs.service.ts +++ b/src/clubs/clubs.service.ts @@ -1,5 +1,5 @@ import { Injectable, NotFoundException } from '@nestjs/common'; -import { PrismaService } from 'src/prisma/prisma.service'; +import { PrismaService } from '../prisma/prisma.service'; import { CreateClubDto } from './entities/club.dto'; import { Club } from './entities/club.interface'; import { ImgData } from 'src/types/image.data';