Files

80 lines
2.7 KiB
JavaScript
Raw Permalink Normal View History

/**
* Train Module Unit Tests
* Tests for Train and TrainController classes
*/
const { Train, TrainController } = require('../../js/train.js');
describe('Train Module', () => {
describe('Train Class', () => {
test('should create train with default properties', () => {
const train = new Train();
expect(train.id).toBeDefined();
expect(train.speed).toBe(0);
expect(train.maxSpeed).toBeGreaterThan(0);
});
test('should initialize with custom properties', () => {
const train = new Train({ id: 'TEST', speed: 10, maxSpeed: 20 });
expect(train.id).toBe('TEST');
expect(train.speed).toBe(10);
expect(train.maxSpeed).toBe(20);
});
test('should accelerate and brake', () => {
const train = new Train({ speed: 0, maxSpeed: 10 });
train.accelerate();
expect(train.speed).toBeGreaterThan(0);
train.brake();
expect(train.speed).toBeLessThan(10);
});
test('should reverse direction', () => {
const train = new Train({ speed: 10, direction: 1 });
train.reverse();
expect(train.direction).toBe(-1);
});
});
describe('TrainController Class', () => {
test('should create controller with default config', () => {
const controller = new TrainController();
expect(controller.trackPath).toEqual([]);
expect(controller.progress).toBe(0);
});
test('should accept track path configuration', () => {
const path = [
{ x: 0, y: 0 },
{ x: 10, y: 0 },
{ x: 10, y: 10 }
];
const controller = new TrainController(path);
expect(controller.trackPath.length).toBe(3);
});
test('should calculate progress along track', () => {
const path = [{ x: 0, y: 0 }, { x: 10, y: 0 }];
const controller = new TrainController(path);
controller.setDistance(5);
expect(controller.progress).toBeGreaterThanOrEqual(0);
expect(controller.progress).toBeLessThan(1);
});
test('should return current position on track', () => {
const path = [{ x: 0, y: 0 }, { x: 10, y: 0 }];
const controller = new TrainController(path);
const position = controller.getCurrentPosition();
expect(position).toBeDefined();
expect(position.x).toBeGreaterThanOrEqual(0);
expect(position.x).toBeLessThanOrEqual(10);
});
});
});