- Add vitest config and test script to web app - Auth validation tests: phone format, password rules, registration flow - Listing validation tests: all schema steps, constants, merged schema - Utils tests: cn() class merging with Tailwind conflict resolution - 36 tests across 3 test files Co-Authored-By: Paperclip <noreply@paperclip.ing>
87 lines
2.9 KiB
TypeScript
87 lines
2.9 KiB
TypeScript
import { describe, it, expect } from 'vitest';
|
|
import { loginSchema, registerSchema } from '../validations/auth';
|
|
|
|
describe('loginSchema', () => {
|
|
it('should accept valid Vietnamese phone and password', () => {
|
|
const result = loginSchema.safeParse({ phone: '0912345678', password: 'abc123' });
|
|
expect(result.success).toBe(true);
|
|
});
|
|
|
|
it('should accept +84 format phone', () => {
|
|
const result = loginSchema.safeParse({ phone: '+84912345678', password: 'abc123' });
|
|
expect(result.success).toBe(true);
|
|
});
|
|
|
|
it('should reject empty phone', () => {
|
|
const result = loginSchema.safeParse({ phone: '', password: 'abc123' });
|
|
expect(result.success).toBe(false);
|
|
});
|
|
|
|
it('should reject invalid phone format', () => {
|
|
const result = loginSchema.safeParse({ phone: '12345', password: 'abc123' });
|
|
expect(result.success).toBe(false);
|
|
});
|
|
|
|
it('should reject empty password', () => {
|
|
const result = loginSchema.safeParse({ phone: '0912345678', password: '' });
|
|
expect(result.success).toBe(false);
|
|
});
|
|
|
|
it('should accept various valid Vietnamese prefixes', () => {
|
|
const prefixes = ['032', '056', '070', '081', '090'];
|
|
for (const prefix of prefixes) {
|
|
const result = loginSchema.safeParse({ phone: `${prefix}1234567`, password: 'pass' });
|
|
expect(result.success).toBe(true);
|
|
}
|
|
});
|
|
});
|
|
|
|
describe('registerSchema', () => {
|
|
const validData = {
|
|
fullName: 'Nguyen Van A',
|
|
phone: '0912345678',
|
|
password: '12345678',
|
|
confirmPassword: '12345678',
|
|
};
|
|
|
|
it('should accept valid registration data', () => {
|
|
const result = registerSchema.safeParse(validData);
|
|
expect(result.success).toBe(true);
|
|
});
|
|
|
|
it('should accept with optional email', () => {
|
|
const result = registerSchema.safeParse({ ...validData, email: 'test@example.com' });
|
|
expect(result.success).toBe(true);
|
|
});
|
|
|
|
it('should accept empty string email', () => {
|
|
const result = registerSchema.safeParse({ ...validData, email: '' });
|
|
expect(result.success).toBe(true);
|
|
});
|
|
|
|
it('should reject invalid email', () => {
|
|
const result = registerSchema.safeParse({ ...validData, email: 'not-email' });
|
|
expect(result.success).toBe(false);
|
|
});
|
|
|
|
it('should reject short password', () => {
|
|
const result = registerSchema.safeParse({ ...validData, password: '123', confirmPassword: '123' });
|
|
expect(result.success).toBe(false);
|
|
});
|
|
|
|
it('should reject mismatched passwords', () => {
|
|
const result = registerSchema.safeParse({ ...validData, confirmPassword: 'different' });
|
|
expect(result.success).toBe(false);
|
|
});
|
|
|
|
it('should reject short full name', () => {
|
|
const result = registerSchema.safeParse({ ...validData, fullName: 'A' });
|
|
expect(result.success).toBe(false);
|
|
});
|
|
|
|
it('should reject empty full name', () => {
|
|
const result = registerSchema.safeParse({ ...validData, fullName: '' });
|
|
expect(result.success).toBe(false);
|
|
});
|
|
});
|