submitGoodsSnapshot.test.mjs 3.29 KB
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import {
  buildBoutiqueSubOrderList,
  buildSubmitGoodsSnapshot
} from '../services/submitGoodsSnapshot.js'

describe('submitGoodsSnapshot', () => {
  it('maps goodsList and subOrder fields without dormant approval state', () => {
    const snapshot = buildSubmitGoodsSnapshot({
        goodsList: [
          {
            goodsFlag: 1,
            count: 2,
            marketingFlag: 1,
            activityId: 9,
            goods: {
              sellPrice: 100,
              activitySellPrice: 299,
              marketingFlag: 1,
              activityId: 9,
              validMinSellPrice: 'Y',
              minSellPrice: 200
            }
          }
        ],
        orderObj: {
          subOrderList: [
            {
              merchantGoodsId: 1,
              totalPrice: 100,
              finalPrice: 200,
              marketingFlag: 1,
              activityId: 9,
              modifyFlag: 1,
              number: 2
            }
          ]
        }
      })

    assert.equal(snapshot.goodsList.length, 1)
    assert.equal(snapshot.goodsList[0].goods.activitySellPrice, 299)
    assert.equal('approvalItems' in snapshot, false)
    assert.equal(snapshot.subOrderList[0].marketingFlag, 1)
    assert.equal(snapshot.subOrderList[0].activityId, 9)
  })

  it('maps single, multi-spec and package rows with exact totals', () => {
    const result = buildBoutiqueSubOrderList(
      [
        {
          goodsFlag: 1,
          count: 2,
          goods: {
            merchantGoodsId: 'single',
            sellPrice: '10.01',
            discountType: 2,
            discountValue: '0.01',
            discountAmount: '0.01'
          }
        },
        {
          goodsFlag: 2,
          count: 1,
          multipleSpecificationsGoods: {
            goods: {
              merchantGoodsId: 'sku',
              sellPrice: '20.02',
              discountType: 3
            }
          }
        },
        {
          goodsFlag: 3,
          count: 1,
          packageGoods: {
            packageCode: 'pkg',
            packageName: '套餐',
            goodsList: [
              {
                merchantGoodsId: 'pkg-good',
                sellPrice: '30.03',
                discountType: 4
              }
            ]
          }
        }
      ],
      { djOrderType: 2, selectPayType: 1 }
    )

    assert.equal(result.ok, true)
    assert.equal(result.subOrderList.length, 3)
    assert.equal(result.subOrderList[0].finalPrice, 20)
    assert.equal(result.subOrderList[0].singleProductFinalPrice, 10)
    assert.equal(result.subOrderList[1].finalPrice, 20.02)
    assert.equal(result.subOrderList[2].packageCode, 'pkg')
    assert.equal(result.subOrderList[2].finalPrice, 0)
  })

  it('reports zero quantity and invalid amounts instead of producing NaN', () => {
    const result = buildBoutiqueSubOrderList(
      [
        {
          goodsFlag: 1,
          count: 0,
          goods: { merchantGoodsId: 'g', sellPrice: 'invalid' }
        }
      ],
      { djOrderType: 1, selectPayType: 1 }
    )
    assert.equal(result.ok, false)
    assert.equal(result.errors.length > 0, true)
    assert.equal(
      result.subOrderList.some(item =>
        [item.totalPrice, item.finalPrice].some(Number.isNaN)
      ),
      false
    )
  })
})