goodsPriceBoundary.helpers.test.mjs 3.6 KB
import assert from 'node:assert/strict'
import { describe, it } from 'node:test'
import {
  findGoodsPriceBoundaryError,
  validateGoodsPriceInput
} from '../services/goodsPriceBoundary.helpers.js'

describe('goods price boundary validation', () => {
  it('rejects negative prices without converting them to positive values', () => {
    assert.deepEqual(validateGoodsPriceInput('-1'), {
      valid: false,
      value: null,
      message: '商品价格不能小于0元'
    })
  })

  it('rejects values over 100000 without clamping', () => {
    assert.deepEqual(validateGoodsPriceInput('100000.01'), {
      valid: false,
      value: null,
      message: '商品价格不能大于100000元'
    })
  })

  it('accepts exact zero and zero-valued drafts during editing and blur', () => {
    const drafts = [
      ['0', '0'],
      ['0.', '0.'],
      ['0.0', '0.0'],
      ['0.01', '0.01']
    ]

    for (const [rawPrice, expectedValue] of drafts) {
      assert.deepEqual(
        validateGoodsPriceInput(rawPrice),
        {
          valid: true,
          value: expectedValue,
          message: ''
        }
      )
    }
  })

  it('accepts the upper boundary and normalizes to two decimals', () => {
    assert.deepEqual(validateGoodsPriceInput('100000'), {
      valid: true,
      value: '100000',
      message: ''
    })
    assert.deepEqual(validateGoodsPriceInput('12.345'), {
      valid: true,
      value: '12.34',
      message: ''
    })
  })

  it('finds invalid single, selected SKU and package child prices', () => {
    assert.equal(
      findGoodsPriceBoundaryError([
        { goodsFlag: 1, goods: { priceBoundaryError: '' } },
        {
          goodsFlag: 3,
          packageGoods: {
            goodsList: [
              { priceBoundaryError: '' },
              { priceBoundaryError: '商品价格不能大于100000元' }
            ]
          }
        }
      ]),
      '商品价格不能大于100000元'
    )
  })

  it('rejects invalid prices already present in route or echo data', () => {
    assert.equal(
      findGoodsPriceBoundaryError([
        { goodsFlag: 1, goods: { basePrice: -1, sellPrice: -1 } }
      ]),
      '商品价格不能小于0元'
    )
    assert.equal(
      findGoodsPriceBoundaryError([
        {
          goodsFlag: 2,
          multipleSpecificationsGoods: { goods: { sellPrice: 100000.01 } }
        }
      ]),
      '商品价格不能大于100000元'
    )
  })

  it('allows exact zero prices but rejects decimal-zero drafts on submit', () => {
    const validGoodsLists = [
      [{ goodsFlag: 1, goods: { basePrice: 0, sellPrice: 0 } }],
      [{ goodsFlag: 1, goods: { basePrice: '0', sellPrice: '0' } }]
    ]
    const invalidGoodsLists = [
      [
        {
          goodsFlag: 2,
          multipleSpecificationsGoods: { goods: { sellPrice: '0.' } }
        }
      ],
      [
        {
          goodsFlag: 3,
          packageGoods: {
            goodsList: [{ sellPrice: '0.0' }, { sellPrice: '0.00' }]
          }
        }
      ]
    ]

    for (const goodsList of validGoodsLists) {
      assert.equal(findGoodsPriceBoundaryError(goodsList), '')
    }
    for (const goodsList of invalidGoodsLists) {
      assert.equal(
        findGoodsPriceBoundaryError(goodsList),
        '商品价格不能等于0元'
      )
    }
  })

  it('allows a zero gift final price when its editable base price is positive', () => {
    assert.equal(
      findGoodsPriceBoundaryError([
        {
          goodsFlag: 1,
          goods: {
            basePrice: 10,
            sellPrice: 10,
            finalPrice: 0,
            discountType: 4
          }
        }
      ]),
      ''
    )
  })
})