import { TestBed } from '@angular/core/testing';
import { ScheduleSearchPageUrlParamsResolver } from '@schedule/resolvers/url-params/search-page-url-params.resolver';
import { ScheduleUrlParserService } from '@schedule/services/url/url-parser.service';

describe('ScheduleSearchPageUrlParamsResolver', () => {
    let service: ScheduleSearchPageUrlParamsResolver;
    const parseRouteParamsMock = jasmine.createSpy('parseRouteParams');

    beforeEach(() => {
        TestBed.configureTestingModule({
            providers: [
                ScheduleSearchPageUrlParamsResolver,
                {
                    provide: ScheduleUrlParserService,
                    useValue: {
                        parseRouteParams: parseRouteParamsMock,
                    },
                },
            ],
        });

        service = TestBed.inject(ScheduleSearchPageUrlParamsResolver);
        parseRouteParamsMock.calls.reset();
    });

    it('should return schedule url params without inbound params', () => {
        const activatedRoute = {
            params: {
                params: 'MOW-MRS-20220528-20220529',
            },
        };

        const parsed = {
            departure: 'MOW',
            arrival: 'MRS',
            dateFrom: new Date(2022, 4, 28),
            dateTo: new Date(2022, 4, 29),
        };

        parseRouteParamsMock
            .withArgs(activatedRoute.params.params)
            .and.returnValue(parsed);

        expect(service.resolve(activatedRoute as any)).toEqual({
            route: {
                outbound: parsed,
                inbound: undefined,
            },
        });
    });

    it('should return schedule url params with inbound params', () => {
        const activatedRoute = {
            params: {
                params: 'MOW-MRS-20220528-20220529',
                returnParams: 'MRS-MOW-20220528-20220529',
            },
        };

        const parsed = {
            departure: 'MOW',
            arrival: 'MRS',
            dateFrom: new Date(2022, 4, 28),
            dateTo: new Date(2022, 4, 29),
        };

        const inboundParsed = {
            departure: 'MRS',
            arrival: 'MOW',
            dateFrom: new Date(2022, 4, 28),
            dateTo: new Date(2022, 4, 29),
        };

        parseRouteParamsMock
            .withArgs(activatedRoute.params.params)
            .and.returnValue(parsed);

        parseRouteParamsMock
            .withArgs(activatedRoute.params.returnParams)
            .and.returnValue(inboundParsed);

        expect(service.resolve(activatedRoute as any)).toEqual({
            route: {
                outbound: parsed,
                inbound: inboundParsed,
            },
        });
    });
});
