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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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('scan attachments display their Chinese category and retain the code when edited and saved', () => {
  31. const payload = [{ ossId: '/contract/scan.png', category: 'scan' }]
  32. const files = exports.buildUploadFileListFromField(JSON.stringify(payload))
  33. assert.equal(files[0].category, 'scan')
  34. assert.equal(exports.getCrmAttachmentCategoryLabel(files[0].category), '扫描件')
  35. assert.deepEqual(JSON.parse(exports.serializeContractAttachments(files)), payload)
  36. assert.deepEqual(exports.mapContractAttachmentsPayload(files), payload)
  37. for (const [category, label] of [
  38. ['single_seal', '单章附件'],
  39. ['double_seal', '双章附件'],
  40. ['other', '其他附件']
  41. ]) {
  42. assert.equal(exports.getCrmAttachmentCategoryLabel(category), label)
  43. }
  44. })
  45. test('both mobile and desktop attachment selects resolve the scan code to Chinese', () => {
  46. const template = parse(
  47. readFileSync(
  48. new URL('../src/views/crm/contract/components/ContractAttachmentPanel.vue', import.meta.url),
  49. 'utf8'
  50. )
  51. ).descriptor.template!
  52. const selects: any[] = []
  53. function walk(node: any) {
  54. if (node.tag === 'el-select') selects.push(node)
  55. node.children?.forEach(walk)
  56. }
  57. walk(template.ast)
  58. assert.equal(selects.length, 2)
  59. for (const select of selects) {
  60. const option = select.children.find(
  61. (node: any) =>
  62. node.tag === 'el-option' &&
  63. node.props.some((p: any) => p.name === 'value' && p.value?.content === 'scan')
  64. )
  65. assert.ok(option, 'existing scan attachments must have a matching select option')
  66. assert.equal(option.props.find((p: any) => p.name === 'label').value.content, '扫描件')
  67. }
  68. })