workOrderLowPriceGuard.test.mjs 3.08 KB
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import { readFileSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
import {
  LOW_PRICE_ORDER_EDIT_MESSAGE,
  isLowPriceOrder,
  shouldBlockLowPriceOrderEdit
} from '../utils/workOrderLowPrice.helpers.js'

const read = relativePath =>
  readFileSync(fileURLToPath(new URL(relativePath, import.meta.url)), 'utf8')

describe('work order low-price edit guard', () => {
  it('recognizes numeric and string lowPriceFlag values', () => {
    assert.equal(isLowPriceOrder({ lowPriceFlag: 1 }), true)
    assert.equal(isLowPriceOrder({ lowPriceFlag: '1' }), true)
    assert.equal(isLowPriceOrder({ lowPriceFlag: 0 }), false)
    assert.equal(isLowPriceOrder(null), false)
  })

  it('blocks only an existing low-price order entering createOrder', () => {
    const lowPriceOrder = { orderCode: 'LOW-001', lowPriceFlag: 1 }

    assert.equal(
      shouldBlockLowPriceOrderEdit(lowPriceOrder, 'createOrder'),
      true
    )
    assert.equal(
      shouldBlockLowPriceOrderEdit(lowPriceOrder, 'orderDetail'),
      false
    )
    assert.equal(
      shouldBlockLowPriceOrderEdit(lowPriceOrder, 'confirmOrder'),
      false
    )
    assert.equal(shouldBlockLowPriceOrderEdit(undefined, 'createOrder'), false)
    assert.equal(LOW_PRICE_ORDER_EDIT_MESSAGE, '低价单不可以编辑')
  })

  it('wires lowPriceFlag=0 from the create-order customer archive popup', () => {
    const createOrderWxml = read(
      '../tabWorkOrder/pages/createOrder/createOrder.wxml'
    )
    const customerInfoJs = read(
      '../tabWorkOrder/components/CustomerInfo/CustomerInfo.js'
    )

    assert.match(
      createOrderWxml,
      /<customerInfo[\s\S]*?lowPriceFlag="\{\{ 0 \}\}"[\s\S]*?\/>/
    )
    assert.match(customerInfoJs, /buildCustomerListRequestParams/)
  })

  it('disables confirmation edits and hides customer-info edits on low-price details', () => {
    const confirmWxml = read(
      '../tabWorkOrder/pages/confirmOrder/confirmOrder.wxml'
    )
    const confirmJs = read(
      '../tabWorkOrder/pages/confirmOrder/confirmOrder.js'
    )
    const detailWxml = read(
      '../tabWorkOrder/pages/orderDetail/orderDetail.wxml'
    )
    const constructionDetailWxml = read(
      '../tabWorkOrder/pages/constructionOrderDetail/constructionOrderDetail.wxml'
    )

    assert.match(confirmWxml, /low-price-disabled/)
    assert.match(confirmWxml, /orderInfo\.lowPriceFlag != 1/)
    assert.match(confirmJs, /isLowPriceOrder\(orderInfo\)/)
    assert.match(detailWxml, /orderInfo\.lowPriceFlag == 1 \? 'isHide'/)
    assert.match(
      constructionDetailWxml,
      /orderInfo\.lowPriceFlag != 1/
    )
  })

  it('uses the shared guard in every registered list that can enter createOrder', () => {
    const listFiles = [
      '../pages/tabWorkOrder/pages/index/index.js',
      '../tabWorkOrder/pages/tabWorkOrderRelevance/index.js',
      '../tabWorkOrder/pages/technicianSearchList/pages/index/technicianSearchList.js'
    ]

    listFiles.forEach(file => {
      assert.match(read(file), /shouldBlockLowPriceOrderEdit\(item, name\)/)
    })
  })
})