app-children-value-json.test.mjs 3.39 KB
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import {
	childrenToChildrenValue,
	childrenValueToRowFieldsList,
	serializeFormListForAppJsonStorage,
	buildAppNormalizedListsFromJsonContent
} from '../form-engine/app-subform-json.js'
import { parseJsonContentDetailValues } from '../form-engine/submit-adapter.js'

function selectValueMapId(value) {
	if (value != null && typeof value === 'object' && !Array.isArray(value)) {
		return value.id ?? value.value
	}
	return value
}

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

const sectionWithChildren = {
	key: 'F2-9',
	label: '子表',
	type: 'subform',
	origin_children: originChildren,
	children: [
		[
			{
				key: 'F2-9-1',
				label: '交付专员',
				type: 'select',
				optionsKey: '4sStaffList',
				value: '张三',
				'F2-9-1': { id: 'uuid-1', name: '张三', phone: '13800138000' }
			},
			{ key: 'F2-9-2', label: '今日交车', type: 'numberInput', value: 5 }
		]
	]
}

describe('app childrenValue jsonContent codec', () => {
	it('IT-APP-CV-001: childrenValue round-trip with dual key and scalar', () => {
		const childrenValue = childrenToChildrenValue(sectionWithChildren)
		assert.deepEqual(childrenValue[0]['F2-9']['F2-9-1'], {
			text: '张三',
			value: 'uuid-1'
		})
		assert.equal(childrenValue[0]['F2-9']['F2-9-2'], 5)

		const rows = childrenValueToRowFieldsList({
			key: 'F2-9',
			origin_children: originChildren,
			childrenValue
		})
		assert.equal(rows.length, 1)
		const staff = rows[0].find(f => f.key === 'F2-9-1')
		assert.equal(staff.value, '张三')
		assert.equal(staff['F2-9-1'], 'uuid-1')
		assert.equal(rows[0].find(f => f.key === 'F2-9-2').value, 5)
	})

	it('IT-APP-CV-002: lists from formList before serialize; childrenValue stores text + id', () => {
		const formList = [sectionWithChildren]
		const fromChildren = buildAppNormalizedListsFromJsonContent(JSON.stringify(formList))
		const fromPreSerialize = buildAppNormalizedListsFromJsonContent(
			JSON.stringify(JSON.parse(JSON.stringify(formList)))
		)
		assert.deepEqual(fromPreSerialize.fieldDataList, fromChildren.fieldDataList)
		assert.deepEqual(fromPreSerialize.subformDataList, fromChildren.subformDataList)

		const serialized = serializeFormListForAppJsonStorage(formList)
		assert.deepEqual(serialized[0].childrenValue[0]['F2-9']['F2-9-1'], {
			text: '张三',
			value: 'uuid-1'
		})
		assert.ok(!serialized[0].children)
		assert.ok(Array.isArray(serialized[0].childrenValue))
	})

	it('IT-APP-CV-004: serializeFormListForAppJsonStorage top-level remote select id in dual key', () => {
		const serialized = serializeFormListForAppJsonStorage([
			{
				key: 'F2-9-1',
				type: 'select',
				optionsKey: '4sStaffList',
				value: '张三',
				'F2-9-1': { id: 'uuid-1', name: '张三', phone: '13800138000' }
			}
		])
		assert.equal(serialized[0].value, '张三')
		assert.equal(serialized[0]['F2-9-1'], 'uuid-1')
	})

	it('IT-APP-CV-003: parseJsonContentDetailValues reads childrenValue id from jsonContent', () => {
		const serialized = serializeFormListForAppJsonStorage([sectionWithChildren])
		const fromChildrenValue = parseJsonContentDetailValues(JSON.stringify(serialized), [])
		assert.equal(fromChildrenValue['F2-9'][0]['F2-9-2'], 5)
		assert.equal(selectValueMapId(fromChildrenValue['F2-9'][0]['F2-9-1']), 'uuid-1')
	})
})