161 lines
4.3 KiB
TypeScript
161 lines
4.3 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)
|
|
})
|
|
})
|