diff --git a/apps/user-ui/JEST-TEST-CASES.md b/apps/user-ui/JEST-TEST-CASES.md index d8f7e73..bbfc715 100644 --- a/apps/user-ui/JEST-TEST-CASES.md +++ b/apps/user-ui/JEST-TEST-CASES.md @@ -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” | diff --git a/apps/user-ui/src/components/location/LocationFallbackFlow.tsx b/apps/user-ui/src/components/location/LocationFallbackFlow.tsx index 78c94df..39e21ef 100644 --- a/apps/user-ui/src/components/location/LocationFallbackFlow.tsx +++ b/apps/user-ui/src/components/location/LocationFallbackFlow.tsx @@ -71,6 +71,7 @@ const LocationFallbackFlow: React.FC = ({ const [busy, setBusy] = useState(false) const [message, setMessage] = useState(null) const resolvedRef = useRef(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 = ({ [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 ( diff --git a/apps/user-ui/src/hooks/useRegion.ts b/apps/user-ui/src/hooks/useRegion.ts index 4e2fdc4..2193814 100644 --- a/apps/user-ui/src/hooks/useRegion.ts +++ b/apps/user-ui/src/hooks/useRegion.ts @@ -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 diff --git a/apps/user-ui/tests/components/location-components.test.tsx b/apps/user-ui/tests/components/location-components.test.tsx index 0eddebd..5d49aab 100644 --- a/apps/user-ui/tests/components/location-components.test.tsx +++ b/apps/user-ui/tests/components/location-components.test.tsx @@ -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( + , + { 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( + , + { 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() + }) }) diff --git a/apps/user-ui/tests/lib/use-region.test.tsx b/apps/user-ui/tests/lib/use-region.test.tsx index cd62ddf..ad50e87 100644 --- a/apps/user-ui/tests/lib/use-region.test.tsx +++ b/apps/user-ui/tests/lib/use-region.test.tsx @@ -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((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 + } + }) + }) }) diff --git a/change-log.txt b/change-log.txt index ad950f3..6fe6a43 100644 --- a/change-log.txt +++ b/change-log.txt @@ -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 ================================================================================