Skip to content

Commit a190d6c

Browse files
authored
React/Jest tests for BramTable.js (#236)
1 parent 2a1e707 commit a190d6c

File tree

1 file changed

+134
-0
lines changed

1 file changed

+134
-0
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import React from 'react';
2+
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
3+
import '@testing-library/jest-dom';
4+
import BramTable from '../../components/Tables/BramTable';
5+
import * as server from '../../utils/serverAPI';
6+
import * as common from '../../utils/common';
7+
8+
jest.mock('../../components/ModalWindows/BramModal', () => ({
9+
__esModule: true,
10+
default: ({ onSubmit, closeModal }) => (
11+
<div data-testid="bram-modal">
12+
<button data-testid="submit" onClick={() => onSubmit({ name: 'BRAM Test' })}>Submit</button>
13+
<button data-testid="close-modal" onClick={closeModal}>Close</button>
14+
</div>
15+
),
16+
}));
17+
18+
jest.mock('../../utils/serverAPI');
19+
jest.mock('../../ClockSelectionProvider', () => ({
20+
useClockSelection: () => ({ defaultClock: jest.fn().mockReturnValue('100 MHz') })
21+
}));
22+
jest.mock('../../GlobalStateProvider', () => ({
23+
useGlobalState: () => ({
24+
updateGlobalState: jest.fn(),
25+
GetOptions: jest.fn().mockReturnValue([{ id: 1, name: 'BRAM_Type' }]),
26+
})
27+
}));
28+
jest.mock('../../SOCTotalPowerProvider', () => ({
29+
useSocTotalPower: () => ({ updateTotalPower: jest.fn() })
30+
}));
31+
32+
jest.spyOn(common, 'fixed').mockImplementation((number, precision = 3) => {
33+
if (isNaN(number)) {
34+
return '0'; // default value for invalid numbers
35+
}
36+
return Number(number).toFixed(precision);
37+
});
38+
39+
describe('BramTable Component', () => {
40+
beforeEach(() => {
41+
server.GET.mockImplementation((url, callback) => {
42+
const mockBramData = [
43+
{
44+
name: 'BRAM Block 1',
45+
enable: true,
46+
type: 0,
47+
bram_used: 10,
48+
port_a: {
49+
clock: '200 MHz',
50+
width: 18,
51+
write_enable_rate: 50,
52+
read_enable_rate: 50,
53+
toggle_rate: 20
54+
},
55+
port_b: {
56+
clock: '200 MHz',
57+
width: 18,
58+
write_enable_rate: 50,
59+
read_enable_rate: 50,
60+
toggle_rate: 20
61+
},
62+
consumption: {
63+
port_a: { clock_frequency: '200 MHz', output_signal_rate: 10, ram_depth: 1024 },
64+
port_b: { clock_frequency: '200 MHz', output_signal_rate: 10, ram_depth: 1024 },
65+
block_power: 5,
66+
interconnect_power: 2,
67+
percentage: 50,
68+
messages: []
69+
}
70+
}
71+
];
72+
callback(mockBramData);
73+
});
74+
75+
server.PATCH.mockImplementation((url, data, callback) => {
76+
callback();
77+
});
78+
79+
server.DELETE.mockImplementation((url, callback) => {
80+
callback();
81+
});
82+
});
83+
84+
test('renders table headers and power table', () => {
85+
render(<BramTable device="device1" update={true} notify={jest.fn()} />);
86+
87+
expect(screen.getByText('BRAM power')).toBeInTheDocument();
88+
const usedHeaders = screen.getAllByText('Used');
89+
expect(usedHeaders.length).toBeGreaterThan(0);
90+
91+
const totalHeaders = screen.getAllByText('Total');
92+
expect(totalHeaders.length).toBeGreaterThan(0);
93+
expect(screen.getByText(/BRAM Type/i)).toBeInTheDocument();
94+
expect(screen.getByText(/Action/i)).toBeInTheDocument();
95+
expect(screen.getByText(/Name\/Hierarchy/i)).toBeInTheDocument();
96+
});
97+
98+
test('handles enabling/disabling BRAM rows', async () => {
99+
render(<BramTable device="device1" update={true} notify={jest.fn()} />);
100+
101+
await waitFor(() => {
102+
const checkbox = screen.getByRole('checkbox');
103+
expect(checkbox).toBeChecked();
104+
});
105+
106+
const checkbox = screen.getByRole('checkbox');
107+
fireEvent.click(checkbox);
108+
109+
await waitFor(() => {
110+
expect(server.PATCH).toHaveBeenCalledTimes(1);
111+
});
112+
});
113+
114+
test('handles add/edit BRAM row via modal', async () => {
115+
render(<BramTable device="device1" update={true} notify={jest.fn()} />);
116+
117+
fireEvent.click(screen.getByText(/Add/i));
118+
119+
await waitFor(() => {
120+
expect(screen.getByTestId('bram-modal')).toBeInTheDocument();
121+
});
122+
123+
fireEvent.click(screen.getByTestId('submit'));
124+
125+
await waitFor(() => {
126+
expect(server.POST).toHaveBeenCalledTimes(1);
127+
});
128+
129+
// closing the modal
130+
fireEvent.click(screen.getByTestId('close-modal'));
131+
132+
expect(screen.queryByTestId('bram-modal')).not.toBeInTheDocument();
133+
});
134+
});

0 commit comments

Comments
 (0)