flnxi-Wu/lib/__tests__/template-field-validation.test.ts
2026-06-01 11:24:54 +08:00

203 lines
5.5 KiB
TypeScript

import { validateSubmittedTemplateFields } from '../template-field-validation'
jest.mock('../template-validator', () => ({
getTemplateWithFields: jest.fn(),
}))
const { getTemplateWithFields } = require('../template-validator')
describe('template-field-validation', () => {
beforeEach(() => {
jest.resetAllMocks()
})
test('没有 templateId 时应直接通过', async () => {
const result = await validateSubmittedTemplateFields({
templateId: null,
templateFields: {},
})
expect(result.valid).toBe(true)
})
test('应拦截缺失的必填字段', async () => {
getTemplateWithFields.mockResolvedValue({
id: 'template-1',
name: '测试模板',
fields: [
{ key: 'productModel', label: 'SPU型号', type: 'text', required: true },
{ key: 'customField', label: '自定义字段', type: 'text', required: true },
],
})
const result = await validateSubmittedTemplateFields({
templateId: 'template-1',
templateFields: {
productModel: 'SPU-001',
},
allowUnknownKeys: false,
})
expect(result.valid).toBe(false)
if (!result.valid) {
expect(result.errorCode).toBe('TEMPLATE_REQUIRED_FIELDS_MISSING')
}
})
test('条件不满足的必填字段不应阻止提交', async () => {
getTemplateWithFields.mockResolvedValue({
id: 'template-1',
name: '测试模板',
fields: [
{ key: 'category', label: '分类', type: 'text', required: true },
{
key: 'detailCode',
label: '细分编码',
type: 'text',
required: true,
conditions: JSON.stringify({
logic: 'AND',
conditions: [{ fieldKey: 'category', operator: 'equals', value: '需要编码' }],
}),
},
],
})
const result = await validateSubmittedTemplateFields({
templateId: 'template-1',
templateFields: {
category: '普通分类',
},
allowUnknownKeys: false,
})
expect(result.valid).toBe(true)
if (result.valid) {
expect(result.templateFields).toEqual({ category: '普通分类' })
expect(result.visibleFieldKeys).toEqual(['category'])
}
})
test('条件满足的必填字段缺失时应拦截', async () => {
getTemplateWithFields.mockResolvedValue({
id: 'template-1',
name: '测试模板',
fields: [
{ key: 'category', label: '分类', type: 'text', required: true },
{
key: 'detailCode',
label: '细分编码',
type: 'text',
required: true,
conditions: JSON.stringify({
logic: 'AND',
conditions: [{ fieldKey: 'category', operator: 'equals', value: '需要编码' }],
}),
},
],
})
const result = await validateSubmittedTemplateFields({
templateId: 'template-1',
templateFields: {
category: '需要编码',
},
allowUnknownKeys: false,
})
expect(result.valid).toBe(false)
if (!result.valid) {
expect(result.errorCode).toBe('TEMPLATE_REQUIRED_FIELDS_MISSING')
expect(result.details?.missingRequiredFields).toEqual([
{ key: 'detailCode', label: '细分编码', type: 'text' },
])
}
})
test('应拦截模板中不存在的字段', async () => {
getTemplateWithFields.mockResolvedValue({
id: 'template-1',
name: '测试模板',
fields: [
{ key: 'productModel', label: 'SPU型号', type: 'text', required: true },
],
})
const result = await validateSubmittedTemplateFields({
templateId: 'template-1',
templateFields: {
productModel: 'SPU-001',
unknownField: 'xxx',
},
allowUnknownKeys: false,
})
expect(result.valid).toBe(false)
if (!result.valid) {
expect(result.errorCode).toBe('UNKNOWN_TEMPLATE_FIELDS')
}
})
test('checkbox 字段值为 false 也应视为已填写', async () => {
getTemplateWithFields.mockResolvedValue({
id: 'template-1',
name: '测试模板',
fields: [
{ key: 'agree', label: '同意', type: 'checkbox', required: true },
],
})
const result = await validateSubmittedTemplateFields({
templateId: 'template-1',
templateFields: {
agree: false,
},
allowUnknownKeys: false,
})
expect(result.valid).toBe(true)
})
test('应按模板字段校验规则拦截不合规内容', async () => {
getTemplateWithFields.mockResolvedValue({
id: 'template-1',
name: '测试模板',
fields: [
{
key: 'customCode',
label: '自定义编码',
type: 'text',
required: false,
validations: JSON.stringify({
maxLength: 6,
pattern: '^CODE-',
}),
},
],
})
const result = await validateSubmittedTemplateFields({
templateId: 'template-1',
templateFields: {
customCode: 'BAD-CODE-001',
},
allowUnknownKeys: false,
})
expect(result.valid).toBe(false)
if (!result.valid) {
expect(result.errorCode).toBe('TEMPLATE_FIELD_VALIDATION_FAILED')
expect(result.details?.invalidFields).toEqual([
expect.objectContaining({
key: 'customCode',
label: '自定义编码',
errors: expect.arrayContaining([
'自定义编码长度不能超过6个字符',
'自定义编码格式不正确',
]),
}),
])
}
})
})