refactor: simplify contact form and improve UI elements

This commit is contained in:
2025-02-22 21:53:57 +01:00
parent a1c4adc4b1
commit 82d5ab5fcf
2 changed files with 111 additions and 53 deletions

View File

@@ -1,4 +1,4 @@
import { render, screen, fireEvent } from '@testing-library/react';
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
import Contact from '@/app/components/Contact';
import '@testing-library/jest-dom';
@@ -10,20 +10,34 @@ global.fetch = jest.fn(() =>
) as jest.Mock;
describe('Contact', () => {
beforeAll(() => {
jest.useFakeTimers('modern');
});
afterAll(() => {
jest.useRealTimers();
});
it('renders the contact form', () => {
render(<Contact />);
expect(screen.getByPlaceholderText('Name')).toBeInTheDocument();
expect(screen.getByPlaceholderText('Email')).toBeInTheDocument();
expect(screen.getByPlaceholderText('Message')).toBeInTheDocument();
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 />);
fireEvent.change(screen.getByPlaceholderText('Name'), { target: { value: 'John Doe' } });
fireEvent.change(screen.getByPlaceholderText('Email'), { target: { value: 'john@example.com' } });
fireEvent.change(screen.getByPlaceholderText('Message'), { target: { value: 'Hello!' } });
fireEvent.click(screen.getByText('Send'));
expect(await screen.findByText('Email sent')).toBeInTheDocument();
// 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());
});
});