Skip to content

Commit

Permalink
reduzindo duplicidade
Browse files Browse the repository at this point in the history
  • Loading branch information
fonteeboa committed Dec 18, 2023
1 parent 0ecb937 commit 391611a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 21 deletions.
15 changes: 13 additions & 2 deletions src/infra/requests/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
import { ServiceParams } from "./domain";

/**
* Sets the authentication token in the given headers object.
*
* @param {Headers} headers - The headers object to set the token in.
* @return {Promise<Headers>} The updated headers object with the token set.
*/
const setTokenAuth = async (headers: Headers): Promise<Headers> => {
const authToken = await sessionStorage.getItem('authToken');
if (authToken) {
headers.set('Authorization', 'Bearer ' + authToken);
}
return headers;
}

/**
* Makes a fetch request to the specified URL using the provided parameters.
*
* @param {ServiceParams} params - The parameters for the fetch request.
* @param {string} method - The HTTP method for the request.
* @return {Promise<T | boolean>} - A promise that resolves to the response data or false if there was an error.
*/
export async function makeFetchRequest<T>(params: ServiceParams, method: string): Promise<T | boolean> {
const { baseUrl, route = '', body, authToken, headers: customHeaders } = params;
const url = method === 'GET' && body ? new URL(baseUrl + route, body) : baseUrl + route;
Expand Down
28 changes: 13 additions & 15 deletions src/test/src/renderField.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { render, fireEvent } from '@testing-library/react';
import { render, fireEvent, screen } from '@testing-library/react';
import { renderfields } from '../../utils/renderField'; // Atualize com o caminho correto
import { setMatchMediaMock } from '../mock/matchMedia';

Expand All @@ -11,6 +11,12 @@ setMatchMediaMock();
const mockSetFieldValues = jest.fn();
const formInstance = { getFieldValue: jest.fn() };

const validfields = (fieldData, testId) => {
const { getByTestId } = render( <div> {renderfields(fieldData, 0, formInstance, mockSetFieldValues)} </div> );
const field = getByTestId(testId);
expect(field).toBeInTheDocument();
}

// Exemplo de teste para o campo select
describe('renderfields function', () => {
it('renders input fields correctly', async () => {
Expand Down Expand Up @@ -47,32 +53,24 @@ describe('renderfields function', () => {

it('renders select fields correctly', async () => {
const field = { label: 'common.select', type: 'select', name: 'select', rules: [{ required: true, message:"common.required.field" }] }
const { getByTestId } = render( <div> {renderfields(field, 0, formInstance, mockSetFieldValues)} </div> );
const selectField = getByTestId(`dataTestId${field.name}`);
expect(selectField).toBeInTheDocument();
validfields(field, `dataTestId${field.name}`);
});

it('renders datePicker fields correctly', async () => {
const field = { label: 'common.datePicker', type: 'datePicker', name: 'datePicker', rules: [{ required: true, message:"common.required.field" }] }
const { getByTestId } = render( <div> {renderfields(field, 0, formInstance, mockSetFieldValues)} </div> );
const selectField = getByTestId(`dataTestId-${field.name}`);
expect(selectField).toBeInTheDocument();
validfields(field, `dataTestId-${field.name}`);
});

it('renders slider fields correctly', async () => {
const field = { label: 'common.slider', type: 'slider', name: 'slider', rules: [{ required: true, message:"common.required.field" }] }
const { getByTestId } = render( <div> {renderfields(field, 0, formInstance, mockSetFieldValues)} </div> );
const sliderField = getByTestId(`dataTestId${field.name}`);
expect(sliderField).toBeInTheDocument();
validfields(field, `dataTestId${field.name}`);
});

it('renders number fields correctly', async () => {
const field = { label: 'common.number', type: 'number', name: 'number', required: true }
const { getByTestId } = render( <div> {renderfields(field, 0, formInstance, mockSetFieldValues)} </div> );
const selectField = getByTestId(`dataTestId${field.name}`);
expect(selectField).toBeInTheDocument();
fireEvent.change(selectField, { target: { value: "11" } });
expect(selectField.value).toBe("11");
validfields(field, `dataTestId${field.name}`);
fireEvent.change(screen.getByTestId(`dataTestId${field.name}`), { target: { value: "11" } });
expect(screen.getByTestId(`dataTestId${field.name}`).value).toBe("11");
});

});
23 changes: 19 additions & 4 deletions src/utils/renderField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@ import { Field } from '../components/baseLayouts/modals/types';
import { Form, Row, Col,DatePicker, Switch, Input } from 'antd';
import dayjs from 'dayjs';
import { customStyles } from '../components/baseLayouts/modals/constants/constants';

/**
* Renders a field based on its type.
*
* @param {Field} field - The field object to render.
* @param {any} styleField - Optional styling for the field.
* @param {number} index - The index of the field.
* @param {FormInstance} form - The form instance.
* @param {(value: any, fieldName: string) => void} setFieldValues - Function to set field values.
* @return {*} The rendered field component.
*/
const renderField = (
field: Field,
styleField: any = {},
Expand Down Expand Up @@ -78,13 +87,19 @@ const renderField = (
)
}
};


/**
* Renders the fields based on the provided parameters.
*
* @param {Field} field - The field object to render.
* @param {number} index - The index of the field.
* @param {FormInstance} form - The instance of the form.
* @param {(value: any, fieldName: string) => void} setFieldValues - The function to set field values.
* @return {JSX.Element} The rendered fields.
*/
export const renderfields = (field: Field, index: number, form: FormInstance, setFieldValues: (value: any, fieldName: string) => void) => {
if (field.required && !field.rules) {
field.rules = ['required'];
}

if (field.doublelines) {
return (
<Row gutter={16}>
Expand Down

0 comments on commit 391611a

Please sign in to comment.