import { NO_ERRORS_SCHEMA } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { OnlineBoardDetailsViewComponent } from '@online-board/pages/flight-details/components/view/online-board-details.component';
import DeprecatedFlightUtils from '@shared/models-legacy/get-flight.strategy';
import { APP_SETTINGS } from '@shared/services';
import { RouteType } from '@typings/enums';

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

    const emitOpenMock = jasmine.createSpy('open.emit');
    const emitDateChangeMock = jasmine.createSpy('dateChange.emit');

    const flightLegacy: any = {
        routeType: RouteType.DIRECT,
        pId: '1',
        flightId: {
            flightNumber: '0001',
            carrier: 'SU',
            date: '2022-05-28T00:00:00',
        },
        leg: {
            departure: {
                latest: {
                    city: 'Москва',
                },
            },
            arrival: {
                latest: {
                    city: 'Самара',
                },
            },
        },
    };

    beforeEach(async () => {
        await TestBed.configureTestingModule({
            declarations: [OnlineBoardDetailsViewComponent],
            providers: [
                {
                    provide: APP_SETTINGS,
                    useValue: {},
                },
            ],
            schemas: [NO_ERRORS_SCHEMA],
        });
    });

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

        getFirstLegMock = spyOn(DeprecatedFlightUtils, 'getFirstLeg')
            .withArgs(flightLegacy)
            .and.returnValue(flightLegacy.leg as any);

        emitOpenMock.calls.reset();
        emitDateChangeMock.calls.reset();

        component.flightLegacy = flightLegacy;
        (component.open as any) = {
            emit: emitOpenMock,
        };
        (component.dateChange as any) = {
            emit: emitDateChangeMock,
        };
    });

    it('should not call getFirstLeg from utils', () => {
        component.ngOnChanges({});
        expect(getFirstLegMock).not.toHaveBeenCalled();
    });

    it('should set firstLeg', () => {
        component.ngOnChanges({
            flight: {} as any,
        });
        expect(component.firstLeg).toEqual(flightLegacy.leg);
    });

    it('should not emit open event because pIds are equal', () => {
        component.handleOpenEvent({ pId: '1' } as any);
        expect(emitOpenMock).not.toHaveBeenCalled();
    });

    it('should emit open event with provided flight', () => {
        const flight = { pId: '2' } as any;
        component.handleOpenEvent(flight);
        expect(emitOpenMock).toHaveBeenCalledWith(flight);
    });

    it('should emit date', () => {
        const date = new Date(2022, 4, 28);
        component.handleDateChanged(date);

        expect(emitDateChangeMock).toHaveBeenCalledWith(date);
    });
});
