Files
shanxia2.0/web-admin/scripts/mobile-pl-plan-date-field.test.ts

109 lines
3.1 KiB
TypeScript

import assert from 'node:assert/strict'
import { readFileSync } from 'node:fs'
import test from 'node:test'
import { parse } from '@vue/compiler-sfc'
import ts from 'typescript'
import { effectScope, nextTick, reactive, ref, watch } from 'vue'
const source = parse(
readFileSync(new URL('../src/views/m/pl/plan/PlanDateField.vue', import.meta.url), 'utf8')
).descriptor.scriptSetup!.content
function harness() {
const props = reactive({
modelValue: '2026-10-24',
label: '计划日期',
clearable: true,
required: false,
readonly: false,
disabled: false
})
const events: any[] = []
const scope = effectScope()
const bindings = {
ref,
watch,
defineProps: () => props,
defineEmits:
() =>
(...args: any[]) =>
events.push(args)
}
const field = scope.run(() =>
new Function(
...Object.keys(bindings),
ts.transpile(source, { target: ts.ScriptTarget.ESNext }) +
';return {open,selected,openPicker,confirm,clear}'
)(...Object.values(bindings))
)
return { props, events, field, scope }
}
test('Vant date field prefills saved date and only emits confirmed selection', () => {
const h = harness()
try {
h.field.openPicker()
assert.deepEqual(h.field.selected.value, ['2026', '10', '24'])
h.field.selected.value = ['2026', '10', '25']
h.field.open.value = false
h.field.confirm({ selectedValues: ['2026', '10', '25'] })
assert.deepEqual(h.events, [])
h.field.openPicker()
h.field.confirm({ selectedValues: ['2026', '10', '26'] })
assert.deepEqual(h.events, [['update:modelValue', '2026-10-26']])
assert.equal(h.field.open.value, false)
} finally {
h.scope.stop()
}
})
test('readonly and disabled dates cannot open; external changes invalidate pending selection', async () => {
const h = harness()
try {
h.props.readonly = true
h.field.openPicker()
assert.equal(h.field.open.value, false)
h.props.readonly = false
h.props.disabled = true
h.field.openPicker()
assert.equal(h.field.open.value, false)
h.props.disabled = false
await nextTick()
h.field.openPicker()
h.props.modelValue = '2026-11-01'
await nextTick()
assert.equal(h.field.open.value, false)
h.field.confirm({ selectedValues: ['2026', '10', '26'] })
assert.deepEqual(h.events, [])
h.field.openPicker()
assert.deepEqual(h.field.selected.value, ['2026', '11', '01'])
} finally {
h.scope.stop()
}
})
test('date can only be cleared from an open optional editable picker', () => {
const h = harness()
try {
h.field.clear()
assert.deepEqual(h.events, [])
for (const key of ['readonly', 'disabled', 'required'] as const) {
h.field.open.value = true
h.props[key] = true
h.field.clear()
assert.deepEqual(h.events, [])
h.props[key] = false
}
h.props.clearable = false
h.field.clear()
assert.deepEqual(h.events, [])
h.props.clearable = true
h.field.openPicker()
h.field.clear()
assert.deepEqual(h.events, [['update:modelValue', '']])
assert.equal(h.field.open.value, false)
} finally {
h.scope.stop()
}
})