From 2ffa40aa53d568155a022065af56178900f2b860 Mon Sep 17 00:00:00 2001 From: sbriat Date: Tue, 11 Apr 2023 10:48:19 +0200 Subject: [PATCH] test time entity --- src/modules/matcher/domain/entities/time.ts | 1 - .../interfaces/time-request.interface.ts | 6 +- src/modules/matcher/tests/unit/time.spec.ts | 79 +++++++++++++++++++ 3 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 src/modules/matcher/tests/unit/time.spec.ts diff --git a/src/modules/matcher/domain/entities/time.ts b/src/modules/matcher/domain/entities/time.ts index f29c5e0..45b5b09 100644 --- a/src/modules/matcher/domain/entities/time.ts +++ b/src/modules/matcher/domain/entities/time.ts @@ -26,7 +26,6 @@ export class Time { this._validateBaseDate(); this._validatePunctualDate(); this._validateRecurrentDate(); - console.log(this.fromDate); } _validateBaseDate() { diff --git a/src/modules/matcher/domain/interfaces/time-request.interface.ts b/src/modules/matcher/domain/interfaces/time-request.interface.ts index 44ee14e..6425441 100644 --- a/src/modules/matcher/domain/interfaces/time-request.interface.ts +++ b/src/modules/matcher/domain/interfaces/time-request.interface.ts @@ -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; } diff --git a/src/modules/matcher/tests/unit/time.spec.ts b/src/modules/matcher/tests/unit/time.spec.ts new file mode 100644 index 0000000..b19ce8a --- /dev/null +++ b/src/modules/matcher/tests/unit/time.spec.ts @@ -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(); + }); + }); +});