This commit is contained in:
shafi54 2026-10-20 17:41:23 +05:30
parent 8cf3cc9776
commit 234812f3f9
6 changed files with 187 additions and 4 deletions

View file

@ -1338,6 +1338,8 @@ Covered by HOME-81..98, plus:
| LOC-52 | edge | gate back | onBack |
| LOC-53 | edge | QuickDelivery regular | earliest slot label; change slot callback |
| LOC-54 | edge | QuickDelivery flash | “within 1 hour”; back; LocationBar |
| LOC-55 | edge | startup gate, one non-default address | auto-opens region + selects address |
| LOC-56 | edge | regular switcher, one non-default address | no auto-select/close |
| SLOTQ-01 | edge | foreign address | warning; switch confirm; clear cart/home |
| SLOTQ-02 | edge | unavailable region | alert |
| SLOTQ-03 | edge | no selected/default address | “Select address” |

View file

@ -71,6 +71,7 @@ const LocationFallbackFlow: React.FC<LocationFallbackFlowProps> = ({
const [busy, setBusy] = useState(false)
const [message, setMessage] = useState<string | null>(null)
const resolvedRef = useRef<ResolvedRegion | null>(null)
const autoOpenedSoleRef = useRef(false)
// The gate renders outside the root navigator (see LocationTestWrapper), so we
// cannot router.push to /login here — render the login screen in place. Once
@ -191,6 +192,18 @@ const LocationFallbackFlow: React.FC<LocationFallbackFlowProps> = ({
[activate, onClose, setSelectedAddressId]
)
// Startup gate only: if there is exactly one saved address, open it without
// making the user tap a one-item list. This also covers a login that happens
// from this gate after the hook's one-shot launch decision has settled.
useEffect(() => {
if (highlightActiveAddress) return
if (!isAuthenticated || addressesQuery.isLoading) return
if (addresses.length !== 1) return
if (autoOpenedSoleRef.current) return
autoOpenedSoleRef.current = true
void handleSelect(addresses[0])
}, [highlightActiveAddress, isAuthenticated, addressesQuery.isLoading, addresses, handleSelect])
if (mode === 'login') {
return (
<View style={tw`flex-1 bg-white`}>

View file

@ -189,9 +189,12 @@ export function useRegion(): UseRegionResult {
// Signed-in users: wait for the default address and the address list (+
// regions) so a sole address can anchor the decision before we settle.
// `isFetched` matters here: a query that just flipped from disabled (auth
// was still loading) can report `isLoading: false` for one render before its
// first request starts, which would make us settle with no addresses.
if (isAuthenticated) {
if (defaultAddressQuery.isLoading) return
if (!defaultAddress && addressesQuery.isLoading) return
if (!defaultAddressQuery.isFetched && !defaultAddressQuery.isError) return
if (!defaultAddress && !addressesQuery.isFetched && !addressesQuery.isError) return
if ((defaultAddress?.regionId ?? soleAddress?.regionId) != null && !regions) return
}
@ -274,13 +277,32 @@ export function useRegion(): UseRegionResult {
queryClient,
isAuthenticated,
isAuthLoading,
defaultAddressQuery.isLoading,
addressesQuery.isLoading,
defaultAddressQuery.isFetched,
defaultAddressQuery.isError,
addressesQuery.isFetched,
addressesQuery.isError,
defaultAddress,
soleAddress,
regions,
])
// Sole-address safety net. The launch decision above runs once per app launch
// (`startupResolved`/`startupCheckDone`), but the address list can arrive
// later — e.g. the user was signed out when the gate first settled and logged
// in from the address chooser. In that case the one-shot decision can no
// longer adopt the address, so keep watching for the sole address here.
useEffect(() => {
if (!hydrated || isAuthLoading) return
if (!isAuthenticated || regionId != null) return
if (!soleAddress || soleAddress.regionId == null || !regions) return
const region = regions.find((candidate) => candidate.id === soleAddress.regionId) ?? null
if (!region) return
setRegion(region, soleAddress.boundId ?? null)
useAddressStore.getState().setSelectedAddressId(soleAddress.id)
}, [hydrated, isAuthLoading, isAuthenticated, regionId, soleAddress, regions, setRegion])
// Safety net: a hanging resolve must not strand the first-run spinner.
useEffect(() => {
if (!hydrated || startupResolved || !shouldResolve) return

View file

@ -888,4 +888,39 @@ describe('Location components (LOC)', () => {
fireEvent.press(getByTestId('quick-delivery-flash-back'))
expect(router.back).toHaveBeenCalled()
})
it('LOC-55: the startup gate auto-opens a sole non-default address', async () => {
signIn()
const sole = { ...S1, isDefault: false }
setAddresses([sole])
const onClose = jest.fn()
const { getByTestId } = renderWithProviders(
<LocationFallbackFlow highlightActiveAddress={false} onClose={onClose} />,
{ auth: false }
)
await flush(5)
expect(useRegionStore.getState().selectedRegionId).toBe(1)
expect(useRegionStore.getState().selectedBoundId).toBe(7)
expect(useAddressStore.getState().selectedAddressId).toBe(sole.id)
expect(onClose).toHaveBeenCalled()
expect(getByTestId(`location-address-${sole.id}`)).toBeTruthy()
})
it('LOC-56: the regular address switcher does not auto-select a sole address', async () => {
signIn()
const sole = { ...S1, isDefault: false }
setAddresses([sole])
const onClose = jest.fn()
const { getByTestId } = renderWithProviders(
<LocationFallbackFlow onClose={onClose} />,
{ auth: false }
)
await flush(5)
expect(useRegionStore.getState().selectedRegionId).toBeNull()
expect(useAddressStore.getState().selectedAddressId).toBeNull()
expect(onClose).not.toHaveBeenCalled()
expect(getByTestId(`location-address-${sole.id}`)).toBeTruthy()
})
})

View file

@ -460,6 +460,85 @@ describe('useRegion', () => {
view.unmount()
})
})
it('REGION-23: signing in after the gate settled still opens the sole address', async () => {
await withFreshModules(async (ctx) => {
const { A1 } = ctx.fixtures
const sole = { ...A1, isDefault: false }
ctx.auth.__setAuthState({ isAuthenticated: false, isLoading: false })
ctx.locationMock.__configure({ permission: 'denied' })
const { result, view, rerender } = ctx.renderRegion()
await ctx.testUtils.flush(4)
expect(result.current.isResolvingRegion).toBe(false)
expect(result.current.regionId).toBeNull()
// Login from the chooser: auth and the address list arrive after the
// one-shot launch decision already settled.
ctx.trpc.setProcedure('user.address.getDefaultAddress', { data: { data: null } })
ctx.trpc.setProcedure('user.address.getUserAddresses', { data: { data: [sole] } })
ctx.auth.__setAuthState({ isAuthenticated: true, isLoading: false })
await ctx.rtl.act(async () => {
rerender()
})
await ctx.testUtils.flush(6)
expect(result.current.regionId).toBe(1)
expect(result.current.boundId).toBe(7)
expect(ctx.addressStore.getState().selectedAddressId).toBe(sole.id)
view.unmount()
})
})
it('REGION-24: waits for the address list instead of settling early', async () => {
await withFreshModules(async (ctx) => {
const { A1 } = ctx.fixtures
const sole = { ...A1, isDefault: false }
ctx.auth.__setAuthState({ isAuthenticated: false, isLoading: true })
ctx.locationMock.__configure({ permission: 'denied' })
const networkMock = (global as any).fetch
let releaseAddresses!: () => void
const addressesGate = new Promise<void>((resolve) => {
releaseAddresses = resolve
})
;(global as any).fetch = jest.fn(async (input: any, init: any) => {
const response = await networkMock(input, init)
if (String(input).includes('user.address.getUserAddresses')) await addressesGate
return response
})
try {
ctx.trpc.setProcedure('user.address.getDefaultAddress', { data: { data: null } })
ctx.trpc.setProcedure('user.address.getUserAddresses', { data: { data: [sole] } })
const { result, view, rerender } = ctx.renderRegion()
await ctx.testUtils.flush(2)
// Auth lands; the default address is null, but the address list is held.
ctx.auth.__setAuthState({ isAuthenticated: true, isLoading: false })
await ctx.rtl.act(async () => {
rerender()
})
await ctx.testUtils.flush(3)
expect(result.current.isResolvingRegion).toBe(true)
expect(result.current.regionId).toBeNull()
await ctx.rtl.act(async () => {
releaseAddresses()
})
await ctx.testUtils.flush(5)
expect(result.current.regionId).toBe(1)
expect(result.current.boundId).toBe(7)
expect(ctx.addressStore.getState().selectedAddressId).toBe(sole.id)
view.unmount()
} finally {
;(global as any).fetch = networkMock
}
})
})
})

View file

@ -8280,6 +8280,38 @@ Run: bun run test -- tests/components/address-form.test.tsx -> 37/37 passed (no
assertions needed changing; address-city / address-city-option-* IDs preserved).
Typecheck: user-ui clean; web-ui `npm run typecheck` clean.
================================================================================
[2026-10-20 17:35:35 IST] FIX-2026-10-20-23 sole address still not selected
================================================================================
apps/user-ui/src/hooks/useRegion.ts
- Replace the startup `isLoading` checks with `isFetched`/`isError`: a query that
just flipped from disabled (auth still loading) to enabled can report
isLoading=false for one render, letting the launch decision settle before the
default/address list exists.
- Add a dedicated sole-address effect, independent of `startupResolved` /
`startupCheckDone`. It adopts the sole address's region (and selects it) as
soon as auth, the address list and regions are available. This covers the case
where the startup decision already settled (e.g. signed out at launch, then
logged in from the gate) and the old one-shot effect would never reconsider.
apps/user-ui/tests/lib/use-region.test.tsx
- REGION-23: launch signed out -> gate settles -> sign in with one non-default
address -> region/address auto-selected without a manual tap.
- REGION-24: hold the address list response; the startup decision must stay
unresolved until it lands, then adopt the sole address.
apps/user-ui/src/components/location/LocationFallbackFlow.tsx
- When the startup gate (`highlightActiveAddress={false}`) renders and the
signed-in user has exactly one address, auto-invoke handleSelect once the list
loads. This is the last-line fallback if the hook's one-shot startup decision
already settled (e.g. login from the gate) and guarantees the address opens.
apps/user-ui/tests/components/location-components.test.tsx
- LOC-55: startup gate with one non-default address auto-selects it (region,
selected id, onClose) without tapping.
- LOC-56: the same one-address list in the regular address switcher
(`highlightActiveAddress` default) does NOT auto-close/auto-select.
Run: `npm test -- tests/components/location-components.test.tsx
tests/lib/use-region.test.tsx --runInBand` -> 80/80 passed (LOC-55/56 and
REGION-23/24 added).
================================================================================
[2026-10-20 17:29:14 IST] TEST-2026-10-20-22 sole-address Home integration tests
================================================================================