import { TestBed } from '@angular/core/testing';
import { DetailsScheduleDatesService } from '@schedule/pages/flight-details/services/details-schedule-dates.service';
import { ScheduleDateValidationService } from '@schedule/services/date-validation.service';
import { AppSettingsService } from '@shared/services';
import { areSame } from '@utils/date';

describe('DetailsScheduleDatesService', () => {
    let service: DetailsScheduleDatesService;
    const scheduleMaxDate = new Date(2022, 5, 30);
    const scheduleMinDate = new Date(2022, 5, 1);

    beforeEach(() => {
        TestBed.configureTestingModule({
            providers: [
                DetailsScheduleDatesService,
                ScheduleDateValidationService,
                {
                    provide: AppSettingsService,
                    useValue: {
                        getSettings: () => {
                            return Promise.resolve({
                                scheduleMaxDate,
                                scheduleMinDate,
                            });
                        },
                    },
                },
            ],
        });

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

    it('should return normal mini list dates range', async () => {
        const datesRange = await service.getMiniListDatesRange(
            new Date(2022, 5, 20),
        );

        expect(areSame(datesRange.dateFrom, new Date(2022, 5, 19))).toBe(true);
        expect(areSame(datesRange.dateTo, new Date(2022, 5, 21))).toBe(true);
    });

    it('should return mini list dates range which dateFrom equals to scheduleMinDate', async () => {
        const datesRange = await service.getMiniListDatesRange(scheduleMinDate);

        expect(areSame(datesRange.dateFrom, scheduleMinDate)).toBe(true);
        expect(areSame(datesRange.dateTo, new Date(2022, 5, 2))).toBe(true);
    });

    it('should return mini list dates range which dateTp equals to scheduleMaxDate', async () => {
        const datesRange = await service.getMiniListDatesRange(scheduleMaxDate);

        expect(areSame(datesRange.dateFrom, new Date(2022, 5, 29))).toBe(true);
        expect(areSame(datesRange.dateTo, scheduleMaxDate)).toBe(true);
    });

    it('should return week dates range which is between schedule date borders', async () => {
        const datesRange = await service.getScheduleDatesRange(
            new Date(2022, 5, 20),
        );

        expect(areSame(datesRange.dateFrom, new Date(2022, 5, 20))).toBe(true);
        expect(areSame(datesRange.dateTo, new Date(2022, 5, 26))).toBe(true);
    });

    it('should return week dates range which dateFrom is scheduleMinDate', async () => {
        const datesRange = await service.getScheduleDatesRange(
            new Date(2022, 5, 3),
        );

        expect(areSame(datesRange.dateFrom, scheduleMinDate)).toBe(true);
        expect(areSame(datesRange.dateTo, new Date(2022, 5, 5))).toBe(true);
    });

    it('should return week dates range which dateTo is scheduleMaxDate', async () => {
        const datesRange = await service.getScheduleDatesRange(
            new Date(2022, 5, 29),
        );

        expect(areSame(datesRange.dateFrom, new Date(2022, 5, 27))).toBe(true);
        expect(areSame(datesRange.dateTo, scheduleMaxDate)).toBe(true);
    });
});
