153 lines
4.8 KiB
TypeScript
153 lines
4.8 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
/**
|
|
* Catalogue section 24 — `CouponBreakdownDialog` (CBD-01..07).
|
|
*
|
|
* The dialog renders the per-line split of a product-targeted coupon
|
|
* (`computeCouponLineBreakdowns`). Rows come from the central product store for
|
|
* names/images and from the `cartLines` prop for the qty x price line.
|
|
*/
|
|
import * as React from 'react'
|
|
import { fireEvent } from '@testing-library/react-native'
|
|
import CouponBreakdownDialog from '@/src/components/CouponBreakdownDialog'
|
|
import { renderWithProviders, seedCentralStores } from '../helpers/test-utils'
|
|
import { resetAllMocks } from '../helpers/reset'
|
|
import type { CartLine, CouponLineBreakdown } from '@packages/shared'
|
|
|
|
/** The Unicode minus sign the component renders before the rupee amount. */
|
|
const MINUS = '\u2212'
|
|
|
|
const line = (skuId: number, quantity: number, unitPrice: number): CartLine => ({
|
|
skuId,
|
|
quantity,
|
|
unitPrice,
|
|
})
|
|
|
|
const breakdown = (
|
|
couponId: number,
|
|
discount: number,
|
|
lines: { skuId: number; discount: number }[]
|
|
): CouponLineBreakdown => ({ couponId, discount, lines })
|
|
|
|
interface SetupProps {
|
|
coupon?: { id: number; code: string } | null
|
|
breakdown?: CouponLineBreakdown | null
|
|
cartLines?: CartLine[]
|
|
onClose?: jest.Mock
|
|
}
|
|
|
|
function setup(props: SetupProps = {}) {
|
|
seedCentralStores()
|
|
const onClose = props.onClose ?? jest.fn()
|
|
const result = renderWithProviders(
|
|
<CouponBreakdownDialog
|
|
coupon={props.coupon === undefined ? { id: 21, code: 'SAVE10' } : props.coupon}
|
|
breakdown={
|
|
props.breakdown === undefined
|
|
? breakdown(21, 40, [{ skuId: 2, discount: 40 }])
|
|
: props.breakdown
|
|
}
|
|
cartLines={props.cartLines ?? [line(2, 2, 215)]}
|
|
onClose={onClose}
|
|
/>,
|
|
{ auth: false }
|
|
)
|
|
return { ...result, onClose }
|
|
}
|
|
|
|
describe('CouponBreakdownDialog', () => {
|
|
beforeEach(() => resetAllMocks())
|
|
|
|
it('CBD-01: keeps the dialog closed when the coupon or the breakdown is null', () => {
|
|
seedCentralStores()
|
|
const first = renderWithProviders(
|
|
<CouponBreakdownDialog
|
|
coupon={null}
|
|
breakdown={breakdown(21, 40, [{ skuId: 2, discount: 40 }])}
|
|
cartLines={[line(2, 2, 215)]}
|
|
onClose={jest.fn()}
|
|
/>,
|
|
{ auth: false }
|
|
)
|
|
expect(first.queryByTestId('coupon-breakdown-dialog')).toBeNull()
|
|
first.unmount()
|
|
|
|
const second = renderWithProviders(
|
|
<CouponBreakdownDialog
|
|
coupon={{ id: 21, code: 'SAVE10' }}
|
|
breakdown={null}
|
|
cartLines={[line(2, 2, 215)]}
|
|
onClose={jest.fn()}
|
|
/>,
|
|
{ auth: false }
|
|
)
|
|
expect(second.queryByTestId('coupon-breakdown-dialog')).toBeNull()
|
|
})
|
|
|
|
it('CBD-02: shows the empty-split message when every row discount is zero', () => {
|
|
const { getByTestId, queryByText, queryByTestId } = setup({
|
|
breakdown: breakdown(21, 0, [
|
|
{ skuId: 2, discount: 0 },
|
|
{ skuId: 5, discount: 0 },
|
|
]),
|
|
})
|
|
|
|
expect(getByTestId('coupon-breakdown-dialog')).toBeTruthy()
|
|
expect(queryByText('No per-product split to show for this order.')).toBeTruthy()
|
|
expect(queryByTestId('coupon-breakdown-row-2')).toBeNull()
|
|
})
|
|
|
|
it('CBD-03: lists only discount-bearing rows, largest first', () => {
|
|
const { getAllByTestId } = setup({
|
|
breakdown: breakdown(21, 70, [
|
|
{ skuId: 2, discount: 10 },
|
|
{ skuId: 5, discount: 40 },
|
|
{ skuId: 8, discount: 20 },
|
|
]),
|
|
})
|
|
|
|
const ids = getAllByTestId(/^coupon-breakdown-row-/).map((node) => node.props.testID)
|
|
expect(ids).toEqual([
|
|
'coupon-breakdown-row-5',
|
|
'coupon-breakdown-row-8',
|
|
'coupon-breakdown-row-2',
|
|
])
|
|
})
|
|
|
|
it('CBD-04: falls back to "Product #id" for an unknown SKU', () => {
|
|
const { getByText, getByTestId } = setup({
|
|
breakdown: breakdown(21, 40, [{ skuId: 999, discount: 40 }]),
|
|
cartLines: [],
|
|
})
|
|
|
|
expect(getByTestId('coupon-breakdown-row-999')).toBeTruthy()
|
|
expect(getByText('Product #999')).toBeTruthy()
|
|
})
|
|
|
|
it('CBD-05: renders the row without the qty x price line when the cart line is missing', () => {
|
|
const { getByTestId, getByText, queryByText } = setup({
|
|
breakdown: breakdown(21, 40, [{ skuId: 2, discount: 40 }]),
|
|
cartLines: [],
|
|
})
|
|
|
|
expect(getByTestId('coupon-breakdown-row-2')).toBeTruthy()
|
|
expect(getByText('Chicken Breast')).toBeTruthy()
|
|
expect(queryByText(new RegExp(`\\u00d7 \\u20b9`))).toBeNull()
|
|
})
|
|
|
|
it('CBD-06: truncates the coupon total to whole rupees', () => {
|
|
const { getByText } = setup({
|
|
breakdown: breakdown(21, 43.9, [{ skuId: 2, discount: 43.9 }]),
|
|
})
|
|
|
|
expect(getByText(`${MINUS}\u20b943`)).toBeTruthy()
|
|
})
|
|
|
|
it('CBD-07: the close button calls onClose', () => {
|
|
const onClose = jest.fn()
|
|
const { getByTestId } = setup({ onClose })
|
|
|
|
fireEvent.press(getByTestId('coupon-breakdown-close'))
|
|
|
|
expect(onClose).toHaveBeenCalledTimes(1)
|
|
})
|
|
})
|