| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- /**
- * Tests for cfc-frontend utils/format.js
- *
- * Tests the price/rate/score formatting utilities.
- * These are pure functions - no uni mock needed.
- */
- describe('format.js', () => {
- let format
- beforeAll(() => {
- format = require('@/utils/format')
- })
- // --------------- formatPrice ---------------
- describe('formatPrice', () => {
- test('converts cents to yuan with 2 decimals', () => {
- expect(format.formatPrice(1250)).toBe('12.50')
- expect(format.formatPrice(0)).toBe('0.00')
- expect(format.formatPrice(999)).toBe('9.99')
- expect(format.formatPrice(10000)).toBe('100.00')
- })
- test('handles null and undefined', () => {
- expect(format.formatPrice(null)).toBeNull()
- expect(format.formatPrice(undefined)).toBeNull()
- })
- test('handles zero cents', () => {
- expect(format.formatPrice(0)).toBe('0.00')
- })
- })
- // --------------- formatPriceWithSymbol ---------------
- describe('formatPriceWithSymbol', () => {
- test('adds ¥ prefix and 2 decimals', () => {
- expect(format.formatPriceWithSymbol(1250)).toBe('¥12.50')
- expect(format.formatPriceWithSymbol(0)).toBe('¥0.00')
- expect(format.formatPriceWithSymbol(999)).toBe('¥9.99')
- })
- test('returns login message for null/undefined', () => {
- expect(format.formatPriceWithSymbol(null)).toBe('登录查看')
- expect(format.formatPriceWithSymbol(undefined)).toBe('登录查看')
- })
- })
- // --------------- formatRate ---------------
- describe('formatRate', () => {
- test('converts permillage to percentage string', () => {
- expect(format.formatRate(235)).toBe('23.5%')
- expect(format.formatRate(0)).toBe('0%')
- expect(format.formatRate(1000)).toBe('100%')
- expect(format.formatRate(5)).toBe('0.5%')
- })
- test('returns -- for null/undefined', () => {
- expect(format.formatRate(null)).toBe('--')
- expect(format.formatRate(undefined)).toBe('--')
- })
- })
- // --------------- formatScore ---------------
- describe('formatScore', () => {
- test('converts percent-scale to fixed decimal', () => {
- expect(format.formatScore(450)).toBe('4.5')
- expect(format.formatScore(0)).toBe('0.0')
- expect(format.formatScore(1000)).toBe('10.0')
- })
- test('uses default 1 decimal place', () => {
- expect(format.formatScore(450)).toBe('4.5')
- })
- test('supports custom decimal places', () => {
- expect(format.formatScore(450, 2)).toBe('4.50')
- expect(format.formatScore(450, 0)).toBe('5')
- })
- test('returns -- for null/undefined', () => {
- expect(format.formatScore(null)).toBe('--')
- expect(format.formatScore(undefined)).toBe('--')
- })
- })
- // --------------- parseDate ---------------
- describe('parseDate', () => {
- // 断言使用本地时间字段(getFullYear/getMonth/getDate/...),
- // 避免 Node(V8)对纯日期字符串按 UTC 解析带来的时区差异。
- const ymd = (d) => {
- return d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate()
- }
- test('parses MySQL DATETIME with space separator "yyyy-MM-dd HH:mm"', () => {
- // iOS 原生 new Date() 无法解析空格分隔格式,parseDate 必须归一化成功
- const d = format.parseDate('2026-08-22 14:00')
- expect(d).not.toBeNull()
- expect(ymd(d)).toBe('2026-8-22')
- expect(d.getHours()).toBe(14)
- expect(d.getMinutes()).toBe(0)
- })
- test('parses MySQL DATETIME with seconds', () => {
- const d = format.parseDate('2026-08-22 14:00:30')
- expect(d).not.toBeNull()
- expect(ymd(d)).toBe('2026-8-22')
- expect(d.getSeconds()).toBe(30)
- })
- test('parses Java "EEE MMM dd HH:mm:ss zzz yyyy" (CST) format', () => {
- // 后端 Java 常见格式,iOS 原生无法解析
- const d = format.parseDate('Sat Aug 22 14:00:00 CST 2026')
- expect(d).not.toBeNull()
- expect(ymd(d)).toBe('2026-8-22')
- expect(d.getHours()).toBe(14)
- })
- test('parses slash-separated "yyyy/MM/dd"', () => {
- const d = format.parseDate('2026/08/22')
- expect(d).not.toBeNull()
- expect(ymd(d)).toBe('2026-8-22')
- })
- test('parses ISO 8601 "yyyy-MM-ddTHH:mm:ss"', () => {
- const d = format.parseDate('2026-08-22T14:00:00')
- expect(d).not.toBeNull()
- expect(ymd(d)).toBe('2026-8-22')
- })
- test('parses plain date "yyyy-MM-dd"', () => {
- const d = format.parseDate('2026-08-22')
- expect(d).not.toBeNull()
- expect(ymd(d)).toBe('2026-8-22')
- })
- test('parses numeric timestamp', () => {
- const d = format.parseDate(1780000000000)
- expect(d).not.toBeNull()
- expect(d.getTime()).toBe(1780000000000)
- })
- test('returns same Date object when given a Date', () => {
- const date = new Date()
- expect(format.parseDate(date)).toBe(date)
- })
- test('returns null for null/undefined/empty string', () => {
- expect(format.parseDate(null)).toBeNull()
- expect(format.parseDate(undefined)).toBeNull()
- expect(format.parseDate('')).toBeNull()
- })
- test('returns null for unparseable string', () => {
- expect(format.parseDate('not-a-date')).toBeNull()
- expect(format.parseDate('2026-13-45')).toBeNull()
- })
- })
- })
|