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

describe('OnlineBoardDepartureTitleComponent', () => {
    let component: OnlineBoardDepartureTitleComponent;
    let fixture: ComponentFixture<OnlineBoardDepartureTitleComponent>;
    const getDepartureTitleMock = jasmine.createSpy('getDepartureTitle');
    const data: IOnlineBoardRouteData = {
        urlParams: {},
    };

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

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

        data.urlParams.route = undefined;
        getDepartureTitleMock.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 = {
            departure: 'MOW',
            date: new Date(2022, 4, 28),
        };

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

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