Files
dabai_web_f/tests/utils/validator.test.js

62 lines
1.9 KiB
JavaScript
Raw Normal View History

2026-03-17 14:52:07 +08:00
/**
* 校验工具类单元测试
*/
import { describe, test, expect } from '@jest/globals'
import { isPhone, isEmail, isStrongPassword, FormValidator } from '@/utils/validator'
describe('校验工具测试', () => {
describe('手机号校验', () => {
test('应该正确验证有效手机号', () => {
expect(isPhone('13800138000')).toBe(true)
expect(isPhone('15912345678')).toBe(true)
})
test('应该拒绝无效手机号', () => {
expect(isPhone('12345678901')).toBe(false)
expect(isPhone('1380013800')).toBe(false)
expect(isPhone('abc')).toBe(false)
})
})
describe('邮箱校验', () => {
test('应该正确验证有效邮箱', () => {
expect(isEmail('test@example.com')).toBe(true)
expect(isEmail('user.name@domain.co.uk')).toBe(true)
})
test('应该拒绝无效邮箱', () => {
expect(isEmail('invalid')).toBe(false)
expect(isEmail('@example.com')).toBe(false)
})
})
describe('密码强度校验', () => {
test('应该正确验证强密码', () => {
expect(isStrongPassword('abc123')).toBe(true)
expect(isStrongPassword('Password123')).toBe(true)
})
test('应该拒绝弱密码', () => {
expect(isStrongPassword('123456')).toBe(false)
expect(isStrongPassword('abcdef')).toBe(false)
expect(isStrongPassword('abc')).toBe(false)
})
})
describe('表单校验器', () => {
test('应该正确校验表单', () => {
const validator = new FormValidator({
phone: [
{ required: true, message: '手机号不能为空' },
{ validator: isPhone, message: '手机号格式不正确' }
]
})
expect(validator.validate({ phone: '13800138000' })).toBe(true)
expect(validator.validate({ phone: '' })).toBe(false)
expect(validator.validate({ phone: 'invalid' })).toBe(false)
})
})
})