quotationSecondStep.events.test.mjs 3.99 KB
import assert from 'node:assert/strict'
import { readFileSync } from 'node:fs'
import path from 'node:path'
import test from 'node:test'
import { fileURLToPath } from 'node:url'

const projectRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..')
const read = relativePath => readFileSync(path.join(projectRoot, relativePath), 'utf8')

test('quotation step components never obtain or mutate a parent page instance', () => {
  for (const relativePath of [
    'packageEquity/pages/carQuotation/components/FirstStep/FirstStep.js',
    'packageEquity/pages/carQuotation/components/SecondStep/SecondStep.js',
    'packageEquity/pages/carQuotation/components/SecondStepSimple/SecondStep.js'
  ]) {
    assert.doesNotMatch(read(relativePath), /getPageInstance\s*\(/, relativePath)
  }
})

test('both SecondStep components publish order patches', () => {
  for (const relativePath of [
    'packageEquity/pages/carQuotation/components/SecondStep/SecondStep.js',
    'packageEquity/pages/carQuotation/components/SecondStepSimple/SecondStep.js'
  ]) {
    assert.match(read(relativePath), /triggerEvent\(['"]orderChange['"],\s*\{\s*patch:/, relativePath)
  }
})

test('standard and simple pages bind SecondStep order patches', () => {
  for (const relativePath of [
    'packageEquity/pages/carQuotation/createOrderCarQuotation/createOrderCarQuotation.wxml',
    'packageEquity/pages/carQuotation/createOrderCarQuotationSimple/createOrderCarQuotationSimple.wxml'
  ]) {
    assert.match(read(relativePath), /<second-step[\s\S]*bind:orderChange="handleOrderChange"/)
  }
})

test('simple quotation goods components report selection through events instead of mutating a page instance', () => {
  const goodsPackage = read(
    'packageEquity/pages/carQuotation/components/GoodsPackage/Goods.js'
  )
  const skuGoods = read('packageEquity/components/SkuGoods/Goods.js')

  for (const source of [goodsPackage, skuGoods]) {
    assert.doesNotMatch(source, /getPageInstance\s*\(/)
    assert.match(source, /triggerEvent\(['"]confirm['"]/)
    assert.match(source, /triggerEvent\(['"]cancel['"]/)
  }
  assert.match(goodsPackage, /triggerEvent\(['"]refreshRequested['"]/)
  assert.match(goodsPackage, /triggerEvent\(['"]skuRequested['"]/)
  assert.match(skuGoods, /triggerEvent\(['"]visibilityChange['"]/)
})

test('simple quotation owns goods events and boutique consumes the shared sku payload', () => {
  const simpleStep = read(
    'packageEquity/pages/carQuotation/components/SecondStepSimple/SecondStep.js'
  )
  const simpleStepTemplate = read(
    'packageEquity/pages/carQuotation/components/SecondStepSimple/SecondStep.wxml'
  )
  const simplePage = read(
    'packageEquity/pages/carQuotation/createOrderCarQuotationSimple/createOrderCarQuotationSimple.js'
  )
  const simplePageTemplate = read(
    'packageEquity/pages/carQuotation/createOrderCarQuotationSimple/createOrderCarQuotationSimple.wxml'
  )
  const boutiquePage = read('tabWorkOrder/pages/createOrder/createOrder.js')

  assert.match(simpleStepTemplate, /bind:refreshRequested="onGoodsRefreshRequested"/)
  assert.match(simpleStepTemplate, /bind:skuRequested="onGoodsSkuRequested"/)
  assert.match(simpleStepTemplate, /<goods[\s\S]*bind:cancel="onGoodsCancel"/)
  assert.match(simpleStepTemplate, /<sku-goods[\s\S]*bindconfirm="onSkuGoodsConfirm"/)
  assert.match(simpleStepTemplate, /<sku-goods[\s\S]*bindcancel="onSkuGoodsCancel"/)
  assert.match(simpleStepTemplate, /<sku-goods[\s\S]*bind:visibilityChange="onSkuVisibilityChange"/)
  assert.match(simpleStep, /triggerEvent\(['"]catalogRefreshRequested['"]/)
  assert.match(simpleStep, /triggerEvent\(['"]popupVisibilityChange['"]/)
  assert.match(simplePageTemplate, /bind:catalogRefreshRequested="handleCatalogRefreshRequested"/)
  assert.match(simplePageTemplate, /bind:popupVisibilityChange="handlePopupVisibilityChange"/)
  assert.match(simplePage, /handleCatalogRefreshRequested\s*\(/)
  assert.match(simplePage, /handlePopupVisibilityChange\s*\(/)
  assert.match(boutiquePage, /onSkuGoodsConfirm\s*\(e\)[\s\S]*e\.detail[\s\S]*setGoodsTotal/)
})