contract-product-table-widths.test.ts 1.8 KB

1234567891011121314151617181920212223242526272829
  1. import assert from 'node:assert/strict'
  2. import { describe, it } from 'node:test'
  3. import {
  4. CONTRACT_PRODUCT_COL_WIDTHS_PX,
  5. normalizeContractProductTableWidths
  6. } from './contract-product-table-widths'
  7. const sample = `
  8. <table style="width: 680px; table-layout: fixed;">
  9. <colgroup><col style="width: 73px;"><col style="width: 49px;"><col style="width: 34px;"><col style="width: 39px;"><col style="width: 37px;"><col style="width: 34px;"><col style="width: 82px;"><col style="width: 82px;"><col style="width: 68px;"><col style="width: 67px;"><col style="width: 62px;"><col style="width: 53px;"></colgroup>
  10. <tr><th style="width: 73px;">产品名称</th><th style="width: 49px;">规格</th><th style="width: 34px;">品牌</th><th style="width: 39px;">单位</th><th style="width: 37px;">用量</th><th style="width: 34px;">税率</th><th style="width: 82px;">含税单价(元)</th><th style="width: 82px;">未含税金额(元)</th><th style="width: 68px;">税费(元)</th><th style="width: 67px;">含税金额(元)</th><th style="width: 62px;">包装规格</th><th style="width: 53px;">备注</th></tr>
  11. </table>`
  12. describe('normalizeContractProductTableWidths', () => {
  13. it('rewrites saved pixel columns so the product name is 100px', () => {
  14. const out = normalizeContractProductTableWidths(sample)
  15. assert.match(out, /<col style="width: 100px;">/)
  16. assert.equal(out.includes('width: 73px'), false)
  17. assert.match(out, /<th style="width: 100px;">产品名称<\/th>/)
  18. assert.match(out, /<table style="width: 680px;/)
  19. const amount = CONTRACT_PRODUCT_COL_WIDTHS_PX.slice(2, 6).reduce((sum, n) => sum + n, 0)
  20. assert.ok(amount >= 180)
  21. })
  22. it('leaves unrelated tables unchanged', () => {
  23. const other = '<table><tr><th style="width: 10px;">其他</th></tr></table>'
  24. assert.equal(normalizeContractProductTableWidths(other), other)
  25. })
  26. })