Skip to content

Commit d4d23dd

Browse files
committed
React/Jest tests for PowerSummaryTable.js
1 parent a190d6c commit d4d23dd

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import React from 'react';
2+
import { render, screen, within } from '@testing-library/react';
3+
import '@testing-library/jest-dom';
4+
import PowerSummaryTable from '../../components/Tables/PowerSummaryTable';
5+
import { Tooltip } from 'antd';
6+
7+
jest.mock('antd', () => ({
8+
Tooltip: jest.fn(({ title, children }) => <div>{children} <span>{title}</span></div>),
9+
}));
10+
11+
jest.mock('../../utils/common', () => ({
12+
fixed: jest.fn((number, precision = 2) => number.toFixed(precision)),
13+
color: jest.fn((isError, isWarning) => (isError ? 'red' : isWarning ? 'yellow' : 'green')),
14+
}));
15+
16+
describe('PowerSummaryTable Component', () => {
17+
const mockData = [
18+
{
19+
text: 'Item 1',
20+
power: 10,
21+
percent: 20,
22+
messages: [[{ type: 'warn', text: 'Warning message' }]],
23+
},
24+
{
25+
text: 'Item 2',
26+
power: 20,
27+
percent: 40,
28+
messages: [[{ type: 'error', text: 'Error message' }]],
29+
},
30+
{
31+
text: 'Item 3',
32+
power: 30,
33+
percent: 60,
34+
messages: [],
35+
},
36+
];
37+
38+
test('renders the table title and bottom total', () => {
39+
render(<PowerSummaryTable title="Power Summary" data={mockData} total={60} percent={80} />);
40+
expect(screen.getByText('Power Summary')).toBeInTheDocument();
41+
expect(screen.getByText((content) => content.includes('60.00') && content.includes('W'))).toBeInTheDocument();
42+
expect(screen.getByText('80 %')).toBeInTheDocument();
43+
const progressBar = screen.getByRole('progressbar');
44+
expect(progressBar).toHaveAttribute('value', '80');
45+
expect(progressBar).toHaveAttribute('max', '100');
46+
});
47+
48+
test('renders power cells and percentage for each row', () => {
49+
render(<PowerSummaryTable title="Power Summary" data={mockData} total={60} percent={80} />);
50+
mockData.forEach((item) => {
51+
expect(screen.getByText(item.text)).toBeInTheDocument();
52+
expect(screen.getByText(`${item.power.toFixed(3)} W`)).toBeInTheDocument();
53+
expect(screen.getByText(`${item.percent.toFixed(0)} %`)).toBeInTheDocument();
54+
});
55+
});
56+
57+
test('renders warning and error tooltips', () => {
58+
render(<PowerSummaryTable title="Power Summary" data={mockData} total={60} percent={80} />);
59+
const warningRow = screen.getByText('Item 1').closest('tr');
60+
const warningTooltip = within(warningRow).getByText('Warning message');
61+
expect(warningTooltip).toBeInTheDocument();
62+
const errorRow = screen.getByText('Item 2').closest('tr');
63+
const errorTooltip = within(errorRow).getByText('Error message');
64+
expect(errorTooltip).toBeInTheDocument();
65+
});
66+
67+
test('does not render tooltip for rows without messages', () => {
68+
render(<PowerSummaryTable title="Power Summary" data={mockData} total={60} percent={80} />);
69+
const noMessageRow = screen.getByText('Item 3').closest('tr');
70+
expect(within(noMessageRow).queryByText('Warning message')).toBeNull();
71+
expect(within(noMessageRow).queryByText('Error message')).toBeNull();
72+
});
73+
});

0 commit comments

Comments
 (0)