refactor
This commit is contained in:
31
core/racing/domain/value-objects/driver/DriverBio.test.ts
Normal file
31
core/racing/domain/value-objects/driver/DriverBio.test.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { DriverBio } from './DriverBio';
|
||||
|
||||
describe('DriverBio', () => {
|
||||
it('should create a driver bio', () => {
|
||||
const bio = DriverBio.create('A passionate racer.');
|
||||
expect(bio.toString()).toBe('A passionate racer.');
|
||||
});
|
||||
|
||||
it('should allow empty string', () => {
|
||||
const bio = DriverBio.create('');
|
||||
expect(bio.toString()).toBe('');
|
||||
});
|
||||
|
||||
it('should throw on bio too long', () => {
|
||||
const longBio = 'a'.repeat(501);
|
||||
expect(() => DriverBio.create(longBio)).toThrow('Driver bio cannot exceed 500 characters');
|
||||
});
|
||||
|
||||
it('should equal same bio', () => {
|
||||
const b1 = DriverBio.create('Racer');
|
||||
const b2 = DriverBio.create('Racer');
|
||||
expect(b1.equals(b2)).toBe(true);
|
||||
});
|
||||
|
||||
it('should not equal different bio', () => {
|
||||
const b1 = DriverBio.create('Racer');
|
||||
const b2 = DriverBio.create('Driver');
|
||||
expect(b1.equals(b2)).toBe(false);
|
||||
});
|
||||
});
|
||||
29
core/racing/domain/value-objects/driver/DriverBio.ts
Normal file
29
core/racing/domain/value-objects/driver/DriverBio.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { RacingDomainValidationError } from '../../errors/RacingDomainError';
|
||||
import type { IValueObject } from '@core/shared/domain';
|
||||
|
||||
export interface DriverBioProps {
|
||||
value: string;
|
||||
}
|
||||
|
||||
export class DriverBio implements IValueObject<DriverBioProps> {
|
||||
private constructor(private readonly value: string) {}
|
||||
|
||||
static create(value: string): DriverBio {
|
||||
if (value.length > 500) {
|
||||
throw new RacingDomainValidationError('Driver bio cannot exceed 500 characters');
|
||||
}
|
||||
return new DriverBio(value);
|
||||
}
|
||||
|
||||
toString(): string {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
equals(other: IValueObject<DriverBioProps>): boolean {
|
||||
return this.props.value === other.props.value;
|
||||
}
|
||||
|
||||
get props(): DriverBioProps {
|
||||
return { value: this.value };
|
||||
}
|
||||
}
|
||||
29
core/racing/domain/value-objects/driver/DriverId.test.ts
Normal file
29
core/racing/domain/value-objects/driver/DriverId.test.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { DriverId } from './DriverId';
|
||||
|
||||
describe('DriverId', () => {
|
||||
it('should create a driver id', () => {
|
||||
const id = DriverId.create('driver1');
|
||||
expect(id.toString()).toBe('driver1');
|
||||
});
|
||||
|
||||
it('should throw on empty id', () => {
|
||||
expect(() => DriverId.create('')).toThrow('Driver ID is required');
|
||||
});
|
||||
|
||||
it('should throw on whitespace id', () => {
|
||||
expect(() => DriverId.create(' ')).toThrow('Driver ID is required');
|
||||
});
|
||||
|
||||
it('should equal same id', () => {
|
||||
const id1 = DriverId.create('driver1');
|
||||
const id2 = DriverId.create('driver1');
|
||||
expect(id1.equals(id2)).toBe(true);
|
||||
});
|
||||
|
||||
it('should not equal different id', () => {
|
||||
const id1 = DriverId.create('driver1');
|
||||
const id2 = DriverId.create('driver2');
|
||||
expect(id1.equals(id2)).toBe(false);
|
||||
});
|
||||
});
|
||||
29
core/racing/domain/value-objects/driver/DriverId.ts
Normal file
29
core/racing/domain/value-objects/driver/DriverId.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { RacingDomainValidationError } from '../../errors/RacingDomainError';
|
||||
import type { IValueObject } from '@core/shared/domain';
|
||||
|
||||
export interface DriverIdProps {
|
||||
value: string;
|
||||
}
|
||||
|
||||
export class DriverId implements IValueObject<DriverIdProps> {
|
||||
private constructor(private readonly value: string) {}
|
||||
|
||||
static create(value: string): DriverId {
|
||||
if (!value || value.trim().length === 0) {
|
||||
throw new RacingDomainValidationError('Driver ID is required');
|
||||
}
|
||||
return new DriverId(value.trim());
|
||||
}
|
||||
|
||||
toString(): string {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
equals(other: IValueObject<DriverIdProps>): boolean {
|
||||
return this.props.value === other.props.value;
|
||||
}
|
||||
|
||||
get props(): DriverIdProps {
|
||||
return { value: this.value };
|
||||
}
|
||||
}
|
||||
34
core/racing/domain/value-objects/driver/DriverName.test.ts
Normal file
34
core/racing/domain/value-objects/driver/DriverName.test.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { DriverName } from './DriverName';
|
||||
|
||||
describe('DriverName', () => {
|
||||
it('should create a driver name', () => {
|
||||
const name = DriverName.create('John Doe');
|
||||
expect(name.toString()).toBe('John Doe');
|
||||
});
|
||||
|
||||
it('should trim whitespace', () => {
|
||||
const name = DriverName.create(' John Doe ');
|
||||
expect(name.toString()).toBe('John Doe');
|
||||
});
|
||||
|
||||
it('should throw on empty name', () => {
|
||||
expect(() => DriverName.create('')).toThrow('Driver name is required');
|
||||
});
|
||||
|
||||
it('should throw on whitespace only', () => {
|
||||
expect(() => DriverName.create(' ')).toThrow('Driver name is required');
|
||||
});
|
||||
|
||||
it('should equal same name', () => {
|
||||
const n1 = DriverName.create('John Doe');
|
||||
const n2 = DriverName.create('John Doe');
|
||||
expect(n1.equals(n2)).toBe(true);
|
||||
});
|
||||
|
||||
it('should not equal different name', () => {
|
||||
const n1 = DriverName.create('John Doe');
|
||||
const n2 = DriverName.create('Jane Doe');
|
||||
expect(n1.equals(n2)).toBe(false);
|
||||
});
|
||||
});
|
||||
29
core/racing/domain/value-objects/driver/DriverName.ts
Normal file
29
core/racing/domain/value-objects/driver/DriverName.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { RacingDomainValidationError } from '../../errors/RacingDomainError';
|
||||
import type { IValueObject } from '@core/shared/domain';
|
||||
|
||||
export interface DriverNameProps {
|
||||
value: string;
|
||||
}
|
||||
|
||||
export class DriverName implements IValueObject<DriverNameProps> {
|
||||
private constructor(private readonly value: string) {}
|
||||
|
||||
static create(value: string): DriverName {
|
||||
if (!value || value.trim().length === 0) {
|
||||
throw new RacingDomainValidationError('Driver name is required');
|
||||
}
|
||||
return new DriverName(value.trim());
|
||||
}
|
||||
|
||||
toString(): string {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
equals(other: IValueObject<DriverNameProps>): boolean {
|
||||
return this.props.value === other.props.value;
|
||||
}
|
||||
|
||||
get props(): DriverNameProps {
|
||||
return { value: this.value };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user