format.test.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. // --------------- parseDate ---------------
  73. describe('parseDate', () => {
  74. // 断言使用本地时间字段(getFullYear/getMonth/getDate/...),
  75. // 避免 Node(V8)对纯日期字符串按 UTC 解析带来的时区差异。
  76. const ymd = (d) => {
  77. return d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate()
  78. }
  79. test('parses MySQL DATETIME with space separator "yyyy-MM-dd HH:mm"', () => {
  80. // iOS 原生 new Date() 无法解析空格分隔格式,parseDate 必须归一化成功
  81. const d = format.parseDate('2026-08-22 14:00')
  82. expect(d).not.toBeNull()
  83. expect(ymd(d)).toBe('2026-8-22')
  84. expect(d.getHours()).toBe(14)
  85. expect(d.getMinutes()).toBe(0)
  86. })
  87. test('parses MySQL DATETIME with seconds', () => {
  88. const d = format.parseDate('2026-08-22 14:00:30')
  89. expect(d).not.toBeNull()
  90. expect(ymd(d)).toBe('2026-8-22')
  91. expect(d.getSeconds()).toBe(30)
  92. })
  93. test('parses Java "EEE MMM dd HH:mm:ss zzz yyyy" (CST) format', () => {
  94. // 后端 Java 常见格式,iOS 原生无法解析
  95. const d = format.parseDate('Sat Aug 22 14:00:00 CST 2026')
  96. expect(d).not.toBeNull()
  97. expect(ymd(d)).toBe('2026-8-22')
  98. expect(d.getHours()).toBe(14)
  99. })
  100. test('parses slash-separated "yyyy/MM/dd"', () => {
  101. const d = format.parseDate('2026/08/22')
  102. expect(d).not.toBeNull()
  103. expect(ymd(d)).toBe('2026-8-22')
  104. })
  105. test('parses ISO 8601 "yyyy-MM-ddTHH:mm:ss"', () => {
  106. const d = format.parseDate('2026-08-22T14:00:00')
  107. expect(d).not.toBeNull()
  108. expect(ymd(d)).toBe('2026-8-22')
  109. })
  110. test('parses plain date "yyyy-MM-dd"', () => {
  111. const d = format.parseDate('2026-08-22')
  112. expect(d).not.toBeNull()
  113. expect(ymd(d)).toBe('2026-8-22')
  114. })
  115. test('parses numeric timestamp', () => {
  116. const d = format.parseDate(1780000000000)
  117. expect(d).not.toBeNull()
  118. expect(d.getTime()).toBe(1780000000000)
  119. })
  120. test('returns same Date object when given a Date', () => {
  121. const date = new Date()
  122. expect(format.parseDate(date)).toBe(date)
  123. })
  124. test('returns null for null/undefined/empty string', () => {
  125. expect(format.parseDate(null)).toBeNull()
  126. expect(format.parseDate(undefined)).toBeNull()
  127. expect(format.parseDate('')).toBeNull()
  128. })
  129. test('returns null for unparseable string', () => {
  130. expect(format.parseDate('not-a-date')).toBeNull()
  131. expect(format.parseDate('2026-13-45')).toBeNull()
  132. })
  133. })
  134. })