|
| 1 | +import React from 'react'; |
| 2 | +import { render, screen, fireEvent } from '@testing-library/react'; |
| 3 | +import '@testing-library/jest-dom'; |
| 4 | +import FleModal from '../../components/ModalWindows/FleModal'; |
| 5 | +import { FieldType } from '../../utils/common'; |
| 6 | + |
| 7 | +const mockCloseModal = jest.fn(); |
| 8 | +const mockOnSubmit = jest.fn(); |
| 9 | + |
| 10 | +const mockDefaultValue = { |
| 11 | + name: 'Test Name', |
| 12 | + lut6: 32, |
| 13 | + flip_flop: 16, |
| 14 | + clock: 'Clock1', |
| 15 | + toggle_rate: 1.5, |
| 16 | + glitch_factor: 'Factor1', |
| 17 | + clock_enable_rate: 50, |
| 18 | +}; |
| 19 | + |
| 20 | +const mockClocks = ['Clock1', 'Clock2']; |
| 21 | +const mockGlitchFactor = ['Factor1', 'Factor2']; |
| 22 | + |
| 23 | +jest.mock('../../ClockSelectionProvider', () => ({ |
| 24 | + useClockSelection: () => ({ |
| 25 | + clocks: mockClocks, |
| 26 | + }), |
| 27 | +})); |
| 28 | + |
| 29 | +jest.mock('../../GlobalStateProvider', () => ({ |
| 30 | + useGlobalState: () => ({ |
| 31 | + GetOptions: (option) => (option === 'Glitch_Factor' ? mockGlitchFactor : []), |
| 32 | + }), |
| 33 | +})); |
| 34 | + |
| 35 | +describe('FleModal Component', () => { |
| 36 | + beforeEach(() => { |
| 37 | + render( |
| 38 | + <FleModal |
| 39 | + title="Test Fle Modal" |
| 40 | + defaultValue={mockDefaultValue} |
| 41 | + closeModal={mockCloseModal} |
| 42 | + onSubmit={mockOnSubmit} |
| 43 | + /> |
| 44 | + ); |
| 45 | + }); |
| 46 | + |
| 47 | + it('renders the modal with the correct title', () => { |
| 48 | + expect(screen.getByText('Test Fle Modal')).toBeInTheDocument(); |
| 49 | + }); |
| 50 | + |
| 51 | + it('renders the Name/Hierarchy field with correct default value', () => { |
| 52 | + const nameInput = screen.getByDisplayValue('Test Name'); |
| 53 | + expect(nameInput).toBeInTheDocument(); |
| 54 | + }); |
| 55 | + |
| 56 | + it('renders the LUT6 field with correct default value', () => { |
| 57 | + const lut6Input = screen.getByDisplayValue('32'); |
| 58 | + expect(lut6Input).toBeInTheDocument(); |
| 59 | + }); |
| 60 | + |
| 61 | + it('renders the FF/Latch field with correct default value', () => { |
| 62 | + const ffInput = screen.getByDisplayValue('16'); |
| 63 | + expect(ffInput).toBeInTheDocument(); |
| 64 | + }); |
| 65 | + |
| 66 | + it('renders the clock select field with correct default value', () => { |
| 67 | + const clockSelect = screen.getByDisplayValue('Clock1'); |
| 68 | + expect(clockSelect).toBeInTheDocument(); |
| 69 | + }); |
| 70 | + |
| 71 | + it('renders the toggle rate field with correct default value', () => { |
| 72 | + const toggleRateInput = screen.getByDisplayValue('150.0'); |
| 73 | + expect(toggleRateInput).toBeInTheDocument(); |
| 74 | + }); |
| 75 | + |
| 76 | + it('renders the clock enable rate field with correct default value', () => { |
| 77 | + const clockEnableInput = screen.getByDisplayValue('5000'); |
| 78 | + expect(clockEnableInput).toBeInTheDocument(); |
| 79 | + }); |
| 80 | + |
| 81 | + |
| 82 | + it('calls closeModal when the close button is clicked', () => { |
| 83 | + const closeButton = screen.getByRole('button', { name: /cancel/i }); |
| 84 | + fireEvent.click(closeButton); |
| 85 | + expect(mockCloseModal).toHaveBeenCalled(); |
| 86 | + }); |
| 87 | + |
| 88 | + it('calls onSubmit with correct data when the form is submitted', () => { |
| 89 | + const submitButton = screen.getByRole('button', { name: /ok/i }); |
| 90 | + fireEvent.click(submitButton); |
| 91 | + expect(mockOnSubmit).toHaveBeenCalledWith(mockDefaultValue); |
| 92 | + }); |
| 93 | + |
| 94 | + it('updates the Name/Hierarchy field when changed', () => { |
| 95 | + const nameInput = screen.getByDisplayValue('Test Name'); |
| 96 | + fireEvent.change(nameInput, { target: { value: 'Updated Name' } }); |
| 97 | + expect(nameInput.value).toBe('Updated Name'); |
| 98 | + }); |
| 99 | + |
| 100 | + it('updates the LUT6 field when changed', () => { |
| 101 | + const lut6Input = screen.getByDisplayValue('32'); |
| 102 | + fireEvent.change(lut6Input, { target: { value: '64' } }); |
| 103 | + expect(lut6Input.value).toBe('64'); |
| 104 | + }); |
| 105 | + |
| 106 | + it('updates the clock select field when changed', () => { |
| 107 | + const clockSelect = screen.getByDisplayValue('Clock1'); |
| 108 | + fireEvent.change(clockSelect, { target: { value: 'Clock2' } }); |
| 109 | + expect(screen.getByDisplayValue('Clock2')).toBeInTheDocument(); |
| 110 | + }); |
| 111 | +}); |
0 commit comments