-
Notifications
You must be signed in to change notification settings - Fork 299
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: cover getMany and create for partners entity (#154)
- Melhora a cobertura de testes para a entidade `Partner`
- Loading branch information
Showing
2 changed files
with
87 additions
and
19 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,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(); | ||
}); | ||
}); |
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,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(), | ||
}, | ||
}); | ||
}); | ||
}); |