|
| 1 | +import { render, screen } from '@testing-library/react'; |
| 2 | +import userEvent from '@testing-library/user-event'; |
| 3 | + |
| 4 | +import { ContactForm } from '../components/contact-form'; |
| 5 | + |
| 6 | +jest.mock('@/shared/services/email-service', () => ({ |
| 7 | + sendEmail: jest.fn(async () => await Promise.resolve(true)) |
| 8 | +})); |
| 9 | + |
| 10 | +jest.mock('@/shared/helpers/notify', () => ({ |
| 11 | + notify: { |
| 12 | + success: jest.fn(), |
| 13 | + error: jest.fn() |
| 14 | + } |
| 15 | +})); |
| 16 | + |
| 17 | +describe('ContactForm', () => { |
| 18 | + it('should validate and submit', async () => { |
| 19 | + render(<ContactForm />); |
| 20 | + const user = userEvent.setup(); |
| 21 | + |
| 22 | + await user.type(screen.getByTestId('email-input'), 'ramin@ramin.com'); |
| 23 | + await user.type(screen.getByTestId('subject-input'), 'this is a subject'); |
| 24 | + await user.type( |
| 25 | + screen.getByTestId('message-input'), |
| 26 | + 'This is a long enough message to pass validation rules.' |
| 27 | + ); |
| 28 | + |
| 29 | + const submitButton = await screen.findByTestId('submit-button'); |
| 30 | + await user.click(submitButton); |
| 31 | + |
| 32 | + expect(true).toBe(true); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should show validation errors', async () => { |
| 36 | + render(<ContactForm />); |
| 37 | + const user = userEvent.setup(); |
| 38 | + |
| 39 | + await user.type(screen.getByTestId('subject-input'), 'short'); |
| 40 | + await user.type(screen.getByTestId('email-input'), 'ramin@ramin'); |
| 41 | + await user.type(screen.getByTestId('message-input'), 'short message'); |
| 42 | + |
| 43 | + const submitButton = await screen.findByTestId('submit-button'); |
| 44 | + await user.click(submitButton); |
| 45 | + |
| 46 | + expect(screen.getByText(/Your email address is invalid!/i)).toBeInTheDocument(); |
| 47 | + expect( |
| 48 | + screen.getByText(/Your subject should be more than 10 characters/i) |
| 49 | + ).toBeInTheDocument(); |
| 50 | + expect( |
| 51 | + screen.getByText(/Your message should be more than 30 characters/i) |
| 52 | + ).toBeInTheDocument(); |
| 53 | + }); |
| 54 | +}); |
0 commit comments