Skip to content

Feat/unit tests #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 13, 2025
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ dist-ssr
*.njsproj
*.sln
*.sw?

coverage
12 changes: 1 addition & 11 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -416,18 +416,8 @@ const App: React.FC = () => {
},
];

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const renderCustomPagination = () => {
return <div>Custom Pagination </div>;
};

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const renderCustomExpandButton = () => {
return <div>➡</div>;
};

return (
<div className="app" style={{ backgroundColor: theme.colors?.background }}>
<div className="app" data-testid="app-container" style={{ backgroundColor: theme.colors?.background }}>
<header
className="app-header"
style={{ backgroundColor: theme.table?.header?.background }}
Expand Down
54 changes: 54 additions & 0 deletions tests/App.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import React from 'react';
import { render, screen, fireEvent } from '@testing-library/react';
import '@testing-library/jest-dom';
import App from '../src/App';

describe('App Component', () => {
it('renders without crashing', () => {
render(<App />);
expect(screen.getByRole('button', { name: /dark mode/i })).toBeInTheDocument();
});

it('renders the MultiLevelTable component', () => {
render(<App />);
// Check for table headers
expect(screen.getByText('Name')).toBeInTheDocument();
expect(screen.getByText('Value')).toBeInTheDocument();
expect(screen.getByText('Status')).toBeInTheDocument();
});

it('toggles theme when theme button is clicked', () => {
render(<App />);
const themeButton = screen.getByRole('button', { name: /dark mode/i });
const appDiv = screen.getByTestId('app-container');

// Initial state should be light theme
expect(appDiv).toHaveStyle({ backgroundColor: '#ffffff' });

// Click to toggle to dark theme
fireEvent.click(themeButton);
expect(appDiv).toHaveStyle({ backgroundColor: '#212529' });

// Click again to toggle back to light theme
fireEvent.click(themeButton);
expect(appDiv).toHaveStyle({ backgroundColor: '#ffffff' });
});

it('renders parent items correctly', () => {
render(<App />);
// Check for some parent items
expect(screen.getByText('Parent 1')).toBeInTheDocument();
expect(screen.getByText('Parent 2')).toBeInTheDocument();
});

it('renders status cells with correct colors', () => {
render(<App />);
const activeStatus = screen.getAllByText('Active')[0];
const inactiveStatus = screen.getAllByText('Inactive')[0];
const pendingStatus = screen.getAllByText('Pending')[0];

expect(activeStatus).toHaveStyle({ color: '#2ecc71' });
expect(inactiveStatus).toHaveStyle({ color: '#e74c3c' });
expect(pendingStatus).toHaveStyle({ color: '#f1c40f' });
});
});
18 changes: 18 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { describe, it, expect } from 'vitest';
import { MultiLevelTable } from '../src/index';
import { MultiLevelTable as OriginalMultiLevelTable } from '../src/components/MultiLevelTable';

describe('index', () => {
it('exports MultiLevelTable component', () => {
expect(MultiLevelTable).toBe(OriginalMultiLevelTable);
});

it('exports MultiLevelTableProps type', () => {
// TypeScript will verify this at compile time
const props: Parameters<typeof MultiLevelTable>[0] = {
data: [],
columns: [],
};
expect(props).toBeDefined();
});
});
26 changes: 26 additions & 0 deletions tests/main.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { describe, it, expect, vi } from 'vitest';
import React from 'react';
import ReactDOM from 'react-dom/client';

// Mock ReactDOM
vi.mock('react-dom/client', () => ({
default: {
createRoot: vi.fn(() => ({
render: vi.fn(),
})),
},
}));

describe('main', () => {
it('renders App component in strict mode', async () => {
const root = document.createElement('div');
root.id = 'root';
document.body.appendChild(root);

// Import the main module and wait for it to complete
await import('../src/main');

// Verify that createRoot was called with the root element
expect(ReactDOM.createRoot).toHaveBeenCalledWith(root);
});
});
1 change: 1 addition & 0 deletions tests/setup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import '@testing-library/jest-dom';
import { vi } from 'vitest';

// Mock window.matchMedia
Object.defineProperty(window, 'matchMedia', {
Expand Down
4 changes: 4 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@ export default defineConfig({
setupFiles: ['./tests/setup.ts'],
globals: true,
include: ['tests/**/*.test.{ts,tsx}'],
coverage: {
provider: 'v8',
reporter: ['text', 'json', 'html'],
},
},
});