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

describe('OnlineBoardArrivalTitleComponent', () => {
    let component: OnlineBoardArrivalTitleComponent;
    let fixture: ComponentFixture<OnlineBoardArrivalTitleComponent>;
    const getArrivalTitleMock = jasmine.createSpy('getArrivalTitle');
    const data: IOnlineBoardRouteData = {
        urlParams: {},
    };

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

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

        data.urlParams.route = undefined;
        getArrivalTitleMock.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),
        };

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

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