You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
125 lines
8.0 KiB
125 lines
8.0 KiB
import fs from 'node:fs'
|
|
import assert from 'node:assert/strict'
|
|
const source = fs.readFileSync(new URL('../common/courseForm.js', import.meta.url), 'utf8')
|
|
const helper = await import('data:text/javascript;base64,' + Buffer.from(source).toString('base64'))
|
|
const { prepareCourseFields, changeCourseChoice, courseFormError, visibleCourseFields, requiredCourseField, courseSubmission } = helper
|
|
const raw = [
|
|
{ id: 1, field: 'finance', name: '融资', edit_input: 'radio', sort: 1, select_item: [{ key: 'road', value: '路演' }, { key: 'later', value: '事后对接' }, { key: 'no', value: '不需要' }] },
|
|
{ id: 2, field: 'venues', name: '会场', edit_input: 'checkbox', sort: 2, select_item: [{ key: 'tax', value: '财税' }, { key: 'legal', value: '法律' }, { key: 'ip', value: '知识产权' }], config: { min_choices: 2, max_choices: 2, option_notes: { tax: { required: true, placeholder: '咨询问题' } }, visible_when: { field: 'finance', values: ['road', 'later'], required_values: ['road'] } } },
|
|
{ id: 3, field: 'username', name: '姓名', edit_input: 'text', sort: 3, belong_user: true },
|
|
{ id: 4, field: 'number', name: '数量', edit_input: 'number', sort: 4, value: 0 }
|
|
]
|
|
const fields = prepareCourseFields(raw, { name: '当前学员' })
|
|
assert.equal(fields[2].value, '当前学员')
|
|
assert.equal(fields[3].value, 0)
|
|
assert.deepEqual(visibleCourseFields(fields).map(field => field.field), ['finance', 'username', 'number'])
|
|
changeCourseChoice(fields[0], ['路演'])
|
|
assert.ok(requiredCourseField(fields[1], fields))
|
|
assert.match(courseFormError(fields), /请填写/)
|
|
changeCourseChoice(fields[1], ['法律'])
|
|
assert.match(courseFormError(fields), /至少选择2项/)
|
|
changeCourseChoice(fields[1], ['财税', '法律'])
|
|
assert.match(courseFormError(fields), /补充说明/)
|
|
fields[1].option_texts.tax = '税务筹划'
|
|
assert.equal(courseFormError(fields), '')
|
|
changeCourseChoice(fields[1], ['财税', '法律', '知识产权'])
|
|
assert.match(courseFormError(fields), /最多选择2项/)
|
|
changeCourseChoice(fields[1], ['法律', '知识产权'])
|
|
assert.deepEqual(fields[1].option_texts, {})
|
|
changeCourseChoice(fields[0], ['事后对接'])
|
|
assert.equal(requiredCourseField(fields[1], fields), false)
|
|
changeCourseChoice(fields[1], [])
|
|
assert.equal(courseFormError(fields), '')
|
|
changeCourseChoice(fields[0], ['不需要'])
|
|
fields[1].option_texts.tax = '不应提交'
|
|
const hidden = courseSubmission(fields)[1]
|
|
assert.equal(hidden.value, null)
|
|
assert.deepEqual(hidden.option_texts, {})
|
|
assert.deepEqual(hidden.selected_keys, [])
|
|
assert.equal(hidden.hidden, true)
|
|
const restored = prepareCourseFields([{ ...raw[2], value: '报名时姓名' }, { ...raw[1], config: {}, value: '财税,法律' }], { name: '最新个人资料' })
|
|
assert.equal(restored[1].value, '报名时姓名')
|
|
assert.deepEqual(restored[0].selected_keys, ['tax', 'legal'])
|
|
assert.equal(restored[0].config.min_choices, undefined)
|
|
|
|
const componentSource = fs.readFileSync(new URL('../components/applyForm/index.vue', import.meta.url), 'utf8')
|
|
const script = componentSource.split('<script>')[1].split('</script>')[0]
|
|
.replace(/import[\s\S]*?from\s+['"][^'"]+['"]/g, '').replace('export default', 'return')
|
|
const names = ['prepareCourseFields', 'visibleCourseFields', 'requiredCourseField', 'changeCourseChoice', 'courseNotes', 'courseSelected', 'courseChoiceHint', 'courseFormError', 'courseSubmission']
|
|
const component = new Function(...names, 'CompanySelector', 'baseUrl', script)(...names.map(name => helper[name]), {}, 'https://example.invalid')
|
|
let picked, notice
|
|
const state = { formFields: prepareCourseFields(raw), now_fileld: null, selectShow: false,
|
|
base: { toast: message => { notice = message } }, $set: (target, key, value) => { target[key] = value },
|
|
$emit: (_, payload) => { picked = payload } }
|
|
Object.assign(state, component.methods)
|
|
Object.defineProperty(state, 'visibleFields', { get: () => component.computed.visibleFields.call(state) })
|
|
// Hidden preceding fields must not shift date/radio picker targets to the wrong field.
|
|
state.showSelect(state.formFields[0], 0)
|
|
state.selectConfirm([0])
|
|
assert.deepEqual(state.formFields[0].selected_keys, ['road'])
|
|
state.checkboxGroupChange({ detail: { value: ['财税', '法律'] } }, state.formFields[1])
|
|
state.formFields[1].option_texts.tax = '咨询'
|
|
state.submit()
|
|
assert.equal(picked[1].value, '财税,法律')
|
|
assert.equal(picked[1].option_texts.tax, '咨询')
|
|
state.checkboxGroupChange({ detail: { value: ['财税', '法律', '知识产权'] } }, state.formFields[1])
|
|
assert.equal(notice, '最多选择2项')
|
|
assert.equal(state.formFields[1].selected_keys.length, 2)
|
|
console.log('Course form: legacy prefill and answers, conditional required/min/max, notes, hidden payloads, picker indices and real component events passed')
|
|
|
|
// Model the mini-program native prop bridge: the parent supplies deserialized,
|
|
// unobserved objects rather than sharing the parent's reactive object graph.
|
|
const { createRequire } = await import('node:module')
|
|
const require = createRequire(import.meta.url)
|
|
const Vue = require('../../sz-kjsxy-web/node_modules/vue')
|
|
const parent = new Vue()
|
|
const bridgeFields = prepareCourseFields([...raw,
|
|
{ id: 5, field: 'arrival', name: '日期', sort: 5, edit_input: 'date' },
|
|
{ id: 6, field: 'consult', name: '咨询内容', sort: 6, edit_input: 'text', config: { visible_when: { field: 'venues', values: ['tax'] } } }
|
|
])
|
|
const live = new (Vue.extend(component))({ parent, propsData: { course_forms: bridgeFields } })
|
|
live.base = { isNull: value => value == null || value === '', toast: () => {} }
|
|
assert.equal(live.visibleFields.some(field => field.field === 'venues'), false)
|
|
live.showSelect({ ...live.visibleFields[0] }, 0)
|
|
live.selectConfirm([0])
|
|
await Vue.nextTick()
|
|
assert.equal(live.visibleFields.some(field => field.field === 'venues'), true)
|
|
assert.equal(live.isRequired(live.formFields[1]), true)
|
|
assert.deepEqual(bridgeFields[0].selected_keys, []) // No prop mutation.
|
|
live.checkboxGroupChange({ detail: { value: ['财税', '法律'] } }, { ...live.formFields[1] })
|
|
await Vue.nextTick()
|
|
assert.equal(live.visibleFields.some(field => field.field === 'consult'), true)
|
|
live.showSelect({ ...live.formFields[0] }, 0)
|
|
live.selectConfirm([1])
|
|
await Vue.nextTick()
|
|
assert.equal(live.visibleFields.some(field => field.field === 'venues'), true)
|
|
assert.equal(live.isRequired(live.formFields[1]), false)
|
|
live.showSelect({ ...live.formFields[0] }, 0)
|
|
live.selectConfirm([2])
|
|
await Vue.nextTick()
|
|
assert.equal(live.visibleFields.some(field => ['venues', 'consult'].includes(field.field)), false)
|
|
const date = live.visibleFields.find(field => field.field === 'arrival')
|
|
live.showDate({ ...date }, live.visibleFields.indexOf(date))
|
|
live.dateConfirm({ year: '2026', month: '11', day: '01' })
|
|
assert.equal(live.formFields.find(field => field.field === 'arrival').value, '2026-11-01')
|
|
assert.equal(live.formFields.find(field => field.field === 'username').value, '')
|
|
let sent
|
|
live.$on('backForm', payload => { sent = payload })
|
|
live.submit()
|
|
assert.equal(sent.find(field => field.field === 'venues').hidden, true)
|
|
assert.equal(sent.find(field => field.field === 'venues').value, null)
|
|
assert.deepEqual(sent.find(field => field.field === 'venues').option_texts, {})
|
|
live.$destroy()
|
|
const multiBridge = prepareCourseFields(raw.map(field => field.field === 'finance' ? { ...field, edit_input: 'checkbox' } : field))
|
|
const multi = new (Vue.extend(component))({ parent, propsData: { course_forms: multiBridge } })
|
|
multi.base = live.base
|
|
assert.equal(multi.visibleFields.some(field => field.field === 'venues'), false)
|
|
multi.checkboxGroupChange({ detail: { value: ['路演'] } }, { ...multi.formFields[0] })
|
|
await Vue.nextTick()
|
|
assert.equal(multi.visibleFields.some(field => field.field === 'venues'), true)
|
|
multi.checkboxGroupChange({ detail: { value: [] } }, { ...multi.formFields[0] })
|
|
await Vue.nextTick()
|
|
assert.equal(multi.visibleFields.some(field => field.field === 'venues'), false)
|
|
multi.$destroy(); parent.$destroy()
|
|
console.log('Native prop bridge reactivity: radio/checkbox trigger fields, dynamic required flags, hidden chains, copied event objects and picker indices passed')
|