|
| 1 | +import React from 'react'; |
| 2 | +import { render, screen, waitFor } from '@testing-library/react'; |
| 3 | +import userEvent from '@testing-library/user-event'; |
| 4 | +import { SignIn } from '../../../components/auth/SignIn'; |
| 5 | + |
| 6 | +jest.mock('../../../contexts/AuthContext', () => ({ |
| 7 | + useAuth: () => ({ |
| 8 | + signIn: mockSignIn, |
| 9 | + }), |
| 10 | +})); |
| 11 | + |
| 12 | +jest.mock('../../../services/api', () => ({ |
| 13 | + apiService: { |
| 14 | + forgotPassword: jest.fn(), |
| 15 | + }, |
| 16 | +})); |
| 17 | + |
| 18 | +const mockSignIn = jest.fn(); |
| 19 | +const mockOnSignInSuccess = jest.fn(); |
| 20 | +const mockOnSwitchToSignUp = jest.fn(); |
| 21 | + |
| 22 | +describe('SignIn component', () => { |
| 23 | + beforeEach(() => { |
| 24 | + jest.clearAllMocks(); |
| 25 | + }); |
| 26 | + |
| 27 | + it('renders username, password fields and sign in button', () => { |
| 28 | + render( |
| 29 | + <SignIn |
| 30 | + onSignInSuccess={mockOnSignInSuccess} |
| 31 | + onSwitchToSignUp={mockOnSwitchToSignUp} |
| 32 | + />, |
| 33 | + ); |
| 34 | + |
| 35 | + expect(screen.getByLabelText(/Username or Email/i)).toBeInTheDocument(); |
| 36 | + expect(screen.getByLabelText(/Password/i)).toBeInTheDocument(); |
| 37 | + expect(screen.getByRole('button', { name: /Sign In/i })).toBeInTheDocument(); |
| 38 | + }); |
| 39 | + |
| 40 | + it('shows validation error when submitting with empty fields', async () => { |
| 41 | + render( |
| 42 | + <SignIn |
| 43 | + onSignInSuccess={mockOnSignInSuccess} |
| 44 | + onSwitchToSignUp={mockOnSwitchToSignUp} |
| 45 | + />, |
| 46 | + ); |
| 47 | + |
| 48 | + await userEvent.click(screen.getByRole('button', { name: /Sign In/i })); |
| 49 | + |
| 50 | + expect( |
| 51 | + await screen.findByText(/Please fill in all fields/i), |
| 52 | + ).toBeInTheDocument(); |
| 53 | + expect(mockSignIn).not.toHaveBeenCalled(); |
| 54 | + }); |
| 55 | + |
| 56 | + it('calls signIn and onSignInSuccess on successful submit', async () => { |
| 57 | + const fakeUser = { id: '1', emailVerified: true }; |
| 58 | + mockSignIn.mockResolvedValueOnce(fakeUser); |
| 59 | + |
| 60 | + render( |
| 61 | + <SignIn |
| 62 | + onSignInSuccess={mockOnSignInSuccess} |
| 63 | + onSwitchToSignUp={mockOnSwitchToSignUp} |
| 64 | + />, |
| 65 | + ); |
| 66 | + |
| 67 | + await userEvent.type( |
| 68 | + screen.getByLabelText(/Username or Email/i), |
| 69 | + 'john@example.com', |
| 70 | + ); |
| 71 | + await userEvent.type(screen.getByLabelText(/Password/i), 'password123!'); |
| 72 | + await userEvent.click(screen.getByRole('button', { name: /Sign In/i })); |
| 73 | + |
| 74 | + await waitFor(() => { |
| 75 | + expect(mockSignIn).toHaveBeenCalledWith( |
| 76 | + 'john@example.com', |
| 77 | + 'password123!', |
| 78 | + ); |
| 79 | + }); |
| 80 | + expect(mockOnSignInSuccess).toHaveBeenCalledWith(fakeUser); |
| 81 | + }); |
| 82 | + |
| 83 | + it('shows error when signIn throws', async () => { |
| 84 | + mockSignIn.mockRejectedValueOnce(new Error('Invalid credentials')); |
| 85 | + |
| 86 | + render( |
| 87 | + <SignIn |
| 88 | + onSignInSuccess={mockOnSignInSuccess} |
| 89 | + onSwitchToSignUp={mockOnSwitchToSignUp} |
| 90 | + />, |
| 91 | + ); |
| 92 | + |
| 93 | + await userEvent.type( |
| 94 | + screen.getByLabelText(/Username or Email/i), |
| 95 | + 'john@example.com', |
| 96 | + ); |
| 97 | + await userEvent.type(screen.getByLabelText(/Password/i), 'wrong'); |
| 98 | + await userEvent.click(screen.getByRole('button', { name: /Sign In/i })); |
| 99 | + |
| 100 | + expect( |
| 101 | + await screen.findByText(/Invalid credentials/i), |
| 102 | + ).toBeInTheDocument(); |
| 103 | + }); |
| 104 | + |
| 105 | + it('opens forgot password modal and validates email', async () => { |
| 106 | + const { apiService } = jest.requireMock('../../../services/api') as { |
| 107 | + apiService: { forgotPassword: jest.Mock }; |
| 108 | + }; |
| 109 | + |
| 110 | + render( |
| 111 | + <SignIn |
| 112 | + onSignInSuccess={mockOnSignInSuccess} |
| 113 | + onSwitchToSignUp={mockOnSwitchToSignUp} |
| 114 | + />, |
| 115 | + ); |
| 116 | + |
| 117 | + await userEvent.click( |
| 118 | + screen.getByRole('button', { name: /Forgot your password\?/i }), |
| 119 | + ); |
| 120 | + |
| 121 | + const emailInput = await screen.findByLabelText(/Registered Email Address/i); |
| 122 | + const submitButton = screen.getByRole('button', { |
| 123 | + name: /Submit Request/i, |
| 124 | + }); |
| 125 | + |
| 126 | + // Invalid email |
| 127 | + await userEvent.type(emailInput, 'not-an-email'); |
| 128 | + await userEvent.click(submitButton); |
| 129 | + expect( |
| 130 | + await screen.findByText(/Please enter a valid email address/i), |
| 131 | + ).toBeInTheDocument(); |
| 132 | + |
| 133 | + // Valid email -> API is called |
| 134 | + apiService.forgotPassword.mockResolvedValueOnce({ |
| 135 | + message: 'Reset email sent', |
| 136 | + data: {}, |
| 137 | + }); |
| 138 | + |
| 139 | + await userEvent.clear(emailInput); |
| 140 | + await userEvent.type(emailInput, 'user@example.com'); |
| 141 | + await userEvent.click(submitButton); |
| 142 | + |
| 143 | + await waitFor(() => { |
| 144 | + expect(apiService.forgotPassword).toHaveBeenCalledWith('user@example.com'); |
| 145 | + }); |
| 146 | + }); |
| 147 | +}); |
| 148 | + |
0 commit comments