app-submit-order.test.mjs 2.7 KB
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import { createFormEngine } from '../form-engine/core.js'
import { buildAppDailySavePayload } from '../form-engine/app-daily-save.js'
import {
	buildAppDailyReportSubmitPayload,
	buildAppNormalizedListsFromJsonContent
} 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 formListWithChildren = [
	{
		key: 'F2-9',
		label: '子表',
		type: 'subform',
		origin_children: originChildren,
		children: [
			[
				{
					key: 'F2-9-1',
					type: 'select',
					optionsKey: '4sStaffList',
					value: '张三',
					'F2-9-1': { id: 'uuid-1', name: '张三' }
				},
				{ key: 'F2-9-2', type: 'numberInput', value: 5 }
			]
		]
	}
]

describe('app submit order (mixins.submitReport)', () => {
	it('IT-APP-ORDER-001: lists from children then jsonContent as childrenValue', () => {
		const listsViaStringify = buildAppNormalizedListsFromJsonContent(
			JSON.stringify(formListWithChildren)
		)
		const payload = buildAppDailyReportSubmitPayload(formListWithChildren)

		const parsed = JSON.parse(payload.jsonContent)
		const sub = parsed.find(item => item.key === 'F2-9')
		assert.ok(Array.isArray(formListWithChildren[0].children))
		assert.equal(sub.children, undefined)
		assert.ok(Array.isArray(sub.childrenValue) && sub.childrenValue.length > 0)

		assert.deepEqual(payload.fieldDataList, listsViaStringify.fieldDataList)
		assert.deepEqual(payload.subformDataList, listsViaStringify.subformDataList)
	})

	it('IT-APP-ORDER-002: buildAppDailySavePayload uses unified submit entry', () => {
		const engine = createFormEngine({
			list: [
				{
					key: 'F3-1',
					label: '洽谈',
					type: 'subform',
					children: [[{ key: 'F3-1-3', type: 'select', optionsKey: '4sStaffList' }]]
				}
			]
		})
		engine.init(
			{
				list: [
					{
						key: 'F3-1',
						label: '洽谈',
						type: 'subform',
						children: [[{ key: 'F3-1-3', type: 'select', optionsKey: '4sStaffList' }]]
					}
				]
			},
			{ baseSceneCode: 110201, brandIds: [3454] }
		)
		engine.setAppGroupFieldValue('F3-1', 0, 'F3-1-3', {
			id: 'uuid-staff-003',
			displayLabel: '王五'
		})

		const payload = buildAppDailySavePayload({ reportTemplateId: 10003, engine })
		assert.equal(payload.fieldDataList.length, 0)
		assert.ok(
			payload.subformDataList.some(
				item => item.columnFieldKey === 'F3-1-3' && item.parentFieldKey === 'F3-1'
			)
		)
		const parsed = JSON.parse(payload.jsonContent)
		assert.deepEqual(parsed[0].childrenValue[0]['F3-1']['F3-1-3'], {
			text: '王五',
			value: 'uuid-staff-003'
		})
	})
})