customerProfileOnlyImport.integration.test.mjs 3.9 KB
import assert from 'node:assert/strict'
import { register } from 'node:module'
import { describe, it } from 'node:test'

register(
  '../../../../tests/work-order-migration/shims/create-order-context-loader.mjs',
  import.meta.url
)

async function loadPageConfig() {
  let pageConfig
  globalThis.getApp = () => ({ globalData: {} })
  globalThis.Page = config => {
    pageConfig = config
  }
  globalThis.wx = {
    getAccountInfoSync: () => ({
      miniProgram: { appId: 'test-app-id', envVersion: 'develop' }
    }),
    getSystemInfoSync: () => ({ platform: 'devtools' }),
    getStorageSync: () => '',
    $util: {
      getUserInfo: () => ({}),
      showLoading: () => {},
      hideLoading: () => {}
    }
  }

  await import(`../createOrder.js?customer-only=${Date.now()}-${Math.random()}`)
  return pageConfig
}

function createPage(pageConfig, lowPriceFlag) {
  const currentGoods = [{ goodsCode: 'CURRENT' }]
  let profileEchoCustomerId
  let routeCount = 0
  const page = {
    data: {
      ...pageConfig.data,
      customerInfoBox: true,
      goodsList: currentGoods,
      orderObj: {
        ...pageConfig.data.orderObj,
        lowPriceFlag,
        merchantId: 100,
        selectPayType: 3,
        payeeType: 1,
        remark: '旧客户备注',
        subOrderList: currentGoods,
        customer: { customerId: 1, customerName: '旧客户' }
      }
    },
    setData(patch, callback) {
      Object.assign(this.data, patch)
      callback?.()
    },
    async echoCustomerProfileIfNeeded(detail) {
      profileEchoCustomerId = detail?.customer?.customerId
    },
    async onRoute(detail) {
      routeCount += 1
      this.data.orderObj = { ...detail }
      await this.echoCustomerProfileIfNeeded(detail)
    },
    settingPayType() {}
  }
  return {
    currentGoods,
    page,
    getProfileEchoCustomerId: () => profileEchoCustomerId,
    getRouteCount: () => routeCount
  }
}

const selectedRecord = {
  selectPayType: 5,
  payeeType: 0,
  remark: '新客户备注',
  subOrderList: [{ goodsCode: 'HISTORY' }],
  packageGoodsList: [{ packageCode: 'HISTORY-PKG' }],
  customer: {
    customerId: 2,
    customerName: '新客户',
    customerPhone: '13800000000'
  }
}

describe('createOrder customer profile import', () => {
  it('low-price profile selection keeps current goods and payment fields', async () => {
    const pageConfig = await loadPageConfig()
    const context = createPage(pageConfig, 1)

    await pageConfig.chooseCustomerInfo.call(context.page, {
      detail: structuredClone(selectedRecord)
    })

    assert.equal(context.page.data.orderObj.customer.customerId, 2)
    assert.equal(context.page.data.orderObj.customer.customerName, '新客户')
    assert.equal(context.page.data.orderObj.customer.customerSex, 1)
    assert.equal(context.page.data.orderObj.customer.experienceFlag, 'N')
    assert.equal(context.page.data.orderObj.customer.salesType, 0)
    assert.equal(context.page.data.orderObj.remark, '新客户备注')
    assert.equal(context.page.data.currentWordNumber, 5)
    assert.equal(context.page.data.orderObj.selectPayType, 3)
    assert.equal(context.page.data.orderObj.payeeType, 1)
    assert.deepEqual(context.page.data.orderObj.subOrderList, [
      { goodsCode: 'CURRENT' }
    ])
    assert.equal(context.page.data.orderObj.packageGoodsList, undefined)
    assert.equal(context.page.data.goodsList, context.currentGoods)
    assert.equal(context.page.data.customerInfoBox, false)
    assert.equal(context.getProfileEchoCustomerId(), 2)
    assert.equal(context.getRouteCount(), 0)
  })

  it('normal-order profile selection keeps the existing order routing path', async () => {
    const pageConfig = await loadPageConfig()
    const context = createPage(pageConfig, 0)

    await pageConfig.chooseCustomerInfo.call(context.page, {
      detail: structuredClone(selectedRecord)
    })

    assert.equal(context.getRouteCount(), 1)
    assert.equal(context.page.data.orderObj.customer.customerId, 2)
  })
})