| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- /**
- * 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('--')
- })
- })
- })
|