|
| 1 | +import { fireEvent, render, screen } from '@testing-library/react'; |
| 2 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 3 | +import Register from '../../src/pages/auth/register.tsx'; |
| 4 | + |
| 5 | +const mockSignUp = vi.fn(); |
| 6 | +const mockSetErrors = vi.fn(); |
| 7 | +const mockUseAuth = vi.fn(); |
| 8 | + |
| 9 | +vi.mock('../../src/hooks/use-auth.tsx', () => ({ |
| 10 | + useAuth: () => mockUseAuth(), |
| 11 | +})); |
| 12 | + |
| 13 | +describe('Register page', () => { |
| 14 | + beforeEach(() => { |
| 15 | + mockSignUp.mockReset(); |
| 16 | + mockSetErrors.mockReset(); |
| 17 | + mockUseAuth.mockReturnValue({ |
| 18 | + SignUp: mockSignUp, |
| 19 | + isLoading: false, |
| 20 | + errors: null, |
| 21 | + setErrors: mockSetErrors, |
| 22 | + }); |
| 23 | + }); |
| 24 | + |
| 25 | + it('submits trimmed form data when validation succeeds', async () => { |
| 26 | + render(<Register />); |
| 27 | + |
| 28 | + fireEvent.change(screen.getByLabelText(/First Name/i), { |
| 29 | + target: { name: 'first_name', value: ' Jane ' }, |
| 30 | + }); |
| 31 | + fireEvent.change(screen.getByLabelText(/Last Name/i), { |
| 32 | + target: { name: 'last_name', value: ' Doe ' }, |
| 33 | + }); |
| 34 | + fireEvent.change(screen.getByLabelText(/Email address/i), { |
| 35 | + target: { name: 'email', value: ' [email protected] ' }, |
| 36 | + }); |
| 37 | + fireEvent.change(screen.getByLabelText(/^Password$/i), { |
| 38 | + target: { name: 'password', value: 'password123' }, |
| 39 | + }); |
| 40 | + fireEvent.change(screen.getByLabelText(/Confirm password/i), { |
| 41 | + target: { name: 'confirmPassword', value: 'password123' }, |
| 42 | + }); |
| 43 | + |
| 44 | + fireEvent.click(screen.getByRole('button', { name: /Create account/i })); |
| 45 | + |
| 46 | + expect(mockSetErrors).toHaveBeenCalledWith(null); |
| 47 | + expect(mockSignUp).toHaveBeenCalledWith('Jane', 'Doe', '[email protected]', 'password123'); |
| 48 | + }); |
| 49 | + |
| 50 | + it('prevents submission when passwords do not match', async () => { |
| 51 | + render(<Register />); |
| 52 | + |
| 53 | + fireEvent.change(screen.getByLabelText(/First Name/i), { |
| 54 | + target: { name: 'first_name', value: 'John' }, |
| 55 | + }); |
| 56 | + fireEvent.change(screen.getByLabelText(/Last Name/i), { |
| 57 | + target: { name: 'last_name', value: 'Smith' }, |
| 58 | + }); |
| 59 | + fireEvent.change(screen.getByLabelText(/Email address/i), { |
| 60 | + target: { name: 'email', value: '[email protected]' }, |
| 61 | + }); |
| 62 | + fireEvent.change(screen.getByLabelText(/^Password$/i), { |
| 63 | + target: { name: 'password', value: 'password123' }, |
| 64 | + }); |
| 65 | + fireEvent.change(screen.getByLabelText(/Confirm password/i), { |
| 66 | + target: { name: 'confirmPassword', value: 'password456' }, |
| 67 | + }); |
| 68 | + |
| 69 | + fireEvent.click(screen.getByRole('button', { name: /Create account/i })); |
| 70 | + |
| 71 | + expect(mockSetErrors).toHaveBeenNthCalledWith(1, null); |
| 72 | + expect(mockSetErrors).toHaveBeenNthCalledWith(2, { |
| 73 | + confirmPassword: ['Passwords do not match.'], |
| 74 | + }); |
| 75 | + expect(mockSignUp).not.toHaveBeenCalled(); |
| 76 | + }); |
| 77 | +}); |
0 commit comments