Skip to content

Commit

Permalink
Merge pull request #40 from fga-eps-mds/aumentar-cobertura
Browse files Browse the repository at this point in the history
Aumentar cobertura
  • Loading branch information
JPedroCh authored Jul 8, 2023
2 parents 9f5cc21 + 46499ab commit 46ca6fb
Show file tree
Hide file tree
Showing 9 changed files with 463 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/useCases/find-order-service/find-order-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class FindOrderService
const ordersServices = await this.osReposiory.findOrderServiceGeneric(
newQuery
)
if (ordersServices !== null) {
if (ordersServices !== undefined) {
return {
isSuccess: true,
data: ordersServices
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ListOneEquipmentRepository } from './../../repository/equipment/list-on
import { Equipment } from './../../domain/entities/equipment'
import { UseCase, UseCaseReponse } from '../protocol/useCase'

class EquipmentNotFoundError extends Error {
export class EquipmentNotFoundError extends Error {
constructor() {
super('Equipment not found')
this.name = 'EquipmentNotFoundError'
Expand Down
52 changes: 52 additions & 0 deletions tests/find-one-equipment-controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { mock } from 'jest-mock-extended'
import { Equipment } from '../src/db/entities/equipment'
import { FindOneEquipmentUseCase } from '../src/useCases/findOneEquipment/find-one-equipment-usecase'
import { FindOneEquipmentController } from '../src/presentation/controller/find-one-equipment-controller'
import { datatype } from 'faker'
import { ok, serverError } from '../src/presentation/helpers'

const useCaseMocked = mock<FindOneEquipmentUseCase>()
const findOneEquipmentController = new FindOneEquipmentController(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

describe('Should test FindOneEquipmentController', () => {
it('should find equipment with success', async () => {
useCaseMocked.execute.mockResolvedValue({
isSuccess: true,
data: mockedEquipmentBase
})
const query = { equipmentId: mockedEquipmentBase.id, tippingNumber: mockedEquipmentBase.tippingNumber }
const response = await findOneEquipmentController.perform(query)
expect(response).toEqual(ok(response.data))
expect(useCaseMocked.execute).toHaveBeenCalled()
})

it('should not find equipment', async () => {
useCaseMocked.execute.mockResolvedValue({
isSuccess: true,
data: undefined
})
const query = { equipmentId: datatype.string(), tippingNumber: datatype.string() }
const response = await findOneEquipmentController.perform(query)
expect(response).toEqual(serverError())
expect(useCaseMocked.execute).toHaveBeenCalled()
})
})
52 changes: 52 additions & 0 deletions tests/find-one-equipment-usecase.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { EquipmentNotFoundError, FindOneEquipmentUseCase } from '../src/useCases/findOneEquipment/find-one-equipment-usecase';
import { MockProxy, mock } from 'jest-mock-extended'
import { Estado } from '../src/domain/entities/equipamentEnum/estado'
import { Status } from '../src/domain/entities/equipamentEnum/status'
import { Type } from '../src/domain/entities/equipamentEnum/type'
import { ListOneEquipmentRepository } from '../src/repository/equipment/list-one-equipment'

describe('FindOneEquipment', () => {
let listOneEquipmentRepository: MockProxy<ListOneEquipmentRepository>
let findOneEquipmentUseCase: FindOneEquipmentUseCase

beforeEach(() => {
listOneEquipmentRepository = mock()
findOneEquipmentUseCase = new FindOneEquipmentUseCase(
listOneEquipmentRepository
)
})

test('should return equipment when found', async () => {
const expectedData = {
id: 'id',
acquisitionDate: new Date(),
createdAt: new Date(),
updatedAt: new Date(),
situacao: Status.ACTIVE,
estado: Estado.Novo,
tippingNumber: '12345',
model: 'DELL G15',
serialNumber: 'any',
type: Type.CPU
};
listOneEquipmentRepository.findOne.mockResolvedValue(expectedData);

const query = { tippingNumber: '12345', id: 'id' };
const result = await findOneEquipmentUseCase.execute(query);

expect(listOneEquipmentRepository.findOne).toHaveBeenCalledWith(query);
expect(result.isSuccess).toBe(true);
expect(result.data).toEqual(expectedData);
});

test('should return EquipmentNotFoundError when no equipment is found', async () => {
listOneEquipmentRepository.findOne.mockResolvedValue(null);

const query = { tippingNumber: '543' };
const result = await findOneEquipmentUseCase.execute(query);

expect(listOneEquipmentRepository.findOne).toHaveBeenCalledWith(query);
expect(result.isSuccess).toBe(false);
expect(result.error).toBeInstanceOf(EquipmentNotFoundError);
});
});
89 changes: 89 additions & 0 deletions tests/find-order-service-usecase.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { NotOSFoundError, FindOrderService } from '../src/useCases/find-order-service/find-order-service';
import { MockProxy, mock } from 'jest-mock-extended'
import { Equipment } from '../src/domain/entities/equipment'
import { Estado } from '../src/domain/entities/equipamentEnum/estado'
import { Status } from '../src/domain/entities/equipamentEnum/status'
import { Status as OSStatus } from '../src/domain/entities/serviceOrderEnum/status'
import { Type } from '../src/domain/entities/equipamentEnum/type'
import { OrderServiceRepositoryProtocol } from '../src/repository/protocol/orderServiceRepositoryProtocol'

describe('FindOrderService', () => {
let orderServiceRepositoryProtocol: MockProxy<OrderServiceRepositoryProtocol>
let findOrderServiceUseCase: FindOrderService

const equipment: Equipment = {
id: 'id',
acquisitionDate: new Date(),
createdAt: new Date(),
updatedAt: new Date(),
situacao: Status.ACTIVE,
estado: Estado.Novo,
tippingNumber: 'any',
model: 'DELL G15',
serialNumber: 'any',
type: Type.CPU
}

beforeEach(() => {
orderServiceRepositoryProtocol = mock()
findOrderServiceUseCase = new FindOrderService(
orderServiceRepositoryProtocol
)
})

test('should return order services when they are found', async () => {
const expectedData = [{
id: 2,
equipment,
description: 'any_description',
seiProcess: '123456789',
senderPhone: '61992809831',
senderDocument: '12345678910',
technicianId: '123456',
technicianName: 'Pessoa',
createdAt: new Date(),
updatedAt: new Date(),
status: OSStatus.MAINTENANCE,
authorId: '123456789',
senderName: 'Pessoa 2'
}];
orderServiceRepositoryProtocol.findOrderServiceGeneric.mockResolvedValue(expectedData);

const query = { type: 'type1', unit: 'unit1' };
const result = await findOrderServiceUseCase.execute(query);

expect(orderServiceRepositoryProtocol.findOrderServiceGeneric).toHaveBeenCalledWith(query);
expect(result.isSuccess).toBe(true);
expect(result.data).toEqual(expectedData);
});

test('should return NotOSFoundError when no order services are found', async () => {
orderServiceRepositoryProtocol.findOrderServiceGeneric.mockResolvedValue(undefined);

const query = { model: 'abc' };
const result = await findOrderServiceUseCase.execute(query);

expect(orderServiceRepositoryProtocol.findOrderServiceGeneric).toHaveBeenCalledWith(query);
expect(result.isSuccess).toBe(false);
expect(result.error).toBeInstanceOf(NotOSFoundError);
});

test('should set default values for "take" and "skip" if not provided in query', async () => {
orderServiceRepositoryProtocol.findOrderServiceGeneric.mockResolvedValue([]);

const query = { model: 'abc' };
await findOrderServiceUseCase.execute(query);

expect(orderServiceRepositoryProtocol.findOrderServiceGeneric).toHaveBeenCalledWith({
type: undefined,
unit: undefined,
date: undefined,
brand: undefined,
search: undefined,
model: 'abc',
status: undefined,
take: 0,
skip: 0
});
});
});
73 changes: 73 additions & 0 deletions tests/findAllAcquisitionController.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 { FindAllAcquisitionUseCase } from '../src/useCases/findAcquisition/findAllAcquisitionUseCase'
import { FindAllAcquisitionsController } from '../src/presentation/controller/findAllAcquisitionsController'
import { datatype } from 'faker'
import { ok } from '../src/presentation/helpers'
import { EquipmentAcquisition } from '../src/db/entities/equipment-acquisition'
import { ServerError } from '../src/presentation/errors'

const useCaseMocked = mock<FindAllAcquisitionUseCase>()
const findAllAcquisitionsController = new FindAllAcquisitionsController(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 mockedAcquisition = {
id: datatype.string(),
name: 'compra',
equipment: mockedEquipmentBase
} as unknown as EquipmentAcquisition

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

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

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

const response = await findAllAcquisitionsController.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 findAllAcquisitionsController.perform()

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

describe('FindOneEquipment', () => {
let acquisitionRepository: MockProxy<AcquisitionRepository>
let findAllAcquisitionUseCase: FindAllAcquisitionUseCase

beforeEach(() => {
acquisitionRepository = mock()
findAllAcquisitionUseCase = new FindAllAcquisitionUseCase(
acquisitionRepository
)
})

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 acquisitions when found', async () => {
const expectedData = [{
id: 'abc',
name: 'compra',
equipment: [mockedEquipmentBase]
}];
acquisitionRepository.findAll.mockResolvedValue(expectedData);

const result = await findAllAcquisitionUseCase.execute();

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

test('should return NotAcquisitionsFound when no acquisition is found', async () => {
acquisitionRepository.findAll.mockResolvedValue(null);

const result = await findAllAcquisitionUseCase.execute();

expect(acquisitionRepository.findAll).toHaveBeenCalled();
expect(result.isSuccess).toBe(false);
expect(result.error).toBeInstanceOf(NotAcquisitionsFound);
});
});
Loading

0 comments on commit 46ca6fb

Please sign in to comment.