freshyo/apps/user-ui/tests/home-page/home-cards.test.tsx
2026-09-26 00:46:08 +05:30

102 lines
3 KiB
TypeScript

/**
* Home card/list details: combo-only SKUs are hidden, every store renders with
* its display name, and ProductCard's market-price and image-error branches.
*/
jest.mock('expo-router', () => require('../mocks/router').expoRouterMock())
import { Image } from 'react-native'
import { fireEvent, screen, within } from '@testing-library/react-native'
import { outOfStockIds, resetStorage, setupApiMocks } from '../mocks/api'
import storesResponse from '../mocks/mock-stores-response.json'
import { useNavigationStore } from '@/src/store/navigationStore'
import {
expandTag,
marketPriceById,
priceById,
productIdsForTag,
renderLoadedHome,
tags,
} from './render'
const COMBO_ONLY_ID = 149
const firstInStockInTag = () => {
for (const tag of tags) {
for (const id of productIdsForTag(tag)) {
if (!outOfStockIds.has(id)) return { tagId: tag.id, productId: id }
}
}
throw new Error('[tests] no in-stock product in any tag')
}
/** First in-stock tag product whose marketPrice is above its selling price. */
const productWithMarkup = () => {
for (const tag of tags) {
for (const id of productIdsForTag(tag)) {
if (outOfStockIds.has(id)) continue
const marketPrice = marketPriceById.get(id)
const price = priceById.get(id)
if (marketPrice != null && price != null && marketPrice > price) {
return { tagId: tag.id, productId: id, marketPrice }
}
}
}
throw new Error('[tests] no in-stock product with a market price above its price')
}
describe('Home cards + lists', () => {
beforeEach(async () => {
setupApiMocks()
await resetStorage()
useNavigationStore.setState({
isNavigatedFromHome: false,
selectedStoreId: null,
})
})
it('never renders a combo-only SKU', async () => {
await renderLoadedHome()
expect(screen.queryByTestId(`product-item-${COMBO_ONLY_ID}`)).toBeNull()
for (const tag of tags) {
expandTag(tag.id)
expect(screen.queryByTestId(`tag-product-${COMBO_ONLY_ID}`)).toBeNull()
}
})
it('renders every store with the leading "The " stripped', async () => {
await renderLoadedHome()
for (const store of storesResponse.stores) {
const row = screen.getByTestId(`store-item-${store.id}`)
expect(
within(row).getByText(store.name.replace(/^The\s+/i, ''))
).toBeVisible()
}
})
it('strikes through the market price when it is above the selling price', async () => {
await renderLoadedHome()
const target = productWithMarkup()
expandTag(target.tagId)
const card = screen.getByTestId(`tag-product-${target.productId}`)
expect(within(card).getByText(`₹${target.marketPrice}`)).toBeVisible()
})
it('shows a fallback when a product image fails to load', async () => {
await renderLoadedHome()
const target = firstInStockInTag()
expandTag(target.tagId)
const card = screen.getByTestId(`tag-product-${target.productId}`)
fireEvent(within(card).UNSAFE_getByType(Image), 'error')
expect(within(card).getByText('Image failed to load')).toBeVisible()
})
})