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

describe('OnlineBoardRouteMetaTagsComponent', () => {
    let component: OnlineBoardRouteMetaTagsComponent;
    let fixture: ComponentFixture<OnlineBoardRouteMetaTagsComponent>;
    const getRouteMetaTagsMock = jasmine.createSpy('getRouteMetaTags');
    const data: IOnlineBoardRouteData = {
        urlParams: {},
    };

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

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

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

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

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