|
| 1 | +import React from 'react'; |
| 2 | +import { render, fireEvent, waitFor } from '@testing-library/react-native'; |
| 3 | +import { Provider } from 'react-redux'; |
| 4 | +import configureStore from 'redux-mock-store'; |
| 5 | +import SigninScreen from '../../screens/SigninScreen'; |
| 6 | +import { startLoginWithEmail } from '../../actions/userActions'; |
| 7 | + |
| 8 | +jest.mock('../../actions/userActions'); |
| 9 | + |
| 10 | +const mockStore = configureStore([]); |
| 11 | + |
| 12 | +describe('SigninScreen Integration Test', () => { |
| 13 | + let store; |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + store = mockStore({ |
| 17 | + user: { |
| 18 | + isAuthenticated: false, |
| 19 | + error: null, |
| 20 | + }, |
| 21 | + }); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should update email and password fields', () => { |
| 25 | + const { getByPlaceholderText } = render( |
| 26 | + <Provider store={store}> |
| 27 | + <SigninScreen /> |
| 28 | + </Provider> |
| 29 | + ); |
| 30 | + |
| 31 | + const emailInput = getByPlaceholderText('Email'); |
| 32 | + const passwordInput = getByPlaceholderText('Password'); |
| 33 | + |
| 34 | + fireEvent.changeText(emailInput, '[email protected]'); |
| 35 | + fireEvent.changeText(passwordInput, 'password123'); |
| 36 | + |
| 37 | + expect(emailInput.props.value).toBe('[email protected]'); |
| 38 | + expect(passwordInput.props.value).toBe('password123'); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should display error message for invalid email', () => { |
| 42 | + const { getByPlaceholderText, getByText } = render( |
| 43 | + <Provider store={store}> |
| 44 | + <SigninScreen /> |
| 45 | + </Provider> |
| 46 | + ); |
| 47 | + |
| 48 | + const emailInput = getByPlaceholderText('Email'); |
| 49 | + const signInButton = getByText('Sign In'); |
| 50 | + |
| 51 | + fireEvent.changeText(emailInput, 'invalid-email'); |
| 52 | + fireEvent.press(signInButton); |
| 53 | + |
| 54 | + const errorMessage = getByText('Enter valid email.'); |
| 55 | + expect(errorMessage).toBeTruthy(); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should dispatch startLoginWithEmail action on valid input', async () => { |
| 59 | + const { getByPlaceholderText, getByText } = render( |
| 60 | + <Provider store={store}> |
| 61 | + <SigninScreen /> |
| 62 | + </Provider> |
| 63 | + ); |
| 64 | + |
| 65 | + const emailInput = getByPlaceholderText('Email'); |
| 66 | + const passwordInput = getByPlaceholderText('Password'); |
| 67 | + const signInButton = getByText('Sign In'); |
| 68 | + |
| 69 | + fireEvent.changeText(emailInput, '[email protected]'); |
| 70 | + fireEvent.changeText(passwordInput, 'password123'); |
| 71 | + fireEvent.press(signInButton); |
| 72 | + |
| 73 | + await waitFor(() => { |
| 74 | + expect(startLoginWithEmail).toHaveBeenCalledWith('[email protected]', 'password123'); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should navigate to ForgotPasswordScreen on button press', () => { |
| 79 | + const navigation = { navigate: jest.fn() }; |
| 80 | + |
| 81 | + const { getByText } = render( |
| 82 | + <Provider store={store}> |
| 83 | + <SigninScreen navigation={navigation} /> |
| 84 | + </Provider> |
| 85 | + ); |
| 86 | + |
| 87 | + const forgotButton = getByText('Forgot your Username/Password ?'); |
| 88 | + fireEvent.press(forgotButton); |
| 89 | + |
| 90 | + expect(navigation.navigate).toHaveBeenCalledWith('Forgot'); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should toggle lock status on icon press', () => { |
| 94 | + const { getByPlaceholderText, getByTestId } = render( |
| 95 | + <Provider store={store}> |
| 96 | + <SigninScreen /> |
| 97 | + </Provider> |
| 98 | + ); |
| 99 | + |
| 100 | + const passwordInput = getByPlaceholderText('Password'); |
| 101 | + const lockIcon = getByTestId('lock-icon'); |
| 102 | + |
| 103 | + fireEvent.press(lockIcon); |
| 104 | + expect(passwordInput.props.secureTextEntry).toBe(false); |
| 105 | + |
| 106 | + fireEvent.press(lockIcon); |
| 107 | + expect(passwordInput.props.secureTextEntry).toBe(true); |
| 108 | + }); |
| 109 | +}); |
0 commit comments