From aa677822fc49793471f94f9d62de840d438bd9e7 Mon Sep 17 00:00:00 2001 From: Siarhei Karol Date: Fri, 8 Nov 2024 12:50:19 +0500 Subject: [PATCH] unit tests --- .../common/helpers/validations.helper.test.ts | 24 ++++++++--------- .../__tests__/components/TableFlex.test.tsx | 26 +++++++++++++++---- 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/src/test/__tests__/common/helpers/validations.helper.test.ts b/src/test/__tests__/common/helpers/validations.helper.test.ts index 6248394e..c2a06b6e 100644 --- a/src/test/__tests__/common/helpers/validations.helper.test.ts +++ b/src/test/__tests__/common/helpers/validations.helper.test.ts @@ -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', () => { @@ -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(); }); }); }); diff --git a/src/test/__tests__/components/TableFlex.test.tsx b/src/test/__tests__/components/TableFlex.test.tsx index 9f806c67..dc3734a1 100644 --- a/src/test/__tests__/components/TableFlex.test.tsx +++ b/src/test/__tests__/components/TableFlex.test.tsx @@ -16,9 +16,11 @@ describe('TableFlex Component', () => { const onRowClick = jest.fn(); const onHeaderCellClick = jest.fn(); - test('renders TableFlex component', () => { + beforeEach(() => { render(); + }); + test('renders TableFlex component', () => { expect(screen.getByTestId('table')).toBeInTheDocument(); expect(screen.getByTestId('th-name')).toHaveTextContent('Name'); expect(screen.getByTestId('th-age')).toHaveTextContent('Age'); @@ -26,18 +28,32 @@ describe('TableFlex Component', () => { }); test('calls onHeaderCellClick when header cell is clicked', () => { - render(); - fireEvent.click(screen.getByTestId('th-name')); expect(onHeaderCellClick).toHaveBeenCalledWith({ name: header.name }); }); test('calls onRowClick when row is clicked', () => { - render(); - 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]); + }); });