import { END_DAY, END_MONTH, END_YEAR } from '../src/constants/age-can-valid-until'; import { TicketDto } from '../src/interfaces/ticket/ticket.dto'; import { UserAge } from '../src/rules/ticket/user-age'; import { TICKET_UNITAIRE_MULHOUSE } from './test-config'; /** * DATES PRIS POUR EXEMPLE * - Moke de la date du jour: Date() ou moment() = 2022-09-20 16:30:00 * - 14ans Pile = 2008-09-20 * - 18ans Pile = 2004-09-20 * - 13ans Pile = 2003-09-20 * * ageUntil est compris jusqu'à la fin de l'année */ describe('Unit Tests of User Age rule', () => { const rule = new UserAge(); beforeAll(() => { jest .spyOn(global.Date, 'now') .mockImplementation(() => new Date('2022-09-20 16:30:00').valueOf()); }); it('1.1 Age (end-day) SUCCESS - He have 14 today', () => { expect( rule.validate({ ...TICKET_UNITAIRE_MULHOUSE, ageFrom: 14, ageUntil: 18, ageCanValidUntil: END_DAY, passengers: [{ birthdate: '2008-09-20' }], //14 today } as unknown as TicketDto), ).toBe(true); }); it('1.2 Age (end-day) FAIL - He have 19 today', () => { expect( rule.validate({ ...TICKET_UNITAIRE_MULHOUSE, ageFrom: 14, ageUntil: 18, ageCanValidUntil: END_DAY, passengers: [{ birthdate: '2003-09-20' }], //19 today } as unknown as TicketDto), ).toBe(false); }); it('1.3 Age (end-day) SUCCESS - He have 18 and its last day before birthday', () => { expect( rule.validate({ ...TICKET_UNITAIRE_MULHOUSE, ageFrom: 14, ageUntil: 18, ageCanValidUntil: END_DAY, passengers: [{ birthdate: '2003-09-21' }], //18 but last day } as unknown as TicketDto), ).toBe(true); }); it('1.4 Age (end-day) SUCCESS - He have more than 14 or just equal 14 (HB bro)', () => { //Equal to 14 expect( rule.validate({ ...TICKET_UNITAIRE_MULHOUSE, ageFrom: 14, ageUntil: -1, ageCanValidUntil: END_DAY, passengers: [{ birthdate: '2008-09-20' }], //14 today } as unknown as TicketDto), ).toBe(true); //More than 14 expect( rule.validate({ ...TICKET_UNITAIRE_MULHOUSE, ageFrom: 14, ageUntil: -1, ageCanValidUntil: END_DAY, passengers: [{ birthdate: '2006-09-20' }], //16 today } as unknown as TicketDto), ).toBe(true); /* less than 14 - fail */ expect( rule.validate({ ...TICKET_UNITAIRE_MULHOUSE, ageFrom: 14, ageUntil: -1, ageCanValidUntil: END_DAY, passengers: [{ birthdate: '2009-09-20' }], // 13 } as unknown as TicketDto), ).toBe(false); }); it('1.5 Age (end-day) SUCCESS - He have less than 18', () => { expect( rule.validate({ ...TICKET_UNITAIRE_MULHOUSE, ageFrom: 0, ageUntil: 17, //17 compris ageCanValidUntil: END_DAY, passengers: [{ birthdate: '2004-09-21' }], // 17 but last day } as unknown as TicketDto), ).toBe(true); }); it('1.6 Age (end-day) FAIL - He have less than 18', () => { expect( rule.validate({ ...TICKET_UNITAIRE_MULHOUSE, ageFrom: 0, ageUntil: 17, //17 compris ageCanValidUntil: END_DAY, passengers: [{ birthdate: '2004-09-20' }], // 18 today } as unknown as TicketDto), ).toBe(false); }); it('2.1 Age (end-month) SUCCESS - He have 14 today', () => { expect( rule.validate({ ...TICKET_UNITAIRE_MULHOUSE, ageFrom: 14, ageUntil: 18, ageCanValidUntil: END_MONTH, passengers: [{ birthdate: '2008-09-20' }], //14 today } as unknown as TicketDto), ).toBe(true); }); it('2.2 Age (end-month) SUCCESS - He have 19 today', () => { expect( rule.validate({ ...TICKET_UNITAIRE_MULHOUSE, ageFrom: 14, ageUntil: 18, ageCanValidUntil: END_MONTH, passengers: [{ birthdate: '2003-09-20' }], } as unknown as TicketDto), ).toBe(true); }); it('2.3 Age (end-month) SUCCESS - He have 19 this month', () => { expect( rule.validate({ ...TICKET_UNITAIRE_MULHOUSE, ageFrom: 14, ageUntil: 18, ageCanValidUntil: END_MONTH, passengers: [{ birthdate: '2003-09-10' }], //19 past } as unknown as TicketDto), ).toBe(true); }); it('2.4 Age (end-month) FAIL - He have 19 preview month', () => { expect( rule.validate({ ...TICKET_UNITAIRE_MULHOUSE, ageFrom: 14, ageUntil: 18, ageCanValidUntil: END_MONTH, passengers: [{ birthdate: '2003-08-28' }], //19 preview month } as unknown as TicketDto), ).toBe(false); }); it('2.5 Age (end-month) SUCCESS - He have more than 14 or just equal 14 (HB bro)', () => { expect( rule.validate({ ...TICKET_UNITAIRE_MULHOUSE, ageFrom: 14, ageUntil: -1, ageCanValidUntil: END_MONTH, passengers: [{ birthdate: '2008-09-20' }], } as unknown as TicketDto), ).toBe(true); }); it('2.6 Age (end-month) SUCCESS - He have less than 18', () => { expect( rule.validate({ ...TICKET_UNITAIRE_MULHOUSE, ageFrom: 0, ageUntil: 17, ageCanValidUntil: END_MONTH, passengers: [{ birthdate: '2004-09-21' }], //17 but last day } as unknown as TicketDto), ).toBe(true); }); it('2.7 Age (end-month) SUCCESS - He have more than 18 but in month', () => { expect( rule.validate({ ...TICKET_UNITAIRE_MULHOUSE, ageFrom: 0, ageUntil: 18, ageCanValidUntil: END_MONTH, passengers: [{ birthdate: '2003-09-19' }], //19 same month } as unknown as TicketDto), ).toBe(true); }); it('2.8 Age (end-month) FAIL - He have more than 18 but not in month', () => { expect( rule.validate({ ...TICKET_UNITAIRE_MULHOUSE, ageFrom: 0, ageUntil: 18, ageCanValidUntil: END_MONTH, passengers: [{ birthdate: '2003-08-30' }], //19 previous month } as unknown as TicketDto), ).toBe(false); }); it('3.1 Age (end-year) SUCCESS - He have 14 today', () => { expect( rule.validate({ ...TICKET_UNITAIRE_MULHOUSE, ageFrom: 14, ageUntil: 18, ageCanValidUntil: END_YEAR, passengers: [{ birthdate: '2008-09-20' }], } as unknown as TicketDto), ).toBe(true); }); it('3.2 Age (end-year) SUCCESS - He have 19 today', () => { expect( rule.validate({ ...TICKET_UNITAIRE_MULHOUSE, ageFrom: 14, ageUntil: 18, ageCanValidUntil: END_YEAR, passengers: [{ birthdate: '2003-09-20' }], } as unknown as TicketDto), ).toBe(true); }); it('3.3 Age (end-year) SUCCESS - He have 19 this year', () => { expect( rule.validate({ ...TICKET_UNITAIRE_MULHOUSE, ageFrom: 14, ageUntil: 18, ageCanValidUntil: END_YEAR, passengers: [{ birthdate: '2003-01-31' }], } as unknown as TicketDto), ).toBe(true); }); it('3.4 Age (end-year) FAIL - He have 19 preview year', () => { expect( rule.validate({ ...TICKET_UNITAIRE_MULHOUSE, ageFrom: 14, ageUntil: 18, ageCanValidUntil: END_YEAR, passengers: [{ birthdate: '2002-12-15' }], } as unknown as TicketDto), ).toBe(false); }); it('3.5 Age (end-month) SUCCESS - He have more than 14 or just equal 14 (HB bro)', () => { expect( rule.validate({ ...TICKET_UNITAIRE_MULHOUSE, ageFrom: 14, ageUntil: -1, ageCanValidUntil: END_YEAR, passengers: [{ birthdate: '2008-09-20' }], } as unknown as TicketDto), ).toBe(true); }); it('3.6 Age (end-month) SUCCESS - He have less than 18', () => { expect( rule.validate({ ...TICKET_UNITAIRE_MULHOUSE, ageFrom: 0, ageUntil: 17, ageCanValidUntil: END_YEAR, passengers: [{ birthdate: '2004-09-21' }], //17 but last day } as unknown as TicketDto), ).toBe(true); }); it('3.7 Age (end-month) SUCCESS - He have more than 18 but in month', () => { expect( rule.validate({ ...TICKET_UNITAIRE_MULHOUSE, ageFrom: 0, ageUntil: 18, ageCanValidUntil: END_YEAR, passengers: [{ birthdate: '2003-09-19' }], //19 same year } as unknown as TicketDto), ).toBe(true); }); it('3.8 Age (end-month) FAIL - He have more than 18 but not in year', () => { expect( rule.validate({ ...TICKET_UNITAIRE_MULHOUSE, ageFrom: 0, ageUntil: 18, ageCanValidUntil: END_YEAR, passengers: [{ birthdate: '2002-12-01' }], //19 previous year } as unknown as TicketDto), ).toBe(false); }); });