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

describe('OnlineBoardFlightNumberMetaTagsComponent', () => {
    let component: OnlineBoardFlightNumberMetaTagsComponent;
    let fixture: ComponentFixture<OnlineBoardFlightNumberMetaTagsComponent>;
    const getFlightNumberMetaTagsMock = jasmine.createSpy(
        'getFlightNumberMetaTags',
    );
    const data: IOnlineBoardRouteData = {
        urlParams: {},
    };

    beforeEach(async () => {
        await TestBed.configureTestingModule({
            declarations: [OnlineBoardFlightNumberMetaTagsComponent],
            providers: [
                {
                    provide: OnlineBoardMetaTagsService,
                    useValue: {
                        getFlightNumberMetaTags: getFlightNumberMetaTagsMock,
                    },
                },
                {
                    provide: ActivatedRoute,
                    useValue: {
                        data: new BehaviorSubject(data),
                    },
                },
            ],
            schemas: [NO_ERRORS_SCHEMA],
        }).compileComponents();
    });

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

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

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

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

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

        getFlightNumberMetaTagsMock
            .withArgs(data.urlParams.flightNumber)
            .and.returnValue({
                title: 'flight number title',
                description: 'flight number description',
            });
        component.ngOnInit();

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