/** * AddressForm (src/components/AddressForm.tsx) — create/update + validation. * * The City (region) and bound are derived from the attached location, so these * cases seed the device location via useLocationStore before rendering. * Coordinates are mandatory: Add Address stays disabled until a location is * attached. */ jest.mock('expo-router', () => require('../mocks/router').expoRouterMock()) import React from 'react' import { fireEvent, screen, waitFor } from '@testing-library/react-native' import AddressForm from '@/src/components/AddressForm' import { useLocationStore } from '@/src/store/locationStore' import { useRegionStore } from '@/src/store/regionStore' import { resetStorage, setupApiMocks } from '../mocks/api' import { expectProcedureCalled, lastProcedureInput } from '../mocks/procedures' import { renderWithProviders } from '../helpers/render' const LOCATION = { latitude: 16.7488, longitude: 77.9851 } /** Pretend the launch fix already produced coordinates (or was denied). */ function seedLocation(coords: typeof LOCATION | null) { useLocationStore.setState({ coords, status: coords ? 'granted' : 'denied', error: null, }) } describe('AddressForm', () => { beforeEach(async () => { const Location = require('expo-location') Location.requestForegroundPermissionsAsync.mockResolvedValue({ status: 'granted', granted: true, }) Location.hasServicesEnabledAsync.mockResolvedValue(true) Location.getCurrentPositionAsync.mockResolvedValue({ coords: { ...LOCATION, accuracy: 5 }, timestamp: Date.now(), }) await resetStorage() // zustand stores are module singletons. useLocationStore.getState().stop() useRegionStore.setState({ selectedRegionId: null, selectedRegion: null, selectedBoundId: null, regionGeneration: 0, }) }) it('renders the add-address form without a service-area notice', () => { setupApiMocks() renderWithProviders(, { centralStores: false }) expect(screen.getAllByText('Add Address').length).toBeGreaterThan(0) expect(screen.getByTestId('address-name')).toBeVisible() expect(screen.queryByText(/currently serve/)).toBeNull() }) it('keeps Add Address disabled until a location is attached', () => { const Location = require('expo-location') Location.requestForegroundPermissionsAsync.mockResolvedValue({ status: 'denied', granted: false, }) useLocationStore.getState().stop() setupApiMocks() renderWithProviders(, { centralStores: false }) expect(screen.getByTestId('address-attach-location')).toBeVisible() expect(screen.getByTestId('address-submit')).toBeDisabled() // The hint only appears once the first field has been touched. fireEvent.changeText(screen.getByTestId('address-name'), 'Home') fireEvent(screen.getByTestId('address-name'), 'blur') expect(screen.getByText('Attach a location to add address')).toBeVisible() }) it('shows the device-source heading once a location is attached', async () => { seedLocation(LOCATION) setupApiMocks() renderWithProviders(, { centralStores: false }) await waitFor(() => expect(screen.getByText('Current Location Attached')).toBeVisible()) expect(screen.getByText('Choose Another Location in Map')).toBeVisible() }) it('validates the required fields', async () => { seedLocation(LOCATION) setupApiMocks() renderWithProviders(, { centralStores: false }) await waitFor(() => expect(screen.getByTestId('address-submit')).not.toBeDisabled()) fireEvent.press(screen.getByTestId('address-submit')) await waitFor(() => expect(screen.getByText('Name is required')).toBeVisible()) expect(screen.getByText('Phone is required')).toBeVisible() expect(screen.getByText('Address Line 1 is required')).toBeVisible() }) it('creates an address with the region and coordinates derived from the location', async () => { seedLocation(LOCATION) const api = setupApiMocks({ procedures: { 'user.address.createAddress': { data: { id: 7 } } }, }) renderWithProviders(, { centralStores: false }) // The City shows the region resolved from the attached location. await waitFor(() => expect(screen.getByText('Mahabubnagar')).toBeVisible()) fireEvent.changeText(screen.getByTestId('address-name'), 'Home') fireEvent.changeText(screen.getByTestId('address-phone'), '9876543210') fireEvent.changeText(screen.getByTestId('address-line1'), '12 Main Road') fireEvent.press(screen.getByTestId('address-submit')) await waitFor(() => expectProcedureCalled(api.fetchMock, 'user.address.createAddress')) expect(lastProcedureInput(api.fetchMock, 'user.address.createAddress')).toMatchObject({ name: 'Home', phone: '9876543210', addressLine1: '12 Main Road', city: 'Mahabubnagar', regionId: 1, latitude: LOCATION.latitude, longitude: LOCATION.longitude, }) }) it('updates an address in edit mode via user.address.updateAddress', async () => { const api = setupApiMocks({ procedures: { 'user.address.updateAddress': { success: true } }, }) renderWithProviders( , { centralStores: false } ) expect(screen.getByText('Edit Address')).toBeVisible() await waitFor(() => expect(screen.getByTestId('address-submit')).not.toBeDisabled()) fireEvent.press(screen.getByTestId('address-submit')) await waitFor(() => expectProcedureCalled(api.fetchMock, 'user.address.updateAddress')) expect(lastProcedureInput(api.fetchMock, 'user.address.updateAddress')).toMatchObject({ id: 3, name: 'Work', regionId: 1, }) }) })