goodsPositionBucket.helpers.test.mjs 3.16 KB
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import {
  KNOWN_POSITIONS,
  resolveGoodsPosition,
  isKnownPosition,
  createEmptyPositionBuckets,
  pushToPositionBucket,
  filterUpgradeListByPopupType
} from '../tabWorkOrder/services/goodsPositionBucket.helpers.js'

describe('goodsPositionBucket.helpers', () => {
  it('resolveGoodsPosition prefers merchantGoodsPosition then upgradeGoodsPosition', () => {
    assert.equal(resolveGoodsPosition({ merchantGoodsPosition: 2 }), 2)
    assert.equal(
      resolveGoodsPosition({ upgradeGoodsPosition: 3 }),
      3
    )
    assert.equal(resolveGoodsPosition({}), undefined)
    assert.equal(resolveGoodsPosition(null), undefined)
  })

  it('isKnownPosition accepts 1-7 only', () => {
    assert.equal(isKnownPosition(1), true)
    assert.equal(isKnownPosition(7), true)
    assert.equal(isKnownPosition(8), false)
    assert.equal(isKnownPosition(null), false)
  })

  it('pushToPositionBucket routes by position regardless of goodsType', () => {
    const buckets = createEmptyPositionBuckets()
    const film = { merchantGoodsPosition: 1, goodsInfo: { goodsType: 1 } }
    const other = { merchantGoodsPosition: 1, goodsInfo: { goodsType: 99 } }
    pushToPositionBucket(buckets, film)
    pushToPositionBucket(buckets, other)
    assert.equal(buckets.frontGearArr.length, 2)
    assert.equal(buckets.other.length, 0)
  })

  it('pushToPositionBucket sends unknown position to other', () => {
    const buckets = createEmptyPositionBuckets()
    pushToPositionBucket(buckets, { merchantGoodsPosition: 99 })
    pushToPositionBucket(buckets, { merchantGoodsPosition: null })
    assert.equal(buckets.other.length, 2)
    assert.equal(buckets.frontGearArr.length, 0)
  })

  it('pushToPositionBucket covers all known positions', () => {
    const buckets = createEmptyPositionBuckets()
    KNOWN_POSITIONS.forEach(pos => {
      pushToPositionBucket(buckets, { id: pos }, pos)
    })
    assert.equal(buckets.frontGearArr.length, 1)
    assert.equal(buckets.sideBeforeRearGearArr.length, 1)
    assert.equal(buckets.sideRearGearArr.length, 1)
    assert.equal(buckets.sideAfterRearGearArr.length, 1)
    assert.equal(buckets.skylightArr.length, 3)
    assert.equal(buckets.other.length, 0)
  })

  it('filterUpgradeListByPopupType type 4 returns items without known position', () => {
    const list = [
      { id: 1, merchantGoodsPosition: 1, goodsInfo: { goodsType: 99 } },
      { id: 2, goodsInfo: { goodsType: 2 } },
      { id: 3, merchantGoodsPosition: 8 }
    ]
    const result = filterUpgradeListByPopupType(list, 4)
    assert.deepEqual(
      result.map(i => i.id),
      [2, 3]
    )
  })

  it('filterUpgradeListByPopupType type 1/2/3 filter by position', () => {
    const list = [
      { id: 'a', merchantGoodsPosition: 1 },
      { id: 'b', merchantGoodsPosition: 2 },
      { id: 'c', merchantGoodsPosition: 5 }
    ]
    assert.deepEqual(
      filterUpgradeListByPopupType(list, 1).map(i => i.id),
      ['a']
    )
    assert.deepEqual(
      filterUpgradeListByPopupType(list, 2, 2).map(i => i.id),
      ['b']
    )
    assert.deepEqual(
      filterUpgradeListByPopupType(list, 3).map(i => i.id),
      ['c']
    )
  })
})