refactor
This commit is contained in:
118
core/racing/domain/entities/Car.test.ts
Normal file
118
core/racing/domain/entities/Car.test.ts
Normal file
@@ -0,0 +1,118 @@
|
||||
import { Car } from './Car';
|
||||
import { RacingDomainValidationError } from '../errors/RacingDomainError';
|
||||
|
||||
describe('Car', () => {
|
||||
describe('create', () => {
|
||||
it('should create a car with required fields', () => {
|
||||
const car = Car.create({
|
||||
id: '1',
|
||||
name: 'Ferrari 488',
|
||||
manufacturer: 'Ferrari',
|
||||
gameId: 'iracing',
|
||||
});
|
||||
|
||||
expect(car.id.toString()).toBe('1');
|
||||
expect(car.name.toString()).toBe('Ferrari 488');
|
||||
expect(car.manufacturer.toString()).toBe('Ferrari');
|
||||
expect(car.gameId.toString()).toBe('iracing');
|
||||
});
|
||||
|
||||
it('should create a car with all fields', () => {
|
||||
const car = Car.create({
|
||||
id: '1',
|
||||
name: 'Ferrari 488',
|
||||
shortName: '488',
|
||||
manufacturer: 'Ferrari',
|
||||
carClass: 'gt',
|
||||
license: 'Pro',
|
||||
year: 2018,
|
||||
horsepower: 661,
|
||||
weight: 1320,
|
||||
imageUrl: 'http://example.com/car.jpg',
|
||||
gameId: 'iracing',
|
||||
});
|
||||
|
||||
expect(car.id.toString()).toBe('1');
|
||||
expect(car.name.toString()).toBe('Ferrari 488');
|
||||
expect(car.shortName).toBe('488');
|
||||
expect(car.manufacturer.toString()).toBe('Ferrari');
|
||||
expect(car.carClass.toString()).toBe('gt');
|
||||
expect(car.license.toString()).toBe('Pro');
|
||||
expect(car.year.toNumber()).toBe(2018);
|
||||
expect(car.horsepower?.toNumber()).toBe(661);
|
||||
expect(car.weight?.toNumber()).toBe(1320);
|
||||
expect(car.imageUrl?.toString()).toBe('http://example.com/car.jpg');
|
||||
expect(car.gameId.toString()).toBe('iracing');
|
||||
});
|
||||
|
||||
it('should use defaults for optional fields', () => {
|
||||
const car = Car.create({
|
||||
id: '1',
|
||||
name: 'Ferrari 488',
|
||||
manufacturer: 'Ferrari',
|
||||
gameId: 'iracing',
|
||||
});
|
||||
|
||||
expect(car.carClass.toString()).toBe('gt');
|
||||
expect(car.license.toString()).toBe('D');
|
||||
expect(car.year.toNumber()).toBe(new Date().getFullYear());
|
||||
expect(car.shortName).toBe('Ferrari 48');
|
||||
});
|
||||
|
||||
it('should throw error for invalid id', () => {
|
||||
expect(() => Car.create({
|
||||
id: '',
|
||||
name: 'Ferrari 488',
|
||||
manufacturer: 'Ferrari',
|
||||
gameId: 'iracing',
|
||||
})).toThrow(RacingDomainValidationError);
|
||||
});
|
||||
|
||||
it('should throw error for invalid name', () => {
|
||||
expect(() => Car.create({
|
||||
id: '1',
|
||||
name: '',
|
||||
manufacturer: 'Ferrari',
|
||||
gameId: 'iracing',
|
||||
})).toThrow(RacingDomainValidationError);
|
||||
});
|
||||
|
||||
it('should throw error for invalid manufacturer', () => {
|
||||
expect(() => Car.create({
|
||||
id: '1',
|
||||
name: 'Ferrari 488',
|
||||
manufacturer: '',
|
||||
gameId: 'iracing',
|
||||
})).toThrow(RacingDomainValidationError);
|
||||
});
|
||||
|
||||
it('should throw error for invalid gameId', () => {
|
||||
expect(() => Car.create({
|
||||
id: '1',
|
||||
name: 'Ferrari 488',
|
||||
manufacturer: 'Ferrari',
|
||||
gameId: '',
|
||||
})).toThrow(RacingDomainValidationError);
|
||||
});
|
||||
|
||||
it('should throw error for invalid horsepower', () => {
|
||||
expect(() => Car.create({
|
||||
id: '1',
|
||||
name: 'Ferrari 488',
|
||||
manufacturer: 'Ferrari',
|
||||
gameId: 'iracing',
|
||||
horsepower: 0,
|
||||
})).toThrow(RacingDomainValidationError);
|
||||
});
|
||||
|
||||
it('should throw error for invalid weight', () => {
|
||||
expect(() => Car.create({
|
||||
id: '1',
|
||||
name: 'Ferrari 488',
|
||||
manufacturer: 'Ferrari',
|
||||
gameId: 'iracing',
|
||||
weight: -1,
|
||||
})).toThrow(RacingDomainValidationError);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user