orderSettingMerchantContext.test.mjs 3.53 KB
import assert from 'node:assert/strict'
import { register } from 'node:module'
import test from 'node:test'
import { loadMiniProgramApi } from '../../../../tests/order-lifecycle/support/loadMiniProgramApi.mjs'

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

test('order settings use the selected 4S store and normalize its employee-app settings', async () => {
  const app = {
    globalData: {
      YQorderSetting: { marker: 'wrong-cache' },
      YQorderSettingMerchantId: 999,
      YQorderSettingYqMerchantId: 100
    }
  }
  const requests = []
  globalThis.getApp = () => app
  globalThis.wx = {
    getAccountInfoSync: () => ({
      miniProgram: { appId: 'test-app-id', envVersion: 'develop' }
    }),
    getSystemInfoSync: () => ({ platform: 'devtools' }),
    getStorageSync: () => '',
    setStorageSync: () => {},
    removeStorageSync: () => {},
    $util: {
      getUserInfo: () => ({
        shopMerchantId: 888,
        subLoginUser: { merchantId: 777, merchantName: '登录园区店' }
      }),
      showLoading: () => {},
      hideLoading: () => {}
    }
  }
  globalThis.__WORK_ORDER_MOCK_API__ = {
    getMerchantRecordingSettings(params) {
      requests.push(params)
      return {
        code: 0,
        data: {
          orderFieldSettingList: [
            { fieldKey: 'carBrand', isRequired: true },
            { fieldKey: 'carNo', isRequired: false },
            { fieldKey: 'plateNo', isRequired: false },
            { fieldKey: 'customerPhone', isRequired: false }
          ],
          yqPhoneConfig: JSON.stringify([
            {
              yqMerchantId: 100,
              shopMerchantPhoneList: [{ shopMerchantId: 44210895 }]
            },
            {
              yqMerchantId: 200,
              shopMerchantPhoneList: [{ shopMerchantId: 20001 }]
            }
          ])
        }
      }
    }
  }

  const { getYQorderInfo } = await import(
    `../../../../store/index.js?selected-order-setting=${Date.now()}`
  )
  const result = await getYQorderInfo({
    shopMerchantId: 44210895,
    shopMerchantName: '波波的单店门店',
    yqMerchantId: 100
  })

  assert.deepEqual(requests, [{ scene: 1, merchantId: 44210895 }])
  assert.equal(result.carTypeRequiredFlag, 'Y')
  assert.equal(result.standNoRequiredFlag, 'N')
  assert.equal(result.plateNumRequiredFlag, 'N')
  assert.equal(result.customerPhoneRequiredFlag, 'N')
  assert.deepEqual(result.yqPhoneConfig, [{ shopMerchantId: 44210895 }])
  assert.equal(app.globalData.YQorderSettingMerchantId, 44210895)
  assert.equal(app.globalData.YQorderSettingYqMerchantId, 100)
  assert.equal(app.globalData.YQorderSetting.plateNumRequiredFlag, 'N')

  await getYQorderInfo({ shopMerchantId: 44210895, yqMerchantId: 100 })
  assert.equal(requests.length, 1)

  const switchedParkResult = await getYQorderInfo({
    shopMerchantId: 44210895,
    yqMerchantId: 200
  })
  assert.equal(requests.length, 2)
  assert.deepEqual(switchedParkResult.yqPhoneConfig, [
    { shopMerchantId: 20001 }
  ])
})

test('the order-settings API forwards to the employee-app light-change endpoint', async () => {
  const calls = []
  const api = await loadMiniProgramApi('api/api.js', {
    post(method, payload) {
      calls.push({ method, payload })
      return Promise.resolve({ code: 0, data: {} })
    }
  })
  const payload = { scene: 1, merchantId: 44210895 }

  await api.getMerchantRecordingSettings(payload)

  assert.deepEqual(calls, [
    {
      method: 'dj.api.smartcarlife.order.field.lightChange.setting.list',
      payload
    }
  ])
})