import { ComponentFixture, TestBed } from '@angular/core/testing';
import { TimeSelectorComponent } from '@components';
import { IUrlTimeRange } from '@typings/common/url';

describe('PageTimeSelectorComponent', () => {
    let component: TimeSelectorComponent;
    let fixture: ComponentFixture<TimeSelectorComponent>;

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

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

        component.max = 1440;
        component.min = 0;

        fixture.detectChanges();
    });

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

    [
        {
            // normalizing "to"
            input: { timeFrom: '2340', timeTo: '2350' },
            expected: [1420, 1480],
        },
        {
            // no normalization
            input: { timeFrom: '0100', timeTo: '0300' },
            expected: [60, 180],
        },
        {
            // normalizing both
            input: { timeFrom: '0000', timeTo: '2400' },
            expected: [0, 1440],
        },

        {
            // normalizing "to"
            input: { timeFrom: '0010', timeTo: '0020' },
            expected: [10, 70],
        },
    ].forEach(({ input, expected }) => {
        it(`should write input value ${JSON.stringify(
            input,
        )} to be displayed as ${JSON.stringify(expected)}`, () => {
            component.change = 'to';
            component.writeValue(input);
            expect(component.value).toEqual(expected);
        });
    });

    [
        {
            expected: { timeFrom: '0100', timeTo: '0300' },
            internal: [60, 180],
        },
        {
            expected: {},
            internal: [-10, 21400],
        },
    ].forEach(({ internal, expected }) => {
        it(`should output value ${JSON.stringify(
            internal,
        )} to be outputed as ${JSON.stringify(expected)}`, (done) => {
            component.registerOnChange((value: IUrlTimeRange) => {
                expect(value).toEqual(expected);
                done();
            });
            component.change = 'to';
            component.onModelChange(internal as [number, number]);
        });
    });
});
