mobile-contract-attachment-category.test.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import assert from 'node:assert/strict'
  2. import { readFileSync } from 'node:fs'
  3. import test from 'node:test'
  4. import { parse } from '@vue/compiler-sfc'
  5. import ts from 'typescript'
  6. const source = readFileSync(new URL('../src/utils/crm-attachment.ts', import.meta.url), 'utf8')
  7. const ast = ts.createSourceFile('attachments.ts', source, ts.ScriptTarget.Latest, true)
  8. const names = [
  9. 'parseCrmAttachmentField',
  10. 'crmAttachmentDisplayName',
  11. 'buildUploadFileListFromField',
  12. 'serializeContractAttachments',
  13. 'mapContractAttachmentsPayload',
  14. 'getCrmAttachmentCategoryLabel',
  15. 'CRM_ATTACHMENT_CATEGORY_LABELS'
  16. ]
  17. const selected = ast.statements.filter((node) =>
  18. ts.isFunctionDeclaration(node)
  19. ? names.includes(node.name?.text || '')
  20. : ts.isVariableStatement(node) &&
  21. names.includes(node.declarationList.declarations[0].name.getText(ast))
  22. )
  23. const exports: any = {}
  24. new Function(
  25. 'exports',
  26. ts.transpile(selected.map((node) => node.getText(ast)).join('\n'), {
  27. module: ts.ModuleKind.CommonJS
  28. })
  29. )(exports)
  30. test('an empty attachment list is an explicit empty array so save can clear stored files', () => {
  31. assert.equal(exports.serializeContractAttachments([]), '[]')
  32. assert.deepEqual(exports.mapContractAttachmentsPayload([]), [])
  33. })
  34. test('scan attachments display their Chinese category and retain the code when edited and saved', () => {
  35. const payload = [{ ossId: '/contract/scan.png', category: 'scan' }]
  36. const files = exports.buildUploadFileListFromField(JSON.stringify(payload))
  37. assert.equal(files[0].category, 'scan')
  38. assert.equal(exports.getCrmAttachmentCategoryLabel(files[0].category), '扫描件')
  39. assert.deepEqual(JSON.parse(exports.serializeContractAttachments(files)), payload)
  40. assert.deepEqual(exports.mapContractAttachmentsPayload(files), payload)
  41. for (const [category, label] of [
  42. ['single_seal', '单章附件'],
  43. ['double_seal', '双章附件'],
  44. ['other', '其他附件']
  45. ]) {
  46. assert.equal(exports.getCrmAttachmentCategoryLabel(category), label)
  47. }
  48. })
  49. test('both mobile and desktop attachment selects resolve the scan code to Chinese', () => {
  50. const template = parse(
  51. readFileSync(
  52. new URL('../src/views/crm/contract/components/ContractAttachmentPanel.vue', import.meta.url),
  53. 'utf8'
  54. )
  55. ).descriptor.template!
  56. const selects: any[] = []
  57. function walk(node: any) {
  58. if (node.tag === 'el-select') selects.push(node)
  59. node.children?.forEach(walk)
  60. }
  61. walk(template.ast)
  62. assert.equal(selects.length, 2)
  63. for (const select of selects) {
  64. const option = select.children.find(
  65. (node: any) =>
  66. node.tag === 'el-option' &&
  67. node.props.some((p: any) => p.name === 'value' && p.value?.content === 'scan')
  68. )
  69. assert.ok(option, 'existing scan attachments must have a matching select option')
  70. assert.equal(option.props.find((p: any) => p.name === 'label').value.content, '扫描件')
  71. }
  72. })