| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- import assert from 'node:assert/strict'
- import test from 'node:test'
- import { readFileSync } from 'node:fs'
- import ts from 'typescript'
- function submitHandler(path: string, context: Record<string, any>) {
- const source = readFileSync(new URL(path, import.meta.url), 'utf8')
- const script = source.match(/<script[^>]*>([\s\S]*?)<\/script>/)?.[1]
- assert.ok(script)
- const ast = ts.createSourceFile('page.ts', script, ts.ScriptTarget.Latest, true)
- const node = ast.statements.find(
- (statement) => ts.isFunctionDeclaration(statement) && statement.name?.text === 'handleSubmit'
- )
- assert.ok(node)
- const js = ts.transpileModule(node.getText(ast), {
- compilerOptions: { target: ts.ScriptTarget.ES2022 }
- }).outputText
- return new Function(...Object.keys(context), `${js}; return handleSubmit`)(
- ...Object.values(context)
- )
- }
- for (const page of [
- '../src/views/pl/vehicle/index.vue',
- '../src/views/pl/self-vehicle/index.vue',
- '../src/components/business/pl-vehicle-form-dialog/index.vue'
- ]) {
- test(`${page}: saving and clearing personnel remarks preserves identity`, async () => {
- const requests: any[] = []
- const person = {
- personnelId: 'person-existing',
- personnelName: '司机',
- role: 'driver',
- isDefault: 1,
- remark: '原备注'
- }
- const context = {
- formRef: { value: { validate: async () => true } },
- validateSubTables: () => true,
- certFormListRef: { value: null },
- photoUploadRef: { value: null },
- formData: { id: 'vehicle-existing', licensePlate: '车牌', vehicleType: 'self' },
- carrierList: { value: [] },
- submitLoading: { value: false },
- personnelList: { value: [person] },
- certList: { value: [] },
- isEdit: { value: true },
- dialogVisible: { value: true },
- visible: { value: true },
- props: { vehicleType: 'self' },
- VEHICLE_TYPE_SELF: 'self',
- vehicleApi: {
- update: async (id: string, payload: any) => {
- assert.equal(id, 'vehicle-existing')
- requests.push(JSON.parse(JSON.stringify(payload)))
- },
- get: async () => ({ data: { id: 'vehicle-existing' } })
- },
- pickBxData: (response: any) => response.data,
- ElMessage: { success() {}, warning() {}, error(message: string) { assert.fail(message) } },
- emit() {},
- fetchData() {}
- }
- const submit = submitHandler(page, context)
- await submit()
- person.remark = ''
- person.isDefault = 0
- await submit()
- assert.equal(requests.length, 2)
- assert.equal(requests[0].personnelList[0].remark, '原备注')
- const saved = requests[1].personnelList[0]
- assert.equal(saved.personnelId, 'person-existing')
- assert.equal(saved.isDefault, 0)
- assert.equal(saved.remark, '', 'explicitly cleared remark must survive JSON serialization')
- assert.equal(Object.hasOwn(saved, 'phoneNo'), false, 'omitted read-only phone stays omitted')
- assert.equal(context.submitLoading.value, false)
- })
- }
|