Skip to content

Commit 3aa9e36

Browse files
authored
React/Jest tests for ClockingTable.js (#239)
1 parent 2822507 commit 3aa9e36

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import React from 'react';
2+
import { render, screen, fireEvent } from '@testing-library/react';
3+
import '@testing-library/jest-dom';
4+
import ClockingTable from '../../components/Tables/ClockingTable';
5+
import * as server from '../../utils/serverAPI';
6+
7+
jest.mock('../../utils/serverAPI', () => ({
8+
GET: jest.fn(),
9+
PATCH: jest.fn(),
10+
DELETE: jest.fn(),
11+
POST: jest.fn(),
12+
api: {
13+
fetch: jest.fn((elem, device) => `/api/${elem}/${device}`),
14+
index: jest.fn((elem, device, index) => `/api/${elem}/${device}/${index}`),
15+
consumption: jest.fn((elem, device) => `/api/${elem}/${device}/consumption`),
16+
},
17+
Elem: {
18+
clocking: 'clocking',
19+
},
20+
}));
21+
22+
jest.mock('../../utils/common', () => ({
23+
fixed: jest.fn((number, precision = 2) => number.toFixed(precision)),
24+
GetText: jest.fn((id, options) => options.find((option) => option.id === id)?.name || ''),
25+
color: jest.fn((isError, isWarning, isInfo) => (isError ? 'red' : isWarning ? 'yellow' : 'green')), // Mock the color function
26+
}));
27+
28+
jest.mock('../../ClockSelectionProvider', () => ({
29+
useClockSelection: jest.fn(() => ({
30+
setClocks: jest.fn(),
31+
})),
32+
}));
33+
34+
jest.mock('../../GlobalStateProvider', () => ({
35+
useGlobalState: jest.fn(() => ({
36+
GetOptions: jest.fn((key) => {
37+
if (key === 'Clock_State') return [{ id: 1, name: 'Enabled' }, { id: 0, name: 'Disabled' }];
38+
if (key === 'Source') return [{ id: 0, name: 'External' }, { id: 1, name: 'Internal' }];
39+
return [];
40+
}),
41+
updateGlobalState: jest.fn(),
42+
})),
43+
}));
44+
45+
jest.mock('../../SOCTotalPowerProvider', () => ({
46+
useSocTotalPower: jest.fn(() => ({
47+
updateTotalPower: jest.fn(),
48+
})),
49+
}));
50+
51+
describe('ClockingTable Component', () => {
52+
const mockDevice = 'device1';
53+
const mockClockingData = [
54+
{
55+
description: 'Clock 1',
56+
source: 0,
57+
port: 'CLK0',
58+
frequency: 1000000,
59+
enable: true,
60+
state: 1,
61+
consumption: {
62+
fan_out: 5,
63+
block_power: 10,
64+
interconnect_power: 3,
65+
percentage: 50,
66+
messages: [],
67+
},
68+
},
69+
];
70+
71+
beforeEach(() => {
72+
server.GET.mockImplementation((url, callback) => {
73+
if (url.includes('consumption')) {
74+
callback({
75+
total_clock_block_power: 10,
76+
total_clock_interconnect_power: 3,
77+
total_pll_power: 5,
78+
total_clocks_used: 2,
79+
total_clocks_available: 5,
80+
total_plls_used: 1,
81+
total_plls_available: 2,
82+
});
83+
} else {
84+
callback(mockClockingData);
85+
}
86+
});
87+
});
88+
89+
test('renders the component and the power table', async () => {
90+
render(<ClockingTable device={mockDevice} update={false} notify={jest.fn()} />);
91+
92+
expect(screen.getByText('Clocking')).toBeInTheDocument();
93+
expect(screen.getByText('Clock power')).toBeInTheDocument();
94+
expect(screen.getByText('Clocks')).toBeInTheDocument();
95+
expect(screen.getByText('PLLs')).toBeInTheDocument();
96+
});
97+
});

0 commit comments

Comments
 (0)