|
| 1 | +import { cleanup, fireEvent, render, screen, waitFor } from '@/core/test-utils'; |
| 2 | + |
| 3 | +import { |
| 4 | + ForgotPasswordForm, |
| 5 | + type ForgotPasswordFormProps, |
| 6 | +} from './forgot-password-form'; |
| 7 | + |
| 8 | +afterEach(cleanup); |
| 9 | + |
| 10 | +const onSubmitMock: jest.Mock<ForgotPasswordFormProps['onSubmit']> = jest.fn(); |
| 11 | + |
| 12 | +describe('ForgotPasswordForm', () => { |
| 13 | + const SEND_EMAIL_BUTTON = 'send-email-button'; |
| 14 | + const EMAIL_INPUT = 'email-input'; |
| 15 | + |
| 16 | + it('renders correctly', async () => { |
| 17 | + render(<ForgotPasswordForm onSubmit={onSubmitMock} />); |
| 18 | + expect(await screen.findByText(/Forgot your password?/i)).toBeOnTheScreen(); |
| 19 | + }); |
| 20 | + |
| 21 | + it('should display error when email is empty', async () => { |
| 22 | + render(<ForgotPasswordForm onSubmit={onSubmitMock} />); |
| 23 | + const button = screen.getByTestId(SEND_EMAIL_BUTTON); |
| 24 | + fireEvent.press(button); |
| 25 | + expect(await screen.findByText(/Required/i)).toBeOnTheScreen(); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should display error when email is invalid', async () => { |
| 29 | + render(<ForgotPasswordForm onSubmit={onSubmitMock} />); |
| 30 | + const emailInput = screen.getByTestId(EMAIL_INPUT); |
| 31 | + const button = screen.getByTestId(SEND_EMAIL_BUTTON); |
| 32 | + |
| 33 | + fireEvent.changeText(emailInput, 'invalid-email'); |
| 34 | + fireEvent.press(button); |
| 35 | + |
| 36 | + expect(await screen.findByText(/Invalid email format/i)).toBeOnTheScreen(); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should call onSubmit with correct values when email is valid', async () => { |
| 40 | + render(<ForgotPasswordForm onSubmit={onSubmitMock} />); |
| 41 | + const emailInput = screen.getByTestId(EMAIL_INPUT); |
| 42 | + const button = screen.getByTestId(SEND_EMAIL_BUTTON); |
| 43 | + |
| 44 | + fireEvent.changeText(emailInput, '[email protected]'); |
| 45 | + fireEvent.press(button); |
| 46 | + |
| 47 | + await waitFor(() => { |
| 48 | + expect(onSubmitMock).toHaveBeenCalledTimes(1); |
| 49 | + }); |
| 50 | + |
| 51 | + expect(onSubmitMock).toHaveBeenCalledWith({ |
| 52 | + |
| 53 | + }); |
| 54 | + }); |
| 55 | +}); |
0 commit comments