import { NO_ERRORS_SCHEMA } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { TranslateService } from '@ngx-translate/core';
import { LocalizationService } from '@shared/services';
import { getMockPipe } from '@shared/pipes/pipe.mock';
import { CalendarInputComponent } from './calendar-input.component';
import { of } from 'rxjs';

describe('CalendarInputComponent', () => {
    let component: CalendarInputComponent;
    let fixture: ComponentFixture<CalendarInputComponent>;
    const date = new Date(2021, 11, 11);

    const translate: TranslateService = {
        get: jasmine
            .createSpy()
            .withArgs('SHARED.DATE_FORMAT')
            .and.returnValue(of('DD.MM.YYYY'))
            .and.callFake((a) => {
                return of(a);
            }),
    } as any;
    const localization: LocalizationService = {} as any;
    beforeEach(async () => {
        await TestBed.configureTestingModule({
            declarations: [CalendarInputComponent, getMockPipe('translate')],
            providers: [
                { provide: TranslateService, useValue: translate },
                { provide: LocalizationService, useValue: localization },
            ],
            schemas: [NO_ERRORS_SCHEMA],
        }).compileComponents();

        fixture = TestBed.createComponent(CalendarInputComponent);
        component = fixture.componentInstance;
        component.calendar = {
            hideOverlay: jasmine.createSpy().and.callThrough(),
        } as any;
        fixture.detectChanges();
    });

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

    it('should write value', () => {
        component.writeValue(date);
        expect(component.value).toEqual(date);
    });
    it('should not output invalid value but output valid value when manual input', (done) => {
        component.registerOnChange((value) => {
            expect(value).toEqual(date);
            done();
        });

        component.dateStr = '11';
        component.inputKeyup();

        component.dateStr = '11.12';
        component.inputKeyup();

        component.dateStr = '11.12.2021';
        component.inputKeyup();
    });
});
