|
| 1 | +import React from 'react'; |
| 2 | +import { render, screen, waitFor } from '@testing-library/react'; |
| 3 | +import '@testing-library/jest-dom'; |
| 4 | +import MemoryTable from '../../components/Tables/MemoryTable'; |
| 5 | +import { GlobalStateProvider } from '../../GlobalStateProvider'; |
| 6 | +import { SocTotalPowerProvider } from '../../SOCTotalPowerProvider'; |
| 7 | +import * as server from '../../utils/serverAPI'; |
| 8 | + |
| 9 | +jest.mock('../../utils/serverAPI', () => ({ |
| 10 | + GET: jest.fn(), |
| 11 | + PATCH: jest.fn(), |
| 12 | + peripheralPath: jest.fn(), |
| 13 | + Elem: { |
| 14 | + memory: 'memory', |
| 15 | + }, |
| 16 | +})); |
| 17 | + |
| 18 | +jest.mock('../../utils/events', () => ({ |
| 19 | + publish: jest.fn(), |
| 20 | +})); |
| 21 | + |
| 22 | +describe('MemoryTable Component', () => { |
| 23 | + const mockPeripherals = [ |
| 24 | + { href: 'mock-memory-href1', id: 'ddr' }, |
| 25 | + { href: 'mock-memory-href2', id: 'ocm' }, |
| 26 | + ]; |
| 27 | + |
| 28 | + const mockMemoryData = { |
| 29 | + name: 'DDR Memory', |
| 30 | + enable: true, |
| 31 | + usage: 0, |
| 32 | + memory_type: 1, |
| 33 | + data_rate: 800, |
| 34 | + width: 32, |
| 35 | + consumption: { |
| 36 | + block_power: 0.5, |
| 37 | + percentage: 40, |
| 38 | + messages: [{ text: 'Test message' }], |
| 39 | + write_bandwidth: 10, |
| 40 | + read_bandwidth: 15, |
| 41 | + }, |
| 42 | + }; |
| 43 | + |
| 44 | + beforeEach(() => { |
| 45 | + server.GET.mockImplementation((url, callback) => { |
| 46 | + if (url.includes('mock-memory-href')) { |
| 47 | + callback(mockMemoryData); |
| 48 | + } |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + test('renders MemoryTable header correctly', () => { |
| 53 | + render( |
| 54 | + <GlobalStateProvider> |
| 55 | + <SocTotalPowerProvider> |
| 56 | + <MemoryTable device="mock-device" peripherals={mockPeripherals} update={false} notify={jest.fn()} /> |
| 57 | + </SocTotalPowerProvider> |
| 58 | + </GlobalStateProvider> |
| 59 | + ); |
| 60 | + |
| 61 | + const memoryHeaders = screen.getAllByText('Memory'); |
| 62 | + expect(memoryHeaders[0]).toBeInTheDocument(); |
| 63 | + }); |
| 64 | + |
| 65 | + test('renders the correct power total in the MemoryTable', async () => { |
| 66 | + render( |
| 67 | + <GlobalStateProvider> |
| 68 | + <SocTotalPowerProvider> |
| 69 | + <MemoryTable device="mock-device" peripherals={mockPeripherals} update={false} notify={jest.fn()} /> |
| 70 | + </SocTotalPowerProvider> |
| 71 | + </GlobalStateProvider> |
| 72 | + ); |
| 73 | + |
| 74 | + await waitFor(() => { |
| 75 | + expect(screen.getByText('Memory power')).toBeInTheDocument(); |
| 76 | + expect(screen.getByText('0.00')).toBeInTheDocument(); |
| 77 | + }); |
| 78 | + }); |
| 79 | +}); |
0 commit comments