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 { OnlineBoardMetaTagsService } from '@online-board/services/meta-tags.service';
import { IOnlineBoardRouteData } from '@online-board/types';
import { BehaviorSubject } from 'rxjs';

describe('OnlineBoardArrivalMetaTagsComponent', () => {
    let component: OnlineBoardArrivalMetaTagsComponent;
    let fixture: ComponentFixture<OnlineBoardArrivalMetaTagsComponent>;
    const getArrivalMetaTagsMock = jasmine.createSpy('getArrivalMetaTags');
    const data: IOnlineBoardRouteData = {
        urlParams: {},
    };

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

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

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

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

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