forked from fga-eps-mds/2022-2-Alectrion-EquipamentApi
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: adiciona testes para find all brands controller e usecase
- Loading branch information
1 parent
25185d2
commit 46499ab
Showing
2 changed files
with
134 additions
and
0 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 |
---|---|---|
@@ -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) | ||
}) | ||
}) |
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 |
---|---|---|
@@ -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); | ||
}); | ||
}); |