orderSettingMerchantContext.test.mjs 2.47 KB
import assert from 'node:assert/strict'
import { readFileSync } from 'node:fs'
import { register } from 'node:module'
import test from 'node:test'

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

test('order settings use and cache the park store selected before low-price order creation', async () => {
  const app = {
    globalData: {
      YQorderSetting: { marker: 'wrong-cache' },
      YQorderSettingMerchantId: 999
    }
  }
  const requests = []
  globalThis.getApp = () => app
  globalThis.wx = {
    getAccountInfoSync: () => ({
      miniProgram: { appId: 'test-app-id', envVersion: 'develop' }
    }),
    getSystemInfoSync: () => ({ platform: 'devtools' }),
    getStorageSync: () => '',
    setStorageSync: () => {},
    removeStorageSync: () => {},
    $util: {
      getUserInfo: () => ({
        subLoginUser: { merchantId: 777, merchantName: '登录园区店' }
      }),
      showLoading: () => {},
      hideLoading: () => {}
    }
  }
  globalThis.__WORK_ORDER_MOCK_API__ = {
    getMerchantRecordingSettings(params) {
      requests.push(params)
      return {
        code: 0,
        data: {
          marker: 'selected-store-setting',
          yqPhoneConfig: JSON.stringify([
            {
              yqMerchantId: 100,
              shopMerchantPhoneList: [{ shopMerchantId: 201 }]
            }
          ])
        }
      }
    }
  }

  const { getYQorderInfo } = await import(
    `../../../../store/index.js?selected-order-setting=${Date.now()}`
  )
  const result = await getYQorderInfo({
    merchantId: 100,
    merchantName: '已选园区店'
  })

  assert.deepEqual(requests, [
    { merchantId: 100, merchantName: '已选园区店' }
  ])
  assert.equal(result.marker, 'selected-store-setting')
  assert.deepEqual(result.yqPhoneConfig, [{ shopMerchantId: 201 }])
  assert.equal(app.globalData.YQorderSettingMerchantId, 100)
  assert.equal(app.globalData.YQorderSetting.marker, 'selected-store-setting')
})

test('createOrder passes the selected park store to the order-settings query', () => {
  const source = readFileSync(new URL('../createOrder.js', import.meta.url), 'utf8')

  assert.match(source, /const selectedYqMerchantId =\s*orderObj\.yqMerchantId \|\| orderObj\.sellerMerchantId/)
  assert.match(source, /merchantId: selectedYqMerchantId/)
  assert.match(source, /merchantName:\s*orderObj\.yqMerchantName \|\| orderObj\.sellerMerchantName/)
  assert.match(source, /getYQorderInfo\(selectedYqMerchant\)/)
})