From 99c1370dd1bcd958544a97bcf9dd9b3aa08d1dcd Mon Sep 17 00:00:00 2001 From: Kev Date: Thu, 16 May 2024 17:14:06 +0200 Subject: [PATCH] testing with +96 --- .DS_Store | Bin 8196 -> 8196 bytes package.json | 3 +- src/app.module.ts | 3 +- src/clubs/clubs.controller.spec.ts | 71 ++++++++++++++++++++++++++++- src/clubs/clubs.controller.ts | 23 +--------- src/clubs/clubs.module.ts | 6 ++- src/clubs/clubs.service.spec.ts | 66 ++++++++++++++++++++++++++- src/clubs/clubs.service.ts | 2 +- 8 files changed, 144 insertions(+), 30 deletions(-) diff --git a/.DS_Store b/.DS_Store index 883af07a9846b6a35bcda089f78c6eda3d44f960..68449ce64b7ffd94b88188bbd5b45f463132bd5c 100644 GIT binary patch delta 220 zcmZp1XmQw}CJ_65CIbTl3xgg*IzuKyNp8N2OHxjL5>Si-i21dDA9X~PPr)l+kYN~{ zoS$0&)Wg7_x?yv(z-dON>cx`-gp}AA*mQuV0hwZx`-FH{K-|gmgdCW}j3z%2l4dT@ z{5|=fkPJ(MtlIC%a>5@OXKWS_(P3oT|9G;Us02#`Gf;Vys1yssW{KaMD@2d5t>NSrplLv+*yKJT9+rlgTYgQRC*;5+`E&9E zA!%lXUB4#(6Ov(J*uC@DWI5rFjMFv?i0CjfZPu7 { 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';