/** * Login + Register (app/(auth)/login.tsx, register.tsx). * * AuthContext is the mocked one (tests/mocks/auth.ts) so loginWithToken/register * are spies; the tRPC mutations go through the fetch mock and are asserted with * expectProcedureCalled. * * login flow: mobile -> (sendOtp) -> otp -> (verifyOtp) -> loginWithToken. */ jest.mock('expo-router', () => require('../mocks/router').expoRouterMock()) import React from 'react' import { Alert, ActivityIndicator, TextInput } from 'react-native' import { fireEvent, screen, waitFor } from '@testing-library/react-native' import Login from '@/app/(auth)/login' import Register from '@/app/(auth)/register' import AuthLayout from '@/app/(auth)/_layout' import { resetStorage, setupApiMocks } from '../mocks/api' import { authMock, resetAuthMock } from '../mocks/auth' import { expectProcedureCalled } from '../mocks/procedures' import { routerMock } from '../mocks/router' import { renderWithProviders } from '../helpers/render' const OTP_SUCCESS = { success: true, message: 'OTP sent' } const VERIFY_SUCCESS = { success: true, token: 'tok', user: { id: 1, mobile: '9876543210' } } const LOGIN_SUCCESS = { data: { token: 'tok', user: { id: 1 } } } async function renderLogin(extraProcedures: Record = {}) { const api = setupApiMocks({ procedures: { 'user.auth.sendOtp': OTP_SUCCESS, 'user.auth.verifyOtp': VERIFY_SUCCESS, 'user.auth.login': LOGIN_SUCCESS, ...extraProcedures, }, }) renderWithProviders(, { centralStores: false }) await waitFor(() => expect(screen.getByText('Welcome')).toBeVisible()) return api } describe('Login', () => { beforeEach(async () => { resetAuthMock() await resetStorage() jest.spyOn(Alert, 'alert').mockImplementation(() => {}) }) it('renders the mobile step with the submit button', async () => { await renderLogin() expect(screen.getByTestId('user-login-mobile')).toBeVisible() expect(screen.getByTestId('user-login-submit')).toBeVisible() }) it('rejects an invalid mobile number without calling the API', async () => { const api = await renderLogin() fireEvent.changeText(screen.getByTestId('user-login-mobile'), '12345') fireEvent.press(screen.getByTestId('user-login-submit')) await waitFor(() => expect( screen.getByText('Please enter a valid 10-digit mobile number') ).toBeVisible() ) expect( api.fetchMock.mock.calls.some(([url]) => String(url).includes('user.auth.sendOtp') ) ).toBe(false) }) it('sends an OTP and moves to the OTP step', async () => { const api = await renderLogin() fireEvent.changeText(screen.getByTestId('user-login-mobile'), '9876543210') fireEvent.press(screen.getByTestId('user-login-submit')) await waitFor(() => expectProcedureCalled(api.fetchMock, 'user.auth.sendOtp')) expect(await screen.findByText('Enter 4-digit OTP')).toBeVisible() }) it('verifies the OTP and hands the token to the auth context', async () => { const api = await renderLogin() fireEvent.changeText(screen.getByTestId('user-login-mobile'), '9876543210') fireEvent.press(screen.getByTestId('user-login-submit')) await waitFor(() => expectProcedureCalled(api.fetchMock, 'user.auth.sendOtp')) await screen.findByText('Enter 4-digit OTP') // The four OTP cells are the only raw TextInputs on this step. const otpInputs = screen.UNSAFE_getAllByType(TextInput) expect(otpInputs).toHaveLength(4) '1234'.split('').forEach((digit, index) => { fireEvent.changeText(otpInputs[index], digit) }) fireEvent.press(screen.getByTestId('user-login-submit')) await waitFor(() => expectProcedureCalled(api.fetchMock, 'user.auth.verifyOtp')) await waitFor(() => expect(authMock.loginWithToken).toHaveBeenCalledWith('tok', VERIFY_SUCCESS.user) ) }) it('logs in with identifier + password via the password path', async () => { const api = await renderLogin() fireEvent.press(screen.getByTestId('user-login-other-ways')) await waitFor(() => expect(screen.getByTestId('user-login-identifier')).toBeVisible()) fireEvent.changeText(screen.getByTestId('user-login-identifier'), 'me@example.com') fireEvent.changeText(screen.getByTestId('user-login-password'), 'secret123') fireEvent.press(screen.getByTestId('user-login-submit')) await waitFor(() => expectProcedureCalled(api.fetchMock, 'user.auth.login')) await waitFor(() => expect(authMock.loginWithToken).toHaveBeenCalledWith('tok', LOGIN_SUCCESS.data.user) ) }) it('opens the terms dialog from the terms link', async () => { await renderLogin() fireEvent.press(screen.getByText('Terms and Conditions')) await waitFor(() => expect(screen.getByText('Terms & Conditions')).toBeVisible()) }) }) describe('Register', () => { beforeEach(async () => { resetAuthMock() await resetStorage() }) it('renders the registration form and links to sign in', async () => { setupApiMocks() renderWithProviders(, { centralStores: false }) // "Create Account" is both the screen title and the form's submit label. expect(screen.getAllByText('Create Account').length).toBeGreaterThan(0) fireEvent.press(screen.getByText('Sign in')) expect(routerMock.push).toHaveBeenCalledWith('/(auth)/login') }) }) describe('Auth layout', () => { beforeEach(() => resetAuthMock()) it('shows a spinner while the session is loading', () => { authMock.isLoading = true setupApiMocks() renderWithProviders(, { centralStores: false }) expect(screen.UNSAFE_queryByType(ActivityIndicator)).not.toBeNull() }) it('renders the stack once loading finishes', () => { authMock.isLoading = false setupApiMocks() renderWithProviders(, { centralStores: false }) expect(screen.UNSAFE_queryByType(ActivityIndicator)).toBeNull() }) })