import { NO_ERRORS_SCHEMA } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ActivatedRoute } from '@angular/router';
import { DictionariesService } from '@modules/components/page-filters/services/dictionaries-service';
import { TranslatePipe } from '@ngx-translate/core';
import { OnlineBoardFlightDetailsMetaTagsComponent } from '@online-board/pages/flight-details/components/meta-tags/flight-details-meta-tags.component';
import { OnlineBoardUrlParserService } from '@online-board/services/url';
import { DateTransformerService } from '@shared/services/date/date-transformer.service';
import { BehaviorSubject } from 'rxjs';

describe('OnlineBoardFlightDetailsMetaTagsComponent', () => {
    let fixture: ComponentFixture<OnlineBoardFlightDetailsMetaTagsComponent>;
    let component: OnlineBoardFlightDetailsMetaTagsComponent;

    const date = new Date(2022, 4, 28);
    const transformedDate = '2022-05-28';
    const routeParams = {
        params: 'SU0001-20220528',
    };
    const title = 'flight details meta tag title';
    const description = 'flight details meta tag description';

    const getCityOrAirportMock = jasmine.createSpy('getCityOrAirport');

    const transformMock = jasmine
        .createSpy('transform')
        .withArgs('SEO.BOARD.FLIGHT-DETAILS.TITLE', {
            flightNumber: 'SU 0001',
            date: transformedDate,
        })
        .and.returnValue(title)
        .withArgs('SEO.BOARD.FLIGHT-DETAILS.DESCRIPTION', {
            flightNumber: 'SU 0001',
            date: transformedDate,
        })
        .and.returnValue(description);

    const parseFlightIdMock = jasmine
        .createSpy('parseFlightId')
        .withArgs(routeParams.params)
        .and.returnValue({
            flightNumber: '0001',
            carrier: 'SU',
            date,
        });

    const toStringMock = jasmine
        .createSpy('toString')
        .withArgs(date)
        .and.returnValue(transformedDate);

    beforeEach(async () => {
        await TestBed.configureTestingModule({
            declarations: [OnlineBoardFlightDetailsMetaTagsComponent],
            providers: [
                {
                    provide: ActivatedRoute,
                    useValue: {
                        params: new BehaviorSubject(routeParams),
                    },
                },
                {
                    provide: DictionariesService,
                    useValue: {
                        ready$: Promise.resolve(),
                        getCityOrAirport: getCityOrAirportMock,
                    },
                },
                {
                    provide: TranslatePipe,
                    useValue: {
                        transform: transformMock,
                    },
                },
                {
                    provide: OnlineBoardUrlParserService,
                    useValue: {
                        parseFlightId: parseFlightIdMock,
                    },
                },
                {
                    provide: DateTransformerService,
                    useValue: {
                        toString: toStringMock,
                    },
                },
            ],
            schemas: [NO_ERRORS_SCHEMA],
        }).compileComponents();
    });

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

        getCityOrAirportMock.calls.reset();
        transformMock.calls.reset();
        parseFlightIdMock.calls.reset();
        toStringMock.calls.reset();
    });

    it('should set title and description fields', async () => {
        component.ngOnInit();
        await Promise.resolve();

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