Skip to content

Commit 9281bc7

Browse files
Feat/unit tests (#17)
* feat: Unit test setup * test: Add unit tests for TableRow, Pagination, and TableHeader components - Introduced unit tests for TableRow component, covering rendering, expansion, and custom rendering. - Added tests for Pagination component, validating navigation and page size changes. - Implemented tests for TableHeader component, ensuring correct rendering and filtering functionality. - Included necessary imports and mock data for testing. * feat: Add unit tests for SortType and defaultTheme - Introduced unit tests for the SortType enum, verifying its values and count. - Added tests for the defaultTheme structure, ensuring it contains the correct properties and valid configuration values for colors, table, pagination, and expand icon. - Updated vitest configuration to include test files with .ts and .tsx extensions. - Added @vitest/coverage-v8 dependency for improved test coverage reporting. * test: Add unit tests for MultiLevelTable component - Introduced comprehensive unit tests for the MultiLevelTable component, covering rendering, row expansion, sorting, pagination, custom theming, and filtering functionalities. - Validated the correct application of custom styles and rendering for status cells and pagination controls. - Ensured that the component behaves as expected with mock data and various configurations. * chore: Update test configuration and add coverage reporting - Added coverage reporting configuration to vitest for improved test insights. - Updated .gitignore to exclude coverage directory. - Enhanced App component with a data-testid for easier testing. - Introduced unit tests for the App component, validating rendering, theme toggling, and status cell colors. - Added tests for index exports to ensure correct component and type exports. - Created main test to verify App component rendering in strict mode. --------- Co-authored-by: anitta-keyvalue <[email protected]>
1 parent c296bd2 commit 9281bc7

File tree

7 files changed

+106
-11
lines changed

7 files changed

+106
-11
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,5 @@ dist-ssr
2222
*.njsproj
2323
*.sln
2424
*.sw?
25+
26+
coverage

src/App.tsx

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -416,18 +416,8 @@ const App: React.FC = () => {
416416
},
417417
];
418418

419-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
420-
const renderCustomPagination = () => {
421-
return <div>Custom Pagination </div>;
422-
};
423-
424-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
425-
const renderCustomExpandButton = () => {
426-
return <div></div>;
427-
};
428-
429419
return (
430-
<div className="app" style={{ backgroundColor: theme.colors?.background }}>
420+
<div className="app" data-testid="app-container" style={{ backgroundColor: theme.colors?.background }}>
431421
<header
432422
className="app-header"
433423
style={{ backgroundColor: theme.table?.header?.background }}

tests/App.test.tsx

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import React from 'react';
2+
import { render, screen, fireEvent } from '@testing-library/react';
3+
import '@testing-library/jest-dom';
4+
import App from '../src/App';
5+
6+
describe('App Component', () => {
7+
it('renders without crashing', () => {
8+
render(<App />);
9+
expect(screen.getByRole('button', { name: /dark mode/i })).toBeInTheDocument();
10+
});
11+
12+
it('renders the MultiLevelTable component', () => {
13+
render(<App />);
14+
// Check for table headers
15+
expect(screen.getByText('Name')).toBeInTheDocument();
16+
expect(screen.getByText('Value')).toBeInTheDocument();
17+
expect(screen.getByText('Status')).toBeInTheDocument();
18+
});
19+
20+
it('toggles theme when theme button is clicked', () => {
21+
render(<App />);
22+
const themeButton = screen.getByRole('button', { name: /dark mode/i });
23+
const appDiv = screen.getByTestId('app-container');
24+
25+
// Initial state should be light theme
26+
expect(appDiv).toHaveStyle({ backgroundColor: '#ffffff' });
27+
28+
// Click to toggle to dark theme
29+
fireEvent.click(themeButton);
30+
expect(appDiv).toHaveStyle({ backgroundColor: '#212529' });
31+
32+
// Click again to toggle back to light theme
33+
fireEvent.click(themeButton);
34+
expect(appDiv).toHaveStyle({ backgroundColor: '#ffffff' });
35+
});
36+
37+
it('renders parent items correctly', () => {
38+
render(<App />);
39+
// Check for some parent items
40+
expect(screen.getByText('Parent 1')).toBeInTheDocument();
41+
expect(screen.getByText('Parent 2')).toBeInTheDocument();
42+
});
43+
44+
it('renders status cells with correct colors', () => {
45+
render(<App />);
46+
const activeStatus = screen.getAllByText('Active')[0];
47+
const inactiveStatus = screen.getAllByText('Inactive')[0];
48+
const pendingStatus = screen.getAllByText('Pending')[0];
49+
50+
expect(activeStatus).toHaveStyle({ color: '#2ecc71' });
51+
expect(inactiveStatus).toHaveStyle({ color: '#e74c3c' });
52+
expect(pendingStatus).toHaveStyle({ color: '#f1c40f' });
53+
});
54+
});

tests/index.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { describe, it, expect } from 'vitest';
2+
import { MultiLevelTable } from '../src/index';
3+
import { MultiLevelTable as OriginalMultiLevelTable } from '../src/components/MultiLevelTable';
4+
5+
describe('index', () => {
6+
it('exports MultiLevelTable component', () => {
7+
expect(MultiLevelTable).toBe(OriginalMultiLevelTable);
8+
});
9+
10+
it('exports MultiLevelTableProps type', () => {
11+
// TypeScript will verify this at compile time
12+
const props: Parameters<typeof MultiLevelTable>[0] = {
13+
data: [],
14+
columns: [],
15+
};
16+
expect(props).toBeDefined();
17+
});
18+
});

tests/main.test.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { describe, it, expect, vi } from 'vitest';
2+
import React from 'react';
3+
import ReactDOM from 'react-dom/client';
4+
5+
// Mock ReactDOM
6+
vi.mock('react-dom/client', () => ({
7+
default: {
8+
createRoot: vi.fn(() => ({
9+
render: vi.fn(),
10+
})),
11+
},
12+
}));
13+
14+
describe('main', () => {
15+
it('renders App component in strict mode', async () => {
16+
const root = document.createElement('div');
17+
root.id = 'root';
18+
document.body.appendChild(root);
19+
20+
// Import the main module and wait for it to complete
21+
await import('../src/main');
22+
23+
// Verify that createRoot was called with the root element
24+
expect(ReactDOM.createRoot).toHaveBeenCalledWith(root);
25+
});
26+
});

tests/setup.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import '@testing-library/jest-dom';
2+
import { vi } from 'vitest';
23

34
// Mock window.matchMedia
45
Object.defineProperty(window, 'matchMedia', {

vitest.config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,9 @@ export default defineConfig({
66
setupFiles: ['./tests/setup.ts'],
77
globals: true,
88
include: ['tests/**/*.test.{ts,tsx}'],
9+
coverage: {
10+
provider: 'v8',
11+
reporter: ['text', 'json', 'html'],
12+
},
913
},
1014
});

0 commit comments

Comments
 (0)