gongxiangp2/lib/__tests__/product-model.test.ts

40 lines
1.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import {
extractPlausibleSpuFromFileName,
getSpuProductModelValidationError,
isPlausibleSpuProductModel,
normalizeSpuProductModel,
} from '@/lib/product-model'
describe('product-model', () => {
test.each(['DF-A3DZ0008', 'BJ0037', 'GBPM-SN0204', '626DNZ0385-1', 'GBGJ-DNZ0366+GBGJ-SJ0251'])(
'接受线上已有的多种 SPU 格式:%s',
(value) => expect(isPlausibleSpuProductModel(value)).toBe(true),
)
test.each(['-624-456', '624-456', '456', '-1', '视频-624-456', 'SPU-', ''])(
'拒绝可以明确判定不是 SPU 的值:%s',
(value) => expect(isPlausibleSpuProductModel(value)).toBe(false),
)
test('只自动转成大写,不删除用户输入的任何字符', () => {
expect(normalizeSpuProductModel(' df-a3dz0008 中文 ')).toBe(' DF-A3DZ0008 中文 ')
expect(normalizeSpuProductModel('ß1-ſ2-ı3-ff4')).toBe('ß1-ſ2-ı3-ff4')
expect(isPlausibleSpuProductModel('ß1')).toBe(false)
})
test('不合规内容原样保留后由校验给出明确原因', () => {
expect(getSpuProductModelValidationError(' DF-A3DZ0008')).toBe('SPU 前后不能包含空格')
expect(getSpuProductModelValidationError('DF-A3DZ0008中文')).toContain('只能使用')
})
test.each([
['抖音素材-624-456.mp4', ''],
['GBGJ-DNZ0366+GBGJ-SJ0251.mp4', 'GBGJ-DNZ0366+GBGJ-SJ0251'],
['GBPM-SN0204K12.mp4', 'GBPM-SN0204K12'],
['626DNZ0385-1.mp4', '626DNZ0385-1'],
['产品-GBPM-SN0204-展示.mp4', ''],
])('从文件名 %s 安全识别完整 SPU', (fileName, expected) => {
expect(extractPlausibleSpuFromFileName(fileName)).toBe(expected)
})
})