import { TestBed } from '@angular/core/testing';
import { CanActivateFlightNumberSearch } from '@online-board/guards/can-activate-flight-number-search';
import { CanActivateDateParams } from '@online-board/guards/utils/can-activate-date-params';
import { OnlineBoardDateValidationService } from '@online-board/services/date-validation.service';
import { OnlineBoardNavigationService } from '@online-board/services/navigation.service';
import { OnlineBoardUrlParserService } from '@online-board/services/url/url-parser.service';
import { AppSettingsService } from '@shared/services';
import { TimeRangeValidationService } from '@shared/services/validators/time-range.service';

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

    const toStartPageMock = jasmine.createSpy('toStartPage');
    const navigationService: Partial<OnlineBoardNavigationService> = {
        toStartPage: toStartPageMock,
    };

    const boardMinDate = new Date(2022, 4, 23);
    const boardMaxDate = new Date(2022, 4, 29);
    const appSettingsService: Partial<AppSettingsService> = {
        getSettings: () => {
            return Promise.resolve({
                boardMinDate,
                boardMaxDate,
            }) as any;
        },
    };

    beforeEach(() => {
        TestBed.configureTestingModule({
            providers: [
                CanActivateDateParams,
                CanActivateFlightNumberSearch,
                TimeRangeValidationService,
                OnlineBoardDateValidationService,
                OnlineBoardUrlParserService,
                {
                    provide: OnlineBoardNavigationService,
                    useValue: navigationService,
                },
                {
                    provide: AppSettingsService,
                    useValue: appSettingsService,
                },
            ],
        });

        service = TestBed.inject(CanActivateFlightNumberSearch);
    });

    it('should return true for valid flight number search page params', async () => {
        const params = 'SU0001-20220525';
        const result = await service.canActivate({
            params: {
                params,
            },
        } as any);

        expect(result).toBe(true);
    });

    it('should redirect to to start page because date is invalid', async () => {
        const params = 'SU0001-20220521';
        const result = await service.canActivate({
            params: {
                params,
            },
        } as any);

        expect(result).not.toBe(true);
        expect(toStartPageMock).toHaveBeenCalled();
    });
});
