Skip to content

Commit 5186ed1

Browse files
authored
React/Jest tests for DspTable.js (#241)
1 parent 6e6cdd8 commit 5186ed1

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import React from 'react';
2+
import { render, screen, fireEvent } from '@testing-library/react';
3+
import '@testing-library/jest-dom';
4+
import DspTable from '../../components/Tables/DspTable';
5+
import * as server from '../../utils/serverAPI';
6+
import * as common from '../../utils/common';
7+
8+
jest.mock('../../utils/serverAPI', () => ({
9+
GET: jest.fn(),
10+
PATCH: jest.fn(),
11+
DELETE: jest.fn(),
12+
POST: jest.fn(),
13+
api: {
14+
fetch: jest.fn((elem, deviceId) => `/api/${elem}/${deviceId}`),
15+
consumption: jest.fn((elem, deviceId) => `/api/consumption/${elem}/${deviceId}`),
16+
index: jest.fn((elem, deviceId, index) => `/api/${elem}/${deviceId}/${index}`),
17+
},
18+
Elem: {
19+
dsp: 'dsp',
20+
},
21+
}));
22+
23+
jest.mock('../../utils/common', () => ({
24+
fixed: jest.fn((number) => number.toFixed(2)),
25+
GetText: jest.fn((key, options) => options.find((option) => option.id === key)?.text || ''),
26+
color: jest.fn(() => 'green'),
27+
}));
28+
29+
jest.mock('../../ClockSelectionProvider', () => ({
30+
useClockSelection: jest.fn(() => ({ defaultClock: jest.fn(() => '100 MHz') })),
31+
}));
32+
33+
jest.mock('../../GlobalStateProvider', () => ({
34+
useGlobalState: jest.fn(() => ({
35+
GetOptions: jest.fn(() => [{ id: 1, text: 'Mode1' }, { id: 2, text: 'Pipeline1' }]),
36+
updateGlobalState: jest.fn(),
37+
})),
38+
}));
39+
40+
jest.mock('../../SOCTotalPowerProvider', () => ({
41+
useSocTotalPower: jest.fn(() => ({ updateTotalPower: jest.fn() })),
42+
}));
43+
44+
describe('DspTable Component', () => {
45+
const mockDspData = [
46+
{
47+
name: 'DSP Block 1',
48+
enable: true,
49+
number_of_multipliers: 10,
50+
dsp_mode: 1,
51+
a_input_width: 16,
52+
b_input_width: 16,
53+
clock: '100 MHz',
54+
pipelining: 2,
55+
toggle_rate: 0.8,
56+
consumption: {
57+
dsp_blocks_used: 5,
58+
clock_frequency: 1000000,
59+
output_signal_rate: 3.2,
60+
block_power: 15,
61+
interconnect_power: 5,
62+
percentage: 50,
63+
messages: [],
64+
},
65+
},
66+
];
67+
68+
beforeEach(() => {
69+
jest.clearAllMocks();
70+
});
71+
72+
test('renders the component and the power table', async () => {
73+
server.GET.mockImplementation((url, callback) => {
74+
if (url.includes('/api/dsp/')) {
75+
callback(mockDspData);
76+
} else if (url.includes('/api/consumption/dsp/')) {
77+
callback({
78+
total_dsp_block_power: 15,
79+
total_dsp_interconnect_power: 5,
80+
total_dsp_blocks_used: 5,
81+
total_dsp_blocks_available: 10,
82+
});
83+
}
84+
});
85+
86+
render(<DspTable device="device1" update={false} notify={jest.fn()} />);
87+
88+
expect(screen.getByText('DSP')).toBeInTheDocument();
89+
expect(screen.getByText('DSP power')).toBeInTheDocument();
90+
expect(screen.getByText('Name/Hierarchy')).toBeInTheDocument();
91+
expect(screen.getByText('Block Power')).toBeInTheDocument();
92+
});
93+
94+
test('renders DSP data in the table', async () => {
95+
server.GET.mockImplementation((url, callback) => {
96+
if (url.includes('/api/dsp/')) {
97+
callback(mockDspData);
98+
} else if (url.includes('/api/consumption/dsp/')) {
99+
callback({
100+
total_dsp_block_power: 15,
101+
total_dsp_interconnect_power: 5,
102+
total_dsp_blocks_used: 5,
103+
total_dsp_blocks_available: 10,
104+
});
105+
}
106+
});
107+
108+
render(<DspTable device="device1" update={false} notify={jest.fn()} />);
109+
110+
expect(screen.getByText('DSP Block 1')).toBeInTheDocument();
111+
expect(screen.getByText('Mode1')).toBeInTheDocument();
112+
expect(screen.getAllByText('16')[0]).toBeInTheDocument();
113+
expect(screen.getByText('5')).toBeInTheDocument();
114+
expect(screen.getByText('100 MHz')).toBeInTheDocument();
115+
expect(screen.getByText('15.00 W')).toBeInTheDocument();
116+
});
117+
});

0 commit comments

Comments
 (0)