format.test.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /**
  2. * Tests for cfc-frontend utils/format.js
  3. *
  4. * Tests the price/rate/score formatting utilities.
  5. * These are pure functions - no uni mock needed.
  6. */
  7. describe('format.js', () => {
  8. let format
  9. beforeAll(() => {
  10. format = require('@/utils/format')
  11. })
  12. // --------------- formatPrice ---------------
  13. describe('formatPrice', () => {
  14. test('converts cents to yuan with 2 decimals', () => {
  15. expect(format.formatPrice(1250)).toBe('12.50')
  16. expect(format.formatPrice(0)).toBe('0.00')
  17. expect(format.formatPrice(999)).toBe('9.99')
  18. expect(format.formatPrice(10000)).toBe('100.00')
  19. })
  20. test('handles null and undefined', () => {
  21. expect(format.formatPrice(null)).toBeNull()
  22. expect(format.formatPrice(undefined)).toBeNull()
  23. })
  24. test('handles zero cents', () => {
  25. expect(format.formatPrice(0)).toBe('0.00')
  26. })
  27. })
  28. // --------------- formatPriceWithSymbol ---------------
  29. describe('formatPriceWithSymbol', () => {
  30. test('adds ¥ prefix and 2 decimals', () => {
  31. expect(format.formatPriceWithSymbol(1250)).toBe('¥12.50')
  32. expect(format.formatPriceWithSymbol(0)).toBe('¥0.00')
  33. expect(format.formatPriceWithSymbol(999)).toBe('¥9.99')
  34. })
  35. test('returns login message for null/undefined', () => {
  36. expect(format.formatPriceWithSymbol(null)).toBe('登录查看')
  37. expect(format.formatPriceWithSymbol(undefined)).toBe('登录查看')
  38. })
  39. })
  40. // --------------- formatRate ---------------
  41. describe('formatRate', () => {
  42. test('converts permillage to percentage string', () => {
  43. expect(format.formatRate(235)).toBe('23.5%')
  44. expect(format.formatRate(0)).toBe('0%')
  45. expect(format.formatRate(1000)).toBe('100%')
  46. expect(format.formatRate(5)).toBe('0.5%')
  47. })
  48. test('returns -- for null/undefined', () => {
  49. expect(format.formatRate(null)).toBe('--')
  50. expect(format.formatRate(undefined)).toBe('--')
  51. })
  52. })
  53. // --------------- formatScore ---------------
  54. describe('formatScore', () => {
  55. test('converts percent-scale to fixed decimal', () => {
  56. expect(format.formatScore(450)).toBe('4.5')
  57. expect(format.formatScore(0)).toBe('0.0')
  58. expect(format.formatScore(1000)).toBe('10.0')
  59. })
  60. test('uses default 1 decimal place', () => {
  61. expect(format.formatScore(450)).toBe('4.5')
  62. })
  63. test('supports custom decimal places', () => {
  64. expect(format.formatScore(450, 2)).toBe('4.50')
  65. expect(format.formatScore(450, 0)).toBe('5')
  66. })
  67. test('returns -- for null/undefined', () => {
  68. expect(format.formatScore(null)).toBe('--')
  69. expect(format.formatScore(undefined)).toBe('--')
  70. })
  71. })
  72. })