|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +import { describe, it, expect, vi } from 'vitest'; |
| 10 | +import React from 'react'; |
| 11 | +import { render, screen, waitFor, fireEvent } from '@testing-library/react'; |
| 12 | +import userEvent from '@testing-library/user-event'; |
| 13 | +import { AuthCtx, type AuthContextValue } from '../AuthContext'; |
| 14 | +import { RegisterForm } from '../RegisterForm'; |
| 15 | + |
| 16 | +function createAuthContext(overrides: Partial<AuthContextValue> = {}): AuthContextValue { |
| 17 | + return { |
| 18 | + user: null, |
| 19 | + session: null, |
| 20 | + isAuthenticated: false, |
| 21 | + isLoading: false, |
| 22 | + error: null, |
| 23 | + isPreviewMode: false, |
| 24 | + previewMode: null, |
| 25 | + signIn: vi.fn().mockResolvedValue(undefined), |
| 26 | + signUp: vi.fn().mockResolvedValue(undefined), |
| 27 | + signOut: vi.fn().mockResolvedValue(undefined), |
| 28 | + updateUser: vi.fn().mockResolvedValue(undefined), |
| 29 | + forgotPassword: vi.fn().mockResolvedValue(undefined), |
| 30 | + resetPassword: vi.fn().mockResolvedValue(undefined), |
| 31 | + ...overrides, |
| 32 | + }; |
| 33 | +} |
| 34 | + |
| 35 | +function renderWithAuth(ui: React.ReactElement, authOverrides: Partial<AuthContextValue> = {}) { |
| 36 | + const ctx = createAuthContext(authOverrides); |
| 37 | + return { |
| 38 | + ctx, |
| 39 | + ...render(<AuthCtx.Provider value={ctx}>{ui}</AuthCtx.Provider>), |
| 40 | + }; |
| 41 | +} |
| 42 | + |
| 43 | +describe('RegisterForm', () => { |
| 44 | + it('renders with default title', () => { |
| 45 | + renderWithAuth(<RegisterForm />); |
| 46 | + expect(screen.getByText('Create an account')).toBeTruthy(); |
| 47 | + }); |
| 48 | + |
| 49 | + it('renders all form fields', () => { |
| 50 | + renderWithAuth(<RegisterForm />); |
| 51 | + expect(screen.getByLabelText('Name')).toBeTruthy(); |
| 52 | + expect(screen.getByLabelText('Email')).toBeTruthy(); |
| 53 | + expect(screen.getByLabelText('Password')).toBeTruthy(); |
| 54 | + expect(screen.getByLabelText('Confirm Password')).toBeTruthy(); |
| 55 | + }); |
| 56 | + |
| 57 | + it('shows loading state', () => { |
| 58 | + renderWithAuth(<RegisterForm />, { isLoading: true }); |
| 59 | + expect(screen.getByRole('button', { name: 'Creating account...' })).toBeTruthy(); |
| 60 | + }); |
| 61 | + |
| 62 | + it('shows login link', () => { |
| 63 | + renderWithAuth(<RegisterForm loginUrl="/login" />); |
| 64 | + expect(screen.getByText('Sign in')).toBeTruthy(); |
| 65 | + }); |
| 66 | + |
| 67 | + it('shows error when passwords do not match', async () => { |
| 68 | + renderWithAuth(<RegisterForm />); |
| 69 | + const user = userEvent.setup(); |
| 70 | + |
| 71 | + await user.type(screen.getByLabelText('Name'), 'John'); |
| 72 | + await user.type(screen.getByLabelText('Email'), 'john@example.com'); |
| 73 | + await user.type(screen.getByLabelText('Password'), 'password123'); |
| 74 | + await user.type(screen.getByLabelText('Confirm Password'), 'different'); |
| 75 | + await user.click(screen.getByRole('button', { name: 'Create Account' })); |
| 76 | + |
| 77 | + await waitFor(() => { |
| 78 | + expect(screen.getByText('Passwords do not match')).toBeTruthy(); |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + it('shows error when password is too short', async () => { |
| 83 | + renderWithAuth(<RegisterForm />); |
| 84 | + const user = userEvent.setup(); |
| 85 | + |
| 86 | + await user.type(screen.getByLabelText('Name'), 'John'); |
| 87 | + await user.type(screen.getByLabelText('Email'), 'john@example.com'); |
| 88 | + // Type passwords that match but are under 8 chars |
| 89 | + // Use fireEvent to bypass native minLength validation |
| 90 | + const pwField = screen.getByLabelText('Password'); |
| 91 | + const confirmField = screen.getByLabelText('Confirm Password'); |
| 92 | + fireEvent.change(pwField, { target: { value: 'short' } }); |
| 93 | + fireEvent.change(confirmField, { target: { value: 'short' } }); |
| 94 | + fireEvent.submit(screen.getByRole('button', { name: 'Create Account' }).closest('form')!); |
| 95 | + |
| 96 | + await waitFor(() => { |
| 97 | + expect(screen.getByText('Password must be at least 8 characters')).toBeTruthy(); |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + it('calls signUp on valid submission', async () => { |
| 102 | + const onSuccess = vi.fn(); |
| 103 | + const { ctx } = renderWithAuth(<RegisterForm onSuccess={onSuccess} />); |
| 104 | + const user = userEvent.setup(); |
| 105 | + |
| 106 | + await user.type(screen.getByLabelText('Name'), 'John Doe'); |
| 107 | + await user.type(screen.getByLabelText('Email'), 'john@example.com'); |
| 108 | + await user.type(screen.getByLabelText('Password'), 'password123'); |
| 109 | + await user.type(screen.getByLabelText('Confirm Password'), 'password123'); |
| 110 | + await user.click(screen.getByRole('button', { name: 'Create Account' })); |
| 111 | + |
| 112 | + await waitFor(() => { |
| 113 | + expect(ctx.signUp).toHaveBeenCalledWith('John Doe', 'john@example.com', 'password123'); |
| 114 | + }); |
| 115 | + expect(onSuccess).toHaveBeenCalled(); |
| 116 | + }); |
| 117 | + |
| 118 | + it('shows error on signUp failure', async () => { |
| 119 | + const onError = vi.fn(); |
| 120 | + const signUp = vi.fn().mockRejectedValue(new Error('Email taken')); |
| 121 | + renderWithAuth(<RegisterForm onError={onError} />, { signUp }); |
| 122 | + const user = userEvent.setup(); |
| 123 | + |
| 124 | + await user.type(screen.getByLabelText('Name'), 'John'); |
| 125 | + await user.type(screen.getByLabelText('Email'), 'john@example.com'); |
| 126 | + await user.type(screen.getByLabelText('Password'), 'password123'); |
| 127 | + await user.type(screen.getByLabelText('Confirm Password'), 'password123'); |
| 128 | + await user.click(screen.getByRole('button', { name: 'Create Account' })); |
| 129 | + |
| 130 | + await waitFor(() => { |
| 131 | + expect(screen.getByText('Email taken')).toBeTruthy(); |
| 132 | + }); |
| 133 | + expect(onError).toHaveBeenCalled(); |
| 134 | + }); |
| 135 | +}); |
0 commit comments