import { NO_ERRORS_SCHEMA } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ActivatedRoute } from '@angular/router';
import { OnlineBoardRouteTitleComponent } from '@online-board/pages/search/route-search/components/title/online-board-route-title.component';
import { OnlineBoardTitleService } from '@online-board/services/title.service';
import { IOnlineBoardRouteData } from '@online-board/types';
import { BehaviorSubject } from 'rxjs';

describe('OnlineBoardRouteTitleComponent', () => {
    let component: OnlineBoardRouteTitleComponent;
    let fixture: ComponentFixture<OnlineBoardRouteTitleComponent>;
    const getRouteTitleMock = jasmine.createSpy('getRouteTitle');
    const data: IOnlineBoardRouteData = {
        urlParams: {},
    };

    beforeEach(async () => {
        await TestBed.configureTestingModule({
            declarations: [OnlineBoardRouteTitleComponent],
            providers: [
                {
                    provide: OnlineBoardTitleService,
                    useValue: {
                        getRouteTitle: getRouteTitleMock,
                    },
                },
                {
                    provide: ActivatedRoute,
                    useValue: {
                        data: new BehaviorSubject(data),
                    },
                },
            ],
            schemas: [NO_ERRORS_SCHEMA],
        }).compileComponents();
    });

    beforeEach(() => {
        fixture = TestBed.createComponent(OnlineBoardRouteTitleComponent);
        component = fixture.componentInstance;

        data.urlParams.route = undefined;
        getRouteTitleMock.calls.reset();
    });

    it('should not set title because route param is not provided', () => {
        component.ngOnInit();

        expect(component.title).toBe('');
    });

    it('should set title', async () => {
        data.urlParams.route = {
            arrival: 'MOW',
            date: new Date(2022, 4, 28),
        };

        getRouteTitleMock
            .withArgs(data.urlParams.route)
            .and.returnValue(Promise.resolve('route title'));
        component.ngOnInit();
        await Promise.resolve();

        expect(component.title).toBe('route title');
    });
});
