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

describe('OnlineBoardFlightNumberTitleComponent', () => {
    let component: OnlineBoardFlightNumberTitleComponent;
    let fixture: ComponentFixture<OnlineBoardFlightNumberTitleComponent>;
    const getFlightNumberTitleMock = jasmine.createSpy('getFlightNumberTitle');
    const data: IOnlineBoardRouteData = {
        urlParams: {},
    };

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

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

        data.urlParams.flightNumber = undefined;
        getFlightNumberTitleMock.calls.reset();
    });

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

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

    it('should set title', () => {
        data.urlParams.flightNumber = {
            flightNumber: '0001',
            carrier: 'SU',
            date: new Date(2022, 4, 28),
        };

        getFlightNumberTitleMock
            .withArgs(data.urlParams.flightNumber)
            .and.returnValue('flight number title');
        component.ngOnInit();

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