freshyo/apps/user-ui/tests/components/product-card.test.tsx
2026-09-26 13:41:46 +05:30

131 lines
3.6 KiB
TypeScript

/**
* ProductCard (components/ProductCard.tsx) — component-level branches.
* Seeds the central stores directly so each branch is driven deterministically.
*/
jest.mock('expo-router', () => require('../mocks/router').expoRouterMock())
import React from 'react'
import { Image } from 'react-native'
import { fireEvent, screen } from '@testing-library/react-native'
import dayjs from 'dayjs'
import ProductCard from '@/components/ProductCard'
import { resetStorage, setupApiMocks } from '../mocks/api'
import { useCartStore } from '@/src/store/cartStore'
import { useCentralProductStore } from '@/src/store/centralProductStore'
import { useCentralSlotStore } from '@/src/store/centralSlotStore'
import { renderWithProviders } from '../helpers/render'
const futureSlot = {
id: 1849,
deliveryTime: dayjs().add(3, 'hour').toISOString(),
freezeTime: dayjs().add(2, 'hour').toISOString(),
}
const product = (over: Record<string, unknown> = {}) => ({
id: 2,
name: 'Chicken Breast 500 g',
price: 215,
marketPrice: 220,
flashPrice: null,
images: ['https://assets.freshyo.in/p/2.png'],
incrementStep: 1,
productQuantity: 1,
unitNotation: '0.5Kg',
productType: 'item',
storeId: 1,
...over,
})
const seedStores = (opts: { isOutOfStock?: boolean } = {}) => {
useCentralProductStore.setState({
products: [],
productsById: { 2: product() } as never,
})
useCentralSlotStore.setState({
slots: [futureSlot] as never,
productSlotsMap: {
2: {
slots: [futureSlot],
isOutOfStock: opts.isOutOfStock ?? false,
isFlashAvailable: false,
isSuspended: false,
},
} as never,
})
}
describe('ProductCard', () => {
beforeEach(async () => {
await resetStorage()
setupApiMocks()
useCartStore.getState().clearAddedToCartProduct()
})
it('renders the name, price, struck market price and unit', () => {
seedStores()
renderWithProviders(
<ProductCard item={product()} itemWidth={100} miniView variant="hero" />,
{ centralStores: false }
)
expect(screen.getByText('Chicken Breast 500 g')).toBeVisible()
expect(screen.getByText('₹215')).toBeVisible()
expect(screen.getByText('₹220')).toBeVisible()
expect(screen.getByText('0.5Kg')).toBeVisible()
})
it('shows the out-of-stock overlay when unavailable', () => {
seedStores({ isOutOfStock: true })
renderWithProviders(
<ProductCard item={product()} itemWidth={100} miniView />,
{ centralStores: false }
)
expect(screen.getByTestId('out-of-stock-badge')).toBeVisible()
})
it('queues the product when the add icon is pressed (dialog mode)', () => {
seedStores()
renderWithProviders(
<ProductCard
item={product()}
itemWidth={100}
miniView
variant="hero"
useAddToCartDialog
/>,
{ centralStores: false }
)
fireEvent.press(screen.getByTestId('add-to-cart-icon'))
expect(useCartStore.getState().addedToCartProduct).toMatchObject({ productId: 2 })
})
it('shows the image-error fallback', () => {
seedStores()
renderWithProviders(
<ProductCard item={product()} itemWidth={100} miniView />,
{ centralStores: false }
)
fireEvent(screen.UNSAFE_getAllByType(Image)[0], 'error')
expect(screen.getByText('Image failed to load')).toBeVisible()
})
it('calls onPress when the card is tapped', () => {
seedStores()
const onPress = jest.fn()
renderWithProviders(
<ProductCard item={product()} itemWidth={100} onPress={onPress} miniView />,
{ centralStores: false }
)
fireEvent.press(screen.getByTestId('product-card'))
expect(onPress).toHaveBeenCalled()
})
})