109 lines
3.4 KiB
TypeScript
109 lines
3.4 KiB
TypeScript
/**
|
|
* ProductDetail (components/ProductDetail.tsx) — the four routes render it.
|
|
* Covers loading, loaded, error and the add-to-cart entry point.
|
|
*/
|
|
|
|
jest.mock('expo-router', () => require('../mocks/router').expoRouterMock())
|
|
|
|
import React from 'react'
|
|
import { fireEvent, screen, waitFor } from '@testing-library/react-native'
|
|
|
|
import ProductDetail from '@/components/ProductDetail'
|
|
import { resetStorage, setupApiMocks } from '../mocks/api'
|
|
import { expectProcedureCalled, lastProcedureInput } from '../mocks/procedures'
|
|
import { useCartStore } from '@/src/store/cartStore'
|
|
import { renderWithProviders } from '../helpers/render'
|
|
|
|
const DETAIL = {
|
|
id: 2,
|
|
productId: 2,
|
|
name: 'Chicken Breast 500 g',
|
|
shortDescription: 'Farm Chicken Breast uncut and solid.',
|
|
price: 215,
|
|
marketPrice: 220,
|
|
flashPrice: 199,
|
|
images: ['https://assets.freshyo.in/p/2.png'],
|
|
unitNotation: '0.5Kg',
|
|
unit: '0.5Kg',
|
|
productType: 'item',
|
|
incrementStep: 1,
|
|
}
|
|
|
|
const REVIEWS = { reviews: [], hasMore: false }
|
|
|
|
describe('ProductDetail', () => {
|
|
beforeEach(async () => {
|
|
await resetStorage()
|
|
useCartStore.getState().clearAddedToCartProduct()
|
|
})
|
|
|
|
it('shows the loading state', async () => {
|
|
setupApiMocks({
|
|
procedures: {
|
|
'user.product.getProductDetails': () => new Promise(() => {}),
|
|
'user.product.getProductReviews': REVIEWS,
|
|
},
|
|
})
|
|
renderWithProviders(<ProductDetail productId="2" />)
|
|
|
|
await waitFor(() =>
|
|
expect(screen.getByText('Loading product details...')).toBeVisible()
|
|
)
|
|
})
|
|
|
|
it('renders the product and loads its reviews', async () => {
|
|
const api = setupApiMocks({
|
|
procedures: {
|
|
'user.product.getProductDetails': DETAIL,
|
|
'user.product.getProductReviews': REVIEWS,
|
|
},
|
|
})
|
|
renderWithProviders(<ProductDetail productId="2" />)
|
|
|
|
await waitFor(() => expect(screen.getByText('Chicken Breast 500 g')).toBeVisible())
|
|
expect(screen.getByText('₹215')).toBeVisible()
|
|
// The flash badge comes from the central productSlotsMap, loaded separately.
|
|
await waitFor(() => expect(screen.getByText('1 Hr Delivery')).toBeVisible())
|
|
|
|
await waitFor(() =>
|
|
expectProcedureCalled(api.fetchMock, 'user.product.getProductReviews')
|
|
)
|
|
expect(lastProcedureInput(api.fetchMock, 'user.product.getProductDetails')).toEqual({
|
|
id: '2',
|
|
regionId: 1,
|
|
})
|
|
})
|
|
|
|
it('shows the error state when the product fails to load', async () => {
|
|
setupApiMocks({
|
|
procedures: {
|
|
'user.product.getProductDetails': () => {
|
|
throw new Error('not found')
|
|
},
|
|
'user.product.getProductReviews': REVIEWS,
|
|
},
|
|
})
|
|
renderWithProviders(<ProductDetail productId="2" />)
|
|
|
|
await waitFor(() =>
|
|
expect(screen.getByText('Product not found or error loading')).toBeVisible()
|
|
)
|
|
expect(screen.getByText('Oops!')).toBeVisible()
|
|
})
|
|
|
|
it('queues the add-to-cart dialog from the Add to Cart button', async () => {
|
|
setupApiMocks({
|
|
procedures: {
|
|
'user.product.getProductDetails': DETAIL,
|
|
'user.product.getProductReviews': REVIEWS,
|
|
},
|
|
})
|
|
renderWithProviders(<ProductDetail productId="2" />)
|
|
|
|
await waitFor(() => expect(screen.getByText('Chicken Breast 500 g')).toBeVisible())
|
|
|
|
fireEvent.press(screen.getByText('Add to Cart'))
|
|
|
|
expect(useCartStore.getState().addedToCartProduct).toMatchObject({ productId: 2 })
|
|
})
|
|
})
|