subform-add-row-template.test.mjs 8.91 KB
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import { createFormEngine } from '../form-engine/core.js'
import {
	cloneRowTemplateForAdd,
	clearRowFieldForAdd,
	valueMapToRowFieldsFromOrigin
} from '../form-engine/group-row-adapter.js'
import { buildGroupRowsCache } from '../form-engine/group-rows-cache.js'
import { getSubformRowTemplateForAdd } from '../form-engine/field-types.js'
import {
	buildAppJsonContent,
	mergeAppJsonIntoSchema
} from '../form-engine/app-subform-json.js'

const originChildren = [
	{ key: 'F2-9-1', label: '交付专员', type: 'select', optionsKey: '4sStaffList' },
	{ key: 'F2-9-2', label: '今日交车', type: 'numberInput' }
]

const filledFirstRow = [
	{
		key: 'F2-9-1',
		label: '交付专员',
		type: 'select',
		optionsKey: '4sStaffList',
		value: '交付专员A',
		'F2-9-1': { id: 'u-1', name: '交付专员A' }
	},
	{ key: 'F2-9-2', label: '今日交车', type: 'numberInput', value: 5 }
]

describe('subform add row template (origin_children)', () => {
	it('IT-SUBFORM-ADD-001: cloneRowTemplateForAdd uses origin_children not children[0]', () => {
		const group = {
			key: 'F2-9',
			type: 'subform',
			origin_children: originChildren,
			children: [filledFirstRow]
		}
		const row = cloneRowTemplateForAdd(group)
		const staff = row.find(f => f.key === 'F2-9-1')
		assert.ok(staff)
		assert.equal(staff.value, undefined)
		assert.equal(staff['F2-9-1'], undefined)
		const num = row.find(f => f.key === 'F2-9-2')
		assert.equal(num.value, undefined)
	})

	it('IT-SUBFORM-ADD-002: addGroupRow second row is empty after first row filled', () => {
		const schema = {
			list: [
				{
					key: 'F2-9',
					type: 'subform',
					origin_children: originChildren,
					children: [originChildren]
				}
			]
		}
		const engine = createFormEngine()
		engine.init(schema, {})
		engine.setAppGroupFieldValue('F2-9', 0, 'F2-9-1', {
			id: 'u-1',
			displayLabel: '交付专员A'
		})
		engine.setGroupFieldValue('F2-9', 0, 'F2-9-2', 5)

		const viewState = engine.addGroupRow(schema.list[0])
		const rows = engine.getGroupRows('F2-9')
		assert.equal(rows.length, 2)
		assert.equal(rows[0]['F2-9-1']?.id ?? rows[0]['F2-9-1'], 'u-1')
		assert.equal(rows[0]['F2-9-2'], 5)
		assert.equal(rows[1]['F2-9-1'], undefined)
		assert.equal(rows[1]['F2-9-2'], undefined)

		const cache = viewState.groupRowsCache['F2-9']
		const row1Staff = cache[1].find(f => f.key === 'F2-9-1')
		assert.equal(row1Staff.value, undefined)
		assert.equal(row1Staff['F2-9-1'], undefined)
	})

	it('IT-SUBFORM-ADD-003: fallback children[0] when no origin_children', () => {
		const group = {
			key: 'F2-9',
			type: 'subform',
			children: [filledFirstRow]
		}
		assert.deepEqual(getSubformRowTemplateForAdd(group), filledFirstRow)
		const row = cloneRowTemplateForAdd(group)
		const staff = row.find(f => f.key === 'F2-9-1')
		assert.equal(staff.value, undefined)
		assert.equal(staff['F2-9-1'], undefined)
	})

	it('IT-SUBFORM-ADD-005: buildGroupRowsCache uses values not group.value FormField[][]', () => {
		const group = {
			key: 'F2-9',
			type: 'subform',
			origin_children: originChildren,
			value: [filledFirstRow]
		}
		const values = {
			'F2-9': [
				{ 'F2-9-1': { id: 'u-1', name: '交付专员A' }, 'F2-9-2': 5 },
				{}
			]
		}
		const cache = buildGroupRowsCache([group], values)
		assert.equal(cache['F2-9'].length, 2)
		const row1Staff = cache['F2-9'][1].find(f => f.key === 'F2-9-1')
		assert.equal(row1Staff.value, undefined)
		assert.equal(row1Staff['F2-9-1'], undefined)
	})

	it('IT-SUBFORM-ADD-006: addGroupRow on detail-like schema keeps second row empty', () => {
		const schema = {
			list: [
				{
					key: 'F2-9',
					type: 'subform',
					origin_children: originChildren,
					value: [filledFirstRow]
				}
			]
		}
		const engine = createFormEngine()
		engine.init(schema, {})
		const viewState = engine.addGroupRow('F2-9')
		const cache = viewState.groupRowsCache['F2-9']
		assert.equal(cache.length, 2)
		const row1Staff = cache[1].find(f => f.key === 'F2-9-1')
		assert.equal(row1Staff.value, undefined)
		assert.equal(row1Staff['F2-9-1'], undefined)
	})

	it('IT-SUBFORM-ADD-004: clearRowFieldForAdd strips dual key', () => {
		const cleared = clearRowFieldForAdd({
			key: 'F2-9-1',
			type: 'select',
			value: 'name',
			'F2-9-1': { id: 'x' }
		})
		assert.equal(cleared.value, undefined)
		assert.equal(cleared['F2-9-1'], undefined)
	})

	it('IT-SUBFORM-ADD-007: origin_children numberInput value 0 preserved on add row', () => {
		const originWithZero = [
			{ key: 'F2-9-1', label: '交付专员', type: 'select', optionsKey: '4sStaffList' },
			{ key: 'F2-9-2', label: '今日交车', type: 'numberInput', value: 0 }
		]
		const group = {
			key: 'F2-9',
			type: 'subform',
			origin_children: originWithZero,
			children: [filledFirstRow]
		}
		const row = cloneRowTemplateForAdd(group)
		const num = row.find(f => f.key === 'F2-9-2')
		assert.equal(num.value, 0)
	})

	it('IT-SUBFORM-TPL-001: valueMapToRowFieldsFromOrigin falls back to template value 0', () => {
		const originWithZero = [
			{ key: 'F2-9-1', label: '交付专员', type: 'select', optionsKey: '4sStaffList' },
			{ key: 'F2-9-2', label: '今日交车', type: 'numberInput', value: 0 }
		]
		const row = valueMapToRowFieldsFromOrigin(originWithZero, {})
		const num = row.find(f => f.key === 'F2-9-2')
		assert.equal(num.value, 0)
	})

	it('IT-SUBFORM-TPL-002: valueMapToRowFieldsFromOrigin keeps explicit valueMap 0', () => {
		const originWithZero = [
			{ key: 'F2-9-2', label: '今日交车', type: 'numberInput', value: 0 }
		]
		const row = valueMapToRowFieldsFromOrigin(originWithZero, { 'F2-9-2': 0 })
		const num = row.find(f => f.key === 'F2-9-2')
		assert.equal(num.value, 0)
	})

	it('IT-SUBFORM-ADD-008: addGroupRow new row keeps numberInput 0 in values and cache', () => {
		const originWithZero = [
			{ key: 'F2-9-1', label: '交付专员', type: 'select', optionsKey: '4sStaffList' },
			{ key: 'F2-9-2', label: '今日交车', type: 'numberInput', value: 0 }
		]
		const schema = {
			list: [
				{
					key: 'F2-9',
					type: 'subform',
					origin_children: originWithZero,
					children: [originWithZero]
				}
			]
		}
		const engine = createFormEngine()
		engine.init(schema, { initialValues: { 'F2-9': [{}] } })
		const viewState = engine.addGroupRow(schema.list[0])
		const rows = engine.getGroupRows('F2-9')
		assert.equal(rows.length, 2)
		assert.equal(rows[1]['F2-9-2'], 0)
		const cacheNum = viewState.groupRowsCache['F2-9'][1].find(f => f.key === 'F2-9-2')
		assert.equal(cacheNum.value, 0)
	})
})

describe('subform origin_children jsonContent roundtrip', () => {
	const originWithZero = [
		{ key: 'F2-9-1', label: '交付专员', type: 'select', optionsKey: '4sStaffList' },
		{ key: 'F2-9-2', label: '今日交车', type: 'numberInput', value: 0 }
	]

	const schemaRoot = {
		list: [
			{
				key: 'F2-9',
				type: 'subform',
				origin_children: originWithZero,
				children: [originWithZero]
			}
		]
	}

	it('IT-SUBFORM-SAVE-001: buildAppJsonContent preserves numberInput value 0 in origin_children', () => {
		const row = [
			{ key: 'F2-9-1', type: 'select', optionsKey: '4sStaffList', value: 'A', 'F2-9-1': 'u-1' },
			{ key: 'F2-9-2', type: 'numberInput', value: 5 }
		]
		const jsonList = buildAppJsonContent(schemaRoot.list, { 'F2-9': [row] })
		const section = jsonList.find(f => f.key === 'F2-9')
		const num = section.origin_children.find(f => f.key === 'F2-9-2')
		assert.equal(num.value, 0)
	})

	it('IT-SUBFORM-SAVE-002: save merge roundtrip keeps value 0 for add row', () => {
		const row = [
			{ key: 'F2-9-1', type: 'select', optionsKey: '4sStaffList', value: 'A', 'F2-9-1': 'u-1' },
			{ key: 'F2-9-2', type: 'numberInput', value: 5 }
		]
		const jsonList = buildAppJsonContent(schemaRoot.list, { 'F2-9': [row] })
		const merged = mergeAppJsonIntoSchema(schemaRoot, {
			jsonContent: JSON.stringify(jsonList)
		})
		const subform = merged.list.find(f => f.key === 'F2-9')
		const originNum = subform.origin_children.find(f => f.key === 'F2-9-2')
		assert.equal(originNum.value, 0)
		const addRow = cloneRowTemplateForAdd(subform)
		assert.equal(addRow.find(f => f.key === 'F2-9-2').value, 0)
	})

	it('IT-SUBFORM-SAVE-003: legacy jsonContent without value restores from schema template', () => {
		const legacyJson = JSON.stringify([
			{
				key: 'F2-9',
				type: 'subform',
				origin_children: [
					{ key: 'F2-9-1', label: '交付专员', type: 'select', optionsKey: '4sStaffList' },
					{ key: 'F2-9-2', label: '今日交车', type: 'numberInput' }
				],
				children: [
					[
						{ key: 'F2-9-1', type: 'select', value: 'A', 'F2-9-1': 'u-1' },
						{ key: 'F2-9-2', type: 'numberInput', value: 3 }
					]
				]
			}
		])
		const merged = mergeAppJsonIntoSchema(schemaRoot, { jsonContent: legacyJson })
		const subform = merged.list.find(f => f.key === 'F2-9')
		const originNum = subform.origin_children.find(f => f.key === 'F2-9-2')
		assert.equal(originNum.value, 0)
		const addRow = cloneRowTemplateForAdd(subform)
		assert.equal(addRow.find(f => f.key === 'F2-9-2').value, 0)
	})
})