Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions src/components/ui/Table/tests/Table.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import React from 'react';
import { render, RenderResult } from '@testing-library/react';

import Table from '../Table';

describe('Table Component', () => {
const employeeData = [
{ id: 1, fullName: 'John Smith', age: 23, isIntern: 'No' },
{ id: 2, fullName: 'Anna Donie', age: 35, isIntern: 'Yes' },
{ id: 3, fullName: 'Hannah Brown', age: 20, isIntern: 'Yes' },
{ id: 4, fullName: 'Johnathan White Jr', age: 36, isIntern: 'No' },
];

const employeeKey = [
{ key: 'id', name: 'Employee Id' },
{ key: 'fullName', name: 'Full Name' },
{ key: 'age', name: 'Age' },
{ key: 'isIntern', name: 'In Internship' },
];
Comment on lines +14 to +19
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix trailing comma style issues

The trailing commas in the arrays are causing linting errors.

Apply this fix:

    const employeeKey = [
        { key: 'id', name: 'Employee Id' },
        { key: 'fullName', name: 'Full Name' },
        { key: 'age', name: 'Age' },
-       { key: 'isIntern', name: 'In Internship' },
+       { key: 'isIntern', name: 'In Internship' }
    ];
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const employeeKey = [
{ key: 'id', name: 'Employee Id' },
{ key: 'fullName', name: 'Full Name' },
{ key: 'age', name: 'Age' },
{ key: 'isIntern', name: 'In Internship' },
];
const employeeKey = [
{ key: 'id', name: 'Employee Id' },
{ key: 'fullName', name: 'Full Name' },
{ key: 'age', name: 'Age' },
{ key: 'isIntern', name: 'In Internship' }
];
🧰 Tools
🪛 eslint

[error] 18-18: Unexpected trailing comma.

(comma-dangle)


let result: RenderResult;
let container: HTMLElement;

beforeEach(() => {
result = render(
<Table columns={employeeKey} data={employeeData}></Table>
);
container = result.container;
});
Comment on lines +24 to +29
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add test cleanup

While the test setup is good, it's missing cleanup after each test to prevent potential test interference.

Add this after the beforeEach block:

afterEach(() => {
    result.unmount();
});


it('renders without crashing', () => {
const tableElements = container.querySelectorAll('table');
expect(tableElements.length).toEqual(1);
expect(tableElements[0]).toBeTruthy();
});

it('renders correct user defined headers', () => {
const headerRows = container.querySelectorAll('table thead tr');
expect(headerRows.length).toEqual(1);

const headers = headerRows[0].querySelectorAll('th');
expect(headers.length).toEqual(4);

headers.forEach((header, index) => {
expect(header.textContent).toEqual(employeeKey[index].name);
});
});

it('renders correct user defined data', () => {
const tbodyElements = container.querySelectorAll('tbody');
expect(tbodyElements.length).toEqual(1);

const dataRows = tbodyElements[0].querySelectorAll('tr');
expect(dataRows.length).toEqual(4);

dataRows.forEach((row, rowIndex) => {
const dataCells = row.querySelectorAll('td');
expect(dataCells.length).toEqual(4);

dataCells.forEach((cell, cellIndex) => {
expect(cell.textContent).toEqual(
Object.values(employeeData[rowIndex])[cellIndex].toString()
);
});
});
});
});
Loading