Skip to content

Commit

Permalink
test: cover getMany and create for partners entity (#154)
Browse files Browse the repository at this point in the history
- Melhora a cobertura de testes para a entidade `Partner`
  • Loading branch information
BertBR authored May 23, 2024
1 parent 28a6146 commit bc100dd
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 19 deletions.
65 changes: 56 additions & 9 deletions src/partners/partners.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,76 @@
import { Test, TestingModule } from '@nestjs/testing';
import { PartnersController } from './partners.controller';
import { PartnersService } from './partners.service';
import { PrismaService } from '../prisma/prisma.service';
import { ServerResponse } from '../utils';

describe('PartnersController', () => {
let controller: PartnersController;
let service: PartnersService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [PartnersController],
providers: [PartnersService],
})
.useMocker((token) => {
if (token === PrismaService) {
return {
supplyCategory: { findMany: jest.fn().mockResolvedValue(0) },
};
}
.overrideProvider(PartnersService)
.useValue({
index: jest.fn().mockResolvedValue([
{
id: 1,
name: 'Partner 1',
link: 'https://partner1.com',
},
]),
store: jest.fn().mockResolvedValue({}),
})
.compile();

controller = module.get<PartnersController>(PartnersController);
service = module.get<PartnersService>(PartnersService);
});

it('should be defined', () => {
expect(controller).toBeDefined();
it('should return all partners', async () => {
const expectedResponse = new ServerResponse(
200,
'Successfully get partners',
[
{
id: 1,
name: 'Partner 1',
link: 'https://partner1.com',
},
],
);
const result = await controller.index();
expect(result).toEqual(expectedResponse);
});

it('should create a partner', async () => {
const expectedResponse = new ServerResponse(
201,
'Successfully created partner',
);
const result = await controller.store({
name: 'Partner 1',
link: 'https://partner1.com',
});
expect(result).toEqual(expectedResponse);
});

it('should throw an error when store fails', async () => {
const errorMessage = 'Failed to create partner';
jest.spyOn(service, 'store').mockRejectedValue(new Error(errorMessage));
await expect(
controller.store({
name: 'Partner 1',
link: 'https://partner1.com',
}),
).rejects.toThrow();
});

it('should throw an error when index fails', async () => {
const errorMessage = 'Failed to get partners';
jest.spyOn(service, 'index').mockRejectedValue(new Error(errorMessage));
await expect(controller.index()).rejects.toThrow();
});
});
41 changes: 31 additions & 10 deletions src/partners/partners.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,49 @@
import { Test, TestingModule } from '@nestjs/testing';

import { PartnersService } from './partners.service';
import { PrismaService } from '../prisma/prisma.service';

jest.mock('../prisma/prisma.service');

describe('PartnersService', () => {
let service: PartnersService;
let prismaService: PrismaService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [PartnersService],
providers: [
PartnersService,
{ provide: PrismaService, useClass: PrismaService },
],
})
.useMocker((token) => {
if (token === PrismaService) {
return {
supplyCategory: { findMany: jest.fn().mockResolvedValue(0) },
};
}
.overrideProvider(PrismaService)
.useValue({
partners: {
findMany: jest.fn().mockResolvedValue([]),
create: jest.fn().mockResolvedValue({}),
},
})
.compile();

service = module.get<PartnersService>(PartnersService);
prismaService = module.get<PrismaService>(PrismaService);
});
it('should return all partners', async () => {
const partners = await service.index();
expect(partners).toEqual([]);
});

it('should create a partner', async () => {
await service.store({
name: 'Partner 1',
link: 'https://partner1.com',
});

it('should be defined', () => {
expect(service).toBeDefined();
expect(prismaService.partners.create).toHaveBeenCalledWith({
data: {
name: 'Partner 1',
link: 'https://partner1.com',
createdAt: new Date().toISOString(),
},
});
});
});

0 comments on commit bc100dd

Please sign in to comment.