chore: add test for the Date

This commit is contained in:
Sostene MUNEZERO BAGIRA 2022-05-24 20:46:56 +02:00
parent 3eee930f34
commit 688db9d921
No known key found for this signature in database
GPG Key ID: 1334D6A6DF6FB950
3 changed files with 101 additions and 15 deletions

View File

@ -1,8 +1,95 @@
import moment, { isMoment } from 'moment';
import OneUptimeDate from '../../Types/Date';
import PositiveNumber from '../../Types/PositiveNumber';
describe('class OneUptimeDate', () => {
test('new OneUptimeDate', () => {
const date: OneUptimeDate = new OneUptimeDate();
expect(date).toBeInstanceOf(OneUptimeDate);
test('OneUptimeDate.getCurrentDate should return current date', () => {
expect(OneUptimeDate.getCurrentDate().getFullYear()).toEqual(
new Date().getFullYear()
);
expect(OneUptimeDate.getCurrentDate().getDay()).toEqual(
new Date().getDay()
);
expect(OneUptimeDate.getCurrentDate().getDate()).toEqual(
new Date().getDate()
);
expect(OneUptimeDate.getCurrentDate().getHours()).toEqual(
new Date().getHours()
);
expect(isMoment(OneUptimeDate.getCurrentDate())).toBeFalsy();
});
test('OneUptimeDAte.getCurrentMomentDate() should return moment Date', () => {
expect(isMoment(OneUptimeDate.getCurrentMomentDate())).toBeTruthy();
});
test('OneUptimeDAte.getSomeMinutesAgo should return someMinutes ago Date from current time', () => {
expect(
OneUptimeDate.getSomeMinutesAgo(new PositiveNumber(4)).getMinutes()
).toEqual(moment().add(-4, 'minutes').toDate().getMinutes());
});
test('OneUptimeDAte.getOneMinAgo should return one minute age Date from current time', () => {
expect(OneUptimeDate.getOneMinAgo().getMinutes()).toEqual(
moment().add(-1, 'minute').toDate().getMinutes()
);
});
test('OneUptimeDAte.getOneDayAgo should return oneDate ago Date', () => {
expect(OneUptimeDate.getOneDayAgo().getDay()).toEqual(
moment().add(-1, 'day').toDate().getDay()
);
});
test('OneUptimeDAte.getSomeHoursAgo should return moment Date', () => {
expect(
OneUptimeDate.getSomeHoursAgo(new PositiveNumber(4)).getHours()
).toEqual(moment().add(-4, 'hours').toDate().getHours());
});
test('OneUptimeDAte.getSomeDaysAgo should return moment Date', () => {
expect(
OneUptimeDate.getSomeDaysAgo(new PositiveNumber(4)).getDay()
).toEqual(moment().add(-4, 'days').toDate().getDay());
});
test('OneUptimeDAte.getSomeSecondsAgo should return moment Date', () => {
expect(
OneUptimeDate.getSomeSecondsAgo(new PositiveNumber(4)).getSeconds()
).toEqual(moment().add(-4, 'seconds').toDate().getSeconds());
});
test('OneUptimeDAte.getOneMinAfter should return moment Date', () => {
expect(OneUptimeDate.getOneMinAfter().getMinutes()).toEqual(
moment().add(1, 'minute').toDate().getMinutes()
);
});
test('OneUptimeDAte.getOneDayAfter should return moment Date', () => {
expect(OneUptimeDate.getOneDayAfter()).toEqual(
moment().add(1, 'day').toDate()
);
});
test('OneUptimeDAte.getSomeMinutesAfter should return moment Date', () => {
expect(
OneUptimeDate.getSomeMinutesAfter(
new PositiveNumber(4)
).getMinutes()
).toEqual(moment().add(4, 'minutes').toDate().getMinutes());
});
test('OneUptimeDAte.getSomeHoursAfter should return moment Date', () => {
expect(
OneUptimeDate.getSomeHoursAfter(new PositiveNumber(4)).getHours()
).toEqual(moment().add(4, 'hours').toDate().getHours());
});
test('OneUptimeDAte.getSomeDaysAfter should return moment Date', () => {
expect(
OneUptimeDate.getSomeDaysAfter(new PositiveNumber(4)).getDay()
).toEqual(moment().add(4, 'days').toDate().getDay());
});
test('OneUptimeDAte.getSomeSecondsAfter should return moment Date', () => {
expect(
OneUptimeDate.getSomeSecondsAfter(
new PositiveNumber(4)
).getSeconds()
).toEqual(moment().add(4, 'seconds').toDate().getSeconds());
});
test('OneUptimeDAte.getCurrentYear should return the current year', () => {
expect(
OneUptimeDate.getSomeSecondsAfter(
new PositiveNumber(4)
).getSeconds()
).toEqual(moment().add(4, 'seconds').toDate().getSeconds());
});
});

View File

@ -38,7 +38,7 @@ export default class OneUptimeDate {
public static getSomeSecondsAgo(seconds: PositiveNumber): Date {
return this.getCurrentMomentDate()
.add(-1 * seconds.toNumber(), 'days')
.add(-1 * seconds.toNumber(), 'seconds')
.toDate();
}
@ -70,7 +70,7 @@ export default class OneUptimeDate {
public static getSomeSecondsAfter(seconds: PositiveNumber): Date {
return this.getCurrentMomentDate()
.add(seconds.toNumber(), 'days')
.add(seconds.toNumber(), 'seconds')
.toDate();
}

View File

@ -5,8 +5,14 @@ export default class EmailWithName {
public get email(): Email {
return this._email;
}
public set email(v: Email) {
this._email = v;
public set email(v: Email | string) {
if (typeof v === 'string') {
this._email = new Email(v);
}
if (v instanceof Email) {
this._email = v;
}
}
private _name: string = '';
@ -18,14 +24,7 @@ export default class EmailWithName {
}
public constructor(name: string, email: string | Email) {
if (typeof email === 'string') {
this.email = new Email(email);
}
if (email instanceof Email) {
this.email = email;
}
this.email = email;
this.name = name;
}