freshyo/apps/user-ui/tests/components/registration-form.test.tsx
2026-09-26 13:41:46 +05:30

119 lines
4.2 KiB
TypeScript

/**
* RegistrationForm (components/registration-form.tsx): validation, the
* normalised register payload, and the edit-mode password update.
*/
jest.mock('expo-router', () => require('../mocks/router').expoRouterMock())
import React from 'react'
import { fireEvent, screen, waitFor } from '@testing-library/react-native'
import RegistrationForm from '@/components/registration-form'
import { resetStorage, setupApiMocks } from '../mocks/api'
import { expectProcedureCalled, lastProcedureInput } from '../mocks/procedures'
import { renderWithProviders } from '../helpers/render'
const fillRegisterForm = () => {
fireEvent.changeText(screen.getByPlaceholderText('Enter your full name'), 'Test User')
fireEvent.changeText(
screen.getByPlaceholderText('Enter your email address'),
'Test@Example.com'
)
fireEvent.changeText(screen.getByPlaceholderText('Enter your mobile number'), '9876543210')
fireEvent.changeText(screen.getByPlaceholderText('Create a password'), 'secret123')
fireEvent.changeText(screen.getByPlaceholderText('Confirm your password'), 'secret123')
fireEvent.press(screen.getByText(/I agree to the/))
}
describe('RegistrationForm', () => {
beforeEach(async () => {
await resetStorage()
})
it('renders the registration fields', () => {
setupApiMocks()
renderWithProviders(<RegistrationForm onSubmit={jest.fn()} />, {
centralStores: false,
})
expect(screen.getByText('Create Account')).toBeVisible()
expect(screen.getByPlaceholderText('Enter your full name')).toBeVisible()
})
it('blocks submit with a validation error', async () => {
setupApiMocks()
const onSubmit = jest.fn()
renderWithProviders(<RegistrationForm onSubmit={onSubmit} />, {
centralStores: false,
})
fireEvent.press(screen.getByText('Create Account'))
await waitFor(() => expect(screen.getByText('Name is required')).toBeVisible())
expect(onSubmit).not.toHaveBeenCalled()
})
it('rejects mismatched passwords', async () => {
setupApiMocks()
const onSubmit = jest.fn()
renderWithProviders(<RegistrationForm onSubmit={onSubmit} />, {
centralStores: false,
})
fireEvent.changeText(screen.getByPlaceholderText('Enter your full name'), 'Test User')
fireEvent.changeText(screen.getByPlaceholderText('Enter your email address'), 'a@b.com')
fireEvent.changeText(screen.getByPlaceholderText('Enter your mobile number'), '9876543210')
fireEvent.changeText(screen.getByPlaceholderText('Create a password'), 'secret123')
fireEvent.changeText(screen.getByPlaceholderText('Confirm your password'), 'different')
fireEvent.press(screen.getByText(/I agree to the/))
fireEvent.press(screen.getByText('Create Account'))
await waitFor(() =>
expect(screen.getByText('Passwords do not match')).toBeVisible()
)
expect(onSubmit).not.toHaveBeenCalled()
})
it('submits the normalised register payload', async () => {
setupApiMocks()
const onSubmit = jest.fn()
renderWithProviders(<RegistrationForm onSubmit={onSubmit} />, {
centralStores: false,
})
fillRegisterForm()
fireEvent.press(screen.getByText('Create Account'))
await waitFor(() => expect(onSubmit).toHaveBeenCalled())
expect(onSubmit.mock.calls[0][0]).toMatchObject({
name: 'Test User',
email: 'test@example.com',
mobile: '9876543210',
password: 'secret123',
})
})
it('updates the password in edit mode via user.auth.updatePassword', async () => {
const api = setupApiMocks({
procedures: { 'user.auth.updatePassword': { success: true } },
})
renderWithProviders(<RegistrationForm onSubmit={jest.fn()} isEdit />, {
centralStores: false,
})
expect(screen.getByText('Update Profile')).toBeVisible()
fireEvent.press(screen.getByText('Update Password'))
fireEvent.changeText(screen.getByPlaceholderText('New Password'), 'newpass1')
fireEvent.changeText(screen.getByPlaceholderText('Confirm Password'), 'newpass1')
fireEvent.press(screen.getByText('Update'))
await waitFor(() =>
expectProcedureCalled(api.fetchMock, 'user.auth.updatePassword')
)
expect(lastProcedureInput(api.fetchMock, 'user.auth.updatePassword')).toEqual({
password: 'newpass1',
})
})
})