import { TestBed } from '@angular/core/testing';
import { IBreadCrumbItem } from '@components/page/breadcrumds/page-breadcrumbs.component';
import { ScheduleDetailsBreadCrumbsResolver } from '@schedule/resolvers/bread-crumbs/details-bread-crumbs.resolver';
import { ScheduleRequestDataResolver } from '@schedule/resolvers/request-data.resolver';
import { ScheduleBreadCrumbService } from '@schedule/services/bread-crumb.service';

describe('ScheduleDetailsBreadCrumbsResolver', () => {
    let service: ScheduleDetailsBreadCrumbsResolver;
    const getRootBreadCrumbMock = jasmine.createSpy('getRootBreadCrumb');
    const getSearchPageBreadCrumbMock = jasmine.createSpy(
        'getSearchPageBreadCrumb',
    );

    const resolveMock = jasmine.createSpy('resolve');

    const rootBreadCrumb: IBreadCrumbItem = {
        label: 'root-breadcrumb-label',
        url: 'root-breadcrumb-url',
    };

    beforeEach(() => {
        TestBed.configureTestingModule({
            providers: [
                ScheduleDetailsBreadCrumbsResolver,
                {
                    provide: ScheduleBreadCrumbService,
                    useValue: {
                        getRootBreadCrumb: getRootBreadCrumbMock,
                        getSearchPageBreadCrumb: getSearchPageBreadCrumbMock,
                    },
                },
                {
                    provide: ScheduleRequestDataResolver,
                    useValue: {
                        resolve: resolveMock,
                    },
                },
            ],
        });

        service = TestBed.inject(ScheduleDetailsBreadCrumbsResolver);

        getRootBreadCrumbMock.calls.reset();
        getRootBreadCrumbMock.withArgs().and.returnValue(rootBreadCrumb);
        getSearchPageBreadCrumbMock.calls.reset();

        resolveMock.calls.reset();
    });

    it('should return array with root breadcrumb item', async () => {
        resolveMock.withArgs({}).and.returnValue(undefined);
        const result = await service.resolve({} as any);
        expect(result).toEqual([rootBreadCrumb]);
    });

    it('should return array of two breadcrumb items', async () => {
        const requestData = {
            parsed: {
                outbound: {},
            },
        } as any;
        const item: IBreadCrumbItem = {
            label: 'route-item-label',
            url: 'route-item-url',
        };
        resolveMock.withArgs({}).and.returnValue(requestData);
        getSearchPageBreadCrumbMock
            .withArgs(requestData.parsed.outbound)
            .and.returnValue(Promise.resolve(item));

        const result = await service.resolve({} as any);
        expect(result).toEqual([rootBreadCrumb, item]);
    });
});
