Skip to content

Commit 55a6eaa

Browse files
Merge pull request #2354 from OneCommunityGlobal/aaryaneil-unit-test-ProjectTable
Aaryaneil - Added test cases to Project Table component.
2 parents 43755f9 + a455c53 commit 55a6eaa

2 files changed

Lines changed: 109 additions & 41 deletions

File tree

src/components/Reports/__tests__/ProjectTable.test.js

Lines changed: 0 additions & 41 deletions
This file was deleted.
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import React from 'react';
2+
import { render, screen } from '@testing-library/react';
3+
import '@testing-library/jest-dom';
4+
import ProjectTable from '../ProjectTable';
5+
import { BrowserRouter } from 'react-router-dom';
6+
7+
describe('ProjectTable component', () => {
8+
const mockProjects = [
9+
{ _id: '1', projectName: 'Project Alpha', isActive: true },
10+
{ _id: '2', projectName: 'Project Beta', isActive: false },
11+
];
12+
13+
const renderWithRouter = (ui, { route = '/' } = {}) => {
14+
window.history.pushState({}, 'Test page', route);
15+
return render(ui, { wrapper: BrowserRouter });
16+
};
17+
18+
it('renders the correct number of projects', () => {
19+
renderWithRouter(<ProjectTable projects={mockProjects} />);
20+
const rows = screen.getAllByRole('row');
21+
expect(rows.length).toBe(mockProjects.length + 1); // +1 for the header row
22+
});
23+
24+
it('displays the project names and links correctly', () => {
25+
renderWithRouter(<ProjectTable projects={mockProjects} />);
26+
mockProjects.forEach(project => {
27+
expect(screen.getByText(project.projectName)).toBeInTheDocument();
28+
expect(screen.getByRole('link', { name: project.projectName })).toHaveAttribute('href', `/projectreport/${project._id}`);
29+
});
30+
});
31+
32+
it('shows the correct active status indicator', () => {
33+
const { container } = renderWithRouter(<ProjectTable projects={mockProjects} />);
34+
mockProjects.forEach(project => {
35+
const row = container.querySelector(`#tr_${project._id}`);
36+
const activeIcon = project.isActive ? '.fa-circle' : '.fa-circle-o';
37+
expect(row.querySelector(activeIcon)).toBeInTheDocument();
38+
});
39+
});
40+
41+
it('renders the table with light mode styles when darkMode is false', () => {
42+
renderWithRouter(<ProjectTable projects={mockProjects} darkMode={false} />);
43+
const table = screen.getByRole('table');
44+
expect(table).toHaveClass('table-bordered');
45+
46+
const thead = table.querySelector('thead');
47+
expect(thead).not.toHaveClass('bg-space-cadet');
48+
49+
const links = screen.getAllByRole('link');
50+
links.forEach(link => {
51+
expect(link).not.toHaveClass('text-light');
52+
});
53+
});
54+
55+
it('renders an empty table when no projects are provided', () => {
56+
renderWithRouter(<ProjectTable projects={[]} />);
57+
const rows = screen.getAllByRole('row');
58+
expect(rows.length).toBe(1); // Only the header row
59+
});
60+
61+
it('renders with correct table header labels', () => {
62+
renderWithRouter(<ProjectTable projects={mockProjects} />);
63+
expect(screen.getByText('#')).toBeInTheDocument();
64+
expect(screen.getByText('Project Name')).toBeInTheDocument();
65+
expect(screen.getByText('Active')).toBeInTheDocument();
66+
});
67+
68+
it('should apply ARIA roles correctly', () => {
69+
renderWithRouter(<ProjectTable projects={mockProjects} />);
70+
expect(screen.getAllByRole('row')).toHaveLength(mockProjects.length + 1);
71+
expect(screen.getAllByRole('columnheader')).toHaveLength(3);
72+
});
73+
74+
it('should handle invalid darkMode prop gracefully', () => {
75+
renderWithRouter(<ProjectTable projects={mockProjects} darkMode={undefined} />);
76+
const table = screen.getByRole('table');
77+
expect(table).toHaveClass('table-bordered'); // Assumes default is light mode
78+
});
79+
it('renders the table with dark mode styles when darkMode is true', () => {
80+
renderWithRouter(<ProjectTable projects={mockProjects} darkMode={true} />);
81+
const table = screen.getByRole('table');
82+
const thead = table.querySelector('thead');
83+
84+
expect(table).toHaveClass('bg-yinmn-blue');
85+
const links = screen.getAllByRole('link');
86+
links.forEach(link => {
87+
expect(link).toHaveClass('text-light');
88+
});
89+
});
90+
91+
// it('applies the hover effect class in dark mode', () => {
92+
// renderWithRouter(<ProjectTable projects={mockProjects} darkMode={true} />);
93+
// const rows = screen.getAllByRole('row');
94+
95+
// // Skip the header row
96+
// rows.slice(1).forEach(row => {
97+
// // Simulate hover
98+
// row.dispatchEvent(new MouseEvent('mouseover', { bubbles: true }));
99+
// console.log('Table classes:', row.className);
100+
// // Check if the hover effect class is applied
101+
// expect(row).toHaveClass('bg-yinmn-blue');
102+
103+
// // Optionally, you could simulate mouse out and verify the class removal if needed
104+
// row.dispatchEvent(new MouseEvent('mouseout', { bubbles: true }));
105+
// expect(row).not.toHaveClass('bg-yinmn-blue');
106+
// });
107+
// });
108+
109+
});

0 commit comments

Comments
 (0)