crowdFundingPrice.helpers.test.mjs 1.29 KB
/**
 * @file 众筹金额 helper 单测(与 tests/wxsFilters.test.mjs 分→元算法对齐)
 */
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import { centsToYuan, formatYuan, formatPriceDisplay } from '../packageCrowdFunding/utils/price.helpers.js'

/** wxs priceFilter 复现,用于对齐断言 */
function wxsPriceFilter(num) {
  const pow = Math.pow(10, 2)
  return (Math.round((num / 100) * pow) / pow).toFixed(2)
}

describe('crowdFundingPrice.helpers', () => {
  it('centsToYuan matches wxsFilters IT-WXS-PRICE-001', () => {
    assert.equal(centsToYuan(0), '0.00')
    assert.equal(centsToYuan(100), '1.00')
    assert.equal(centsToYuan(12345), '123.45')
    assert.equal(centsToYuan(0), wxsPriceFilter(0))
    assert.equal(centsToYuan(12345), wxsPriceFilter(12345))
  })

  it('centsToYuan edge values match wxs', () => {
    assert.equal(centsToYuan(-100), '-1.00')
    assert.equal(Number.isNaN(Number(centsToYuan(undefined))), true)
  })

  it('formatYuan rounds yuan amounts', () => {
    assert.equal(formatYuan(12.3), '12.30')
    assert.equal(formatYuan(0), '0.00')
  })

  it('formatPriceDisplay prefixes yen symbol', () => {
    assert.equal(formatPriceDisplay(100, { inputUnit: 'cents' }), '¥1.00')
    assert.equal(formatPriceDisplay(12.3), '¥12.30')
  })
})