100 lines
3.5 KiB
TypeScript
100 lines
3.5 KiB
TypeScript
import {
|
|
profileUpdateSchema,
|
|
changePasswordSchema,
|
|
} from '@/validators/user.validator';
|
|
|
|
describe('profileUpdateSchema', () => {
|
|
it('should pass with empty object (all optional)', () => {
|
|
const { error } = profileUpdateSchema.validate({});
|
|
expect(error).toBeUndefined();
|
|
});
|
|
|
|
it('should pass with all fields', () => {
|
|
const { error } = profileUpdateSchema.validate({
|
|
prefix: { th: 'นาย', en: 'Mr.' },
|
|
first_name: 'John',
|
|
last_name: 'Doe',
|
|
phone: '0812345678',
|
|
avatar_url: 'https://example.com/avatar.jpg',
|
|
birth_date: new Date('1990-01-01'),
|
|
});
|
|
expect(error).toBeUndefined();
|
|
});
|
|
|
|
it('should pass with partial update (first_name only)', () => {
|
|
const { error } = profileUpdateSchema.validate({ first_name: 'Jane' });
|
|
expect(error).toBeUndefined();
|
|
});
|
|
|
|
it('should fail if first_name is empty string', () => {
|
|
const { error } = profileUpdateSchema.validate({ first_name: '' });
|
|
expect(error).toBeDefined();
|
|
});
|
|
|
|
it('should fail if first_name exceeds 100 characters', () => {
|
|
const { error } = profileUpdateSchema.validate({ first_name: 'a'.repeat(101) });
|
|
expect(error).toBeDefined();
|
|
});
|
|
|
|
it('should fail if phone is too short (< 10 chars)', () => {
|
|
const { error } = profileUpdateSchema.validate({ phone: '081234' });
|
|
expect(error).toBeDefined();
|
|
});
|
|
|
|
it('should fail if phone exceeds 15 characters', () => {
|
|
const { error } = profileUpdateSchema.validate({ phone: '1'.repeat(16) });
|
|
expect(error).toBeDefined();
|
|
});
|
|
|
|
it('should pass with valid birth_date', () => {
|
|
const { error } = profileUpdateSchema.validate({ birth_date: new Date('2000-06-15') });
|
|
expect(error).toBeUndefined();
|
|
});
|
|
});
|
|
|
|
describe('changePasswordSchema (user.validator)', () => {
|
|
it('should pass with valid old and new passwords', () => {
|
|
const { error } = changePasswordSchema.validate({
|
|
oldPassword: 'oldpass123',
|
|
newPassword: 'newpass456',
|
|
});
|
|
expect(error).toBeUndefined();
|
|
});
|
|
|
|
it('should fail without oldPassword', () => {
|
|
const { error } = changePasswordSchema.validate({ newPassword: 'newpass456' });
|
|
expect(error).toBeDefined();
|
|
expect(error?.details[0].message).toMatch(/Old password is required/i);
|
|
});
|
|
|
|
it('should fail without newPassword', () => {
|
|
const { error } = changePasswordSchema.validate({ oldPassword: 'oldpass123' });
|
|
expect(error).toBeDefined();
|
|
expect(error?.details[0].message).toMatch(/New password is required/i);
|
|
});
|
|
|
|
it('should fail if oldPassword is shorter than 6 chars', () => {
|
|
const { error } = changePasswordSchema.validate({
|
|
oldPassword: '12345',
|
|
newPassword: 'newpass456',
|
|
});
|
|
expect(error).toBeDefined();
|
|
expect(error?.details[0].message).toMatch(/at least 6 characters/i);
|
|
});
|
|
|
|
it('should fail if newPassword is shorter than 6 chars', () => {
|
|
const { error } = changePasswordSchema.validate({
|
|
oldPassword: 'oldpass123',
|
|
newPassword: '123',
|
|
});
|
|
expect(error).toBeDefined();
|
|
});
|
|
|
|
it('should fail if oldPassword exceeds 100 characters', () => {
|
|
const { error } = changePasswordSchema.validate({
|
|
oldPassword: 'a'.repeat(101),
|
|
newPassword: 'newpass456',
|
|
});
|
|
expect(error).toBeDefined();
|
|
});
|
|
});
|