Skip to content

Commit 62ac4fc

Browse files
authored
React/Jest tests for ModalWindow.js (#221)
1 parent 02be836 commit 62ac4fc

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import React from 'react';
2+
import { render, screen, fireEvent } from '@testing-library/react';
3+
import '@testing-library/jest-dom';
4+
import ModalWindow from '../../components/ModalWindows/ModalWindow';
5+
import { FieldType } from '../../utils/common';
6+
7+
const mockCloseModal = jest.fn();
8+
const mockOnSubmit = jest.fn();
9+
10+
const mockFields = [
11+
{
12+
fieldType: FieldType.textarea,
13+
id: 'name',
14+
text: 'Name',
15+
value: 'Test Name',
16+
},
17+
{
18+
fieldType: FieldType.number,
19+
id: 'age',
20+
text: 'Age',
21+
value: 25,
22+
},
23+
{
24+
fieldType: FieldType.float,
25+
id: 'percentage',
26+
text: 'Percentage',
27+
value: 0.5,
28+
step: 0.1,
29+
},
30+
{
31+
fieldType: FieldType.selectClock,
32+
id: 'clock',
33+
text: 'Clock',
34+
values: ['Clock1', 'Clock2'],
35+
value: 'Clock1',
36+
},
37+
];
38+
39+
const mockDefaultValue = {
40+
name: 'Test Name',
41+
age: 25,
42+
percentage: 0.5,
43+
clock: 'Clock1',
44+
};
45+
46+
describe('ModalWindow Component', () => {
47+
beforeEach(() => {
48+
render(
49+
<ModalWindow
50+
title="Test Modal"
51+
defaultValue={mockDefaultValue}
52+
onSubmit={mockOnSubmit}
53+
closeModal={mockCloseModal}
54+
fields={mockFields}
55+
/>
56+
);
57+
});
58+
59+
it('renders the modal with the correct title', () => {
60+
expect(screen.getByText('Test Modal')).toBeInTheDocument();
61+
});
62+
63+
it('renders all input fields with correct default values', () => {
64+
expect(screen.getByDisplayValue('Test Name')).toBeInTheDocument();
65+
expect(screen.getByDisplayValue('25')).toBeInTheDocument();
66+
expect(screen.getByDisplayValue('50.0')).toBeInTheDocument();
67+
expect(screen.getByDisplayValue('Clock1')).toBeInTheDocument();
68+
});
69+
70+
it('calls closeModal when the close button is clicked', () => {
71+
const closeButton = screen.getByRole('button', { name: /cancel/i });
72+
fireEvent.click(closeButton);
73+
expect(mockCloseModal).toHaveBeenCalled();
74+
});
75+
76+
it('calls onSubmit with correct data when the form is submitted', () => {
77+
const submitButton = screen.getByRole('button', { name: /ok/i });
78+
fireEvent.click(submitButton);
79+
expect(mockOnSubmit).toHaveBeenCalledWith(mockDefaultValue);
80+
});
81+
82+
it('updates the name field when changed', () => {
83+
const nameInput = screen.getByDisplayValue('Test Name');
84+
fireEvent.change(nameInput, { target: { value: 'Updated Name' } });
85+
expect(nameInput.value).toBe('Updated Name');
86+
});
87+
88+
it('updates the age field when changed', () => {
89+
const ageInput = screen.getByDisplayValue('25');
90+
fireEvent.change(ageInput, { target: { value: '30' } });
91+
expect(ageInput.value).toBe('30');
92+
});
93+
94+
it('updates the percentage field when changed', () => {
95+
const percentageInput = screen.getByDisplayValue('50.0');
96+
fireEvent.change(percentageInput, { target: { value: '60.0' } });
97+
expect(percentageInput.value).toBe('60.0');
98+
});
99+
100+
it('updates the clock field when changed', () => {
101+
const clockSelect = screen.getByDisplayValue('Clock1');
102+
fireEvent.change(clockSelect, { target: { value: 'Clock2' } });
103+
expect(screen.getByDisplayValue('Clock2')).toBeInTheDocument();
104+
});
105+
});

0 commit comments

Comments
 (0)