Files
flights_web/tests/e2e/helpers/onlineboard-fixtures.ts
T

58 lines
1.5 KiB
TypeScript

import { addDays, formatIsoDate, formatYmd } from "./dates";
type DateTimeValue = {
utc?: string;
local?: string;
localTime?: string;
};
type OnlineboardDetailsFixture = {
data: {
routes: Array<{
flightId: {
dateLT?: string;
date: string;
};
leg: {
departure: { times: Record<string, DateTimeValue> };
arrival: { times: Record<string, DateTimeValue> };
daysForTabs?: string[];
};
}>;
};
};
function replaceDatePart(value: string | undefined, isoDate: string): string | undefined {
return value?.replace(/^\d{4}-\d{2}-\d{2}/, isoDate);
}
export function nextOnlineboardDetailsFixture(raw: string): {
body: string;
compactDate: string;
} {
const date = addDays(new Date(), 1);
const isoDate = formatIsoDate(date);
const compactDate = formatYmd(date);
const fixture = JSON.parse(raw) as OnlineboardDetailsFixture;
for (const route of fixture.data.routes) {
route.flightId.date = isoDate;
route.flightId.dateLT = isoDate;
for (const point of [route.leg.departure, route.leg.arrival]) {
for (const value of Object.values(point.times)) {
const utc = replaceDatePart(value.utc, isoDate);
if (utc !== undefined) value.utc = utc;
const local = replaceDatePart(value.local, isoDate);
if (local !== undefined) value.local = local;
}
}
route.leg.daysForTabs = Array.from({ length: 15 }, (_, index) =>
formatYmd(addDays(date, index)),
);
}
return { body: JSON.stringify(fixture), compactDate };
}