From 6229ee999942a5ff59c2bdf78e6bda323128e371 Mon Sep 17 00:00:00 2001 From: shafi54 <108669266+shafi-aviz@users.noreply.github.com> Date: Sun, 1 Nov 2026 13:06:05 +0530 Subject: [PATCH] enh --- apps/user-ui/JEST-TEST-CASES.md | 9 +-- apps/user-ui/components/SlotSpecificView.tsx | 52 +++++++++++---- .../tests/pages/flash-products.test.tsx | 53 +++++++++++---- apps/web-ui/src/routes/flash.tsx | 64 +++++++++++++------ 4 files changed, 131 insertions(+), 47 deletions(-) diff --git a/apps/user-ui/JEST-TEST-CASES.md b/apps/user-ui/JEST-TEST-CASES.md index c86dd0e..9f8e04a 100644 --- a/apps/user-ui/JEST-TEST-CASES.md +++ b/apps/user-ui/JEST-TEST-CASES.md @@ -506,12 +506,12 @@ This file covers the route wrapper, `SlotLayout`, `SlotProducts` and the flash p | --- | --- | --- | --- | | FLASH-01 | happy | products pending | “Loading 1 Hr Delivery...” | | FLASH-02 | error | products fail, no cache | “Failed to load 1 Hr Delivery” | -| FLASH-03 | happy | tag tabs | first tag selected; tabs keep cache order | +| FLASH-03 | happy | tag tabs | first tag selected; only non-empty tags get a tab | | FLASH-04 | happy | data | only flash-available + in-stock products of the tag | | FLASH-05 | happy | select another tag | feed swaps to that tag’s products | | FLASH-06 | edge | search query case-insensitive substring | matching products in the active tag | | FLASH-07 | edge | search trimmed/empty | every product in the tag | -| FLASH-08 | edge | tag with no eligible products | “No 1 Hr Delivery products available.” | +| FLASH-08 | edge | tags/stores with no flash products | no tab for them | | FLASH-09 | happy | tap add | `addToCart(skuId,1,0)` on flash cart; alert “delivery in 1hr” | | FLASH-10 | happy | tap card | custom `onProductPress` or flash product-detail route | | FLASH-11 | edge | price | flash price if present, else regular | @@ -523,8 +523,9 @@ This file covers the route wrapper, `SlotLayout`, `SlotProducts` and the flash p | FLASH-17 | edge | `shouldNavigateToCart` false | no push | | FLASH-18 | edge | switch tag | client-side only; no router rewrite | | FLASH-19 | happy | 12 products | first 9 render; Show More reveals 12; button disappears | -| FLASH-20 | happy | Shop by Store | heading + one tab per store; store 1 grid shows its flash products | -| FLASH-21 | edge | empty store | store 2 shows the empty copy; tag section unaffected | +| 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 | --- diff --git a/apps/user-ui/components/SlotSpecificView.tsx b/apps/user-ui/components/SlotSpecificView.tsx index ee86ccd..80e516d 100644 --- a/apps/user-ui/components/SlotSpecificView.tsx +++ b/apps/user-ui/components/SlotSpecificView.tsx @@ -566,20 +566,10 @@ export function FlashDeliveryProducts({ baseUrl, onProductPress }: FlashDelivery const productSlotsMap = useCentralSlotStore((state) => state.productSlotsMap); const tags = productsQuery?.data?.tags || []; - // First tag is the default, matching the home page. - const activeTagId = selectedTagId ?? tags[0]?.id ?? null; - const stores = storesData?.stores || []; - // Every store gets a tab; the first is the default. - const activeStoreId = selectedStoreId ?? stores[0]?.id ?? null; const trimmedQuery = searchQuery.trim().toLowerCase(); - const tagProductIds = React.useMemo(() => { - const tag = tags.find((t: any) => t.id === activeTagId); - return new Set((tag?.productIds || []) as number[]); - }, [tags, activeTagId]); - const flashProducts = React.useMemo(() => { const products = productsQuery?.data?.products || []; return products.filter((p: any) => { @@ -588,6 +578,42 @@ export function FlashDeliveryProducts({ baseUrl, onProductPress }: FlashDelivery }); }, [productsQuery?.data?.products, productSlotsMap]); + const flashProductIds = React.useMemo( + () => new Set(flashProducts.map((p: any) => p.id)), + [flashProducts] + ); + const flashStoreIds = React.useMemo( + () => new Set(flashProducts.map((p: any) => p.storeId)), + [flashProducts] + ); + + // A tab only exists if it has at least one in-stock flash product; empty + // tags and stores are not shown at all. + const tagTabs = React.useMemo( + () => + tags.filter((tag: any) => + ((tag.productIds || []) as number[]).some((id) => flashProductIds.has(id)) + ), + [tags, flashProductIds] + ); + const storeTabs = React.useMemo( + () => stores.filter((store: any) => flashStoreIds.has(store.id)), + [stores, flashStoreIds] + ); + + // First visible tab is the default, matching the home page. + const activeTagId = tagTabs.some((tag: any) => tag.id === selectedTagId) + ? selectedTagId + : tagTabs[0]?.id ?? null; + const activeStoreId = storeTabs.some((store: any) => store.id === selectedStoreId) + ? selectedStoreId + : storeTabs[0]?.id ?? null; + + const tagProductIds = React.useMemo(() => { + const tag = tagTabs.find((t: any) => t.id === activeTagId); + return new Set((tag?.productIds || []) as number[]); + }, [tagTabs, activeTagId]); + const matchesSearch = React.useCallback( (product: any) => !trimmedQuery || product.name?.toLowerCase().includes(trimmedQuery), @@ -723,7 +749,7 @@ export function FlashDeliveryProducts({ baseUrl, onProductPress }: FlashDelivery ({ id: tag.id, label: tag.tagName }))} + items={tagTabs.map((tag: any) => ({ id: tag.id, label: tag.tagName }))} activeId={activeTagId} onSelect={setSelectedTagId} /> @@ -738,7 +764,7 @@ export function FlashDeliveryProducts({ baseUrl, onProductPress }: FlashDelivery {/* Shop by Store — one tab per store. */} - {stores.length > 0 && ( + {storeTabs.length > 0 && ( @@ -748,7 +774,7 @@ export function FlashDeliveryProducts({ baseUrl, onProductPress }: FlashDelivery ({ + items={storeTabs.map((store: any) => ({ id: store.id, label: store.name.replace(/^The\s+/i, ''), }))} diff --git a/apps/user-ui/tests/pages/flash-products.test.tsx b/apps/user-ui/tests/pages/flash-products.test.tsx index e2979c0..5bc4a87 100644 --- a/apps/user-ui/tests/pages/flash-products.test.tsx +++ b/apps/user-ui/tests/pages/flash-products.test.tsx @@ -32,6 +32,7 @@ import { SLOTS, TAGS, productsCacheUrl, + storesCacheUrl, } from '../fixtures' const FLASH_CART_ROUTE = '/(drawer)/(tabs)/flash-delivery/(cart)/cart' @@ -128,7 +129,8 @@ describe('Flash products (FLASH)', () => { expect(view.getByTestId('flash-tag-strip')).toBeTruthy() expect(view.getByTestId('flash-tag-tab-1')).toBeTruthy() expect(view.getByTestId('flash-tag-tab-2')).toBeTruthy() - expect(view.getByTestId('flash-tag-tab-3')).toBeTruthy() + // The "Empty" tag has no eligible product, so it gets no tab. + expect(view.queryByTestId('flash-tag-tab-3')).toBeNull() // Chicken is tag 1, so its product shows and Vegetables stays out of it. expect(tagSection(view).getByText('Chicken Breast')).toBeTruthy() @@ -208,14 +210,20 @@ describe('Flash products (FLASH)', () => { expect(tagSection(view).getByText('No Image Item')).toBeTruthy() }) - it('FLASH-08: a tag with no eligible products shows the flash empty copy', async () => { + it('FLASH-08: tags and stores with no flash products get no tab', async () => { + // Only Chicken Breast (tag 1, store 1) is flash-eligible. seedFlashAvailability(availabilityWith({ 2: { isFlashAvailable: true } })) const view = renderFlash() await load() - fireEvent.press(view.getByTestId('flash-tag-tab-3')) - expect(tagSection(view).getByText(EMPTY_COPY)).toBeTruthy() + expect(view.getByTestId('flash-tag-tab-1')).toBeTruthy() + // Vegetables only sells Cucumber, which is not flash-eligible. + expect(view.queryByTestId('flash-tag-tab-2')).toBeNull() + expect(view.queryByTestId('flash-tag-tab-3')).toBeNull() + // Freshyo Greens has no flash products either. + expect(view.getByTestId('flash-store-tab-1')).toBeTruthy() + expect(view.queryByTestId('flash-store-tab-2')).toBeNull() }) it('FLASH-09: tapping add writes to the flash cart and alerts for 1hr delivery', async () => { @@ -381,7 +389,11 @@ describe('Flash products (FLASH)', () => { count: generated.length, tags: [{ ...TAGS[0], productIds: generated.map((product) => product.id) }], }) - seedFlashAvailability( + // The generated ids need their own productAvailability rows so the central + // slot store builds productSlotsMap entries for them. + useCentralSlotStore.getState().setSlotsData( + SLOTS as any, + generated.map((product) => ({ id: product.id })) as any, generated.map((product) => ({ id: product.id, price: 90, @@ -391,7 +403,7 @@ describe('Flash products (FLASH)', () => { isOutOfStock: false, isSuspended: false, isOffer: false, - })) + })) as any ) const view = renderFlash() @@ -405,7 +417,7 @@ describe('Flash products (FLASH)', () => { expect(view.queryByTestId('flash-products-show-more')).toBeNull() }) - it('FLASH-20: Shop by Store gives every store a tab and shows that store\'s products', async () => { + it('FLASH-20: Shop by Store shows a tab only for stores with flash products', async () => { seedFlashAvailability( availabilityWith({ 2: { isFlashAvailable: true }, @@ -419,14 +431,21 @@ describe('Flash products (FLASH)', () => { expect(view.getByText('Shop by Store')).toBeTruthy() expect(view.getByTestId('flash-store-strip')).toBeTruthy() expect(view.getByTestId('flash-store-tab-1')).toBeTruthy() - expect(view.getByTestId('flash-store-tab-2')).toBeTruthy() + // Freshyo Greens only sells Cucumber, which is not flash-eligible. + expect(view.queryByTestId('flash-store-tab-2')).toBeNull() // Both fixtures belong to store 1. expect(storeSection(view).getByText('Chicken Breast')).toBeTruthy() expect(storeSection(view).getByText('Cucumber')).toBeTruthy() }) - it('FLASH-21: a store with no flash products shows the empty copy in its section only', async () => { + it('FLASH-21: a store with flash products gets its own tab and grid', async () => { + setJsonResponse(productsCacheUrl(), { + ...PRODUCTS_RESPONSE, + products: PRODUCTS_RESPONSE.products.map((product) => + product.id === 5 ? { ...product, storeId: 2 } : product + ), + }) seedFlashAvailability( availabilityWith({ 2: { isFlashAvailable: true }, @@ -438,8 +457,20 @@ describe('Flash products (FLASH)', () => { await load() fireEvent.press(view.getByTestId('flash-store-tab-2')) - expect(storeSection(view).getByText(EMPTY_COPY)).toBeTruthy() - // The tag section is unaffected. + expect(storeSection(view).getByText('Cucumber')).toBeTruthy() + expect(storeSection(view).queryByText('Chicken Breast')).toBeNull() + }) + + it('FLASH-22: with no stores the Shop by Store section is hidden', async () => { + setJsonResponse(storesCacheUrl(), { stores: [] }) + seedFlashAvailability(availabilityWith({ 2: { isFlashAvailable: true } })) + const view = renderFlash() + + await load() + + expect(view.queryByText('Shop by Store')).toBeNull() + expect(view.queryByTestId('flash-store-strip')).toBeNull() + // The tag section still renders. expect(tagSection(view).getByText('Chicken Breast')).toBeTruthy() }) }) diff --git a/apps/web-ui/src/routes/flash.tsx b/apps/web-ui/src/routes/flash.tsx index 7d1c838..10fc41b 100644 --- a/apps/web-ui/src/routes/flash.tsx +++ b/apps/web-ui/src/routes/flash.tsx @@ -36,10 +36,44 @@ function FlashDeliveryPage() { const addToCart = useAddToCart('flash') const { data: cartData } = useGetCart('flash') + // Get flash products from central store + const allFlashProducts = useMemo(() => { + return Object.values(productsById).filter( + (product: any) => + product && + productSlotsMap[product.id]?.isFlashAvailable && + !productSlotsMap[product.id]?.isOutOfStock + ) + }, [productsById, productSlotsMap]) + + const flashProductIds = useMemo( + () => new Set(allFlashProducts.map((p: any) => p.id)), + [allFlashProducts] + ) + const flashStoreIds = useMemo( + () => new Set(allFlashProducts.map((p: any) => p.storeId)), + [allFlashProducts] + ) + + // A tab only exists when it has at least one in-stock flash product. + const tagTabs = useMemo( + () => + tags.filter((tag: any) => + ((tag.productIds || []) as number[]).some((id) => flashProductIds.has(id)) + ), + [tags, flashProductIds] + ) + const storeTabs = useMemo( + () => stores.filter((store: any) => flashStoreIds.has(store.id)), + [stores, flashStoreIds] + ) + // Tag tabs — same order and default as the home page. const [selectedTagId, setSelectedTagId] = useState(null) - const activeTagId = selectedTagId ?? tags[0]?.id ?? null - const activeTag = tags.find((tag: any) => tag.id === activeTagId) || null + const activeTagId = tagTabs.some((tag: any) => tag.id === selectedTagId) + ? selectedTagId + : tagTabs[0]?.id ?? null + const activeTag = tagTabs.find((tag: any) => tag.id === activeTagId) || null const tagProductIds = useMemo( () => new Set((activeTag?.productIds || []) as number[]), [activeTag] @@ -50,7 +84,7 @@ function FlashDeliveryPage() { const tabPositions = useRef([]) const activeTabIndex = Math.max( 0, - tags.findIndex((tag: any) => tag.id === activeTagId) + tagTabs.findIndex((tag: any) => tag.id === activeTagId) ) useEffect(() => { @@ -60,16 +94,6 @@ function FlashDeliveryPage() { } }, [activeTabIndex]) - // Get flash products from central store - const allFlashProducts = useMemo(() => { - return Object.values(productsById).filter( - (product: any) => - product && - productSlotsMap[product.id]?.isFlashAvailable && - !productSlotsMap[product.id]?.isOutOfStock - ) - }, [productsById, productSlotsMap]) - // Products to show: the active tag's flash items, or everything when no // tags are configured. const filteredProducts = useMemo(() => { @@ -84,9 +108,11 @@ function FlashDeliveryPage() { }, [activeTagId]) const visibleProducts = filteredProducts.slice(0, visibleCount) - // Shop by Store — every store gets a tab, first selected by default. + // Shop by Store — one tab per store that has flash products. const [selectedStoreId, setSelectedStoreId] = useState(null) - const activeStoreId = selectedStoreId ?? stores[0]?.id ?? null + const activeStoreId = storeTabs.some((store: any) => store.id === selectedStoreId) + ? selectedStoreId + : storeTabs[0]?.id ?? null const storeProducts = useMemo(() => { if (activeStoreId == null) return allFlashProducts return allFlashProducts.filter((p: any) => p.storeId === activeStoreId) @@ -135,12 +161,12 @@ function FlashDeliveryPage() { {/* Tag tabs — same model as the home page tabs */} - {tags.length > 0 && ( + {tagTabs.length > 0 && (
- {tags.map((tag: any, i: number) => { + {tagTabs.map((tag: any, i: number) => { const active = activeTagId === tag.id return (