Skip to content

Commit

Permalink
Merge pull request #64 from fga-eps-mds/163-aumentar-cobertura-de-testes
Browse files Browse the repository at this point in the history
163 - Aumentando cobertura de testes
  • Loading branch information
JPedroCh authored Jul 8, 2023
2 parents 82ae147 + e9ab384 commit 5b46538
Show file tree
Hide file tree
Showing 11 changed files with 507 additions and 72 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { screen, render, fireEvent, act } from '@testing-library/react';
import { vi } from 'vitest';
import { DeleteExtensiveButton } from '.';

describe('DeleteExtensiveButton', () => {
it('has the correct aria-label', () => {
render(<DeleteExtensiveButton label="usuário" onClick={() => {}} />);
expect(screen.getByText('Excluir usuário')).toBeInTheDocument();
});

it('should be able to call DeleteExtensiveButton onClick function', async () => {
const onClickMock = vi.fn();
render(<DeleteExtensiveButton label="usuário" onClick={onClickMock} />);

const button = screen.getAllByText('Excluir').pop() as HTMLElement;
await act(() => fireEvent.click(button));

expect(onClickMock).toHaveBeenCalled();
});
});
102 changes: 102 additions & 0 deletions src/components/equipment-edit-form/equipment-edit-form.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { screen, render } from '@testing-library/react';
import { vi } from 'vitest';
import EquipmentEditForm from '.';

const EQUIPMENT_MOCK = {
tippingNumber: 'tippingNumber',
serialNumber: 'serialNumber',
type: 'type',
situacao: 'situacao',
model: 'model',
description: 'description',
acquisitionDate: new Date(2000, 1, 1),
screenSize: 'screenSize',
power: 'power',
screenType: 'screenType',
processor: 'processor',
storageType: 'storageType',
storageAmount: 'storageAmount',
brandName: 'brandName',
acquisition: { name: 'name' },
unitId: 'unitId',
ram_size: 'ram_size',
estado: 'estado',
};

describe('EquipmentEditForm', () => {
it('should render correctly', () => {
render(
<EquipmentEditForm
onClose={vi.fn()}
equip={EQUIPMENT_MOCK}
refreshRequest={false}
setRefreshRequest={vi.fn()}
/>
);

expect(screen.getByText('Cancelar')).toBeInTheDocument();
expect(screen.getByText('Editar')).toBeInTheDocument();
});

it('should render cpu fields', () => {
const equipmentMock = { ...EQUIPMENT_MOCK, type: 'CPU' };
render(
<EquipmentEditForm
onClose={vi.fn()}
equip={equipmentMock}
refreshRequest={false}
setRefreshRequest={vi.fn()}
/>
);

expect(screen.getByLabelText('Qtd. Memória RAM (GB)')).toBeInTheDocument();
expect(screen.getByLabelText('Tipo de armazenamento')).toBeInTheDocument();
expect(
screen.getByLabelText('Qtd. Armazenamento (GB)')
).toBeInTheDocument();
expect(screen.getByLabelText('Processador')).toBeInTheDocument();
});

it('should render monitor fields', () => {
const equipmentMock = { ...EQUIPMENT_MOCK, type: 'Monitor' };
render(
<EquipmentEditForm
onClose={vi.fn()}
equip={equipmentMock}
refreshRequest={false}
setRefreshRequest={vi.fn()}
/>
);

expect(screen.getByLabelText('Tipo de monitor')).toBeInTheDocument();
expect(screen.getByLabelText('Tamanho do Monitor')).toBeInTheDocument();
});

it('should render estabilizador fields', () => {
const equipmentMock = { ...EQUIPMENT_MOCK, type: 'Estabilizador' };
render(
<EquipmentEditForm
onClose={vi.fn()}
equip={equipmentMock}
refreshRequest={false}
setRefreshRequest={vi.fn()}
/>
);

expect(screen.getByLabelText('Potência (VA)')).toBeInTheDocument();
});

it('should render Nobreak fields', () => {
const equipmentMock = { ...EQUIPMENT_MOCK, type: 'Nobreak' };
render(
<EquipmentEditForm
onClose={vi.fn()}
equip={equipmentMock}
refreshRequest={false}
setRefreshRequest={vi.fn()}
/>
);

expect(screen.getByLabelText('Potência (VA)')).toBeInTheDocument();
});
});
11 changes: 0 additions & 11 deletions src/components/equipment-edit-form/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,6 @@ export default function EquipmentEditForm({
resetField('type');
}, []);

const listOfYears: Array<{ value: number; label: string }> = (() => {
const endYear: number = new Date().getFullYear();
const startYear: number = endYear - 30;

return Array.from({ length: endYear - startYear + 1 }, (_, index) => {
const year = startYear + index;
return { value: year, label: year.toString() };
}).reverse();
})();

function formatDate(date: Date) {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
Expand All @@ -107,7 +97,6 @@ export default function EquipmentEditForm({
...rest
} = formData;

const options = { year: 'numeric', month: '2-digit', day: '2-digit' };
const dateString = formatDate(acquisitionDate);

const payload = {
Expand Down
86 changes: 86 additions & 0 deletions src/components/equipment-form/equipment-form.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { screen, render, act, fireEvent } from '@testing-library/react';
import { vi } from 'vitest';
import EquipmentForm from '.';

describe('EquipmentForm', () => {
it('should render correctly', () => {
render(
<EquipmentForm
onClose={vi.fn()}
refreshRequest={false}
setRefreshRequest={vi.fn()}
/>
);

expect(screen.getByText('Cancelar')).toBeInTheDocument();
expect(screen.getByText('Confirmar')).toBeInTheDocument();
});

it('should render cpu fields', async () => {
render(
<EquipmentForm
onClose={vi.fn()}
refreshRequest={false}
setRefreshRequest={vi.fn()}
/>
);

const select = screen.getByLabelText('Tipo de equipamento');
await act(() => fireEvent.change(select, { target: { value: 'CPU' } }));

expect(screen.getByLabelText('Qtd. Memória RAM (GB)')).toBeInTheDocument();
expect(screen.getByLabelText('Tipo de armazenamento')).toBeInTheDocument();
expect(
screen.getByLabelText('Qtd. Armazenamento (GB)')
).toBeInTheDocument();
expect(screen.getByLabelText('Processador')).toBeInTheDocument();
});

it('should render monitor fields', async () => {
render(
<EquipmentForm
onClose={vi.fn()}
refreshRequest={false}
setRefreshRequest={vi.fn()}
/>
);

const select = screen.getByLabelText('Tipo de equipamento');
await act(() => fireEvent.change(select, { target: { value: 'Monitor' } }));

expect(screen.getByLabelText('Tipo de monitor')).toBeInTheDocument();
expect(screen.getByLabelText('Tamanho do Monitor')).toBeInTheDocument();
});

it('should render estabilizador fields', async () => {
render(
<EquipmentForm
onClose={vi.fn()}
refreshRequest={false}
setRefreshRequest={vi.fn()}
/>
);

const select = screen.getByLabelText('Tipo de equipamento');
await act(() =>
fireEvent.change(select, { target: { value: 'Estabilizador' } })
);

expect(screen.getByLabelText('Potência (VA)')).toBeInTheDocument();
});

it('should render Nobreak fields', async () => {
render(
<EquipmentForm
onClose={vi.fn()}
refreshRequest={false}
setRefreshRequest={vi.fn()}
/>
);

const select = screen.getByLabelText('Tipo de equipamento');
await act(() => fireEvent.change(select, { target: { value: 'Nobreak' } }));

expect(screen.getByLabelText('Potência (VA)')).toBeInTheDocument();
});
});
10 changes: 0 additions & 10 deletions src/components/equipment-form/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,6 @@ export default function EquipmentForm({
watchStorageAmount,
]);

const listOfYears: Array<{ value: number; label: string }> = (() => {
const endYear: number = new Date().getFullYear();
const startYear: number = endYear - 30;

return Array.from({ length: endYear - startYear + 1 }, (_, index) => {
const year = startYear + index;
return { value: year, label: year.toString() };
}).reverse();
})();

const onSubmit = handleSubmit(async (formData) => {
try {
const { type, estado, storageType, screenType, ...rest } = formData;
Expand Down
Loading

0 comments on commit 5b46538

Please sign in to comment.