bundle-pricing.test.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /**
  2. * Tests for cfc-frontend utils/bundle-pricing.js
  3. *
  4. * 验证购物车套餐offer展示辅助:comboOfferOf 命中/未激活语义;formatComboOfferText 提示文案;
  5. * 总价不再被自动扣减(纯展示,权威计算在后端 POST /api/cart/combo-offer)。
  6. */
  7. const {
  8. comboOfferOf,
  9. formatComboOfferText
  10. } = require('@/utils/bundle-pricing')
  11. describe('comboOfferOf', () => {
  12. it('null/undefined → null', () => {
  13. expect(comboOfferOf(null)).toBeNull()
  14. expect(comboOfferOf(undefined)).toBeNull()
  15. })
  16. it('active 非 true → null', () => {
  17. expect(comboOfferOf({ active: false, comboName: '套餐X' })).toBeNull()
  18. expect(comboOfferOf({})).toBeNull()
  19. })
  20. it('active=true → 返回 offer(纯展示,不篡改总价)', () => {
  21. const offer = { active: true, comboName: '套餐X', comboPrice: 15000, saving: 2000, groups: 1 }
  22. expect(comboOfferOf(offer)).toBe(offer)
  23. })
  24. })
  25. describe('formatComboOfferText', () => {
  26. it('无 offer → 空串', () => {
  27. expect(formatComboOfferText(null)).toBe('')
  28. expect(formatComboOfferText({ active: false })).toBe('')
  29. })
  30. it('有 offer → 「这N件组成套餐「XX」,套餐价 ¥Y(省 ¥Z)」', () => {
  31. const txt = formatComboOfferText({ active: true, comboName: '豪华套餐', comboPrice: 15000, saving: 2000 }, 3)
  32. expect(txt).toContain('3 件')
  33. expect(txt).toContain('豪华套餐')
  34. expect(txt).toContain('150.00')
  35. expect(txt).toContain('20.00')
  36. })
  37. it('有 offer 无 checkedCount → 用 groups 兜底;saving=0 不显示省', () => {
  38. const txt = formatComboOfferText({ active: true, comboName: '豪华套餐', comboPrice: 15000, saving: 0, groups: 2 })
  39. expect(txt).toContain('2 件')
  40. expect(txt).not.toContain('省')
  41. })
  42. })