Skip to content

Commit 64152d8

Browse files
authored
React/Jest tests for PeripheralModal.js (#217)
1 parent 13801a6 commit 64152d8

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import React from 'react';
2+
import { render, screen, fireEvent } from '@testing-library/react';
3+
import '@testing-library/jest-dom';
4+
import PeripheralsModal from '../../components/ModalWindows/PeripheralsModal';
5+
6+
jest.mock('../../components/ModalWindows/ModalWindow', () => ({ title, closeModal, fields, onSubmit }) => (
7+
<div data-testid="modal">
8+
<h1>{title}</h1>
9+
<button onClick={closeModal}>Close</button>
10+
{fields.map((field) => (
11+
<div key={field.id}>
12+
<label>{field.text}</label>
13+
<input defaultValue={field.value} aria-label={field.text} />
14+
</div>
15+
))}
16+
<button onClick={onSubmit}>Submit</button>
17+
</div>
18+
));
19+
20+
const mockCloseModal = jest.fn();
21+
const mockOnSubmit = jest.fn();
22+
23+
const mockDefaultValue = {
24+
usage: ['Low', 'Medium', 'High'],
25+
performance: ['Good', 'Better', 'Best'],
26+
performance_id: 'perf_1',
27+
io_used: true,
28+
data: [
29+
{
30+
data: {
31+
usage: 'High',
32+
name: 'Peripheral 1',
33+
io_used: 5,
34+
performance: 'Best',
35+
},
36+
},
37+
],
38+
};
39+
40+
describe('PeripheralsModal Component', () => {
41+
beforeEach(() => {
42+
render(
43+
<PeripheralsModal
44+
closeModal={mockCloseModal}
45+
onSubmit={mockOnSubmit}
46+
defaultValue={mockDefaultValue}
47+
index={0}
48+
/>
49+
);
50+
});
51+
52+
it('renders the modal window with correct title', () => {
53+
expect(screen.getByText('Peripheral 1')).toBeInTheDocument();
54+
});
55+
56+
it('calls closeModal when the modal is closed', () => {
57+
const closeButton = screen.getByText('Close');
58+
fireEvent.click(closeButton);
59+
expect(mockCloseModal).toHaveBeenCalled();
60+
});
61+
62+
it('calls onSubmit when the form is submitted', () => {
63+
const submitButton = screen.getByText('Submit');
64+
fireEvent.click(submitButton);
65+
expect(mockOnSubmit).toHaveBeenCalled();
66+
});
67+
});

0 commit comments

Comments
 (0)