test time entity

This commit is contained in:
sbriat 2023-04-11 10:48:19 +02:00
parent 8ab1ddbebc
commit 2ffa40aa53
3 changed files with 82 additions and 4 deletions

View File

@ -26,7 +26,6 @@ export class Time {
this._validateBaseDate();
this._validatePunctualDate();
this._validateRecurrentDate();
console.log(this.fromDate);
}
_validateBaseDate() {

View File

@ -1,7 +1,7 @@
import { Schedule } from '../dtos/schedule.type';
export interface IRequestTime {
departure: string;
fromDate: string;
schedule: Schedule;
departure?: string;
fromDate?: string;
schedule?: Schedule;
}

View File

@ -0,0 +1,79 @@
import { Time } from '../../domain/entities/time';
import { IRequestTime } from '../../domain/interfaces/time-request.interface';
const MARGIN_DURATION = 900;
const VALIDITY_DURATION = 365;
const punctualTimeRequest: IRequestTime = {
departure: '2023-04-01 12:24:00',
};
const invalidPunctualTimeRequest: IRequestTime = {
departure: '2023-15-01 12:24:00',
};
const recurrentTimeRequest: IRequestTime = {
fromDate: '2023-04-01',
};
const invalidRecurrentTimeRequest: IRequestTime = {
fromDate: '2023-15-01',
};
const expectedPunctualFromDate = new Date(punctualTimeRequest.departure);
describe('Time entity', () => {
it('should be defined', () => {
const time = new Time(
punctualTimeRequest,
MARGIN_DURATION,
VALIDITY_DURATION,
);
expect(time).toBeDefined();
});
describe('init', () => {
it('should initialize a punctual time request', () => {
const time = new Time(
punctualTimeRequest,
MARGIN_DURATION,
VALIDITY_DURATION,
);
time.init();
expect(time.fromDate.getFullYear()).toBe(
expectedPunctualFromDate.getFullYear(),
);
});
it('should initialize a recurrent time request', () => {
const time = new Time(
recurrentTimeRequest,
MARGIN_DURATION,
VALIDITY_DURATION,
);
time.init();
expect(time.fromDate.getFullYear()).toBe(
expectedPunctualFromDate.getFullYear(),
);
});
it('should throw an exception if no date is provided', () => {
const time = new Time({}, MARGIN_DURATION, VALIDITY_DURATION);
expect(() => time.init()).toThrow();
});
it('should throw an exception if punctual date is invalid', () => {
const time = new Time(
invalidPunctualTimeRequest,
MARGIN_DURATION,
VALIDITY_DURATION,
);
expect(() => time.init()).toThrow();
});
it('should throw an exception if recuurent date is invalid', () => {
const time = new Time(
invalidRecurrentTimeRequest,
MARGIN_DURATION,
VALIDITY_DURATION,
);
expect(() => time.init()).toThrow();
});
});
});