Skip to content

Commit

Permalink
test: adiciona testes para find all brands controller e usecase
Browse files Browse the repository at this point in the history
  • Loading branch information
AlineLermen committed Jul 8, 2023
1 parent 25185d2 commit 46499ab
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 0 deletions.
73 changes: 73 additions & 0 deletions tests/findAllBrandsController.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { mock } from 'jest-mock-extended'
import { Equipment } from '../src/db/entities/equipment'
import { FindAllBrandUseCase } from '../src/useCases/findBrand/findAllBrandUseCase'
import { FindAllBrandsController } from '../src/presentation/controller/findAllBrandsController'
import { datatype } from 'faker'
import { ok } from '../src/presentation/helpers'
import { EquipmentBrand } from '../src/db/entities/equipment-brand'
import { ServerError } from '../src/presentation/errors'

const useCaseMocked = mock<FindAllBrandUseCase>()
const findAllBrandsController = new FindAllBrandsController(useCaseMocked)

const mockedEquipmentBase = {
id: datatype.string(),
tippingNumber: datatype.string(),
serialNumber: datatype.string(),
type: 'CPU',
status: 'ACTIVE',
model: datatype.string(),
description: datatype.string(),
screenSize: null,
power: null,
screenType: null,
processor: datatype.string(),
storageType: datatype.string(),
storageAmount: datatype.number().toString(),
ram_size: datatype.number().toString(),
createdAt: datatype.datetime(),
updatedAt: datatype.datetime()
} as unknown as Equipment

const mockedBrand = {
id: datatype.string(),
name: 'samsung',
equipment: mockedEquipmentBase
} as unknown as EquipmentBrand

describe('Should test FindAllBrandsController', () => {
it('should find brand with success', async () => {
useCaseMocked.execute.mockResolvedValue({
isSuccess: true,
data: [mockedBrand]
})

const response = await findAllBrandsController.perform()
expect(response).toEqual(ok(response.data))
expect(useCaseMocked.execute).toHaveBeenCalled()
})

it('should not find brand', async () => {
useCaseMocked.execute.mockResolvedValue({
isSuccess: true,
data: []
})

const response = await findAllBrandsController.perform()
expect(response).toEqual(ok([]))
expect(useCaseMocked.execute).toHaveBeenCalled()
})

it('should return server error', async () => {
useCaseMocked.execute.mockResolvedValue({
isSuccess: false,
error: new ServerError()
})

const response = await findAllBrandsController.perform()

expect(response).toHaveProperty('statusCode', 500)
expect(response).toHaveProperty('data')
expect(response.data).toBeInstanceOf(ServerError)
})
})
61 changes: 61 additions & 0 deletions tests/findAllBrandsUseCase.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { NotBrandsFound, FindAllBrandUseCase } from '../src/useCases/findBrand/findAllBrandUseCase';
import { MockProxy, mock } from 'jest-mock-extended'
import { BrandRepository } from '../src/repository/brandRepository'
import { Equipment } from '../src/db/entities/equipment'
import { datatype } from 'faker'

describe('FindOneEquipment', () => {
let brandRepository: MockProxy<BrandRepository>
let findAllBrandUseCase: FindAllBrandUseCase

beforeEach(() => {
brandRepository = mock()
findAllBrandUseCase = new FindAllBrandUseCase(
brandRepository
)
})

const mockedEquipmentBase = {
id: datatype.string(),
tippingNumber: datatype.string(),
serialNumber: datatype.string(),
type: 'CPU',
status: 'ACTIVE',
model: datatype.string(),
description: datatype.string(),
screenSize: null,
power: null,
screenType: null,
processor: datatype.string(),
storageType: datatype.string(),
storageAmount: datatype.number().toString(),
ram_size: datatype.number().toString(),
createdAt: datatype.datetime(),
updatedAt: datatype.datetime()
} as unknown as Equipment

test('should return brands when found', async () => {
const expectedData = [{
id: 'abc',
name: 'samsung',
equipment: [mockedEquipmentBase]
}];
brandRepository.findAll.mockResolvedValue(expectedData);

const result = await findAllBrandUseCase.execute();

expect(brandRepository.findAll).toHaveBeenCalled();
expect(result.isSuccess).toBe(true);
expect(result.data).toEqual(expectedData);
});

test('should return NotBrandsFound when no brand is found', async () => {
brandRepository.findAll.mockResolvedValue(null);

const result = await findAllBrandUseCase.execute();

expect(brandRepository.findAll).toHaveBeenCalled();
expect(result.isSuccess).toBe(false);
expect(result.error).toBeInstanceOf(NotBrandsFound);
});
});

0 comments on commit 46499ab

Please sign in to comment.