120 lines
4.7 KiB
TypeScript
120 lines
4.7 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
/**
|
|
* Section 26 — ProductCard coupon price (PCARD-01..08).
|
|
*
|
|
* The card reads `couponOffers` from the central product store (written by the
|
|
* catalogue initializer when `user.coupon.getEligible` lands) and derives the
|
|
* coupon price from the price it actually shows — `price` on the regular card,
|
|
* `flashPrice` on the 1 Hr Delivery card. Nothing here is fetched by the card.
|
|
*
|
|
* Fixtures: Chicken Breast (sku 2) is ₹215, Paneer (sku 8) is ₹90 with a ₹75
|
|
* flash price, Mutton (sku 6) is out of stock.
|
|
*/
|
|
import * as React from 'react'
|
|
import { StyleSheet } from 'react-native'
|
|
import { waitFor } from '@testing-library/react-native'
|
|
import { theme } from 'common-ui'
|
|
import ProductCard from '@/components/ProductCard'
|
|
import { useCentralProductStore } from '@/src/store/centralProductStore'
|
|
import { renderWithProviders, seedBoot, flush } from '../helpers/test-utils'
|
|
import { resetAllMocks } from '../helpers/reset'
|
|
import { P_CHICKEN, P_CUCUMBER, P_FLASH, P_OUT } from '../fixtures'
|
|
|
|
/** 10% capped ₹40 (sku 2): ₹215 → ₹194. */
|
|
const CHICKEN_OFFER = {
|
|
couponId: 24,
|
|
couponCode: 'TARGET10',
|
|
discountPercent: '10',
|
|
flatDiscount: null,
|
|
maxValue: 40,
|
|
}
|
|
|
|
/** 10% capped ₹20 (sku 8): ₹90 → ₹81, flash ₹75 → ₹68. */
|
|
const PANEER_OFFER = {
|
|
couponId: 31,
|
|
couponCode: 'FLASH10',
|
|
discountPercent: '10',
|
|
flatDiscount: null,
|
|
maxValue: 20,
|
|
}
|
|
|
|
async function renderCard(item: any, props: Record<string, any> = {}) {
|
|
seedBoot()
|
|
const utils = renderWithProviders(
|
|
<ProductCard item={item} itemWidth={180} {...props} />,
|
|
{ auth: false }
|
|
)
|
|
await flush(2)
|
|
return utils
|
|
}
|
|
|
|
const fontSizeOf = (node: any) => StyleSheet.flatten(node.props.style)?.fontSize
|
|
const colorOf = (node: any) => StyleSheet.flatten(node.props.style)?.color
|
|
|
|
describe('ProductCard coupon price', () => {
|
|
beforeEach(() => resetAllMocks())
|
|
|
|
it('PCARD-01: no coupon — no coupon row', async () => {
|
|
const { queryByTestId } = await renderCard(P_CHICKEN)
|
|
|
|
expect(queryByTestId('product-coupon-price-2')).toBeNull()
|
|
})
|
|
|
|
it('PCARD-02: a product coupon shows the discounted price with a "With Coupon" label', async () => {
|
|
useCentralProductStore.getState().setCouponOffers({ 2: [CHICKEN_OFFER] })
|
|
const { getByTestId, getByText } = await renderCard(P_CHICKEN)
|
|
|
|
await waitFor(() => expect(getByTestId('product-coupon-price-2')).toBeTruthy())
|
|
expect(getByTestId('product-coupon-price-2')).toHaveTextContent('₹194')
|
|
expect(getByText('With Coupon')).toBeTruthy()
|
|
})
|
|
|
|
it('PCARD-03: the listed price keeps its size, the coupon price matches the struck price', async () => {
|
|
useCentralProductStore.getState().setCouponOffers({ 2: [CHICKEN_OFFER] })
|
|
const { getByText } = await renderCard(P_CHICKEN)
|
|
|
|
// The coupon must not outshine the price: text-base stays text-base (16),
|
|
// the struck price is untouched (12), and the coupon price sits at 12 too.
|
|
expect(fontSizeOf(getByText('₹215'))).toBe(16)
|
|
expect(fontSizeOf(getByText('₹220'))).toBe(12)
|
|
expect(fontSizeOf(getByText('₹194'))).toBe(12)
|
|
})
|
|
|
|
it('PCARD-04: the coupon price is in the brand colour, the label stays muted', async () => {
|
|
useCentralProductStore.getState().setCouponOffers({ 2: [CHICKEN_OFFER] })
|
|
const { getByText } = await renderCard(P_CHICKEN)
|
|
|
|
expect(colorOf(getByText('₹194'))).toBe(theme.colors.brand500)
|
|
expect(colorOf(getByText('With Coupon'))).toBe(theme.colors.gray400)
|
|
})
|
|
|
|
it('PCARD-05: without a coupon the price is unchanged', async () => {
|
|
const { getByText } = await renderCard(P_CHICKEN)
|
|
|
|
expect(fontSizeOf(getByText('₹215'))).toBe(16)
|
|
expect(fontSizeOf(getByText('₹220'))).toBe(12)
|
|
})
|
|
|
|
it('PCARD-06: a coupon for another sku leaves this card alone', async () => {
|
|
useCentralProductStore.getState().setCouponOffers({ 2: [CHICKEN_OFFER] })
|
|
const { queryByTestId } = await renderCard(P_CUCUMBER)
|
|
|
|
expect(queryByTestId('product-coupon-price-5')).toBeNull()
|
|
})
|
|
|
|
it('PCARD-07: a flash card discounts the flash price it displays', async () => {
|
|
useCentralProductStore.getState().setCouponOffers({ 8: [PANEER_OFFER] })
|
|
const { getByTestId } = await renderCard(P_FLASH, { cartType: 'flash' })
|
|
|
|
await waitFor(() => expect(getByTestId('product-coupon-price-8')).toBeTruthy())
|
|
// ₹75 flash - 10% (capped ₹20) = ₹67.5 -> ₹68
|
|
expect(getByTestId('product-coupon-price-8')).toHaveTextContent('₹68')
|
|
})
|
|
|
|
it('PCARD-08: an out-of-stock card shows no coupon price', async () => {
|
|
useCentralProductStore.getState().setCouponOffers({ 6: [CHICKEN_OFFER] })
|
|
const { queryByTestId } = await renderCard(P_OUT)
|
|
|
|
expect(queryByTestId('product-coupon-price-6')).toBeNull()
|
|
})
|
|
})
|