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

describe('OnlineBoardDepartureMetaTagsComponent', () => {
    let component: OnlineBoardDepartureMetaTagsComponent;
    let fixture: ComponentFixture<OnlineBoardDepartureMetaTagsComponent>;
    const getDepartureMetaTagsMock = jasmine.createSpy('getDepartureMetaTags');
    const data: IOnlineBoardRouteData = {
        urlParams: {},
    };

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

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

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

        getDepartureMetaTagsMock.withArgs(data.urlParams.route).and.returnValue(
            Promise.resolve({
                title: 'departure title',
                description: 'departure description',
            }),
        );
        component.ngOnInit();
        await Promise.resolve();

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