Skip to content

Commit

Permalink
Merge pull request #16 from fonteeboa/fix-test
Browse files Browse the repository at this point in the history
sonar: adicionando exportacao para sonar
  • Loading branch information
fonteeboa authored Dec 18, 2023
2 parents ebe85f7 + ab6bdbe commit f37e5d7
Show file tree
Hide file tree
Showing 16 changed files with 337 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"main": "dist/bundle.js",
"types": "dist/types/src/index.d.ts",
"scripts": {
"test": "jest --passWithNoTests --no-cache",
"test": "jest --passWithNoTests --no-cache --coverage",
"lint": "eslint .",
"lint-fix": "eslint . --ext .ts,.tsx --fix",
"build": "rollup -c",
Expand Down
5 changes: 5 additions & 0 deletions sonar-project.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
sonar.projectKey=fonteeboa_FBLibrary
sonar.organization=fonteeboa
sonar.sources=src
sonar.tests=src
sonar.javascript.lcov.reportPaths=coverage/lcov.info
10 changes: 10 additions & 0 deletions src/test/mock/button.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const onClickMock = jest.fn();

export const buttonProps = {
label: 'Click me',
type: 'primary',
size: 'large',
styles: 'custom-style',
onClick: onClickMock,
className: 'custom-class',
};
33 changes: 33 additions & 0 deletions src/test/mock/customTable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Mock das colunas e dados de exemplo
export const columns = [
{
title: 'Nome',
dataIndex: 'name',
key: 'name',
},
{
title: 'Idade',
dataIndex: 'age',
key: 'age',
},
];

export const dataSource = [
{
key: '1',
name: 'Alice',
age: 30,
},
{
key: '2',
name: 'Bob',
age: 25,
},
];

export const bulkAction = [
{
label: 'Excluir Selecionados',
action: jest.fn(), // Mock da ação em lote
},
];
10 changes: 10 additions & 0 deletions src/test/mock/inputField.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const inputFieldProps = {
name: 'email',
type: 'email',
icon: true,
label: 'Email',
onChange: jest.fn(),
styles: { borderColor: 'red' },
required: true,
className: 'custom-class',
};
24 changes: 24 additions & 0 deletions src/test/mock/matchMedia.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

import matchMedia from 'match-media-mock';

// Mock the window.matchMedia function
const originalMatchMedia = window.matchMedia;
const mockMatchMedia = matchMedia.create();

beforeAll(() => {
window.matchMedia = mockMatchMedia;
});

afterAll(() => {
window.matchMedia = originalMatchMedia;
});

export const setMatchMediaMock = () => {
beforeAll(() => {
window.matchMedia = mockMatchMedia;
});

afterAll(() => {
window.matchMedia = originalMatchMedia;
});
};
9 changes: 9 additions & 0 deletions src/test/mock/renderField.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react';

export const renderOptionsApiName = () => {
return (
<option key={0} value={1}>
{'name'}
</option>
)
}
12 changes: 12 additions & 0 deletions src/test/mock/selectComponent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const selectComponentProps = {
name: 'mySelect',
value: 'option2',
label: 'Select an option',
onChange: jest.fn(),
options: [
{ label: 'Option 1', value: 'option1' },
{ label: 'Option 2', value: 'option2' },
{ label: 'Option 3', value: 'option3' },
],
className: 'custom-class',
};
18 changes: 18 additions & 0 deletions src/test/src/bulkActionsDropdown.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import { BulkActionsDropdown } from '../../components';
import '@testing-library/jest-dom';

const makeRender = (actions = []) => {
render(
<BulkActionsDropdown actions={actions} />
);
}

describe('BulkActionsDropdown', () => {
// Teste para verificar se o componente renderiza corretamente
it('renders correctly', () => {
makeRender()
expect(screen.getByRole('button')).toBeInTheDocument();
});
});
14 changes: 14 additions & 0 deletions src/test/src/button.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React from 'react';
import { render, fireEvent } from '@testing-library/react';
import {Button} from '../../components';
import { buttonProps, onClickMock } from '../mock/button';

test('Renderiza o componente Button corretamente', () => {
const { getByText } = render(<Button {...buttonProps} />);
// Verifique se o componente Button foi renderizado com os valores corretos
expect(getByText('Click me')).toBeInTheDocument();

// Simule um clique no botão e verifique se o manipulador de clique é chamado
fireEvent.click(getByText('Click me'));
expect(onClickMock).toHaveBeenCalledTimes(1);
});
23 changes: 23 additions & 0 deletions src/test/src/dynamicTable.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import { Grid } from '../../components';
import '../../i18n';
import { dataSource, columns, bulkAction } from '../mock/customTable';
import { setMatchMediaMock } from '../mock/matchMedia';

setMatchMediaMock();

test('Renderiza o componente CustomTable corretamente', () => {

render(
<Grid
pageTitle="Tabela de Usuários"
dataSource={dataSource}
columns={columns}
bulkAction={bulkAction}
/>
);

// Verifique a renderização do título da tabela
expect(screen.getByText('Tabela de Usuários')).toBeInTheDocument();
});
13 changes: 13 additions & 0 deletions src/test/src/inputField.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React from 'react';
import { render } from '@testing-library/react';
import {InputField} from '../../components';
import { inputFieldProps } from '../mock/inputField';

test('Renderiza o componente InputField corretamente', () => {
const { getByTestId, getByText } = render(<InputField {...inputFieldProps} />);

// Verifique se o componente InputField foi renderizado corretamente
expect(getByText('Email')).toBeInTheDocument();
expect(getByTestId('dataTestId-' + inputFieldProps.name)).toBeInTheDocument();

});
79 changes: 79 additions & 0 deletions src/test/src/renderField.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import React from 'react';
import { render, fireEvent } from '@testing-library/react';
import { renderfields } from '../../utils/renderField'; // Atualize com o caminho correto
import { setMatchMediaMock } from '../mock/matchMedia';

import { renderOptionsApiName } from '../mock/renderField';

setMatchMediaMock();

// Mocks para as funções e propriedades necessárias
const mockSetFieldValues = jest.fn();
const formInstance = { getFieldValue: jest.fn() };


// Exemplo de teste para o campo select
describe('renderfields function', () => {
it('renders input fields correctly', async () => {
const field1 = { label: 'common.name', name: 'Name', rules: [{ required: true, message:"common.required.field" }] }
const field2 = { label: 'common.test', name: 'Test', rules: [{ required: true, message:"common.required.field" }] }
const fields = [field1, field2]
const { getByTestId } = render( <div> {fields.map((field) => renderfields(field, 0, formInstance, mockSetFieldValues))} </div> );
const inputField1 = getByTestId(`dataTestId${field1.name}`);
expect(inputField1).toBeInTheDocument();
const inputField2 = getByTestId(`dataTestId${field2.name}`);
expect(inputField2).toBeInTheDocument();
fireEvent.change(inputField1, { target: { value: "1" } });
expect(inputField1.value).toBe("1");
fireEvent.change(inputField2, { target: { value: "2" } });
expect(inputField2.value).toBe("2");
});

it('renders inputs doublelines fields correctly', async () => {
const fields = [
{ label: 'ID', name: 'ID', type: 'hidden' },
{ name: "doublelines",
doublelines: [
{ label: 'common.name', name: 'ApiID', type: 'select', optionsFunction: renderOptionsApiName },
{ label: 'common.key', name: 'ApiKey' },
]
},
];
const { getByTestId } = render( <div> {fields.map((field) => renderfields(field, 0, formInstance, mockSetFieldValues))} </div> );
const inputField1 = getByTestId(`dataTestIdApiID`);
expect(inputField1).toBeInTheDocument();
const inputField2 = getByTestId(`dataTestIdApiKey`);
expect(inputField2).toBeInTheDocument();
});

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();
});

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();
});

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();
});

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");
});

});
11 changes: 11 additions & 0 deletions src/test/src/selectComponent.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import {Select} from '../../components';
import { selectComponentProps } from '../mock/selectComponent';

test('Renderiza o componente SelectComponent corretamente', () => {
render(<Select {...selectComponentProps} />);

// Verifique se o componente SelectComponent foi renderizado corretamente
expect(screen.getByText('Select an option')).toBeInTheDocument();
});
55 changes: 55 additions & 0 deletions src/test/src/validFields.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { validFields } from './../../helpers/validFields'; // Atualize o caminho conforme necessário
import moment from 'moment';

describe('validFields', () => {
// Teste para 'date'
it('validates date correctly', () => {
const validDate = '2021-01-01';
expect(validFields(validDate, 'date')).toEqual(moment(validDate));

const invalidDate = 'invalid-date';
expect(validFields(invalidDate, 'date')).toBe(false);
});

// Teste para 'string'
it('validates string correctly', () => {
expect(validFields('test string', 'string')).toBe('test string');
expect(validFields(123, 'string')).toBe(false);
});

// Teste para 'number'
it('validates number correctly', () => {
expect(validFields(123, 'number')).toBe(123);
expect(validFields('not a number', 'number')).toBe(false);
});

// Teste para 'boolean'
it('validates boolean correctly', () => {
expect(validFields(true, 'boolean')).toBe(true);
expect(validFields('not a boolean', 'boolean')).toBe(false);
});

// Teste para 'array'
it('validates array correctly', () => {
expect(validFields([1, 2, 3], 'array')).toEqual([1, 2, 3]);
expect(validFields('not an array', 'array')).toBe(false);
});

// Teste para 'object'
it('validates object correctly', () => {
expect(validFields({ key: 'value' }, 'object')).toEqual({ key: 'value' });
expect(validFields('not an object', 'object')).toBe(false);
});

// Teste para 'email'
it('validates email correctly', () => {
expect(validFields('test@example.com', 'email')).toBe('test@example.com');
expect(validFields('invalidemail', 'email')).toBe(false);
});

// Teste para o caso padrão
it('returns the value for unknown types', () => {
const unknownTypeValue = 'unknown type value';
expect(validFields(unknownTypeValue, 'unknown')).toBe(unknownTypeValue);
});
});
20 changes: 20 additions & 0 deletions src/test/src/warningPageScreenSize.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React from 'react';
import { render } from '@testing-library/react';
import { WarningPageScreenSize } from '../../components';

// Mock the useTranslation hook
jest.mock('react-i18next', () => ({
useTranslation: () => ({
t: jest.fn().mockImplementation((key) => key),
}),
}));

describe('WarningPageScreenSize', () => {
it('renders correctly', () => {
const { getByText } = render(<WarningPageScreenSize />);

// Check if the component renders the title and subtitle
expect(getByText('warningPage.title')).toBeInTheDocument();
expect(getByText('warningPage.subTitle')).toBeInTheDocument();
});
});

0 comments on commit f37e5d7

Please sign in to comment.