Skip to content

Commit 1cd7795

Browse files
authored
React/Jest tests for IOModal.js (#231)
1 parent 086fe62 commit 1cd7795

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 IOModal from '../../components/ModalWindows/IOModal';
5+
import { FieldType } from '../../utils/common';
6+
7+
const mockCloseModal = jest.fn();
8+
const mockOnSubmit = jest.fn();
9+
10+
const mockDefaultValue = {
11+
name: 'Test Port',
12+
bus_width: 16,
13+
clock: 'Clock1',
14+
duty_cycle: 50,
15+
direction: 'Input',
16+
io_standard: 'Standard1',
17+
drive_strength: 'Strength1',
18+
slew_rate: 'Fast',
19+
differential_termination: 'Termination1',
20+
io_pull_up_down: 'PullUp',
21+
io_data_type: 'Type1',
22+
input_enable_rate: 100,
23+
output_enable_rate: 200,
24+
synchronization: 'Sync1',
25+
toggle_rate: 1.5,
26+
};
27+
28+
const mockClocks = ['Clock1', 'Clock2'];
29+
const mockOptions = {
30+
IO_Direction: ['Input', 'Output'],
31+
IO_Standard: ['Standard1', 'Standard2'],
32+
IO_Drive_Strength: ['Strength1', 'Strength2'],
33+
IO_Slew_Rate: ['Fast', 'Slow'],
34+
IO_differential_termination: ['Termination1', 'Termination2'],
35+
IO_Data_Type: ['Type1', 'Type2'],
36+
IO_Synchronization: ['Sync1', 'Sync2'],
37+
IO_Pull_up_down: ['PullUp', 'PullDown'],
38+
};
39+
40+
jest.mock('../../ClockSelectionProvider', () => ({
41+
useClockSelection: () => ({
42+
clocks: mockClocks,
43+
}),
44+
}));
45+
46+
jest.mock('../../GlobalStateProvider', () => ({
47+
useGlobalState: () => ({
48+
GetOptions: (option) => mockOptions[option],
49+
}),
50+
}));
51+
52+
describe('IOModal Component', () => {
53+
beforeEach(() => {
54+
render(
55+
<IOModal
56+
title="Test IO Modal"
57+
defaultValue={mockDefaultValue}
58+
closeModal={mockCloseModal}
59+
onSubmit={mockOnSubmit}
60+
/>
61+
);
62+
});
63+
64+
it('renders the modal with the correct title', () => {
65+
expect(screen.getByText('Test IO Modal')).toBeInTheDocument();
66+
});
67+
68+
it('calls closeModal when the close button is clicked', () => {
69+
const closeButton = screen.getByRole('button', { name: /cancel/i });
70+
fireEvent.click(closeButton);
71+
expect(mockCloseModal).toHaveBeenCalled();
72+
});
73+
74+
it('calls onSubmit with correct data when the form is submitted', () => {
75+
const submitButton = screen.getByRole('button', { name: /ok/i });
76+
fireEvent.click(submitButton);
77+
expect(mockOnSubmit).toHaveBeenCalledWith(mockDefaultValue);
78+
});
79+
80+
it('updates the RTL Port Name field when changed', () => {
81+
const nameInput = screen.getByDisplayValue('Test Port');
82+
fireEvent.change(nameInput, { target: { value: 'Updated Port' } });
83+
expect(nameInput.value).toBe('Updated Port');
84+
});
85+
86+
it('updates the bus width field when changed', () => {
87+
const busWidthInput = screen.getByDisplayValue('16');
88+
fireEvent.change(busWidthInput, { target: { value: '32' } });
89+
expect(busWidthInput.value).toBe('32');
90+
});
91+
92+
it('updates the clock field when changed', () => {
93+
const clockSelect = screen.getByDisplayValue('Clock1');
94+
fireEvent.change(clockSelect, { target: { value: 'Clock2' } });
95+
expect(screen.getByDisplayValue('Clock2')).toBeInTheDocument();
96+
});
97+
});

0 commit comments

Comments
 (0)