field-submit-value.test.mjs 3.45 KB
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'

describe('field-submit-value', async () => {
	const {
		buildSelectSubmitValue,
		buildMultipleSelectSubmitValue,
		buildCarBrandSubmitValue,
		extractSelectStorageValueFromRowField,
		parseSelectEchoValue,
		parseMultipleSelectEchoValue,
		parseCarBrandEchoValue
	} = await import('../form-engine/field-submit-value.js')

	const carInfo = {
		vehicleBrandId: 'lantu',
		vehicleBrandName: '岚图',
		vehicleSubBrandId: 'lantu',
		vehicleSubBrandName: '岚图',
		vehicleSeriesId: 'dreamer',
		vehicleSeriesName: '梦想家',
		vehicleShapeId: '2025_ev_long_range_flagship',
		vehicleShapeName: '2025款 EV 超长续航旗舰版'
	}

	it('IT-SUBMIT-001: static select serializes to { id, name }', () => {
		const field = {
			type: 'select',
			options: [{ label: '张三', value: 'u-1' }]
		}
		assert.deepEqual(buildSelectSubmitValue(field, 'u-1'), { id: 'u-1', name: '张三' })
	})

	it('IT-SUBMIT-002: optionsKey select keeps full object', () => {
		const field = { type: 'select', optionsKey: '4sStaffList' }
		const value = { id: 'u-1', name: '张三', phone: '13800138000' }
		assert.deepEqual(buildSelectSubmitValue(field, value), value)
	})

	it('IT-SUBMIT-003: multipleSelect serializes object array', () => {
		const field = { type: 'multipleSelect', optionsKey: 'storeStaffList' }
		const value = [
			{ id: 'u-1', name: '张三' },
			{ id: 'u-2', name: '李四' }
		]
		assert.deepEqual(buildMultipleSelectSubmitValue(field, value), value)
	})

	it('IT-SUBMIT-003b: remote same-name staff keeps each selected id', () => {
		const field = {
			type: 'multipleSelect',
			optionsKey: 'storeStaffList',
			options: [
				{ id: 'u-1', name: '张三', value: 'u-1', label: '张三' },
				{ id: 'u-2', name: '张三', value: 'u-2', label: '张三' }
			]
		}
		assert.deepEqual(buildMultipleSelectSubmitValue(field, [
			{ id: 'u-2', name: '张三' }
		]), [{ id: 'u-2', name: '张三' }])
	})

	it('IT-SUBMIT-004: carBrand serializes to 4-level cascade array', () => {
		assert.deepEqual(buildCarBrandSubmitValue(carInfo), [
			{ id: 'lantu', name: '岚图' },
			{ id: 'lantu', name: '岚图' },
			{ id: 'dreamer', name: '梦想家' },
			{ id: '2025_ev_long_range_flagship', name: '2025款 EV 超长续航旗舰版' }
		])
	})

	it('IT-SUBMIT-005: echo round-trip for select and carBrand', () => {
		const selectField = { type: 'select', optionsKey: '4sStaffList' }
		const staff = { id: 'u-1', name: '张三', phone: '13800138000' }
		assert.deepEqual(parseSelectEchoValue(staff, selectField), staff)

		const restored = parseCarBrandEchoValue(buildCarBrandSubmitValue(carInfo))
		assert.equal(restored.vehicleShapeId, '2025_ev_long_range_flagship')
		assert.equal(restored.vehicleShapeName, '2025款 EV 超长续航旗舰版')
	})

	it('IT-SUBMIT-006: multipleSelect echo preserves object array', () => {
		const field = { type: 'multipleSelect', optionsKey: 'storeStaffList' }
		const value = [{ id: 'u-1', name: '张三' }]
		assert.deepEqual(parseMultipleSelectEchoValue(value, field), value)
	})

	it('IT-SUBMIT-007: extractSelectStorageValueFromRowField keeps dual-key phone', () => {
		const staff = {
			id: '982873a3744e44fbb439432e51edadef',
			name: '林永健test003',
			phone: '17875511831'
		}
		const rowField = {
			key: 'F2-9-1',
			type: 'select',
			optionsKey: '4sStaffList',
			value: '林永健test003',
			'F2-9-1': staff
		}
		assert.deepEqual(extractSelectStorageValueFromRowField(rowField), staff)
	})
})