144 lines
5.1 KiB
TypeScript
144 lines
5.1 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
/**
|
|
* Catalogue coupon pricing (`src/lib/product-coupons.ts`).
|
|
*
|
|
* The card and the product detail screen both read `couponOffers` from the
|
|
* central product store and turn them into a price with `couponPriceFor`. These
|
|
* cases pin the rules that decide *which* coupons are advertised at all and how
|
|
* the money is rounded, because the figure has to match what the cart charges.
|
|
*
|
|
* Fixtures (see tests/fixtures): Chicken Breast (sku 2) is ₹215, Paneer (sku 8)
|
|
* is ₹90 with a ₹75 flash price.
|
|
*/
|
|
import { buildCouponOffers, couponPriceFor } from '@/src/lib/product-coupons'
|
|
import {
|
|
C1,
|
|
C4,
|
|
C7,
|
|
C_SKU2_FIRST,
|
|
C_SKU2_FLAT,
|
|
C_SKU2_MIN,
|
|
C_SKU8_FLASH,
|
|
} from '../fixtures'
|
|
|
|
const priceOf = (offers: Record<number, any[]>, skuId: number, price: number) =>
|
|
couponPriceFor(offers[skuId], price)
|
|
|
|
describe('catalogue coupon pricing', () => {
|
|
it('COUPON-01: a product-specific coupon is mapped onto the sku it targets', () => {
|
|
const offers = buildCouponOffers([C4 as any], { regionId: 1, isFirstOrder: false })
|
|
|
|
expect(offers[2]).toHaveLength(1)
|
|
expect(offers[2][0]).toMatchObject({ couponId: 24, couponCode: 'TARGET10' })
|
|
expect(offers[5]).toBeUndefined()
|
|
})
|
|
|
|
it('COUPON-02: a cart-wide coupon (no skuIds) is not a product offer', () => {
|
|
const offers = buildCouponOffers([C1 as any], { regionId: 1, isFirstOrder: true })
|
|
|
|
expect(offers).toEqual({})
|
|
})
|
|
|
|
it('COUPON-03: a null skuIds list is treated as cart-wide, not as a target', () => {
|
|
const offers = buildCouponOffers(
|
|
[{ ...C4, skuIds: null } as any],
|
|
{ regionId: 1, isFirstOrder: true }
|
|
)
|
|
|
|
expect(offers).toEqual({})
|
|
})
|
|
|
|
it('COUPON-04: a coupon from another region is not advertised', () => {
|
|
expect(buildCouponOffers([{ ...C7, skuIds: [2] } as any], { regionId: 1, isFirstOrder: true })).toEqual({})
|
|
expect(buildCouponOffers([{ ...C7, skuIds: [2] } as any], { regionId: 2, isFirstOrder: true })[2]).toHaveLength(1)
|
|
})
|
|
|
|
it('COUPON-05: a coupon with no region applies in every region', () => {
|
|
const offers = buildCouponOffers(
|
|
[{ ...C4, regionId: null } as any],
|
|
{ regionId: 7, isFirstOrder: true }
|
|
)
|
|
|
|
expect(offers[2]).toHaveLength(1)
|
|
})
|
|
|
|
it('COUPON-06: first-order-only coupons are dropped once the customer has ordered', () => {
|
|
expect(buildCouponOffers([C_SKU2_FIRST as any], { regionId: 1, isFirstOrder: false })).toEqual({})
|
|
expect(buildCouponOffers([C_SKU2_FIRST as any], { regionId: 1, isFirstOrder: true })[2]).toHaveLength(1)
|
|
})
|
|
|
|
it('COUPON-07: an invalidated coupon is never advertised', () => {
|
|
const offers = buildCouponOffers(
|
|
[{ ...C4, isInvalidated: true } as any],
|
|
{ regionId: 1, isFirstOrder: true }
|
|
)
|
|
|
|
expect(offers).toEqual({})
|
|
})
|
|
|
|
it('COUPON-08: minOrder is ignored on the catalogue (it is a cart-level rule)', () => {
|
|
// C_SKU2_MIN needs a ₹1000 cart, but a single ₹215 item is still advertised.
|
|
const offers = buildCouponOffers([C_SKU2_MIN as any], { regionId: 1, isFirstOrder: true })
|
|
|
|
expect(offers[2]).toHaveLength(1)
|
|
expect(priceOf(offers, 2, 215)).toBe(195)
|
|
})
|
|
|
|
it('COUPON-09: every coupon targeting the same sku is kept for price comparison', () => {
|
|
const offers = buildCouponOffers(
|
|
[C4 as any, C_SKU2_FLAT as any],
|
|
{ regionId: 1, isFirstOrder: true }
|
|
)
|
|
|
|
expect(offers[2]).toHaveLength(2)
|
|
})
|
|
|
|
it('COUPON-10: a 10% coupon rounds to whole rupees, matching the cart', () => {
|
|
const offers = buildCouponOffers([C4 as any], { regionId: 1, isFirstOrder: true })
|
|
|
|
// 215 - min(21.5, 40) = 193.5 -> 194
|
|
expect(priceOf(offers, 2, 215)).toBe(194)
|
|
})
|
|
|
|
it('COUPON-11: the best of several coupons wins for the price shown', () => {
|
|
const offers = buildCouponOffers(
|
|
[C4 as any, C_SKU2_FLAT as any],
|
|
{ regionId: 1, isFirstOrder: true }
|
|
)
|
|
|
|
// ₹100 flat beats 10% of ₹215.
|
|
expect(priceOf(offers, 2, 215)).toBe(115)
|
|
})
|
|
|
|
it('COUPON-12: the same offer prices a flash item from its flash price', () => {
|
|
const offers = buildCouponOffers([C_SKU8_FLASH as any], { regionId: 1, isFirstOrder: true })
|
|
|
|
expect(priceOf(offers, 8, 90)).toBe(81)
|
|
// 75 - 7.5 = 67.5 -> 68
|
|
expect(priceOf(offers, 8, 75)).toBe(68)
|
|
})
|
|
|
|
it('COUPON-13: a missing or unknown price yields no coupon price', () => {
|
|
const offers = buildCouponOffers([C4 as any], { regionId: 1, isFirstOrder: true })
|
|
|
|
expect(couponPriceFor(offers[2], 0)).toBeNull()
|
|
expect(couponPriceFor(offers[2], null)).toBeNull()
|
|
expect(couponPriceFor(offers[2], undefined)).toBeNull()
|
|
expect(couponPriceFor(offers[2], 'not-a-number')).toBeNull()
|
|
})
|
|
|
|
it('COUPON-14: a coupon that discounts nothing yields no coupon price', () => {
|
|
const offers = buildCouponOffers(
|
|
[{ ...C4, discountPercent: '0', maxValue: null } as any],
|
|
{ regionId: 1, isFirstOrder: true }
|
|
)
|
|
|
|
expect(priceOf(offers, 2, 215)).toBeNull()
|
|
})
|
|
|
|
it('COUPON-15: an unknown sku has no offers', () => {
|
|
const offers = buildCouponOffers([C4 as any], { regionId: 1, isFirstOrder: true })
|
|
|
|
expect(couponPriceFor(offers[999], 215)).toBeNull()
|
|
})
|
|
})
|