|
| 1 | +import { render, fireEvent, waitFor } from '@testing-library/react-native'; |
1 | 2 | import UserLogin from '@/components/Login/UserLogin'; |
2 | | -import { fireEvent, render, waitFor } from '@testing-library/react-native'; |
| 3 | +import { router } from 'expo-router'; |
| 4 | +import AsyncStorage from '@react-native-async-storage/async-storage'; |
3 | 5 |
|
4 | | -describe('<UserLogin />', () => { |
5 | | - const onSubmitMock = jest.fn(); |
| 6 | +jest.mock('expo-router', () => ({ |
| 7 | + router: { |
| 8 | + push: jest.fn(), |
| 9 | + }, |
| 10 | +})); |
| 11 | + |
| 12 | +jest.mock('@react-native-async-storage/async-storage', () => ({ |
| 13 | + getItem: jest.fn(() => Promise.resolve('Test Organization')), |
| 14 | +})); |
| 15 | + |
| 16 | +describe('UserLogin Component', () => { |
| 17 | + const mockOnSubmit = jest.fn().mockResolvedValue({}); |
6 | 18 |
|
7 | 19 | beforeEach(() => { |
8 | 20 | jest.clearAllMocks(); |
9 | 21 | }); |
10 | 22 |
|
11 | | - test('renders text correctly', () => { |
12 | | - const { getByText } = render(<UserLogin onSubmit={onSubmitMock} />); |
| 23 | + it('renders correctly and fetches organization name from AsyncStorage', async () => { |
| 24 | + const { getByText } = render(<UserLogin onSubmit={mockOnSubmit} />); |
13 | 25 |
|
14 | | - getByText('Welcome to your_organization_name'); |
15 | | - getByText('Switch your organization'); |
16 | | - getByText('Sign In'); |
17 | | - getByText('Forgot Password?'); |
18 | | - getByText('Remember me.'); |
| 26 | + await waitFor(() => expect(AsyncStorage.getItem).toHaveBeenCalledWith('orgName')); |
| 27 | + expect(getByText('Welcome to Test Organization')).toBeTruthy(); |
19 | 28 | }); |
20 | 29 |
|
21 | | - test('displays error messages when validation fails', async () => { |
22 | | - const { getByText, getByPlaceholderText } = render(<UserLogin onSubmit={onSubmitMock} />); |
| 30 | + it('submits form successfully when fields are valid', async () => { |
| 31 | + const { getByPlaceholderText, getByText } = render(<UserLogin onSubmit={mockOnSubmit} />); |
23 | 32 |
|
| 33 | + fireEvent.changeText(getByPlaceholderText('Email'), '[email protected]'); |
| 34 | + fireEvent.changeText(getByPlaceholderText('Password'), 'ValidPassword'); |
24 | 35 | fireEvent.press(getByText('Sign In')); |
25 | 36 |
|
26 | | - await waitFor(() => { |
27 | | - getByText('Please provide your email address'); |
28 | | - getByText('Please provide a password'); |
29 | | - }); |
30 | | - }); |
| 37 | + await waitFor(() => expect(mockOnSubmit).toHaveBeenCalledWith({ |
| 38 | + |
| 39 | + password: 'ValidPassword', |
| 40 | + })); |
31 | 41 |
|
32 | | - test('calls onSubmit with correct values', async () => { |
33 | | - const { getByPlaceholderText, getByText } = render(<UserLogin onSubmit={onSubmitMock} />); |
| 42 | + expect(mockOnSubmit).toHaveBeenCalledTimes(1); |
| 43 | + }); |
34 | 44 |
|
35 | | - fireEvent.changeText(getByPlaceholderText('Email'), '[email protected]'); |
36 | | - fireEvent.changeText(getByPlaceholderText('Password'), 'password123'); |
| 45 | + it('shows validation error messages for empty fields', async () => { |
| 46 | + const { getByText } = render(<UserLogin onSubmit={mockOnSubmit} />); |
37 | 47 |
|
38 | 48 | fireEvent.press(getByText('Sign In')); |
39 | 49 |
|
40 | 50 | await waitFor(() => { |
41 | | - expect(onSubmitMock).toHaveBeenCalledWith({ |
42 | | - |
43 | | - password: 'password123', |
44 | | - }); |
45 | | - expect(onSubmitMock).toHaveBeenCalledTimes(1); |
| 51 | + expect(getByText('Email is required')).toBeTruthy(); |
| 52 | + expect(getByText('Password is required')).toBeTruthy(); |
46 | 53 | }); |
| 54 | + |
| 55 | + expect(mockOnSubmit).not.toHaveBeenCalled(); |
| 56 | + }); |
| 57 | + |
| 58 | + it('navigates to reset password when "Forgot Password?" is pressed', () => { |
| 59 | + const { getByText } = render(<UserLogin onSubmit={mockOnSubmit} />); |
| 60 | + |
| 61 | + fireEvent.press(getByText('Forgot Password?')); |
| 62 | + expect(router.push).toHaveBeenCalledWith('/auth/forgot-password'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('toggles password visibility when eye icon is pressed', () => { |
| 66 | + const { getByPlaceholderText, getByTestId } = render(<UserLogin onSubmit={mockOnSubmit} />); |
| 67 | + |
| 68 | + const passwordInput = getByPlaceholderText('Password'); |
| 69 | + const toggleIcon = getByTestId('password-toggle'); |
| 70 | + |
| 71 | + expect(passwordInput.props.secureTextEntry).toBe(true); |
| 72 | + |
| 73 | + fireEvent.press(toggleIcon); |
| 74 | + expect(passwordInput.props.secureTextEntry).toBe(false); |
| 75 | + |
| 76 | + fireEvent.press(toggleIcon); |
| 77 | + expect(passwordInput.props.secureTextEntry).toBe(true); |
47 | 78 | }); |
48 | 79 | }); |
0 commit comments