import { SimpleChange } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { IStation } from '../../../shared/models-legacy';
import { StationChangeComponent } from './station-change.component';

describe('StationChangeComponent', () => {
    let component: StationChangeComponent;
    let fixture: ComponentFixture<StationChangeComponent>;
    const pragueVaclav: IStation = { latest: { cityCode: 'PRG', airportCode: 'VAH' }, terminal: '1' } as any;
    const pragueLetnany: IStation = { latest: { cityCode: 'PRG', airportCode: 'LTN' }, terminal: '1' } as any;
    const ryazan: IStation = { latest: { cityCode: 'RYZ', airportCode: 'RYZ' }, terminal: 'A' } as any;

    beforeEach(async () => {
        await TestBed.configureTestingModule({
            declarations: [StationChangeComponent],
        }).compileComponents();
    });

    beforeEach(() => {
        fixture = TestBed.createComponent(StationChangeComponent);
        component = fixture.componentInstance;
        fixture.detectChanges();
    });

    it('should create', () => {
        expect(component).toBeTruthy();
    });

    it('should detect no change', () => {
        component.from = component.to = ryazan;
        component.ngOnChanges({
            from: new SimpleChange(null, component.from, true),
            to: new SimpleChange(null, component.to, true),
        });

        expect(component.changeType).toBe('noChange');
    });

    it('should detect city change', () => {
        component.from = pragueVaclav;
        component.to = ryazan;
        component.ngOnChanges({
            from: new SimpleChange(null, component.from, true),
            to: new SimpleChange(null, component.to, true),
        });

        expect(component.changeType).toBe('city');
    });

    it('should detect airport change', () => {
        component.from = pragueVaclav;
        component.to = pragueLetnany;

        component.ngOnChanges({
            from: new SimpleChange(null, component.from, true),
            to: new SimpleChange(null, component.to, true),
        });

        expect(component.changeType).toBe('airport');
    });

    it('should detect terminal change if both terminals are defined', () => {
        const moscowSvoB: IStation = { latest: { cityCode: 'MOW', airportCode: 'SVO' }, terminal: 'B' } as any;
        const moscowSvoD: IStation = { latest: { cityCode: 'MOW', airportCode: 'SVO' }, terminal: 'D' } as any;

        component.from = moscowSvoB;
        component.to = moscowSvoD;

        component.ngOnChanges({
            from: new SimpleChange(null, component.from, true),
            to: new SimpleChange(null, component.to, true),
        });

        expect(component.changeType).toBe('terminal');
    });

    it('should not detect terminal change if one of terminals is not defined', () => {
        const moscowSvoB: IStation = { latest: { cityCode: 'MOW', airportCode: 'SVO' } } as any;
        const moscowSvoD: IStation = { latest: { cityCode: 'MOW', airportCode: 'SVO' }, terminal: 'D' } as any;

        component.from = moscowSvoB;
        component.to = moscowSvoD;

        component.ngOnChanges({
            from: new SimpleChange(null, component.from, true),
            to: new SimpleChange(null, component.to, true),
        });

        expect(component.changeType).toBe('noChange');
    });
});
