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.
sz-kjsxy-wx-new/tests/survey-option-notes.test.mjs

52 lines
3.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import fs from 'node:fs'
import assert from 'node:assert/strict'
import { createRequire } from 'node:module'
const require = createRequire(import.meta.url)
const Vue = require('../../sz-kjsxy-web/node_modules/vue')
const helperSource = fs.readFileSync(new URL('../common/survey.js', import.meta.url), 'utf8')
const helpers = await import('data:text/javascript;base64,' + Buffer.from(helperSource).toString('base64'))
const pageSource = fs.readFileSync(new URL('../packages/surveyFill/index.vue', import.meta.url), 'utf8')
const script = pageSource.split('<script>')[1].split('</script>')[0]
.replace(/import[\s\S]*?from\s+['"][^'"]+['"]/g, '').replace('export default', 'return')
const imports = ['prepareQuestions', 'required', 'showOther', 'firstError', 'progress', 'submission', 'formatDescription', 'fillUserFields', 'visibleQuestions', 'selectedNotes']
const page = new Function('SurveyField', ...imports, script)({}, ...imports.map(name => name === 'formatDescription' ? helpers.descriptionHtml : helpers[name]))
const vm = new Vue(page)
vm.questionList = helpers.prepareQuestions([
{ id: 30, field: 'evening', name: '晚上:参加的主题及人数', edit_input: 'checkbox', need_fill: true, config: { min_choices: 1, max_choices: 3, option_notes: { tax: { required: true }, law: { required: true } } }, select_item: [{ key: 'tax', value: '财税' }, { key: 'law', value: '法律' }, { key: 'hr', value: '人力资源' }] },
{ id: 31, field: 'other', name: '另一题', edit_input: 'radio', config: { option_notes: { '0': { required: true } } }, select_item: [{ key: '0', value: '其他' }] }
])
vm.setValue(vm.questionList[0], ['tax'])
const detached = JSON.parse(JSON.stringify(vm.shownQuestions[0]))
Vue.set(detached.option_texts, 'tax', '国际税,2人')
assert.match(helpers.firstError(vm.questionList).message, /请填写补充说明/) // Reproduce the old serialized-event update.
vm.errorId = 30
vm.setOptionText(detached.id, 'tax', '国际税,2人')
await Vue.nextTick()
assert.equal(vm.questionList[0].option_texts.tax, '国际税,2人')
assert.equal(vm.errorId, null)
assert.equal(helpers.firstError(vm.questionList), null)
assert.deepEqual(helpers.submission(vm.questionList)[0].option_texts, { tax: '国际税,2人' })
// Input and blur can supply either plain text (u-input) or a native detail object.
vm.setOptionText('30', 'tax', { detail: { value: '国际税,3人' } })
assert.equal(helpers.submission(vm.questionList)[0].option_texts.tax, '国际税,3人')
vm.setValue(vm.questionList[0], ['tax', 'law'])
assert.match(helpers.firstError(vm.questionList).message, /请填写补充说明/)
vm.setOptionText(30, 'law', '合同,1人')
assert.equal(helpers.firstError(vm.questionList), null)
vm.setValue(vm.questionList[1], '0')
vm.setOptionText(31, 0, '另一题的说明')
assert.equal(helpers.submission(vm.questionList)[1].option_texts['0'], '另一题的说明')
assert.equal(vm.questionList[0].option_texts.tax, '国际税,3人')
vm.setValue(vm.questionList[0], ['law'])
assert.deepEqual(helpers.submission(vm.questionList)[0].option_texts, { law: '合同,1人' })
vm.setOptionText(30, 'law', ' ')
assert.match(helpers.firstError(vm.questionList).message, /请填写补充说明/)
vm.setOptionText(30, 'law', '')
assert.match(helpers.firstError(vm.questionList).message, /请填写补充说明/)
vm.setOptionText(30, 'law', '正常内容')
vm.submitting = true
vm.setOptionText(30, 'law', '提交期间不得覆盖')
assert.equal(vm.questionList[0].option_texts.law, '正常内容')
vm.$destroy()
console.log('Survey option notes: serialized event regression, reactive input, blur synchronization, independent options and required validation passed')