import { RouteType } from '@typings/enums';
import { getFlightDate } from '@utils/flight/date';
import { IWithLeg } from '@utils/flight/legs/types';

describe('getFlightDate', () => {
    const direct: IWithLeg = {
        routeType: RouteType.DIRECT,
        leg: {
            departure: {
                times: {
                    scheduledDeparture: {
                        local: '2022-05-27T20:30:00+05:00',
                    },
                },
            },
        } as any,
    };

    const multiLeg: IWithLeg = {
        routeType: RouteType.MULTI_LEG,
        legs: [
            {
                departure: {
                    times: {
                        scheduledDeparture: {
                            local: '2022-05-27T17:00:00+03:00',
                        },
                    },
                },
            },
            {
                departure: {
                    times: {
                        scheduledDeparture: {
                            local: '2022-05-27T19:00:00+03:00',
                        },
                    },
                },
            },
        ] as any,
    };

    const connecting1: IWithLeg = {
        routeType: RouteType.CONNECTING,
        flights: [direct, multiLeg] as any,
    };
    const connecting2: IWithLeg = {
        routeType: RouteType.CONNECTING,
        flights: [multiLeg, direct] as any,
    };

    it('should extract date from direct flight', () => {
        expect(getFlightDate(direct)).toEqual(new Date(2022, 4, 27, 20, 30));
    });

    it('should extract date from multi leg flight', () => {
        expect(getFlightDate(multiLeg)).toEqual(new Date(2022, 4, 27, 17));
        expect(getFlightDate(connecting1)).toEqual(
            new Date(2022, 4, 27, 20, 30),
        );
        expect(getFlightDate(connecting2)).toEqual(new Date(2022, 4, 27, 17));
    });

    it('should extract connecting flight with first direct flight', () => {
        expect(getFlightDate(connecting1)).toEqual(
            new Date(2022, 4, 27, 20, 30),
        );
    });

    it('should extract connecting flight with first multileg flight', () => {
        expect(getFlightDate(connecting2)).toEqual(new Date(2022, 4, 27, 17));
    });
});
