664 lines
25 KiB
TypeScript
664 lines
25 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||
/**
|
||
* LOGIN-01..LOGIN-42 — `app/(auth)/login.tsx`.
|
||
*
|
||
* Auth mutations run through the tRPC procedure mock; `useAuth` is replaced by
|
||
* the deterministic harness context so `loginWithToken` can be asserted.
|
||
*
|
||
* NOTE on LOGIN-02..LOGIN-05: the persisted `otp_sent_time` branch is read once
|
||
* on mount, but the resend countdown is only *rendered* on the OTP step — and
|
||
* reaching that step always restarts the countdown at 120s (the `sendOtp`
|
||
* success handler). The stored-timestamp states are therefore exercised through
|
||
* the countdown UI after the OTP step is reached, which is the closest
|
||
* observable behaviour.
|
||
*/
|
||
import React from 'react'
|
||
import { Alert, Modal } from 'react-native'
|
||
import { act, fireEvent } from '@testing-library/react-native'
|
||
|
||
import Login from '@/app/(auth)/login'
|
||
import { renderWithProviders, flush } from '../helpers/test-utils'
|
||
import { resetAllMocks } from '../helpers/reset'
|
||
import {
|
||
setProcedure,
|
||
lastProcedureInput,
|
||
procedureCallCount,
|
||
procedureMutationCalls,
|
||
} from '../mocks/trpc-client'
|
||
import { __set, __snapshot } from '../mocks/secure-store-mock'
|
||
import { loginWithToken } from '../mocks/auth-context'
|
||
import { AUTH_USER } from '../fixtures'
|
||
|
||
jest.mock('@/src/contexts/AuthContext', () => require('../mocks/auth-context'))
|
||
|
||
const MOBILE = '9876543210'
|
||
|
||
let alertSpy: jest.SpyInstance
|
||
|
||
function renderLogin() {
|
||
return renderWithProviders(<Login />, { auth: false })
|
||
}
|
||
|
||
/**
|
||
* Settle async work while Jest fake timers are installed.
|
||
*
|
||
* React Query's notify manager batches through `queueMicrotask`, which Jest
|
||
* fakes along with the timers: without advancing the 0ms timers the mutation's
|
||
* state updates never commit (and the un-drained queue leaks into later cases).
|
||
*/
|
||
async function settle() {
|
||
for (let round = 0; round < 3; round += 1) {
|
||
// eslint-disable-next-line no-await-in-loop
|
||
await act(async () => {
|
||
jest.advanceTimersByTime(0)
|
||
})
|
||
// eslint-disable-next-line no-await-in-loop
|
||
await act(async () => {
|
||
await Promise.resolve()
|
||
})
|
||
}
|
||
}
|
||
|
||
async function typeMobileAndContinue(get: (id: string) => any) {
|
||
fireEvent.changeText(get('user-login-mobile'), MOBILE)
|
||
fireEvent.press(get('user-login-submit'))
|
||
await flush()
|
||
}
|
||
|
||
async function goToPasswordViaOtherWays(view: any, mobile = '') {
|
||
if (mobile) fireEvent.changeText(view.getByTestId('user-login-mobile'), mobile)
|
||
fireEvent.press(view.getByTestId('user-login-other-ways'))
|
||
await flush()
|
||
}
|
||
|
||
async function fillOtp(view: any, digits = '1234') {
|
||
for (let index = 0; index < 4; index += 1) {
|
||
fireEvent.changeText(view.getByTestId(`user-login-otp-${index}`), digits[index])
|
||
}
|
||
}
|
||
|
||
describe('Login (LOGIN-01..LOGIN-42)', () => {
|
||
beforeEach(() => {
|
||
resetAllMocks()
|
||
alertSpy = jest.spyOn(Alert, 'alert').mockImplementation(() => {})
|
||
})
|
||
|
||
it('LOGIN-01: the initial mount shows the mobile step with accepted terms', () => {
|
||
const view = renderLogin()
|
||
|
||
expect(view.getByText('Welcome')).toBeTruthy()
|
||
expect(view.getByTestId('user-login-mobile')).toBeTruthy()
|
||
expect(view.getByTestId('user-login-terms-checkbox').props.accessibilityState.checked).toBe(true)
|
||
expect(view.getByTestId('user-login-submit')).toBeEnabled()
|
||
expect(view.getByText('Continue')).toBeTruthy()
|
||
})
|
||
|
||
it('LOGIN-02: no stored timestamp leaves a fresh 120s resend window', async () => {
|
||
jest.useFakeTimers()
|
||
const view = renderLogin()
|
||
fireEvent.changeText(view.getByTestId('user-login-mobile'), MOBILE)
|
||
fireEvent.press(view.getByTestId('user-login-submit'))
|
||
await settle()
|
||
|
||
expect(view.getByText('Resend in 120s')).toBeTruthy()
|
||
// Still inside the window: pressing the label does not re-request.
|
||
fireEvent.press(view.getByText('Resend in 120s'))
|
||
expect(procedureCallCount('user.auth.sendOtp')).toBe(1)
|
||
})
|
||
|
||
it('LOGIN-03: a stored 30s-old timestamp surfaces as a 90s countdown', async () => {
|
||
__set('otp_sent_time', String(Date.now() - 30_000))
|
||
jest.useFakeTimers()
|
||
const view = renderLogin()
|
||
fireEvent.changeText(view.getByTestId('user-login-mobile'), MOBILE)
|
||
fireEvent.press(view.getByTestId('user-login-submit'))
|
||
await settle()
|
||
|
||
act(() => {
|
||
jest.advanceTimersByTime(30_000)
|
||
})
|
||
|
||
expect(view.getByText('Resend in 90s')).toBeTruthy()
|
||
fireEvent.press(view.getByText('Resend in 90s'))
|
||
expect(procedureCallCount('user.auth.sendOtp')).toBe(1)
|
||
})
|
||
|
||
it('LOGIN-04: a timestamp older than the window ends with an enabled resend', async () => {
|
||
__set('otp_sent_time', String(Date.now() - 130_000))
|
||
jest.useFakeTimers()
|
||
const view = renderLogin()
|
||
fireEvent.changeText(view.getByTestId('user-login-mobile'), MOBILE)
|
||
fireEvent.press(view.getByTestId('user-login-submit'))
|
||
await settle()
|
||
|
||
act(() => {
|
||
jest.advanceTimersByTime(120_000)
|
||
})
|
||
|
||
expect(view.getByText('Resend OTP')).toBeTruthy()
|
||
})
|
||
|
||
it('LOGIN-05: a malformed stored timestamp is treated as no timestamp', async () => {
|
||
__set('otp_sent_time', 'not-a-number')
|
||
jest.useFakeTimers()
|
||
const view = renderLogin()
|
||
fireEvent.changeText(view.getByTestId('user-login-mobile'), MOBILE)
|
||
fireEvent.press(view.getByTestId('user-login-submit'))
|
||
await settle()
|
||
|
||
// No crash, fresh window.
|
||
expect(view.getByText('Resend in 120s')).toBeTruthy()
|
||
})
|
||
|
||
it('LOGIN-06: the countdown stops at zero and enables the resend', async () => {
|
||
jest.useFakeTimers()
|
||
const view = renderLogin()
|
||
fireEvent.changeText(view.getByTestId('user-login-mobile'), MOBILE)
|
||
fireEvent.press(view.getByTestId('user-login-submit'))
|
||
await settle()
|
||
|
||
act(() => {
|
||
jest.advanceTimersByTime(121_000)
|
||
})
|
||
|
||
expect(view.getByText('Resend OTP')).toBeTruthy()
|
||
// The interval is cleared: further ticks do not change the label.
|
||
act(() => {
|
||
jest.advanceTimersByTime(60_000)
|
||
})
|
||
expect(view.getByText('Resend OTP')).toBeTruthy()
|
||
})
|
||
|
||
it('LOGIN-07: unmounting during the countdown clears the interval', async () => {
|
||
jest.useFakeTimers()
|
||
const setIntervalSpy = jest.spyOn(global, 'setInterval')
|
||
const clearIntervalSpy = jest.spyOn(global, 'clearInterval')
|
||
const view = renderLogin()
|
||
fireEvent.changeText(view.getByTestId('user-login-mobile'), MOBILE)
|
||
fireEvent.press(view.getByTestId('user-login-submit'))
|
||
await settle()
|
||
|
||
expect(setIntervalSpy).toHaveBeenCalled()
|
||
const countdownId = setIntervalSpy.mock.results[0].value
|
||
expect(jest.getTimerCount()).toBeGreaterThan(0)
|
||
|
||
view.unmount()
|
||
|
||
// React Query schedules its own gc timers on unmount, so assert the
|
||
// countdown interval itself is cleared rather than the raw timer count.
|
||
expect(clearIntervalSpy).toHaveBeenCalledWith(countdownId)
|
||
setIntervalSpy.mockRestore()
|
||
clearIntervalSpy.mockRestore()
|
||
})
|
||
|
||
it('LOGIN-08: an empty mobile is rejected and never calls the API', async () => {
|
||
const view = renderLogin()
|
||
fireEvent.press(view.getByTestId('user-login-submit'))
|
||
await flush()
|
||
|
||
expect(view.getByText('Mobile number is required')).toBeTruthy()
|
||
expect(procedureCallCount('user.auth.sendOtp')).toBe(0)
|
||
})
|
||
|
||
it('LOGIN-09: a mobile not starting with 6-9 is rejected', async () => {
|
||
const view = renderLogin()
|
||
fireEvent.changeText(view.getByTestId('user-login-mobile'), '5876543210')
|
||
fireEvent.press(view.getByTestId('user-login-submit'))
|
||
await flush()
|
||
|
||
expect(view.getByText('Please enter a valid 10-digit mobile number')).toBeTruthy()
|
||
expect(procedureCallCount('user.auth.sendOtp')).toBe(0)
|
||
})
|
||
|
||
it('LOGIN-10: a 9-digit mobile is rejected', async () => {
|
||
const view = renderLogin()
|
||
fireEvent.changeText(view.getByTestId('user-login-mobile'), '987654321')
|
||
fireEvent.press(view.getByTestId('user-login-submit'))
|
||
await flush()
|
||
|
||
expect(view.getByText('Please enter a valid 10-digit mobile number')).toBeTruthy()
|
||
expect(procedureCallCount('user.auth.sendOtp')).toBe(0)
|
||
})
|
||
|
||
it('LOGIN-11: the mobile input keeps digits only and caps at 10', () => {
|
||
const view = renderLogin()
|
||
|
||
fireEvent.changeText(view.getByTestId('user-login-mobile'), '98 76-54 32 10')
|
||
expect(view.getByTestId('user-login-mobile').props.value).toBe('9876543210')
|
||
|
||
fireEvent.changeText(view.getByTestId('user-login-mobile'), '98765432109')
|
||
expect(view.getByTestId('user-login-mobile').props.value).toBe('9876543210')
|
||
})
|
||
|
||
it('LOGIN-12: a valid mobile requests the OTP', async () => {
|
||
const view = renderLogin()
|
||
fireEvent.changeText(view.getByTestId('user-login-mobile'), MOBILE)
|
||
fireEvent.press(view.getByTestId('user-login-submit'))
|
||
await flush()
|
||
|
||
expect(lastProcedureInput('user.auth.sendOtp')).toEqual({ mobile: MOBILE })
|
||
})
|
||
|
||
it('LOGIN-13: a successful send stores the time, alerts and opens the OTP step', async () => {
|
||
const view = renderLogin()
|
||
fireEvent.changeText(view.getByTestId('user-login-mobile'), MOBILE)
|
||
fireEvent.press(view.getByTestId('user-login-submit'))
|
||
await flush()
|
||
|
||
expect(view.getByText('Enter 4-digit OTP')).toBeTruthy()
|
||
expect(alertSpy).toHaveBeenCalledWith('Success', 'OTP sent successfully')
|
||
expect(Number.isNaN(parseInt(__snapshot().otp_sent_time ?? '', 10))).toBe(false)
|
||
expect(view.getByText('Resend in 120s')).toBeTruthy()
|
||
})
|
||
|
||
it('LOGIN-14: success false alerts the server message and stays on mobile', async () => {
|
||
setProcedure('user.auth.sendOtp', { data: { success: false, message: 'Too many attempts' } })
|
||
const view = renderLogin()
|
||
fireEvent.changeText(view.getByTestId('user-login-mobile'), MOBILE)
|
||
fireEvent.press(view.getByTestId('user-login-submit'))
|
||
await flush()
|
||
|
||
expect(alertSpy).toHaveBeenCalledWith('Error', 'Too many attempts')
|
||
expect(view.getByTestId('user-login-mobile')).toBeTruthy()
|
||
})
|
||
|
||
it('LOGIN-15: a rejection alerts the error message and falls back to a generic one', async () => {
|
||
setProcedure('user.auth.sendOtp', { mode: 'error', error: new Error('boom') })
|
||
const first = renderLogin()
|
||
fireEvent.changeText(first.getByTestId('user-login-mobile'), MOBILE)
|
||
fireEvent.press(first.getByTestId('user-login-submit'))
|
||
await flush()
|
||
expect(alertSpy).toHaveBeenCalledWith('Error', 'boom')
|
||
first.unmount()
|
||
|
||
alertSpy.mockClear()
|
||
setProcedure('user.auth.sendOtp', { mode: 'error', error: new Error('') })
|
||
const second = renderLogin()
|
||
fireEvent.changeText(second.getByTestId('user-login-mobile'), MOBILE)
|
||
fireEvent.press(second.getByTestId('user-login-submit'))
|
||
await flush()
|
||
expect(alertSpy).toHaveBeenCalledWith('Error', 'Failed to send OTP')
|
||
})
|
||
|
||
it('LOGIN-16: unchecking the terms disables the submit', async () => {
|
||
const view = renderLogin()
|
||
|
||
fireEvent.press(view.getByTestId('user-login-terms-checkbox'))
|
||
|
||
expect(view.getByTestId('user-login-terms-checkbox').props.accessibilityState.checked).toBe(
|
||
false
|
||
)
|
||
expect(view.getByTestId('user-login-submit')).toBeDisabled()
|
||
// GAP: the direct-submit alert branch ("Please accept the Terms and
|
||
// Conditions to continue") is unreachable because the button is disabled.
|
||
})
|
||
|
||
it('LOGIN-17: the terms link opens a dialog that Cancel/OK close', () => {
|
||
const view = renderLogin()
|
||
|
||
fireEvent.press(view.getByText('Terms and Conditions'))
|
||
expect(view.getByText('Terms & Conditions')).toBeTruthy()
|
||
expect(view.UNSAFE_getByType(Modal).props.visible).toBe(true)
|
||
|
||
fireEvent.press(view.getByText('Cancel'))
|
||
expect(view.UNSAFE_getByType(Modal).props.visible).toBe(false)
|
||
})
|
||
|
||
it('LOGIN-18: an empty OTP is rejected and never verifies', async () => {
|
||
const view = renderLogin()
|
||
await typeMobileAndContinue(view.getByTestId)
|
||
fireEvent.press(view.getByTestId('user-login-submit'))
|
||
await flush()
|
||
|
||
expect(view.getByText('Please enter a valid OTP')).toBeTruthy()
|
||
expect(procedureCallCount('user.auth.verifyOtp')).toBe(0)
|
||
})
|
||
|
||
it('LOGIN-19: four typed digits fill the four cells', async () => {
|
||
const view = renderLogin()
|
||
await typeMobileAndContinue(view.getByTestId)
|
||
|
||
await fillOtp(view, '1234')
|
||
|
||
expect(view.getByTestId('user-login-otp-0').props.value).toBe('1')
|
||
expect(view.getByTestId('user-login-otp-1').props.value).toBe('2')
|
||
expect(view.getByTestId('user-login-otp-2').props.value).toBe('3')
|
||
expect(view.getByTestId('user-login-otp-3').props.value).toBe('4')
|
||
})
|
||
|
||
it('LOGIN-20: clearing a cell empties it', async () => {
|
||
const view = renderLogin()
|
||
await typeMobileAndContinue(view.getByTestId)
|
||
await fillOtp(view, '1234')
|
||
|
||
fireEvent.changeText(view.getByTestId('user-login-otp-2'), '')
|
||
|
||
expect(view.getByTestId('user-login-otp-2').props.value).toBe('')
|
||
expect(view.getByTestId('user-login-otp-3').props.value).toBe('4')
|
||
})
|
||
|
||
it('LOGIN-21: pasting 12ab34 keeps the first four digits as the form value', async () => {
|
||
const view = renderLogin()
|
||
await typeMobileAndContinue(view.getByTestId)
|
||
|
||
fireEvent.changeText(view.getByTestId('user-login-otp-0'), '12ab34')
|
||
|
||
expect(view.getByTestId('user-login-otp-0').props.value).toBe('1')
|
||
expect(view.getByTestId('user-login-otp-1').props.value).toBe('2')
|
||
expect(view.getByTestId('user-login-otp-2').props.value).toBe('3')
|
||
expect(view.getByTestId('user-login-otp-3').props.value).toBe('4')
|
||
|
||
// The combined value is what the verification submits.
|
||
fireEvent.press(view.getByTestId('user-login-submit'))
|
||
await flush()
|
||
expect(lastProcedureInput('user.auth.verifyOtp')).toEqual({ mobile: MOBILE, otp: '1234' })
|
||
})
|
||
|
||
it('LOGIN-22: pasting more than four digits keeps only the first four', async () => {
|
||
const view = renderLogin()
|
||
await typeMobileAndContinue(view.getByTestId)
|
||
|
||
fireEvent.changeText(view.getByTestId('user-login-otp-0'), '123456')
|
||
|
||
expect(view.getByTestId('user-login-otp-3').props.value).toBe('4')
|
||
fireEvent.press(view.getByTestId('user-login-submit'))
|
||
await flush()
|
||
expect(lastProcedureInput('user.auth.verifyOtp')).toEqual({ mobile: MOBILE, otp: '1234' })
|
||
})
|
||
|
||
it('LOGIN-23: a successful verification logs the user in with the returned token', async () => {
|
||
const view = renderLogin()
|
||
await typeMobileAndContinue(view.getByTestId)
|
||
await fillOtp(view, '1234')
|
||
|
||
alertSpy.mockClear()
|
||
fireEvent.press(view.getByTestId('user-login-submit'))
|
||
await flush()
|
||
|
||
expect(lastProcedureInput('user.auth.verifyOtp')).toEqual({ mobile: MOBILE, otp: '1234' })
|
||
expect(loginWithToken).toHaveBeenCalledWith('test-token', AUTH_USER)
|
||
expect(alertSpy).not.toHaveBeenCalled()
|
||
})
|
||
|
||
it('LOGIN-24: a failed verification alerts Verification failed', async () => {
|
||
setProcedure('user.auth.verifyOtp', { data: { success: false, message: 'Nope' } })
|
||
const view = renderLogin()
|
||
await typeMobileAndContinue(view.getByTestId)
|
||
await fillOtp(view, '1234')
|
||
|
||
fireEvent.press(view.getByTestId('user-login-submit'))
|
||
await flush()
|
||
|
||
expect(alertSpy).toHaveBeenCalledWith('Error', 'Verification failed')
|
||
expect(loginWithToken).not.toHaveBeenCalled()
|
||
})
|
||
|
||
it('LOGIN-25: a success without token/user alerts Verification failed', async () => {
|
||
setProcedure('user.auth.verifyOtp', { data: { success: true } })
|
||
const view = renderLogin()
|
||
await typeMobileAndContinue(view.getByTestId)
|
||
await fillOtp(view, '1234')
|
||
|
||
fireEvent.press(view.getByTestId('user-login-submit'))
|
||
await flush()
|
||
|
||
expect(alertSpy).toHaveBeenCalledWith('Error', 'Verification failed')
|
||
})
|
||
|
||
it('LOGIN-26: a rejected verification alerts the error message or Invalid OTP', async () => {
|
||
setProcedure('user.auth.verifyOtp', { mode: 'error', error: new Error('Invalid OTP') })
|
||
const first = renderLogin()
|
||
await typeMobileAndContinue(first.getByTestId)
|
||
await fillOtp(first, '1234')
|
||
fireEvent.press(first.getByTestId('user-login-submit'))
|
||
await flush()
|
||
expect(alertSpy).toHaveBeenCalledWith('Error', 'Invalid OTP')
|
||
first.unmount()
|
||
|
||
alertSpy.mockClear()
|
||
setProcedure('user.auth.verifyOtp', { mode: 'error', error: new Error('') })
|
||
const second = renderLogin()
|
||
await typeMobileAndContinue(second.getByTestId)
|
||
await fillOtp(second, '1234')
|
||
fireEvent.press(second.getByTestId('user-login-submit'))
|
||
await flush()
|
||
expect(alertSpy).toHaveBeenCalledWith('Error', 'Invalid OTP')
|
||
})
|
||
|
||
it('LOGIN-27: resend during the countdown is a no-op, then shows Sending...', async () => {
|
||
jest.useFakeTimers()
|
||
const view = renderLogin()
|
||
fireEvent.changeText(view.getByTestId('user-login-mobile'), MOBILE)
|
||
fireEvent.press(view.getByTestId('user-login-submit'))
|
||
await settle()
|
||
|
||
fireEvent.press(view.getByText('Resend in 120s'))
|
||
expect(procedureCallCount('user.auth.sendOtp')).toBe(1)
|
||
|
||
act(() => {
|
||
jest.advanceTimersByTime(120_000)
|
||
})
|
||
setProcedure('user.auth.sendOtp', { mode: 'pending' })
|
||
fireEvent.press(view.getByText('Resend OTP'))
|
||
await settle()
|
||
|
||
expect(view.getByText('Sending...')).toBeTruthy()
|
||
})
|
||
|
||
it('LOGIN-28: resend after the countdown uses the selected mobile', async () => {
|
||
jest.useFakeTimers()
|
||
const view = renderLogin()
|
||
fireEvent.changeText(view.getByTestId('user-login-mobile'), MOBILE)
|
||
fireEvent.press(view.getByTestId('user-login-submit'))
|
||
await settle()
|
||
|
||
act(() => {
|
||
jest.advanceTimersByTime(120_000)
|
||
})
|
||
fireEvent.press(view.getByText('Resend OTP'))
|
||
await settle()
|
||
|
||
expect(procedureMutationCalls('user.auth.sendOtp')).toHaveLength(2)
|
||
expect(lastProcedureInput('user.auth.sendOtp')).toEqual({ mobile: MOBILE })
|
||
})
|
||
|
||
it('LOGIN-29: OTP Back returns to the choice step and clears the OTP', async () => {
|
||
const view = renderLogin()
|
||
await typeMobileAndContinue(view.getByTestId)
|
||
await fillOtp(view, '1234')
|
||
|
||
fireEvent.press(view.getByText('Back'))
|
||
await flush()
|
||
|
||
expect(view.getByText(`Choose your login method for ${MOBILE}`)).toBeTruthy()
|
||
expect(view.queryByTestId('user-login-otp-0')).toBeNull()
|
||
})
|
||
|
||
it('LOGIN-30: Change Number returns to the mobile step and clears the input', async () => {
|
||
const view = renderLogin()
|
||
await typeMobileAndContinue(view.getByTestId)
|
||
fireEvent.press(view.getByText('Back'))
|
||
await flush()
|
||
|
||
fireEvent.press(view.getByText('Change Number'))
|
||
await flush()
|
||
|
||
expect(view.getByTestId('user-login-mobile')).toBeTruthy()
|
||
expect(view.getByTestId('user-login-mobile').props.value).toBe('')
|
||
})
|
||
|
||
it('LOGIN-31: Use Password prefills the selected mobile', async () => {
|
||
const view = renderLogin()
|
||
await typeMobileAndContinue(view.getByTestId)
|
||
fireEvent.press(view.getByText('Back'))
|
||
await flush()
|
||
|
||
fireEvent.press(view.getByText('Use Password'))
|
||
await flush()
|
||
|
||
expect(view.getByTestId('user-login-identifier').props.value).toBe(MOBILE)
|
||
expect(view.getByTestId('user-login-password')).toBeTruthy()
|
||
})
|
||
|
||
it('LOGIN-32: Use OTP resends for the selected mobile', async () => {
|
||
const view = renderLogin()
|
||
await typeMobileAndContinue(view.getByTestId)
|
||
fireEvent.press(view.getByText('Back'))
|
||
await flush()
|
||
|
||
fireEvent.press(view.getByText('Use OTP'))
|
||
await flush()
|
||
|
||
expect(lastProcedureInput('user.auth.sendOtp')).toEqual({ mobile: MOBILE })
|
||
expect(procedureMutationCalls('user.auth.sendOtp')).toHaveLength(2)
|
||
})
|
||
|
||
it('LOGIN-33: Or login with Password switches steps with the mobile prefilled', async () => {
|
||
const view = renderLogin()
|
||
await typeMobileAndContinue(view.getByTestId)
|
||
await fillOtp(view, '1234')
|
||
|
||
fireEvent.press(view.getByText('Or login with Password'))
|
||
await flush()
|
||
|
||
expect(view.getByTestId('user-login-identifier').props.value).toBe(MOBILE)
|
||
expect(view.queryByTestId('user-login-otp-0')).toBeNull()
|
||
})
|
||
|
||
it('LOGIN-34: other ways to login prefills the current mobile and Back returns', async () => {
|
||
const view = renderLogin()
|
||
fireEvent.changeText(view.getByTestId('user-login-mobile'), MOBILE)
|
||
|
||
await goToPasswordViaOtherWays(view)
|
||
|
||
expect(view.getByTestId('user-login-identifier').props.value).toBe(MOBILE)
|
||
|
||
fireEvent.press(view.getByText('Back to options'))
|
||
await flush()
|
||
expect(view.getByTestId('user-login-mobile')).toBeTruthy()
|
||
})
|
||
|
||
it('LOGIN-35: an empty password-step identifier shows Username is required', async () => {
|
||
const view = renderLogin()
|
||
await goToPasswordViaOtherWays(view)
|
||
// A valid password passes the field rule so the manual identifier guard runs.
|
||
fireEvent.changeText(view.getByTestId('user-login-password'), 'secret1')
|
||
|
||
fireEvent.press(view.getByTestId('user-login-submit'))
|
||
await flush()
|
||
|
||
expect(view.getByText('Username is required')).toBeTruthy()
|
||
expect(procedureCallCount('user.auth.login')).toBe(0)
|
||
})
|
||
|
||
it('LOGIN-36: a password shorter than six characters is rejected', async () => {
|
||
const view = renderLogin()
|
||
await goToPasswordViaOtherWays(view, MOBILE)
|
||
fireEvent.changeText(view.getByTestId('user-login-password'), '123')
|
||
|
||
fireEvent.press(view.getByTestId('user-login-submit'))
|
||
await flush()
|
||
|
||
expect(view.getByText('Password must be at least 6 characters')).toBeTruthy()
|
||
expect(procedureCallCount('user.auth.login')).toBe(0)
|
||
})
|
||
|
||
it('LOGIN-37: valid credentials call the login mutation and log in', async () => {
|
||
const view = renderLogin()
|
||
await goToPasswordViaOtherWays(view)
|
||
fireEvent.changeText(view.getByTestId('user-login-identifier'), 'test@example.com')
|
||
fireEvent.changeText(view.getByTestId('user-login-password'), 'secret1')
|
||
|
||
fireEvent.press(view.getByTestId('user-login-submit'))
|
||
await flush()
|
||
|
||
expect(lastProcedureInput('user.auth.login')).toEqual({
|
||
identifier: 'test@example.com',
|
||
password: 'secret1',
|
||
})
|
||
expect(loginWithToken).toHaveBeenCalledWith('test-token', AUTH_USER)
|
||
})
|
||
|
||
it('LOGIN-38: a rejected login alerts the server message and stays on password', async () => {
|
||
setProcedure('user.auth.login', { mode: 'error', error: new Error('Bad credentials') })
|
||
const view = renderLogin()
|
||
await goToPasswordViaOtherWays(view, MOBILE)
|
||
fireEvent.changeText(view.getByTestId('user-login-password'), 'secret1')
|
||
|
||
fireEvent.press(view.getByTestId('user-login-submit'))
|
||
await flush()
|
||
|
||
expect(alertSpy).toHaveBeenCalledWith('Error', 'Bad credentials')
|
||
expect(view.getByTestId('user-login-password')).toBeTruthy()
|
||
})
|
||
|
||
it('LOGIN-39: password Back to options returns to the entry step and clears the password', async () => {
|
||
const view = renderLogin()
|
||
fireEvent.changeText(view.getByTestId('user-login-mobile'), MOBILE)
|
||
await goToPasswordViaOtherWays(view)
|
||
fireEvent.changeText(view.getByTestId('user-login-password'), 'secret1')
|
||
|
||
fireEvent.press(view.getByText('Back to options'))
|
||
await flush()
|
||
|
||
// Came from the mobile step.
|
||
expect(view.getByTestId('user-login-mobile')).toBeTruthy()
|
||
|
||
await goToPasswordViaOtherWays(view, MOBILE)
|
||
expect(view.getByTestId('user-login-password').props.value).toBe('')
|
||
})
|
||
|
||
it('LOGIN-40: pending mutations disable the button and show the progress label', async () => {
|
||
setProcedure('user.auth.sendOtp', { mode: 'pending' })
|
||
const processing = renderLogin()
|
||
fireEvent.changeText(processing.getByTestId('user-login-mobile'), MOBILE)
|
||
fireEvent.press(processing.getByTestId('user-login-submit'))
|
||
await flush()
|
||
expect(processing.getByText('Processing...')).toBeTruthy()
|
||
expect(processing.getByTestId('user-login-submit')).toBeDisabled()
|
||
processing.unmount()
|
||
|
||
setProcedure('user.auth.sendOtp', { mode: 'success' })
|
||
setProcedure('user.auth.verifyOtp', { mode: 'pending' })
|
||
const verifying = renderLogin()
|
||
await typeMobileAndContinue(verifying.getByTestId)
|
||
await fillOtp(verifying, '1234')
|
||
fireEvent.press(verifying.getByTestId('user-login-submit'))
|
||
await flush()
|
||
expect(verifying.getByText('Verifying...')).toBeTruthy()
|
||
expect(verifying.getByTestId('user-login-submit')).toBeDisabled()
|
||
verifying.unmount()
|
||
|
||
setProcedure('user.auth.login', { mode: 'pending' })
|
||
const loggingIn = renderLogin()
|
||
await goToPasswordViaOtherWays(loggingIn, MOBILE)
|
||
fireEvent.changeText(loggingIn.getByTestId('user-login-password'), 'secret1')
|
||
fireEvent.press(loggingIn.getByTestId('user-login-submit'))
|
||
await flush()
|
||
expect(loggingIn.getByText('Logging in...')).toBeTruthy()
|
||
expect(loggingIn.getByTestId('user-login-submit')).toBeDisabled()
|
||
})
|
||
|
||
it('LOGIN-41: unchecking the terms after typing disables the submit and blocks the API', async () => {
|
||
const view = renderLogin()
|
||
fireEvent.changeText(view.getByTestId('user-login-mobile'), MOBILE)
|
||
fireEvent.press(view.getByTestId('user-login-terms-checkbox'))
|
||
|
||
expect(view.getByTestId('user-login-submit')).toBeDisabled()
|
||
|
||
fireEvent.press(view.getByTestId('user-login-submit'))
|
||
await flush()
|
||
expect(procedureCallCount('user.auth.sendOtp')).toBe(0)
|
||
})
|
||
|
||
it('LOGIN-42: the terms dialog shows the legal title and sections', () => {
|
||
const view = renderLogin()
|
||
|
||
fireEvent.press(view.getByText('Terms and Conditions'))
|
||
|
||
expect(view.getByText('FRESHYO – Legal Policies')).toBeTruthy()
|
||
expect(view.getByText('A. TERMS & CONDITIONS')).toBeTruthy()
|
||
expect(view.getByText('B. PRIVACY POLICY')).toBeTruthy()
|
||
expect(view.getByText('C. REFUND & CANCELLATION POLICY')).toBeTruthy()
|
||
})
|
||
})
|