/** * Slot views (components/SlotSpecificView.tsx): SlotProducts and * FlashDeliveryProducts, plus the slot-view route's data source. */ jest.mock('expo-router', () => require('../mocks/router').expoRouterMock()) import React from 'react' import { TextInput } from 'react-native' import { fireEvent, screen, waitFor } from '@testing-library/react-native' import { FlashDeliveryProducts, SlotProducts, } from '@/components/SlotSpecificView' import availability from '../mocks/mock-availability-response.json' import productsResponse from '../mocks/mock-products-response.json' import { mockSlots, resetStorage, setupApiMocks } from '../mocks/api' import { routerMock } from '../mocks/router' import { renderWithProviders } from '../helpers/render' const BASE = '/(drawer)/(tabs)/home/slot-view' const SLOT = mockSlots.slots.find((s) => s.products.length > 0)! const PRODUCTS = productsResponse.products.filter((p) => !p.isComboOnly) const AVAIL_BY_ID = new Map(availability.availability.map((a) => [a.id, a])) const isOutOfStock = (id: number) => AVAIL_BY_ID.get(id)?.isOutOfStock === true /** Slot products the screen renders: present in the cache and in stock. */ const slotProducts = (storeId?: number) => SLOT.products .map((p) => PRODUCTS.find((x) => x.id === p.id)) .filter((p): p is (typeof PRODUCTS)[number] => !!p && !isOutOfStock(p.id)) .filter((p) => (storeId ? p.storeId === storeId : true)) /** Products eligible for flash delivery: flash-available and in stock. */ const flashProducts = () => PRODUCTS.filter((p) => { const a = AVAIL_BY_ID.get(p.id) return a?.isFlashAvailable === true && !a?.isOutOfStock }) describe('SlotProducts', () => { beforeEach(async () => { await resetStorage() }) it('shows the loading state while slots load', async () => { setupApiMocks({ pendingSlots: true }) renderWithProviders() await waitFor(() => expect(screen.getByText('Loading slot delivery...')).toBeVisible() ) }) it('shows the error state when slots fail', async () => { setupApiMocks({ failSlots: true }) renderWithProviders() await waitFor(() => expect(screen.getByText('Failed to load slot delivery')).toBeVisible() ) }) it('shows the no-slot state for an unknown slot id', async () => { setupApiMocks() renderWithProviders() await waitFor(() => expect(screen.getByText('No delivery slot available.')).toBeVisible() ) }) it('lists the slot products and navigates on press', async () => { setupApiMocks() renderWithProviders() await waitFor(() => expect(screen.getByTestId('slot-detail-page')).toBeVisible()) const first = slotProducts()[0] expect(first).toBeDefined() await waitFor(() => expect(screen.getAllByText(first.name).length).toBeGreaterThan(0) ) fireEvent.press(screen.getAllByText(first.name)[0]) expect(routerMock.push).toHaveBeenCalledWith( `/(drawer)/(tabs)/home/product-detail/${first.id}` ) }) it('shows the empty message when a store has no products in the slot', async () => { setupApiMocks() renderWithProviders( ) await waitFor(() => expect( screen.getByText('No products from this store in this slot.') ).toBeVisible() ) }) }) describe('FlashDeliveryProducts', () => { beforeEach(async () => { await resetStorage() }) it('lists flash-available in-stock products', async () => { setupApiMocks() renderWithProviders() const first = flashProducts()[0] expect(first).toBeDefined() await waitFor(() => expect(screen.getAllByText(first.name).length).toBeGreaterThan(0) ) }) it('filters products by the search box', async () => { setupApiMocks() renderWithProviders() const first = flashProducts()[0] await waitFor(() => expect(screen.getAllByText(first.name).length).toBeGreaterThan(0) ) fireEvent.changeText(screen.UNSAFE_getAllByType(TextInput)[0], 'zzzzzzz') await waitFor(() => expect(screen.getByText('No 1 Hr Delivery products available.')).toBeVisible() ) }) it('shows the empty message when there are no products', async () => { setupApiMocks({ products: { products: [], tags: [] } }) renderWithProviders() await waitFor(() => expect(screen.getByText('No 1 Hr Delivery products available.')).toBeVisible() ) }) })