|
| 1 | +import React from 'react'; |
| 2 | +import { render, screen, fireEvent } from '@testing-library/react'; |
| 3 | +import '@testing-library/jest-dom'; |
| 4 | +import MemoryModal from '../../components/ModalWindows/MemoryModal'; |
| 5 | +import { useGlobalState } from '../../GlobalStateProvider'; |
| 6 | + |
| 7 | +const mockFieldType = { |
| 8 | + select: 'select', |
| 9 | + number: 'number', |
| 10 | +}; |
| 11 | + |
| 12 | +jest.mock('../../components/ModalWindows/ModalWindow', () => ({ title, closeModal, fields, onSubmit }) => ( |
| 13 | + <div data-testid="modal"> |
| 14 | + <h1>{title}</h1> |
| 15 | + <button onClick={closeModal}>Close</button> |
| 16 | + {fields.map((field) => ( |
| 17 | + <div key={field.id}> |
| 18 | + <label>{field.text}</label> |
| 19 | + {field.fieldType === mockFieldType.select ? ( |
| 20 | + <select value={field.value} aria-label={field.text}> |
| 21 | + {field.values.map((option, index) => ( |
| 22 | + <option key={index} value={option}> |
| 23 | + {option} |
| 24 | + </option> |
| 25 | + ))} |
| 26 | + </select> |
| 27 | + ) : ( |
| 28 | + <input |
| 29 | + type="number" |
| 30 | + value={field.value} |
| 31 | + aria-label={field.text} |
| 32 | + readOnly |
| 33 | + /> |
| 34 | + )} |
| 35 | + </div> |
| 36 | + ))} |
| 37 | + <button onClick={onSubmit}>Submit</button> |
| 38 | + </div> |
| 39 | +)); |
| 40 | + |
| 41 | +jest.mock('../../GlobalStateProvider', () => ({ |
| 42 | + useGlobalState: () => ({ |
| 43 | + GetOptions: jest.fn((type) => { |
| 44 | + switch (type) { |
| 45 | + case 'Peripherals_Usage': |
| 46 | + return ['Low', 'Medium', 'High']; |
| 47 | + case 'Memory_Type': |
| 48 | + return ['DDR4', 'DDR3', 'DDR2']; |
| 49 | + default: |
| 50 | + return []; |
| 51 | + } |
| 52 | + }), |
| 53 | + }), |
| 54 | +})); |
| 55 | + |
| 56 | +const mockCloseModal = jest.fn(); |
| 57 | +const mockOnSubmit = jest.fn(); |
| 58 | + |
| 59 | +const mockDefaultValue = { |
| 60 | + name: 'Memory Component 1', |
| 61 | + usage: 'Medium', |
| 62 | + memory_type: 'DDR4', |
| 63 | + data_rate: 3200, |
| 64 | + width: 64, |
| 65 | +}; |
| 66 | + |
| 67 | +describe('MemoryModal Component', () => { |
| 68 | + beforeEach(() => { |
| 69 | + render( |
| 70 | + <MemoryModal |
| 71 | + closeModal={mockCloseModal} |
| 72 | + onSubmit={mockOnSubmit} |
| 73 | + defaultValue={mockDefaultValue} |
| 74 | + /> |
| 75 | + ); |
| 76 | + }); |
| 77 | + |
| 78 | + it('renders the modal window with correct title', () => { |
| 79 | + expect(screen.getByText('Memory Component 1')).toBeInTheDocument(); |
| 80 | + }); |
| 81 | + |
| 82 | + it('renders the data rate input field with correct default value', () => { |
| 83 | + const dataRateInput = screen.getByLabelText('Data Rate'); |
| 84 | + expect(dataRateInput).toHaveValue(3200); |
| 85 | + }); |
| 86 | + |
| 87 | + it('renders the width input field with correct default value', () => { |
| 88 | + const widthInput = screen.getByLabelText('Width'); |
| 89 | + expect(widthInput).toHaveValue(64); |
| 90 | + }); |
| 91 | + |
| 92 | + it('calls closeModal when the modal is closed', () => { |
| 93 | + const closeButton = screen.getByText('Close'); |
| 94 | + fireEvent.click(closeButton); |
| 95 | + expect(mockCloseModal).toHaveBeenCalled(); |
| 96 | + }); |
| 97 | + |
| 98 | + it('calls onSubmit when the form is submitted', () => { |
| 99 | + const submitButton = screen.getByText('Submit'); |
| 100 | + fireEvent.click(submitButton); |
| 101 | + expect(mockOnSubmit).toHaveBeenCalled(); |
| 102 | + }); |
| 103 | + |
| 104 | + it('renders the modal window', () => { |
| 105 | + const modal = screen.getByTestId('modal'); |
| 106 | + expect(modal).toBeInTheDocument(); |
| 107 | + }); |
| 108 | +}); |
0 commit comments