import { TestBed } from '@angular/core/testing';
import { OnlineBoardFlightNumberFilterValidationService } from '@online-board/components/filter/components/flight-number-filter/services/online-board-flight-number-filter-validation.service';
import { IParsedFlightId } from '@typings/flight/flight-id';

describe('OnlineBoardFlightNumberFilterValidationService', () => {
    let service: OnlineBoardFlightNumberFilterValidationService;

    beforeEach(() => {
        TestBed.configureTestingModule({
            providers: [OnlineBoardFlightNumberFilterValidationService],
        });

        service = TestBed.inject(
            OnlineBoardFlightNumberFilterValidationService,
        );
    });

    it('should return false and set flightNumberError because flight number is not provided', () => {
        const params: Partial<IParsedFlightId> = {};

        expect(service.validate(params)).toBe(false);
        expect(service.flightNumberError).toBe(
            'BOARD.FLIGHT_NUMBER-ERROR-EMPTY',
        );
        expect(service.dateError).toBe(null);
    });

    it('should return false because flight number does not meet pattern', () => {
        const params: Partial<IParsedFlightId> = {
            flightNumber: 'invalid',
        };

        expect(service.validate(params)).toBe(false);
        expect(service.flightNumberError).toBe(
            'BOARD.FLIGHT_NUMBER-ERROR-ONLY-NUMBER',
        );
        expect(service.dateError).toBe(null);
    });

    it('should return false because flight number`s length is more than 4', () => {
        const params: Partial<IParsedFlightId> = {
            flightNumber: '11111',
        };

        expect(service.validate(params)).toBe(false);
        expect(service.flightNumberError).toBe(
            'BOARD.FLIGHT_NUMBER-ERROR-ONLY-NUMBER',
        );
        expect(service.dateError).toBe(null);
    });

    it('should return false because date is not provided', () => {
        const params: Partial<IParsedFlightId> = {
            flightNumber: '0001',
        };

        expect(service.validate(params)).toBe(false);
        expect(service.flightNumberError).toBe(null);
        expect(service.dateError).toBe('SHARED.DATE_FORMAT-WRONG');
    });

    it('should return false because date is invalid', () => {
        const params: Partial<IParsedFlightId> = {
            flightNumber: '0001',
            date: new Date('invalid'),
        };

        expect(service.validate(params)).toBe(false);
        expect(service.flightNumberError).toBe(null);
        expect(service.dateError).toBe('SHARED.DATE_FORMAT-WRONG');
    });

    it('should return true because all params are valid', () => {
        const params: Partial<IParsedFlightId> = {
            flightNumber: '0001',
            date: new Date(2022, 5, 6),
        };

        expect(service.validate(params)).toBe(true);
        expect(service.flightNumberError).toBe(null);
        expect(service.dateError).toBe(null);
    });

    it('should clear errors before validation', () => {
        const params: Partial<IParsedFlightId> = {
            flightNumber: '0001',
            date: new Date('invalid'),
        };

        expect(service.validate(params)).toBe(false);
        expect(service.flightNumberError).toBe(null);
        expect(service.dateError).toBe('SHARED.DATE_FORMAT-WRONG');

        params.date = new Date(2022, 5, 6);
        expect(service.validate(params)).toBe(true);
        expect(service.flightNumberError).toBe(null);
        expect(service.dateError).toBe(null);
    });
});
