merchantStaffPicker.component.test.mjs 3.64 KB
import assert from 'node:assert/strict'
import { register } from 'node:module'
import test from 'node:test'

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

function createComponentInstance(componentConfig, properties) {
  const instance = {
    properties: { ...properties },
    data: structuredClone(componentConfig.data),
    setData(patch) {
      Object.assign(this.data, patch)
    },
    triggerEvent() {}
  }

  Object.entries(componentConfig.methods).forEach(([name, method]) => {
    instance[name] = method.bind(instance)
  })

  return instance
}

test('同一次打开重复同步 show=true 时只加载一次人员数据', async () => {
  let componentConfig
  let employeeRequestCount = 0
  globalThis.Component = config => {
    componentConfig = config
  }
  globalThis.wx = {
    $util: {
      showToast() {}
    }
  }
  globalThis.__WORK_ORDER_MOCK_API__ = {
    getEmployeeList() {
      employeeRequestCount += 1
      return {
        code: 0,
        data: [{ userId: 'staff-1', name: '销售顾问一' }]
      }
    }
  }

  try {
    await import(
      `../components/merchant-staff-picker/merchant-staff-picker.js?single-open=${Date.now()}`
    )
    const instance = createComponentInstance(componentConfig, {
      show: true,
      mode: 'sales',
      orderObj: {
        lowPriceFlag: 1,
        merchantId: 44210895,
        merchantName: '当前下单门店'
      },
      userContext: {},
      pointsMultiRole: 'Y',
      orderCreateType: 'createdOrder'
    })
    const showObserver = componentConfig.properties.show.observer

    showObserver.call(instance, true, false)
    showObserver.call(instance, true, true)
    await new Promise(resolve => setImmediate(resolve))

    assert.equal(employeeRequestCount, 1)
    assert.equal(instance.data.staffList[0].staffName, '销售顾问一')

    instance.properties.show = false
    showObserver.call(instance, false, true)
    instance.properties.show = true
    showObserver.call(instance, true, false)
    await new Promise(resolve => setImmediate(resolve))

    assert.equal(employeeRequestCount, 2)
  } finally {
    delete globalThis.__WORK_ORDER_MOCK_API__
    delete globalThis.Component
  }
})

test('默认选中的交付专员与销售顾问相同时确认也会拦截', async () => {
  let componentConfig
  const toastMessages = []
  const confirmEvents = []
  globalThis.Component = config => {
    componentConfig = config
  }
  globalThis.wx = {
    $util: {
      showToast(message) {
        toastMessages.push(message)
      }
    }
  }

  try {
    await import(
      `../components/merchant-staff-picker/merchant-staff-picker.js?confirm-conflict=${Date.now()}`
    )
    const instance = createComponentInstance(componentConfig, {
      show: true,
      mode: 'delivery',
      orderObj: {
        userId: 'staff-1',
        userName: '秋椰0818'
      },
      userContext: {},
      pointsMultiRole: 'Y',
      orderCreateType: 'createdOrder'
    })
    instance.data.merchantTitle = '请选择合作门店'
    instance.data.staffTitle = '请选择交付专员'
    instance.data.merchantList = [
      { merchantId: 44210895, merchantName: '波波的单店门店', active: true }
    ]
    instance.data.staffList = [
      { uuid: 'staff-1', staffName: '秋椰0818', active: true }
    ]
    instance.triggerEvent = (name, detail) => {
      confirmEvents.push({ name, detail })
    }

    instance.onConfirm()

    assert.deepEqual(toastMessages, [
      '销售顾问/录单员与交付专员不能选取同一人'
    ])
    assert.deepEqual(confirmEvents, [])
  } finally {
    delete globalThis.Component
    delete globalThis.wx
  }
})