import { TestBed } from '@angular/core/testing';
import { TranslateService } from '@ngx-translate/core';
import { DateTransformerService } from '@shared/services/date/date-transformer.service';
import * as timezone_mock from 'timezone-mock';

describe('DateTransformerService', () => {
    let service: DateTransformerService;
    const instantMock = jasmine.createSpy('instant');
    const today = new Date(2022, 4, 28);
    const tomorrow = new Date(2022, 4, 29);

    beforeEach(() => {
        TestBed.configureTestingModule({
            providers: [
                DateTransformerService,
                {
                    provide: TranslateService,
                    useValue: {
                        instant: instantMock,
                    },
                },
            ],
        });

        service = TestBed.inject(DateTransformerService);
        instantMock.calls.reset();

        timezone_mock.register('Etc/GMT-4');
        jasmine.clock().install();
        jasmine.clock().mockDate(today);
    });

    afterEach(() => {
        jasmine.clock().uninstall();
        timezone_mock.unregister();
    });

    it('should return empty string if date was not provided', () => {
        expect(service.toString()).toBe('');
    });

    it("should call translate.instant with 'SHARED.TODAY'", () => {
        service.toString(today);
        expect(instantMock).toHaveBeenCalledWith('SHARED.TODAY');
    });

    it("should call translate.instant with 'SHARED.TOMORROW'", () => {
        service.toString(tomorrow);
        expect(instantMock).toHaveBeenCalledWith('SHARED.TOMORROW');
    });

    it('should return 30.05.2022', () => {
        expect(service.toString(new Date(2022, 4, 30))).toBe('30.05.2022');
    });

    it('should return 15.06.2022', () => {
        expect(service.toDateString('2022-06-15T14:35:00+03:00')).toBe(
            '15.06.2022',
        );
    });

    it("should return '14:35 15.06.2022'", () => {
        // should add one hour because of time zones difference
        expect(service.toDateString('2022-06-15T14:35:00+03:00', true)).toBe(
            '15:35 15.06.2022',
        );
    });

    it('should return 14.06.2022', () => {
        // should return 14th day despite that this date in +4 timezone is 2022-06-15T00:35:00
        expect(service.toLocalDateString('2022-06-14T23:35:00+03:00')).toEqual(
            '14.06.2022',
        );
    });
});
