| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- /**
- * Tests for cfc-frontend utils/bundle-pricing.js
- *
- * 验证套餐优惠的纯函数语义:未命中/未激活 → 0;active=true + discount>0 才生效。
- */
- const {
- bundleDiscountOf,
- formatBundleLines,
- applyBundleDiscount
- } = require('@/utils/bundle-pricing')
- describe('bundleDiscountOf', () => {
- it('null/undefined → 0', () => {
- expect(bundleDiscountOf(null)).toBe(0)
- expect(bundleDiscountOf(undefined)).toBe(0)
- })
- it('active 非 true → 0', () => {
- expect(bundleDiscountOf({ active: false, discount: 11800 })).toBe(0)
- expect(bundleDiscountOf({ active: true, discount: 11800 })).not.toBe(0)
- expect(bundleDiscountOf({})).toBe(0)
- })
- it('active=true + 正折扣 → 折扣值', () => {
- expect(bundleDiscountOf({ active: true, discount: 11800 })).toBe(11800)
- })
- it('active=true + 非正折扣 → 0', () => {
- expect(bundleDiscountOf({ active: true, discount: 0 })).toBe(0)
- expect(bundleDiscountOf({ active: true, discount: -1 })).toBe(0)
- })
- })
- describe('formatBundleLines', () => {
- it('null/空数组 → []', () => {
- expect(formatBundleLines(null)).toEqual([])
- expect(formatBundleLines([])).toEqual([])
- })
- it('过滤无 bundleName 的行', () => {
- const rows = formatBundleLines([
- { bundleName: '套餐一', times: 1, saving: 11800 },
- { times: 2 }
- ])
- expect(rows).toHaveLength(1)
- expect(rows[0].name).toBe('套餐一')
- })
- it('补齐默认 times/saving', () => {
- const rows = formatBundleLines([{ bundleName: '套餐一' }])
- expect(rows[0].times).toBe(1)
- expect(rows[0].saving).toBe(0)
- })
- })
- describe('applyBundleDiscount', () => {
- it('未命中 → 原总价', () => {
- expect(applyBundleDiscount(139800, null)).toBe(139800)
- })
- it('命中 → 扣减套餐优惠', () => {
- expect(applyBundleDiscount(139800, { active: true, discount: 11800 })).toBe(128000)
- })
- })
|