rwadurian/backend/services/referral-service/test/domain/value-objects/referral-code.vo.spec.ts

65 lines
2.4 KiB
TypeScript

import { ReferralCode } from '../../../src/domain/value-objects/referral-code.vo';
describe('ReferralCode Value Object', () => {
describe('create', () => {
it('should create ReferralCode from valid string', () => {
const code = ReferralCode.create('RWATEST123');
expect(code.value).toBe('RWATEST123');
});
it('should convert lowercase to uppercase', () => {
const code = ReferralCode.create('rwatest123');
expect(code.value).toBe('RWATEST123');
});
it('should throw error for too short code', () => {
expect(() => ReferralCode.create('ABC')).toThrow('推荐码长度必须在6-20个字符之间');
});
it('should throw error for too long code', () => {
expect(() => ReferralCode.create('A'.repeat(21))).toThrow('推荐码长度必须在6-20个字符之间');
});
it('should throw error for invalid characters', () => {
expect(() => ReferralCode.create('RWA-TEST')).toThrow('推荐码只能包含大写字母和数字');
});
});
describe('generate', () => {
it('should generate referral code with RWA prefix', () => {
const code = ReferralCode.generate(123456789n);
expect(code.value).toMatch(/^RWA/);
});
it('should generate valid referral code', () => {
const code = ReferralCode.generate(123456789n);
expect(code.value.length).toBeGreaterThanOrEqual(6);
expect(code.value.length).toBeLessThanOrEqual(20);
expect(code.value).toMatch(/^[A-Z0-9]+$/);
});
it('should generate different codes for same user (due to random component)', () => {
const code1 = ReferralCode.generate(123n);
const code2 = ReferralCode.generate(123n);
// Note: there's a small chance they could be the same, but very unlikely
// This test verifies the random component is working
expect(code1.value).toBeDefined();
expect(code2.value).toBeDefined();
});
});
describe('equals', () => {
it('should return true for equal codes', () => {
const code1 = ReferralCode.create('RWATEST123');
const code2 = ReferralCode.create('RWATEST123');
expect(code1.equals(code2)).toBe(true);
});
it('should return false for different codes', () => {
const code1 = ReferralCode.create('RWATEST123');
const code2 = ReferralCode.create('RWATEST456');
expect(code1.equals(code2)).toBe(false);
});
});
});