import { TestBed } from '@angular/core/testing';
import { TranslatePipe, TranslateService } from '@ngx-translate/core';
import { DateTransformerService } from '@shared/services/date/date-transformer.service';
import * as moment from 'moment';
import { DatePipe, LocalDatePipe, TextDatePipe } from './date.pipe';

describe('DatePipe', () => {
    let pipe: DatePipe;
    const date = new Date(2021, 10, 22, 17, 34);
    beforeEach(() => {
        TestBed.configureTestingModule({
            providers: [
                DatePipe,
                DateTransformerService,
                {
                    provide: TranslateService,
                    useValue: {},
                },
            ],
        });
        pipe = TestBed.inject(DatePipe);
    });
    it('should return empty on empty input', () => {
        expect(pipe.transform(null)).toBe('');
        expect(pipe.transform(undefined)).toBe('');
        expect(pipe.transform('' as any)).toBe('');
    });

    it('should format date', () => {
        expect(pipe.transform(date)).toEqual('22.11.2021');
    });

    it('should format date string', () => {
        expect(pipe.transform(date.toISOString())).toEqual('22.11.2021');
    });

    it('should format date with time', () => {
        expect(pipe.transform(date, 'with-time')).toEqual('17:34 22.11.2021');
    });
});

describe('LocalDatePipe', () => {
    let pipe: LocalDatePipe;
    const date = new Date(2021, 10, 22, 17, 34);
    beforeEach(() => {
        TestBed.configureTestingModule({
            providers: [
                LocalDatePipe,
                DateTransformerService,
                {
                    provide: TranslateService,
                    useValue: {},
                },
            ],
        });
        pipe = TestBed.inject(LocalDatePipe);
    });
    it('should return empty on empty input', () => {
        expect(pipe.transform(null)).toBe('');
        expect(pipe.transform(undefined)).toBe('');
        expect(pipe.transform('' as any)).toBe('');
    });

    it('should format date', () => {
        expect(pipe.transform(date)).toEqual('22.11.2021');
    });

    it('should format date string', () => {
        expect(pipe.transform('2021-12-18T19:25:00-05:00')).toEqual(
            '18.12.2021',
        );
    });
});

describe('TextDatePipe', () => {
    let translate: TranslatePipe;
    let pipe: TextDatePipe;

    beforeEach(() => {
        translate = {
            instant: jasmine
                .createSpy('transform')
                .withArgs('SHARED.TODAY')
                .and.returnValue('Today')
                .withArgs('SHARED.TOMORROW')
                .and.returnValue('Tomorrow'),
        } as any;

        TestBed.configureTestingModule({
            providers: [
                TextDatePipe,
                DateTransformerService,
                {
                    provide: TranslateService,
                    useValue: translate,
                },
            ],
        });

        pipe = TestBed.inject(TextDatePipe);
    });

    it('should return empty on empty input', () => {
        expect(pipe.transform(null)).toBe('');
        expect(pipe.transform(undefined)).toBe('');
        expect(pipe.transform('' as any)).toBe('');
    });

    it('should format today date', () => {
        const today = new Date();
        expect(pipe.transform(today)).toEqual('Today');
    });

    it('should format tomorrow date', () => {
        const tomorrow = moment().add(1, 'day').toDate();
        expect(pipe.transform(tomorrow)).toEqual('Tomorrow');
    });

    it('should format other dates as per format', () => {
        const date = new Date(2021, 10, 22, 17, 34);
        expect(pipe.transform(date)).toEqual('22.11.2021');
    });
});
