roymondchen f12f78c9e0 fix(form): 日期/时间字段支持 valueFormat 为 X 的秒级时间戳
- 日期类字段 change 时经 normalizeTimestampValue 把选择器输出收成秒级 number,避免 Element Plus 写出秒字符串导致校验失败
- Date/DateTime/Time/Daterange/Timerange 回填 model 时,valueFormat 为 X 直接使用秒级数字,不再拼接展示格式
- typeMatch 对 X 格式要求有限整数,示例值与报错文案区分秒级/毫秒级
- datetimeFormatter 支持 X 格式输出秒级数字
- 补充相应单元测试,并更新 docs/form-config/rules.md 中的校验规则说明
2026-10-13 17:11:02 +08:00

151 lines
3.9 KiB
TypeScript

/*
* Tencent is pleased to support the open source community by making TMagicEditor available.
*
* Copyright (C) 2025 Tencent. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { describe, expect, test } from 'vitest';
import { nextTick } from 'vue';
import { mount } from '@vue/test-utils';
import ElementPlus, { ElInput } from 'element-plus';
import MagicForm, { MDateTime, MForm } from '@form/index';
const getWrapper = (
config: any = [
{
text: 'dateTime',
type: 'datetime',
name: 'datetime',
},
],
initValues: any = {
datetime: new Date('2021/01/01 12:00:00').toISOString(),
},
) =>
mount(MForm, {
global: {
plugins: [ElementPlus as any, MagicForm as any],
},
props: {
initValues,
config,
},
});
describe('DateTime', () => {
test('基础', async () => {
const wrapper = getWrapper();
await nextTick();
const datetime = wrapper.findComponent(MDateTime);
expect(datetime.exists()).toBe(true);
const value = await (wrapper.vm as any).submitForm();
expect(value.datetime).toMatch('2021/01/01 12:00:00');
});
test('错误类型初始化初始化', async () => {
const wrapper = getWrapper(
[
{
text: 'dateTime',
type: 'datetime',
name: 'datetime',
},
],
{ datetime: 123 },
);
await nextTick();
const datetime = wrapper.findComponent(MDateTime);
expect(datetime.exists()).toBe(true);
const value = await (wrapper.vm as any).submitForm();
expect(value.datetime).toMatch('');
});
test('无效值初始化', async () => {
const wrapper = getWrapper(
[
{
text: 'dateTime',
type: 'datetime',
name: 'datetime',
},
],
{ datetime: 'Invalid Date' },
);
await nextTick();
const datetime = wrapper.findComponent(MDateTime);
expect(datetime.exists()).toBe(true);
const value = await (wrapper.vm as any).submitForm();
expect(value.datetime).toMatch('');
});
test('输入日期', async () => {
const wrapper = getWrapper([
{
text: 'dateTime',
type: 'datetime',
name: 'datetime',
valueFormat: 'YYYY-MM-DD HH:mm:ss',
},
]);
await nextTick();
const input = wrapper.find('input');
await input.setValue('2021/07/28 00:00:00');
const value = await (wrapper.vm as any).submitForm();
expect(value.datetime).toMatch('2021-07-28 00:00:00');
});
test('valueFormat 为 X 时收成秒级数字', async () => {
const source = '2021-07-17T07:37:00.000Z';
const wrapper = getWrapper([{ text: 'dateTime', type: 'datetime', name: 'datetime', valueFormat: 'X' }], {
datetime: source,
});
await nextTick();
expect((wrapper.vm as any).values.datetime).toBe(Math.floor(new Date(source).getTime() / 1000));
wrapper.findComponent({ name: 'TMDatePicker' }).vm.$emit('update:modelValue', '1626507420');
await nextTick();
expect((wrapper.vm as any).values.datetime).toBe(1626507420);
});
test('清空', async () => {
const wrapper = getWrapper();
await nextTick();
const elInput = wrapper.findComponent(ElInput);
await elInput.trigger('mouseenter');
const clear = wrapper.find('.clear-icon');
expect(clear.exists()).toBe(true);
await clear.trigger('click');
expect((wrapper.vm as any).values.datetime).toBeNull();
});
});