mobile-customer-detail-labels.test.ts 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import assert from 'node:assert/strict'
  2. import test from 'node:test'
  3. import { readFileSync } from 'node:fs'
  4. import { parse } from '@vue/compiler-sfc'
  5. import { compile, computed, createSSRApp, ref } from 'vue'
  6. import { renderToString } from 'vue/server-renderer'
  7. import ts from 'typescript'
  8. import { resolveOpportunityStageLabel, resolveOpportunityTagType } from '../src/utils/crm-opportunity-labels'
  9. function source(file: string) {
  10. return parse(readFileSync(new URL(`../src/views/m/crm/customer/${file}.vue`, import.meta.url), 'utf8')).descriptor
  11. }
  12. test('跟进联系人选项和选中回显保留姓名职位及主要标识,值仍是联系人ID', () => {
  13. const code = source('FollowPanel').scriptSetup!.content
  14. const ast = ts.createSourceFile('follow.ts', code, ts.ScriptTarget.Latest, true)
  15. const fields = ast.statements.filter(ts.isVariableStatement).flatMap((n) => n.declarationList.declarations).find((n) => n.name.getText(ast) === 'fields')!
  16. const contacts = ref([
  17. { id: 1, contactName: '张甲', position: '采购', isPrimary: 1 },
  18. { id: 2, contactName: '李乙', position: '经理', isPrimary: 0 }
  19. ])
  20. const result = new Function('computed', 'contacts', ts.transpile(`return ${fields.initializer!.getText(ast)}`))(computed, contacts)
  21. const field = result.value.find((f: any) => f.key === 'contactId')
  22. assert.deepEqual(field.options, [
  23. { value: '1', label: '张甲 · 采购 · 主要' },
  24. { value: '2', label: '李乙 · 经理' }
  25. ])
  26. const selectorCode = source('CustomerFields').scriptSetup!.content
  27. const selectorAst = ts.createSourceFile('fields.ts', selectorCode, ts.ScriptTarget.Latest, true)
  28. const functions = selectorAst.statements.filter((n) => ts.isFunctionDeclaration(n) && ['hasValue', 'options', 'selectLabel'].includes(n.name?.text || '')).map((n) => n.getText(selectorAst)).join('\n')
  29. const label = new Function('props', 'dict', `${ts.transpile(functions)};return selectLabel`)(
  30. { modelValue: { contactId: '1' } }, {}
  31. )
  32. assert.equal(label(field), '张甲 · 采购 · 主要')
  33. })
  34. test('阶段记录用Web语义Tag,名称快照优先,未知阶段保留编码且空起点显示横线', async () => {
  35. const template = source('RelatedPanel').template!.content.split('<h3>阶段记录</h3>')[1]
  36. const fragment = template.match(/<div class="mp-customer-detail-card__title">[^]*?<\/div>/)![0]
  37. const dict = {
  38. dictMap: { crm_opportunity_stage: [
  39. { value: 'initial', label: '初步接触', tagType: 'warning' },
  40. { value: 'won', label: '赢单', tagType: 'success' }
  41. ] },
  42. getLabel: (_: string, value: string) => ({ initial: '初步接触', won: '赢单' }[value] || ''),
  43. getTagType: () => 'info'
  44. }
  45. async function render(row: any) {
  46. const compiled = compile(fragment)
  47. const context = { row, dict, resolveOpportunityStageLabel, resolveOpportunityTagType }
  48. return renderToString(createSSRApp({ render: () => compiled(context, []) }))
  49. }
  50. const html = await render({ fromStage: 'initial', fromStageName: '历史起点', toStage: 'won' })
  51. assert.match(html, /status-tag--warning/)
  52. assert.match(html, /status-tag--success/)
  53. assert.match(html, /历史起点/)
  54. assert.match(html, /赢单/)
  55. const unknown = await render({ toStage: 'legacy' })
  56. assert.match(unknown, /legacy/)
  57. assert.match(unknown, /status-tag--info/)
  58. assert.match(unknown, /—/)
  59. })