687 lines
24 KiB
TypeScript
687 lines
24 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
/**
|
|
* Catalogue section 17.2 — `AddressForm` (`tests/components/address-form.test.tsx`).
|
|
*
|
|
* ADDRF-01..37. The region/coordinate resolution goes through
|
|
* `utils.common.resolveRegionByLocation.fetch` / `utils.common.resolveBound.fetch`,
|
|
* which the procedure mock does NOT record (only `useQuery`/`useMutation` are
|
|
* recorded). So the inputs are captured with an `impl` spy instead, and the
|
|
* observable UI (heading, city, error text, `onResolvedRegion`) is asserted too.
|
|
*
|
|
* The map step is a stubbed WebView: its `source.uri` and `onMessage` come back
|
|
* through the props of the rendered `webview` host.
|
|
*/
|
|
import * as React from 'react'
|
|
import { fireEvent, waitFor, within, act } from '@testing-library/react-native'
|
|
import AddressForm from '@/src/components/AddressForm'
|
|
import LocationMapStep from '@/src/components/location/LocationMapStep'
|
|
import constants from '@/src/constants'
|
|
import { renderWithProviders, seedRegion, flush } from '../helpers/test-utils'
|
|
import { resetAllMocks } from '../helpers/reset'
|
|
import {
|
|
setProcedure,
|
|
lastProcedureInput,
|
|
procedureMutationCalls,
|
|
} from '../mocks/trpc-client'
|
|
import { __configure as configureLocation } from '../mocks/expo-location-mock'
|
|
import { __snapshot as secureSnapshot } from '../mocks/secure-store-mock'
|
|
import { A1, REGION_1, REGION_2, BOUND_1 } from '../fixtures'
|
|
|
|
// The real tRPC client runs; only `global.fetch` is mocked by the harness, so
|
|
// AddressForm's `../trpc-client` import talks to the procedure registry.
|
|
|
|
const MAP_BASE = `${constants.WEB_UI_BASE_URL}/location-selector`
|
|
const PENDING_ADDRESS_KEY = 'freshyo_pending_address'
|
|
|
|
const REGION_1_CENTER = { ...REGION_1, center: { lat: 11, lng: 22 } }
|
|
const REGION_2_CENTER = { ...REGION_2, center: { lat: 33, lng: 44 } }
|
|
|
|
const BASE_VALUES = {
|
|
name: 'Alice',
|
|
phone: '9876543210',
|
|
addressLine1: '12 Main Road',
|
|
addressLine2: '',
|
|
city: 'Mahabubnagar',
|
|
state: 'Telangana',
|
|
isDefault: false,
|
|
}
|
|
|
|
function withCoords(overrides: Record<string, any> = {}) {
|
|
return { ...BASE_VALUES, latitude: 17, longitude: 78, ...overrides }
|
|
}
|
|
|
|
/** No device fix: keeps the "no location yet" branches deterministic. */
|
|
function denyLocation() {
|
|
configureLocation({ permission: 'denied', servicesEnabled: false, position: null })
|
|
}
|
|
|
|
/** Grant a device fix and return the exact rounded coords the store will hold. */
|
|
function grantLocation(latitude = 12.34, longitude = 56.78) {
|
|
configureLocation({
|
|
permission: 'granted',
|
|
servicesEnabled: true,
|
|
position: { coords: { latitude, longitude, accuracy: 5 }, timestamp: 1 },
|
|
})
|
|
return { latitude, longitude }
|
|
}
|
|
|
|
/** Spy on the (un-recorded) `resolveRegionByLocation.fetch` input. */
|
|
function spyResolveRegion(result: any = { region: REGION_1, bound: BOUND_1 }) {
|
|
const impl = jest.fn(() => result)
|
|
setProcedure('common.resolveRegionByLocation', { impl })
|
|
return impl
|
|
}
|
|
|
|
function failResolveRegion(message = 'boom') {
|
|
const impl = jest.fn(() => {
|
|
throw new Error(message)
|
|
})
|
|
setProcedure('common.resolveRegionByLocation', { impl })
|
|
return impl
|
|
}
|
|
|
|
/** Spy on the (un-recorded) `resolveBound.fetch` input. */
|
|
function spyResolveBound(result: any = { regionId: 2, boundId: 8 }) {
|
|
const impl = jest.fn(() => result)
|
|
setProcedure('common.resolveBound', { impl })
|
|
return impl
|
|
}
|
|
|
|
function renderForm(props: Record<string, any> = {}) {
|
|
const onSuccess = jest.fn()
|
|
const onResolvedRegion = jest.fn()
|
|
const result = renderWithProviders(
|
|
<AddressForm onSuccess={onSuccess} onResolvedRegion={onResolvedRegion} {...props} />,
|
|
{ auth: false }
|
|
)
|
|
return { ...result, onSuccess, onResolvedRegion }
|
|
}
|
|
|
|
/** Open the map through the location chooser: attach -> region picker -> region. */
|
|
function openMapViaChooser(screen: any, regionId = 1) {
|
|
fireEvent.press(screen.getByTestId('address-attach-location'))
|
|
fireEvent.press(screen.getByTestId(`address-map-region-${regionId}`))
|
|
}
|
|
|
|
/** Open the map through the City picker's "Select in Map" option. */
|
|
function openMapViaCity(screen: any) {
|
|
fireEvent.press(screen.getByTestId('address-city'))
|
|
fireEvent.press(screen.getByTestId('address-city-option-map'))
|
|
}
|
|
|
|
function sendMapMessage(screen: any, payload: string) {
|
|
fireEvent(screen.getByTestId('webview'), 'message', { nativeEvent: { data: payload } })
|
|
}
|
|
|
|
describe('AddressForm', () => {
|
|
beforeEach(() => resetAllMocks())
|
|
|
|
it('ADDRF-01: add mode shows "Add Address" and no service-area notice', () => {
|
|
denyLocation()
|
|
const { getAllByText, queryByText } = renderForm()
|
|
|
|
expect(getAllByText('Add Address').length).toBeGreaterThan(0)
|
|
expect(queryByText('Edit Address')).toBeNull()
|
|
expect(queryByText(/outside our delivery area/)).toBeNull()
|
|
})
|
|
|
|
it('ADDRF-02: edit mode shows "Edit Address" and prefilled values', () => {
|
|
denyLocation()
|
|
const { getByText, getByTestId } = renderForm({
|
|
isEdit: true,
|
|
initialValues: { id: 42, ...BASE_VALUES },
|
|
})
|
|
|
|
expect(getByText('Edit Address')).toBeTruthy()
|
|
expect(getByTestId('address-name').props.value).toBe('Alice')
|
|
expect(getByTestId('address-phone').props.value).toBe('9876543210')
|
|
expect(getByTestId('address-line1').props.value).toBe('12 Main Road')
|
|
})
|
|
|
|
it('ADDRF-03: a known browsing region preselects the city', async () => {
|
|
denyLocation()
|
|
seedRegion(REGION_1, 7)
|
|
const { getByText } = renderForm()
|
|
await flush()
|
|
|
|
expect(getByText('Mahabubnagar')).toBeTruthy()
|
|
})
|
|
|
|
it('ADDRF-04: with no browsing region the city shows the "Select town" placeholder', () => {
|
|
denyLocation()
|
|
const { getByText } = renderForm()
|
|
|
|
expect(getByText('Select town')).toBeTruthy()
|
|
})
|
|
|
|
it('ADDRF-05: a device fix auto-resolves and shows "Current Location Attached"', async () => {
|
|
const coords = grantLocation(12.34, 56.78)
|
|
const resolve = spyResolveRegion()
|
|
const { getByText, onResolvedRegion } = renderForm()
|
|
|
|
await waitFor(() => expect(getByText('Current Location Attached')).toBeTruthy())
|
|
expect(resolve).toHaveBeenCalledWith({ lat: coords.latitude, lng: coords.longitude })
|
|
await waitFor(() => expect(onResolvedRegion).toHaveBeenCalledWith({ regionId: 1, boundId: 7 }))
|
|
})
|
|
|
|
it('ADDRF-06: a device fix outside the area is refused', async () => {
|
|
denyLocation()
|
|
spyResolveRegion(null)
|
|
const { getByText } = renderForm({ initialValues: withCoords() })
|
|
|
|
await waitFor(() =>
|
|
expect(
|
|
getByText('This location is outside our delivery area. Try selecting on the map.')
|
|
).toBeTruthy()
|
|
)
|
|
})
|
|
|
|
it('ADDRF-07: a throwing resolve shows "Could not verify this location."', async () => {
|
|
denyLocation()
|
|
failResolveRegion()
|
|
const { getByText } = renderForm({ initialValues: withCoords() })
|
|
|
|
await waitFor(() =>
|
|
expect(getByText('Could not verify this location. Please try again.')).toBeTruthy()
|
|
)
|
|
})
|
|
|
|
it('ADDRF-08: initial coordinates resolve on mount', async () => {
|
|
denyLocation()
|
|
const resolve = spyResolveRegion()
|
|
const { getByText, onResolvedRegion } = renderForm({ initialValues: withCoords() })
|
|
|
|
await waitFor(() => expect(resolve).toHaveBeenCalledWith({ lat: 17, lng: 78 }))
|
|
expect(getByText('Current Location Attached')).toBeTruthy()
|
|
await waitFor(() => expect(onResolvedRegion).toHaveBeenCalledWith({ regionId: 1, boundId: 7 }))
|
|
})
|
|
|
|
it('ADDRF-09: without a location the submit is disabled and the hint appears once name is touched', () => {
|
|
denyLocation()
|
|
const { getByTestId, getByText } = renderForm()
|
|
|
|
const submit = getByTestId('address-submit')
|
|
expect(submit.props.accessibilityState.disabled).toBe(true)
|
|
|
|
fireEvent(getByTestId('address-name'), 'blur', { target: {}, nativeEvent: {} })
|
|
expect(getByText('Attach a location to add address')).toBeTruthy()
|
|
})
|
|
|
|
it('ADDRF-10: opening the city picker lists the regions, "Select in Map" and no empty message', async () => {
|
|
denyLocation()
|
|
const { getByTestId, getByText, queryByText } = renderForm()
|
|
await flush()
|
|
|
|
fireEvent.press(getByTestId('address-city'))
|
|
|
|
expect(getByTestId('address-city-option-1')).toBeTruthy()
|
|
expect(getByTestId('address-city-option-2')).toBeTruthy()
|
|
expect(getByText('Select in Map')).toBeTruthy()
|
|
expect(queryByText('No towns available')).toBeNull()
|
|
})
|
|
|
|
it('ADDRF-11: picking a city with no location shows the attach-location error', async () => {
|
|
denyLocation()
|
|
const { getByTestId, getByText } = renderForm()
|
|
await flush()
|
|
|
|
fireEvent.press(getByTestId('address-city'))
|
|
fireEvent.press(getByTestId('address-city-option-2'))
|
|
|
|
await waitFor(() =>
|
|
expect(getByText('Attach or pick a location inside this town.')).toBeTruthy()
|
|
)
|
|
})
|
|
|
|
it('ADDRF-12: picking a city with a location re-resolves the bound', async () => {
|
|
denyLocation()
|
|
spyResolveRegion()
|
|
const resolveBound = spyResolveBound({ regionId: 2, boundId: 8 })
|
|
const { getByTestId, getByText, onResolvedRegion } = renderForm({
|
|
initialValues: withCoords(),
|
|
})
|
|
|
|
await waitFor(() => expect(onResolvedRegion).toHaveBeenCalledWith({ regionId: 1, boundId: 7 }))
|
|
|
|
fireEvent.press(getByTestId('address-city'))
|
|
fireEvent.press(getByTestId('address-city-option-2'))
|
|
|
|
await waitFor(() =>
|
|
expect(resolveBound).toHaveBeenCalledWith({ regionId: 2, lat: 17, lng: 78 })
|
|
)
|
|
await waitFor(() =>
|
|
expect(onResolvedRegion).toHaveBeenLastCalledWith({ regionId: 2, boundId: 8 })
|
|
)
|
|
expect(getByText('Hyderabad')).toBeTruthy()
|
|
})
|
|
|
|
it('ADDRF-13: a failing bound resolve shows the outside-town error', async () => {
|
|
denyLocation()
|
|
spyResolveRegion()
|
|
const resolveBound = jest.fn(() => {
|
|
throw new Error('nope')
|
|
})
|
|
setProcedure('common.resolveBound', { impl: resolveBound })
|
|
const { getByTestId, getByText, onResolvedRegion } = renderForm({
|
|
initialValues: withCoords(),
|
|
})
|
|
|
|
await waitFor(() => expect(onResolvedRegion).toHaveBeenCalled())
|
|
|
|
fireEvent.press(getByTestId('address-city'))
|
|
fireEvent.press(getByTestId('address-city-option-2'))
|
|
|
|
await waitFor(() =>
|
|
expect(
|
|
getByText('Your location is outside this town. Pick a point on the map inside it.')
|
|
).toBeTruthy()
|
|
)
|
|
})
|
|
|
|
it('ADDRF-14: "Select in Map" from the city picker opens the map at the first region centre', async () => {
|
|
denyLocation()
|
|
setProcedure('common.listRegions', { data: [REGION_1_CENTER, REGION_2_CENTER] })
|
|
const { getByTestId } = renderForm()
|
|
await flush()
|
|
|
|
openMapViaCity({ getByTestId } as any)
|
|
|
|
const webview = getByTestId('webview')
|
|
expect(webview.props.source.uri).toBe(`${MAP_BASE}?lat=11&lng=22`)
|
|
})
|
|
|
|
it('ADDRF-15: the location chooser opens the region picker and marks the current region', async () => {
|
|
denyLocation()
|
|
seedRegion(REGION_1, 7)
|
|
const { getByTestId, getByText } = renderForm()
|
|
await flush()
|
|
|
|
fireEvent.press(getByTestId('address-attach-location'))
|
|
|
|
expect(getByTestId('address-map-region-1')).toBeTruthy()
|
|
expect(within(getByTestId('address-map-region-1')).getByText('Current')).toBeTruthy()
|
|
expect(getByText('Select a region')).toBeTruthy()
|
|
})
|
|
|
|
it('ADDRF-16: picking a region in the chooser opens the map at that region centre', async () => {
|
|
denyLocation()
|
|
setProcedure('common.listRegions', { data: [REGION_1_CENTER, REGION_2_CENTER] })
|
|
const { getByTestId } = renderForm()
|
|
await flush()
|
|
|
|
fireEvent.press(getByTestId('address-attach-location'))
|
|
fireEvent.press(getByTestId('address-map-region-2'))
|
|
|
|
expect(getByTestId('webview').props.source.uri).toBe(`${MAP_BASE}?lat=33&lng=44`)
|
|
})
|
|
|
|
it('ADDRF-17: "Select in Map" from the chooser opens the map at the first region', async () => {
|
|
denyLocation()
|
|
setProcedure('common.listRegions', { data: [REGION_1_CENTER, REGION_2_CENTER] })
|
|
const { getByTestId } = renderForm()
|
|
await flush()
|
|
|
|
fireEvent.press(getByTestId('address-attach-location'))
|
|
fireEvent.press(getByTestId('address-map-region-select-in-map'))
|
|
|
|
expect(getByTestId('webview').props.source.uri).toBe(`${MAP_BASE}?lat=11&lng=22`)
|
|
})
|
|
|
|
it('ADDRF-18: cancelling the region chooser closes it', async () => {
|
|
denyLocation()
|
|
const { getByTestId, queryByTestId } = renderForm()
|
|
await flush()
|
|
|
|
fireEvent.press(getByTestId('address-attach-location'))
|
|
expect(getByTestId('address-map-region-1')).toBeTruthy()
|
|
|
|
fireEvent.press(getByTestId('address-map-region-cancel'))
|
|
|
|
expect(queryByTestId('address-map-region-1')).toBeNull()
|
|
})
|
|
|
|
it('ADDRF-19: a map selection closes the map, resolves as "map" and updates the heading', async () => {
|
|
denyLocation()
|
|
const resolve = spyResolveRegion()
|
|
const { getByTestId, getByText, queryByTestId, onResolvedRegion } = renderForm({
|
|
initialValues: withCoords(),
|
|
})
|
|
|
|
await waitFor(() => expect(onResolvedRegion).toHaveBeenCalled())
|
|
resolve.mockClear()
|
|
|
|
fireEvent.press(getByTestId('address-choose-location'))
|
|
fireEvent.press(getByTestId('address-map-region-1'))
|
|
sendMapMessage(
|
|
{ getByTestId } as any,
|
|
JSON.stringify({ type: 'location-selected', latitude: 12.5, longitude: 77.5 })
|
|
)
|
|
|
|
await waitFor(() => expect(getByText('Location Attached from Map')).toBeTruthy())
|
|
expect(resolve).toHaveBeenCalledWith({ lat: 12.5, lng: 77.5 })
|
|
expect(queryByTestId('webview')).toBeNull()
|
|
})
|
|
|
|
it('ADDRF-20: cancelling the map closes it without changing the location', async () => {
|
|
denyLocation()
|
|
spyResolveRegion()
|
|
const { getByTestId, getByText, queryByTestId, onResolvedRegion } = renderForm({
|
|
initialValues: withCoords(),
|
|
})
|
|
|
|
await waitFor(() => expect(onResolvedRegion).toHaveBeenCalled())
|
|
expect(getByText('Current Location Attached')).toBeTruthy()
|
|
|
|
fireEvent.press(getByTestId('address-choose-location'))
|
|
fireEvent.press(getByTestId('address-map-region-1'))
|
|
expect(getByTestId('webview')).toBeTruthy()
|
|
|
|
fireEvent.press(getByText('Cancel'))
|
|
|
|
expect(queryByTestId('webview')).toBeNull()
|
|
expect(getByText('Current Location Attached')).toBeTruthy()
|
|
})
|
|
|
|
it('ADDRF-21: an empty name fails validation', async () => {
|
|
denyLocation()
|
|
spyResolveRegion()
|
|
const { getByTestId, getByText, onResolvedRegion } = renderForm({
|
|
initialValues: withCoords({ name: '' }),
|
|
})
|
|
|
|
await waitFor(() => expect(onResolvedRegion).toHaveBeenCalled())
|
|
fireEvent.press(getByTestId('address-submit'))
|
|
|
|
await waitFor(() => expect(getByText('Name is required')).toBeTruthy())
|
|
})
|
|
|
|
it('ADDRF-22: an invalid phone fails the 10-digit 6-9 rule', async () => {
|
|
denyLocation()
|
|
spyResolveRegion()
|
|
const { getByTestId, getByText, onResolvedRegion } = renderForm({
|
|
initialValues: withCoords({ phone: '12345' }),
|
|
})
|
|
|
|
await waitFor(() => expect(onResolvedRegion).toHaveBeenCalled())
|
|
fireEvent.press(getByTestId('address-submit'))
|
|
|
|
await waitFor(() =>
|
|
expect(
|
|
getByText('Phone must be a 10-digit number starting with 6, 7, 8 or 9')
|
|
).toBeTruthy()
|
|
)
|
|
})
|
|
|
|
it('ADDRF-23: an empty address line 1 fails validation', async () => {
|
|
denyLocation()
|
|
spyResolveRegion()
|
|
const { getByTestId, getByText, onResolvedRegion } = renderForm({
|
|
initialValues: withCoords({ addressLine1: '' }),
|
|
})
|
|
|
|
await waitFor(() => expect(onResolvedRegion).toHaveBeenCalled())
|
|
fireEvent.press(getByTestId('address-submit'))
|
|
|
|
await waitFor(() => expect(getByText('Address Line 1 is required')).toBeTruthy())
|
|
})
|
|
|
|
it('ADDRF-24: state is prefilled "Telangana" and disabled', () => {
|
|
denyLocation()
|
|
const { getByTestId } = renderForm()
|
|
|
|
const state = getByTestId('address-state')
|
|
expect(state.props.value).toBe('Telangana')
|
|
expect(state.props.editable === false || state.props.accessibilityState?.disabled === true).toBe(
|
|
true
|
|
)
|
|
})
|
|
|
|
it('ADDRF-25: an empty address line 2 is accepted on submit', async () => {
|
|
denyLocation()
|
|
spyResolveRegion()
|
|
const { getByTestId, onResolvedRegion } = renderForm({
|
|
initialValues: withCoords({ addressLine2: '' }),
|
|
})
|
|
|
|
await waitFor(() => expect(onResolvedRegion).toHaveBeenCalled())
|
|
fireEvent.press(getByTestId('address-submit'))
|
|
|
|
await waitFor(() => expect(lastProcedureInput('user.address.createAddress')).toBeTruthy())
|
|
expect(lastProcedureInput('user.address.createAddress').addressLine2).toBe('')
|
|
})
|
|
|
|
it('ADDRF-26: the default checkbox toggles isDefault in the payload', async () => {
|
|
denyLocation()
|
|
spyResolveRegion()
|
|
const { getByTestId, onResolvedRegion } = renderForm({
|
|
initialValues: withCoords({ isDefault: false }),
|
|
})
|
|
|
|
await waitFor(() => expect(onResolvedRegion).toHaveBeenCalled())
|
|
fireEvent.press(getByTestId('address-default-checkbox'))
|
|
expect(getByTestId('address-default-checkbox').props.accessibilityState.checked).toBe(true)
|
|
|
|
fireEvent.press(getByTestId('address-submit'))
|
|
|
|
await waitFor(() => expect(lastProcedureInput('user.address.createAddress')).toBeTruthy())
|
|
expect(lastProcedureInput('user.address.createAddress').isDefault).toBe(true)
|
|
})
|
|
|
|
it('ADDRF-27: a create submit sends regionId, city name and coordinates', async () => {
|
|
denyLocation()
|
|
spyResolveRegion()
|
|
const { getByTestId, onResolvedRegion } = renderForm({ initialValues: withCoords() })
|
|
|
|
await waitFor(() => expect(onResolvedRegion).toHaveBeenCalled())
|
|
fireEvent.press(getByTestId('address-submit'))
|
|
|
|
await waitFor(() => expect(procedureMutationCalls('user.address.createAddress')).toHaveLength(1))
|
|
expect(lastProcedureInput('user.address.createAddress')).toEqual({
|
|
...withCoords(),
|
|
city: 'Mahabubnagar',
|
|
regionId: 1,
|
|
latitude: 17,
|
|
longitude: 78,
|
|
googleMapsUrl: undefined,
|
|
})
|
|
})
|
|
|
|
it('ADDRF-28: an edit submit includes the address id', async () => {
|
|
denyLocation()
|
|
spyResolveRegion()
|
|
const { getByTestId, onResolvedRegion } = renderForm({
|
|
isEdit: true,
|
|
initialValues: { id: 42, ...withCoords() },
|
|
})
|
|
|
|
await waitFor(() => expect(onResolvedRegion).toHaveBeenCalled())
|
|
fireEvent.press(getByTestId('address-submit'))
|
|
|
|
await waitFor(() => expect(procedureMutationCalls('user.address.updateAddress')).toHaveLength(1))
|
|
const input = lastProcedureInput('user.address.updateAddress')
|
|
expect(input.id).toBe(42)
|
|
expect(input).toEqual({
|
|
id: 42,
|
|
...withCoords(),
|
|
city: 'Mahabubnagar',
|
|
regionId: 1,
|
|
latitude: 17,
|
|
longitude: 78,
|
|
googleMapsUrl: undefined,
|
|
})
|
|
})
|
|
|
|
it('ADDRF-29: draft mode saves the pending address, calls onSuccess with no id and skips the API', async () => {
|
|
denyLocation()
|
|
spyResolveRegion()
|
|
const { getByTestId, onSuccess, onResolvedRegion } = renderForm({
|
|
draftMode: true,
|
|
initialValues: withCoords(),
|
|
})
|
|
|
|
await waitFor(() => expect(onResolvedRegion).toHaveBeenCalled())
|
|
fireEvent.press(getByTestId('address-submit'))
|
|
|
|
await waitFor(() => expect(onSuccess).toHaveBeenCalled())
|
|
expect(onSuccess.mock.calls[0][0]).toBeUndefined()
|
|
|
|
const saved = JSON.parse(secureSnapshot()[PENDING_ADDRESS_KEY])
|
|
expect(saved).toMatchObject({
|
|
regionId: 1,
|
|
name: 'Alice',
|
|
phone: '9876543210',
|
|
addressLine1: '12 Main Road',
|
|
city: 'Mahabubnagar',
|
|
state: 'Telangana',
|
|
latitude: 17,
|
|
longitude: 78,
|
|
})
|
|
expect(procedureMutationCalls('user.address.createAddress')).toHaveLength(0)
|
|
})
|
|
|
|
it('ADDRF-30: a rejected create shows the submit error and clears the loading state', async () => {
|
|
denyLocation()
|
|
spyResolveRegion()
|
|
setProcedure('user.address.createAddress', { mode: 'error', error: new Error('Save failed') })
|
|
const { getByTestId, getByText, queryByText, onResolvedRegion } = renderForm({
|
|
initialValues: withCoords(),
|
|
})
|
|
|
|
await waitFor(() => expect(onResolvedRegion).toHaveBeenCalled())
|
|
fireEvent.press(getByTestId('address-submit'))
|
|
|
|
await waitFor(() => expect(getByText('Save failed')).toBeTruthy())
|
|
expect(queryByText('Adding address...')).toBeNull()
|
|
})
|
|
|
|
it('ADDRF-31: a successful create calls onSuccess after 350ms', async () => {
|
|
denyLocation()
|
|
spyResolveRegion()
|
|
const { getByTestId, onSuccess, onResolvedRegion } = renderForm({
|
|
initialValues: withCoords(),
|
|
})
|
|
|
|
await waitFor(() => expect(onResolvedRegion).toHaveBeenCalled())
|
|
jest.useFakeTimers()
|
|
|
|
fireEvent.press(getByTestId('address-submit'))
|
|
// The batch link schedules its request on a timer, so drain the timer queue
|
|
// (which also flushes the promise chain) instead of bare microtasks.
|
|
await act(async () => {
|
|
await jest.advanceTimersByTimeAsync(0)
|
|
})
|
|
expect(procedureMutationCalls('user.address.createAddress')).toHaveLength(1)
|
|
expect(onSuccess).not.toHaveBeenCalled()
|
|
|
|
await act(async () => {
|
|
jest.advanceTimersByTime(349)
|
|
})
|
|
expect(onSuccess).not.toHaveBeenCalled()
|
|
|
|
await act(async () => {
|
|
jest.advanceTimersByTime(1)
|
|
})
|
|
expect(onSuccess).toHaveBeenCalledWith(A1.id)
|
|
jest.useRealTimers()
|
|
})
|
|
|
|
it('ADDRF-32: a pending submit shows "Adding..."/"Updating..." and disables submit', async () => {
|
|
denyLocation()
|
|
spyResolveRegion()
|
|
setProcedure('user.address.createAddress', { mode: 'pending' })
|
|
const add = renderForm({ initialValues: withCoords() })
|
|
await waitFor(() => expect(add.onResolvedRegion).toHaveBeenCalled())
|
|
|
|
fireEvent.press(add.getByTestId('address-submit'))
|
|
|
|
await waitFor(() => expect(add.getByText('Adding...')).toBeTruthy())
|
|
expect(add.getByText('Adding address...')).toBeTruthy()
|
|
expect(add.getByTestId('address-submit').props.accessibilityState.disabled).toBe(true)
|
|
add.unmount()
|
|
|
|
setProcedure('user.address.updateAddress', { mode: 'pending' })
|
|
const edit = renderForm({ isEdit: true, initialValues: { id: 42, ...withCoords() } })
|
|
await waitFor(() => expect(edit.onResolvedRegion).toHaveBeenCalled())
|
|
|
|
fireEvent.press(edit.getByTestId('address-submit'))
|
|
|
|
await waitFor(() => expect(edit.getByText('Updating...')).toBeTruthy())
|
|
expect(edit.getByText('Updating address...')).toBeTruthy()
|
|
})
|
|
|
|
it('ADDRF-33: the map shows "Loading map..." and the step renders its error banner', async () => {
|
|
denyLocation()
|
|
const screen = renderForm()
|
|
await flush()
|
|
openMapViaChooser(screen as any)
|
|
expect(screen.getByText('Loading map...')).toBeTruthy()
|
|
|
|
// AddressForm never passes an `error` to the step, so assert the banner on
|
|
// the step directly with a rejected pick.
|
|
const onSelect = jest.fn()
|
|
const onCancel = jest.fn()
|
|
const step = renderWithProviders(
|
|
<LocationMapStep onSelect={onSelect} onCancel={onCancel} error="Pick a point inside the town" />,
|
|
{ auth: false }
|
|
)
|
|
expect(step.getByText('Loading map...')).toBeTruthy()
|
|
expect(step.getByText('Pick a point inside the town')).toBeTruthy()
|
|
})
|
|
|
|
it('ADDRF-34: a valid map message fires onSelect with the picked coordinates', async () => {
|
|
denyLocation()
|
|
const resolve = spyResolveRegion()
|
|
const { getByTestId, onResolvedRegion } = renderForm({ initialValues: withCoords() })
|
|
|
|
await waitFor(() => expect(onResolvedRegion).toHaveBeenCalled())
|
|
resolve.mockClear()
|
|
|
|
fireEvent.press(getByTestId('address-choose-location'))
|
|
fireEvent.press(getByTestId('address-map-region-1'))
|
|
sendMapMessage(
|
|
{ getByTestId } as any,
|
|
JSON.stringify({ type: 'location-selected', latitude: 9.9, longitude: 8.8 })
|
|
)
|
|
|
|
await waitFor(() => expect(resolve).toHaveBeenCalledWith({ lat: 9.9, lng: 8.8 }))
|
|
})
|
|
|
|
it('ADDRF-35: invalid map messages are ignored', async () => {
|
|
denyLocation()
|
|
const resolve = spyResolveRegion()
|
|
const { getByTestId, queryByText } = renderForm()
|
|
await flush()
|
|
|
|
openMapViaChooser({ getByTestId } as any)
|
|
sendMapMessage({ getByTestId } as any, 'not-json')
|
|
sendMapMessage({ getByTestId } as any, JSON.stringify({ type: 'something-else' }))
|
|
sendMapMessage({ getByTestId } as any, JSON.stringify({ type: 'location-selected', latitude: 'x' }))
|
|
|
|
expect(resolve).not.toHaveBeenCalled()
|
|
expect(getByTestId('webview')).toBeTruthy()
|
|
expect(queryByText('Location Attached from Map')).toBeNull()
|
|
})
|
|
|
|
it('ADDRF-36: the map URL carries lat/lng query params when a centre is known', async () => {
|
|
denyLocation()
|
|
setProcedure('common.listRegions', { data: [REGION_1_CENTER, REGION_2_CENTER] })
|
|
const { getByTestId } = renderForm()
|
|
await flush()
|
|
|
|
openMapViaCity({ getByTestId } as any)
|
|
|
|
const uri = getByTestId('webview').props.source.uri
|
|
expect(uri).toContain('?lat=11')
|
|
expect(uri).toContain('&lng=22')
|
|
})
|
|
|
|
it('ADDRF-37: the map URL is the bare base URL when no centre is known', async () => {
|
|
denyLocation()
|
|
const { getByTestId } = renderForm()
|
|
await flush()
|
|
|
|
openMapViaCity({ getByTestId } as any)
|
|
|
|
expect(getByTestId('webview').props.source.uri).toBe(MAP_BASE)
|
|
})
|
|
})
|