diff --git a/tests/data/EventFactory.ts b/tests/data/EventFactory.ts index d17a4c59..3a13c80e 100644 --- a/tests/data/EventFactory.ts +++ b/tests/data/EventFactory.ts @@ -22,9 +22,9 @@ export class EventFactory { return substitutes.map((sub) => EventModel.merge(EventFactory.fake(), sub)); } - public static fake(substitute?: Partial): EventModel { + public static fake(): EventModel { const [start, end] = EventFactory.randomTime(); - const fake = EventModel.create({ + return EventModel.create({ uuid: uuid(), organization: FactoryUtils.pickRandomValue(EventFactory.ORGS), title: faker.datatype.hexaDecimal(10), @@ -37,19 +37,56 @@ export class EventFactory { requiresStaff: FactoryUtils.getRandomBoolean(), staffPointBonus: EventFactory.randomPointValue(), }); - return EventModel.merge(fake, substitute); + } + + public static fakePastEvent(daysAgo = 1): EventModel { + const [start, end] = EventFactory.randomPastTime(daysAgo); + return EventFactory.with({ start, end })[0]; + } + + public static fakeOngoingEvent(): EventModel { + const [start, end] = EventFactory.randomOngoingTime(); + return EventFactory.with({ start, end })[0]; + } + + public static fakeFutureEvent(daysAhead = 1): EventModel { + const [start, end] = EventFactory.randomFutureTime(daysAhead); + return EventFactory.with({ start, end })[0]; } private static randomTime(): [Date, Date] { - // between last and next week - const days = FactoryUtils.getRandomNumber(-7, 7); - // between 8 AM and 6 PM - const hour = FactoryUtils.getRandomNumber(9, 19); - // between 0.5 and 2.5 hours long, rounded to the half hour - const duration = FactoryUtils.getRandomNumber(30, 150, 30); - const start = moment().subtract(days, 'days').hour(hour); + // random day between last and next week + const day = FactoryUtils.getRandomNumber(-7, 7); + return EventFactory.randomIntervalInDay(day); + } + + private static randomPastTime(daysAgo: number): [Date, Date] { + // random day between daysAgo and a week before daysAgo + const day = FactoryUtils.getRandomNumber(-daysAgo - 7, -daysAgo); + return EventFactory.randomIntervalInDay(day); + } + + private static randomOngoingTime(): [Date, Date] { + // 0, 30 mins, or 1 hour before now + const currentHour = moment().hour(); + const hour = FactoryUtils.getRandomNumber(currentHour - 1, currentHour, 0.5); + return EventFactory.randomIntervalInDay(0, hour); + } + + private static randomFutureTime(daysAhead: number): [Date, Date] { + // random day between daysAhead and a week after daysAhead + const day = FactoryUtils.getRandomNumber(daysAhead, daysAhead + 7); + return EventFactory.randomIntervalInDay(day); + } + + private static randomIntervalInDay(day: number, hour?: number): [Date, Date] { + // default between 8 AM and 6 PM + if (!hour) hour = FactoryUtils.getRandomNumber(9, 19); + // between 1 and 2.5 hours long, rounded to the half hour + const duration = FactoryUtils.getRandomNumber(60, 150, 30); + const start = moment().add(day, 'days').hour(hour); const end = moment(start.valueOf()).add(duration, 'minutes'); - return [new Date(start.valueOf()), new Date(end.valueOf())]; + return [start.toDate(), end.toDate()]; } private static randomPointValue(): number {