import React, { useState } from 'react';
import { render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { ModalOverlay } from './ModalOverlay';
const ModalComponent: React.FC = () => {
const [showModal, setShowModal] = useState(false);
return showModal ? (
setShowModal(false)}>
Modal Content
) : (
);
};
describe('ModalOverlay', () => {
it('renders correctly', () => {
render( {}}>Hello);
expect(screen.getByText('Hello')).toBeInTheDocument();
});
it('should open and close the modal', async () => {
render();
// Should not show by default
expect(screen.queryByText('Modal Content')).not.toBeInTheDocument();
// Should show after clicking button
await userEvent.click(screen.getByTestId('button'));
await screen.findByText('Modal Content');
// Should hide after clicking wrapper
await userEvent.click(screen.getByTestId('modal'));
await waitFor(() => expect(screen.queryByText('Modal Content')).not.toBeInTheDocument());
});
});