Files
portfolio/app/__tests__/components/Contact.test.tsx
denshooter 9088a7cd32 Dev (#44)
* 🚀 refactor: simplify deployment process in workflow file

* 🚀 chore: add IMAGE_NAME to GITHUB_ENV for deployment workflow

*  chore: simplify deployment logging in workflow file

* 🚀 fix: correct container name in deployment script logic

* 🚀 refactor: rename job and streamline deployment steps

* Update README.md

*  fix: prevent multiple form submissions in Contact component

*  feat: honeypot and timestamp checks to form submission

*  refactor: simplify contact form and improve UI elements
2025-02-22 22:29:23 +01:00

43 lines
1.5 KiB
TypeScript

import { render, screen, fireEvent, waitFor } from '@testing-library/react';
import Contact from '@/app/components/Contact';
import '@testing-library/jest-dom';
// Mock the fetch function
global.fetch = jest.fn(() =>
Promise.resolve({
json: () => Promise.resolve({ message: 'Email sent' }),
})
) as jest.Mock;
describe('Contact', () => {
beforeAll(() => {
jest.useFakeTimers('modern');
});
afterAll(() => {
jest.useRealTimers();
});
it('renders the contact form', () => {
render(<Contact />);
expect(screen.getByPlaceholderText('Your Name')).toBeInTheDocument();
expect(screen.getByPlaceholderText('you@example.com')).toBeInTheDocument();
expect(screen.getByPlaceholderText('Your Message...')).toBeInTheDocument();
expect(screen.getByLabelText('I accept the privacy policy.')).toBeInTheDocument();
});
it('submits the form', async () => {
render(<Contact />);
// Fast forward time to ensure the timestamp check passes
jest.advanceTimersByTime(3000);
fireEvent.change(screen.getByPlaceholderText('Your Name'), { target: { value: 'John Doe' } });
fireEvent.change(screen.getByPlaceholderText('you@example.com'), { target: { value: 'john@example.com' } });
fireEvent.change(screen.getByPlaceholderText('Your Message...'), { target: { value: 'Hello!' } });
fireEvent.click(screen.getByLabelText('I accept the privacy policy.'));
fireEvent.click(screen.getByText('Send Message'));
await waitFor(() => expect(screen.getByText('Email sent')).toBeInTheDocument());
});
});