Skip to content

Commit

Permalink
unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SKarolFolio committed Nov 8, 2024
1 parent e03d542 commit aa67782
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
24 changes: 12 additions & 12 deletions src/test/__tests__/common/helpers/validations.helper.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { normalizeLccn, validateLccn } from '@common/helpers/validations.helper';
import { normalizeLccn, validateNormalizedLccn, validateNonNormalizedLccn } from '@common/helpers/validations.helper';

describe('validations.helper', () => {
describe('normalizeLccn', () => {
Expand All @@ -24,23 +24,23 @@ describe('validations.helper', () => {

describe('validateLccn', () => {
test('returns true for valid normalized lccn', () => {
expect(validateLccn('1234567890')).toBeTruthy();
expect(validateNormalizedLccn('1234567890')).toBeTruthy();
});

test('returns true for valid non-normalized lccn', () => {
expect(validateLccn(' 2017000002', false)).toBeTruthy();
expect(validateLccn('1234-1///aarrRRr', false)).toBeTruthy();
expect(validateNonNormalizedLccn(' 2017000002')).toBeTruthy();
expect(validateNonNormalizedLccn('1234-1///aarrRRr')).toBeTruthy();
});

test('returns false for invalid non-normalized lccn', () => {
expect(validateLccn('20161234567', false)).toBeFalsy();
expect(validateLccn('20121234', false)).toBeFalsy();
expect(validateLccn('20/121234', false)).toBeFalsy();
expect(validateLccn(' 20a17000002', false)).toBeFalsy();
expect(validateLccn('12R34-1r///aarrRRr', false)).toBeFalsy();
expect(validateLccn('aa12345-6-6', false)).toBeFalsy();
expect(validateLccn('1234567-123', false)).toBeFalsy();
expect(validateLccn('12 345-6 7 123', false)).toBeFalsy();
expect(validateNonNormalizedLccn('20161234567')).toBeFalsy();
expect(validateNonNormalizedLccn('20121234')).toBeFalsy();
expect(validateNonNormalizedLccn('20/121234')).toBeFalsy();
expect(validateNonNormalizedLccn(' 20a17000002')).toBeFalsy();
expect(validateNonNormalizedLccn('12R34-1r///aarrRRr')).toBeFalsy();
expect(validateNonNormalizedLccn('aa12345-6-6')).toBeFalsy();
expect(validateNonNormalizedLccn('1234567-123')).toBeFalsy();
expect(validateNonNormalizedLccn('12 345-6 7 123')).toBeFalsy();
});
});
});
26 changes: 21 additions & 5 deletions src/test/__tests__/components/TableFlex.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,44 @@ describe('TableFlex Component', () => {
const onRowClick = jest.fn();
const onHeaderCellClick = jest.fn();

test('renders TableFlex component', () => {
beforeEach(() => {
render(<TableFlex header={header} data={data} onRowClick={onRowClick} onHeaderCellClick={onHeaderCellClick} />);
});

test('renders TableFlex component', () => {
expect(screen.getByTestId('table')).toBeInTheDocument();
expect(screen.getByTestId('th-name')).toHaveTextContent('Name');
expect(screen.getByTestId('th-age')).toHaveTextContent('Age');
expect(screen.getAllByTestId('table-row')).toHaveLength(2);
});

test('calls onHeaderCellClick when header cell is clicked', () => {
render(<TableFlex header={header} data={data} onRowClick={onRowClick} onHeaderCellClick={onHeaderCellClick} />);

fireEvent.click(screen.getByTestId('th-name'));

expect(onHeaderCellClick).toHaveBeenCalledWith({ name: header.name });
});

test('calls onRowClick when row is clicked', () => {
render(<TableFlex header={header} data={data} onRowClick={onRowClick} onHeaderCellClick={onHeaderCellClick} />);

fireEvent.click(screen.getAllByTestId('table-row')[0]);

expect(onRowClick).toHaveBeenCalledWith(data[0]);
});

test('calls onHeaderCellClick when Enter key is pressed on header cell', () => {
const headerCell = screen.getByTestId('th-name');
headerCell.focus();

fireEvent.keyDown(headerCell, { key: 'Enter', code: 'Enter', charCode: 13 });

expect(onHeaderCellClick).toHaveBeenCalledWith({ name: header.name });
});

test('calls onRowClick when Enter key is pressed on a table row', () => {
const tableRow = screen.getAllByTestId('table-row')[0];
tableRow.focus();

fireEvent.keyDown(tableRow, { key: 'Enter', code: 'Enter', charCode: 13 });

expect(onRowClick).toHaveBeenCalledWith(data[0]);
});
});

0 comments on commit aa67782

Please sign in to comment.