bundle-pricing.test.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /**
  2. * Tests for cfc-frontend utils/bundle-pricing.js
  3. *
  4. * 验证套餐优惠的纯函数语义:未命中/未激活 → 0;active=true + discount>0 才生效。
  5. */
  6. const {
  7. bundleDiscountOf,
  8. formatBundleLines,
  9. applyBundleDiscount
  10. } = require('@/utils/bundle-pricing')
  11. describe('bundleDiscountOf', () => {
  12. it('null/undefined → 0', () => {
  13. expect(bundleDiscountOf(null)).toBe(0)
  14. expect(bundleDiscountOf(undefined)).toBe(0)
  15. })
  16. it('active 非 true → 0', () => {
  17. expect(bundleDiscountOf({ active: false, discount: 11800 })).toBe(0)
  18. expect(bundleDiscountOf({ active: true, discount: 11800 })).not.toBe(0)
  19. expect(bundleDiscountOf({})).toBe(0)
  20. })
  21. it('active=true + 正折扣 → 折扣值', () => {
  22. expect(bundleDiscountOf({ active: true, discount: 11800 })).toBe(11800)
  23. })
  24. it('active=true + 非正折扣 → 0', () => {
  25. expect(bundleDiscountOf({ active: true, discount: 0 })).toBe(0)
  26. expect(bundleDiscountOf({ active: true, discount: -1 })).toBe(0)
  27. })
  28. })
  29. describe('formatBundleLines', () => {
  30. it('null/空数组 → []', () => {
  31. expect(formatBundleLines(null)).toEqual([])
  32. expect(formatBundleLines([])).toEqual([])
  33. })
  34. it('过滤无 bundleName 的行', () => {
  35. const rows = formatBundleLines([
  36. { bundleName: '套餐一', times: 1, saving: 11800 },
  37. { times: 2 }
  38. ])
  39. expect(rows).toHaveLength(1)
  40. expect(rows[0].name).toBe('套餐一')
  41. })
  42. it('补齐默认 times/saving', () => {
  43. const rows = formatBundleLines([{ bundleName: '套餐一' }])
  44. expect(rows[0].times).toBe(1)
  45. expect(rows[0].saving).toBe(0)
  46. })
  47. })
  48. describe('applyBundleDiscount', () => {
  49. it('未命中 → 原总价', () => {
  50. expect(applyBundleDiscount(139800, null)).toBe(139800)
  51. })
  52. it('命中 → 扣减套餐优惠', () => {
  53. expect(applyBundleDiscount(139800, { active: true, discount: 11800 })).toBe(128000)
  54. })
  55. })