vehicle-personnel-save.test.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import assert from 'node:assert/strict'
  2. import test from 'node:test'
  3. import { readFileSync } from 'node:fs'
  4. import ts from 'typescript'
  5. function submitHandler(path: string, context: Record<string, any>) {
  6. const source = readFileSync(new URL(path, import.meta.url), 'utf8')
  7. const script = source.match(/<script[^>]*>([\s\S]*?)<\/script>/)?.[1]
  8. assert.ok(script)
  9. const ast = ts.createSourceFile('page.ts', script, ts.ScriptTarget.Latest, true)
  10. const node = ast.statements.find(
  11. (statement) => ts.isFunctionDeclaration(statement) && statement.name?.text === 'handleSubmit'
  12. )
  13. assert.ok(node)
  14. const js = ts.transpileModule(node.getText(ast), {
  15. compilerOptions: { target: ts.ScriptTarget.ES2022 }
  16. }).outputText
  17. return new Function(...Object.keys(context), `${js}; return handleSubmit`)(
  18. ...Object.values(context)
  19. )
  20. }
  21. for (const page of [
  22. '../src/views/pl/vehicle/index.vue',
  23. '../src/views/pl/self-vehicle/index.vue',
  24. '../src/components/business/pl-vehicle-form-dialog/index.vue'
  25. ]) {
  26. test(`${page}: saving and clearing personnel remarks preserves identity`, async () => {
  27. const requests: any[] = []
  28. const person = {
  29. personnelId: 'person-existing',
  30. personnelName: '司机',
  31. role: 'driver',
  32. isDefault: 1,
  33. remark: '原备注'
  34. }
  35. const context = {
  36. formRef: { value: { validate: async () => true } },
  37. validateSubTables: () => true,
  38. certFormListRef: { value: null },
  39. photoUploadRef: { value: null },
  40. formData: { id: 'vehicle-existing', licensePlate: '车牌', vehicleType: 'self' },
  41. carrierList: { value: [] },
  42. submitLoading: { value: false },
  43. personnelList: { value: [person] },
  44. certList: { value: [] },
  45. isEdit: { value: true },
  46. dialogVisible: { value: true },
  47. visible: { value: true },
  48. props: { vehicleType: 'self' },
  49. VEHICLE_TYPE_SELF: 'self',
  50. vehicleApi: {
  51. update: async (id: string, payload: any) => {
  52. assert.equal(id, 'vehicle-existing')
  53. requests.push(JSON.parse(JSON.stringify(payload)))
  54. },
  55. get: async () => ({ data: { id: 'vehicle-existing' } })
  56. },
  57. pickBxData: (response: any) => response.data,
  58. ElMessage: { success() {}, warning() {}, error(message: string) { assert.fail(message) } },
  59. emit() {},
  60. fetchData() {}
  61. }
  62. const submit = submitHandler(page, context)
  63. await submit()
  64. person.remark = ''
  65. person.isDefault = 0
  66. await submit()
  67. assert.equal(requests.length, 2)
  68. assert.equal(requests[0].personnelList[0].remark, '原备注')
  69. const saved = requests[1].personnelList[0]
  70. assert.equal(saved.personnelId, 'person-existing')
  71. assert.equal(saved.isDefault, 0)
  72. assert.equal(saved.remark, '', 'explicitly cleared remark must survive JSON serialization')
  73. assert.equal(Object.hasOwn(saved, 'phoneNo'), false, 'omitted read-only phone stays omitted')
  74. assert.equal(context.submitLoading.value, false)
  75. })
  76. }