diff --git a/apps/user-ui/JEST-TEST-CASES.md b/apps/user-ui/JEST-TEST-CASES.md index 9f8e04a..cef539f 100644 --- a/apps/user-ui/JEST-TEST-CASES.md +++ b/apps/user-ui/JEST-TEST-CASES.md @@ -526,6 +526,8 @@ This file covers the route wrapper, `SlotLayout`, `SlotProducts` and the flash p | FLASH-20 | happy | Shop by Store | heading + tab only for stores with flash products | | FLASH-21 | happy | store with flash products | its own tab and grid | | FLASH-22 | edge | no stores | Shop by Store section hidden; tag section still renders | +| FLASH-24 | edge | background catalogue refetch | pull-to-refresh control stays idle | +| FLASH-25 | happy | pull down | spinner stays up until products + stores settle | --- @@ -1555,6 +1557,7 @@ Covered by HOME-81..98, plus: | HIDE-01 | edge | focus with key | hideTabsNav key true | | HIDE-02 | edge | default key | `default` | | HIDE-03 | edge | blur | false | +| HIDE-04 | edge | re-render | store map identity unchanged (no per-render rewrite) | | PSI-01 | edge | earliest future slot | id | | PSI-02 | edge | past delivery only | null | | PSI-03 | edge | no map/slots | null | @@ -1594,6 +1597,7 @@ Covered by HOME-81..98, plus: | STORE-17 | edge | clear region | nulls + bump | | STORE-18 | edge | clear already empty | no bump | | STORE-19 | edge | region memory only | no persist | +| STORE-20 | edge | same value written twice | map identity preserved (no subscriber wake) | ### App helpers diff --git a/apps/user-ui/components/SlotSpecificView.tsx b/apps/user-ui/components/SlotSpecificView.tsx index 80e516d..065ccb1 100644 --- a/apps/user-ui/components/SlotSpecificView.tsx +++ b/apps/user-ui/components/SlotSpecificView.tsx @@ -559,6 +559,10 @@ export function FlashDeliveryProducts({ baseUrl, onProductPress }: FlashDelivery const [selectedStoreId, setSelectedStoreId] = useState(null); const [tagVisibleCount, setTagVisibleCount] = useState(FLASH_PAGE_SIZE); const [storeVisibleCount, setStoreVisibleCount] = useState(FLASH_PAGE_SIZE); + // Owned by the user's pull, not by React Query: `isFetching` is true for every + // background fetch (remount-after-stale, invalidation, version bump), which + // made the pull-to-refresh spinner pop up on its own. + const [isRefreshing, setIsRefreshing] = useState(false); const router = useRouter(); const productsQuery = useAllProducts(); @@ -652,6 +656,29 @@ export function FlashDeliveryProducts({ baseUrl, onProductPress }: FlashDelivery const visibleTagProducts = tagProducts.slice(0, tagVisibleCount); const visibleStoreProducts = storeProducts.slice(0, storeVisibleCount); + const refetchProducts = productsQuery?.refetch; + const handleRefresh = React.useCallback(async () => { + setIsRefreshing(true); + try { + await Promise.all([refetchProducts?.(), refetchStores()]); + } finally { + setIsRefreshing(false); + } + }, [refetchProducts, refetchStores]); + + // Memoised so the platform control is not rebuilt on every render. + const refreshControl = React.useMemo( + () => ( + + ), + [isRefreshing, handleRefresh] + ); + const renderProduct = (item: any) => ( { - productsQuery.refetch(); - refetchStores(); - }} - tintColor={theme.colors.brand500} - colors={[theme.colors.brand500]} - /> - } + refreshControl={refreshControl} > { const setHideTabsNavKey = useAppStore((state) => state.setHideTabsNavKey); - useFocusEffect(() => { - setHideTabsNavKey(caller, true); // Hide tabs when screen focuses + // `useFocusEffect` re-runs whenever the callback identity changes, so an + // inline callback would unsubscribe/resubscribe and write false -> true into + // the store on every render, flickering the tab bar. + useFocusEffect( + React.useCallback(() => { + setHideTabsNavKey(caller, true); // Hide tabs when screen focuses - return () => { - setHideTabsNavKey(caller, false); // Show tabs when screen blurs - }; - }); -}; \ No newline at end of file + return () => { + setHideTabsNavKey(caller, false); // Show tabs when screen blurs + }; + }, [caller, setHideTabsNavKey]) + ); +}; diff --git a/apps/user-ui/src/store/appStore.ts b/apps/user-ui/src/store/appStore.ts index 129e6ef..ba88174 100644 --- a/apps/user-ui/src/store/appStore.ts +++ b/apps/user-ui/src/store/appStore.ts @@ -7,8 +7,12 @@ interface AppStore { export const useAppStore = create((set) => ({ hideTabsNav: {}, + // Writing the value a key already has would still allocate a new map and + // wake every subscriber (the tab navigator included), so bail out instead. setHideTabsNavKey: (key: string, hide: boolean) => - set((state) => ({ - hideTabsNav: { ...state.hideTabsNav, [key]: hide } - })), + set((state) => + state.hideTabsNav[key] === hide + ? state + : { hideTabsNav: { ...state.hideTabsNav, [key]: hide } } + ), })); \ No newline at end of file diff --git a/apps/user-ui/tests/lib/route-network.test.tsx b/apps/user-ui/tests/lib/route-network.test.tsx index a8fa24a..69b467d 100644 --- a/apps/user-ui/tests/lib/route-network.test.tsx +++ b/apps/user-ui/tests/lib/route-network.test.tsx @@ -274,7 +274,7 @@ describe('trpc client link', () => { }) /* -------------------------------------------------------------------------- */ -/* HIDE-01..03 */ +/* HIDE-01..04 */ /* -------------------------------------------------------------------------- */ function HideProbe({ caller }: { caller?: string }) { @@ -302,6 +302,17 @@ describe('useHideTabNav', () => { act(() => view.unmount()) expect(useAppStore.getState().hideTabsNav.cart).toBe(false) }) + + it('HIDE-04: re-rendering does not rewrite the store', () => { + const view = renderWithProviders(, { auth: false }) + const before = useAppStore.getState().hideTabsNav + + act(() => view.rerender()) + + // An inline focus callback used to unsubscribe/resubscribe per render, + // writing false -> true and flickering the tab bar. + expect(useAppStore.getState().hideTabsNav).toBe(before) + }) }) /* -------------------------------------------------------------------------- */ diff --git a/apps/user-ui/tests/lib/stores.test.ts b/apps/user-ui/tests/lib/stores.test.ts index 9916496..f723278 100644 --- a/apps/user-ui/tests/lib/stores.test.ts +++ b/apps/user-ui/tests/lib/stores.test.ts @@ -50,6 +50,16 @@ describe('stores', () => { expect(useAppStore.getState().hideTabsNav).toEqual({ quick_delivery: false }) }) + it('STORE-20: writing the value a hide key already has keeps the map identity', () => { + useAppStore.getState().setHideTabsNavKey('cart', true) + const hidden = useAppStore.getState().hideTabsNav + + useAppStore.getState().setHideTabsNavKey('cart', true) + + // Same object, so subscribers (the tab navigator) are not woken. + expect(useAppStore.getState().hideTabsNav).toBe(hidden) + }) + it('STORE-02: addressStore selects and clears an address id', () => { useAddressStore.getState().setSelectedAddressId(11) expect(useAddressStore.getState().selectedAddressId).toBe(11) diff --git a/apps/user-ui/tests/pages/flash-products.test.tsx b/apps/user-ui/tests/pages/flash-products.test.tsx index 5bc4a87..cd96da9 100644 --- a/apps/user-ui/tests/pages/flash-products.test.tsx +++ b/apps/user-ui/tests/pages/flash-products.test.tsx @@ -1,7 +1,7 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ /** * Catalogue section 8.3 — Flash products (`tests/pages/flash-products.test.tsx`, - * FLASH-01..21). + * FLASH-01..25). * * The page is `flash-delivery/(products)/index.tsx` plus `FlashDeliveryProducts` * in `components/SlotSpecificView.tsx`. The screen has two independently paged @@ -473,4 +473,35 @@ describe('Flash products (FLASH)', () => { // The tag section still renders. expect(tagSection(view).getByText('Chicken Breast')).toBeTruthy() }) + + it('FLASH-24: a background refetch leaves the pull-to-refresh spinner idle', async () => { + const view = renderFlash() + await load() + + const control = () => view.UNSAFE_getAllByType(RefreshControl)[0] + expect(control().props.refreshing).toBe(false) + + // Something else refetches the catalogue (remount after the stale window, + // an invalidation, a version bump). It must not drive the user's control. + setJsonPending(productsCacheUrl()) + await act(async () => { + void view.queryClient.refetchQueries({ queryKey: ['all-products'] }) + }) + await flush(2) + + expect(control().props.refreshing).toBe(false) + }) + + it('FLASH-25: pulling down keeps the spinner up until the refresh settles', async () => { + const view = renderFlash() + await load() + + setJsonPending(productsCacheUrl()) + await act(async () => { + void view.UNSAFE_getAllByType(RefreshControl)[0].props.onRefresh() + }) + await flush(1) + + expect(view.UNSAFE_getAllByType(RefreshControl)[0].props.refreshing).toBe(true) + }) }) diff --git a/change-log.txt b/change-log.txt index 48cc3a5..eb24332 100644 --- a/change-log.txt +++ b/change-log.txt @@ -142,3 +142,31 @@ Web offers page already uses a 12px grid gap (gap-3), so no web change. ~ Same two sections, store tabs below the tag grid, independent 9-item paging. --- tests ~ Product assertions scoped to the tag grid; new store-section cases. + +[2026-11-01 14:06:51 IST] Flash delivery: the pull-to-refresh spinner no longer reports background fetches. + +Diagnosed with a throwaway cache-subscription test (25s, real timers): the flash +screen itself starts no background fetches (only the 7 initial ones), and the +catalogue URL is built from assetsDomain/apiCacheKey/region/version — stable, so +the products query key does not churn. There is no 10s timer anywhere in +user-ui. What is wrong is the wiring: `refreshing` was bound to +`productsQuery.isFetching`, so *any* fetch (mount-after-stale, invalidation, +version bump, pull) drove the pull-to-refresh control, and the control element +was rebuilt on every render (re-attaching the platform control). + +--- apps/user-ui/components/SlotSpecificView.tsx +~ FlashDeliveryProducts: `isRefreshing` local state + handleRefresh + (refetch products + stores, clear in finally), RefreshControl element + memoised. Mirrors home's handleRefresh. Background fetches are invisible. +--- apps/user-ui/src/hooks/useHideTabNav.ts +~ Focus callback memoised (was recreated every render, so useFocusEffect + unsubscribed/resubscribed per render and wrote false->true into the store, + flickering the tab bar on every render of every screen using it). +--- apps/user-ui/src/store/appStore.ts +~ setHideTabsNavKey no longer allocates when the key already has that value, + so it cannot notify subscribers spuriously. +--- tests ++ FLASH-24: a background refetch leaves the spinner idle. ++ FLASH-25: a pull keeps the spinner up until the refresh settles. ++ HIDE-04: re-render does not rewrite the store. ++ STORE-20: same-value write keeps the map identity.