import { TestBed } from '@angular/core/testing';

import { CacheService } from './cache.service';

describe('CacheService', () => {
    let service: CacheService;

    beforeEach(() => {
        TestBed.configureTestingModule({});
        service = TestBed.inject(CacheService);
    });

    it('should return appropriate result', () => {
        const key = '1';
        const options = 'option=1';
        const anotherOptions = 'option=2';
        const result = { data: true };

        service.set(key, options, result);

        expect(service.get(key, options)).toBe(result);
        expect(service.get(key, anotherOptions)).toBe(undefined);

        service.set(key, anotherOptions, result);
        expect(service.get(key, options)).toBe(undefined);
    });

    it('should invalidate cache after timeout has expired', () => {
        jasmine.clock().install();

        (service as any).INVALID_TIMEOUT = 10;
        service.set('1', '1', '1');
        expect(service.get('1', '1')).toBe('1');
        jasmine.clock().tick(10);
        expect(service.get('1', '1')).toBe(undefined);

        jasmine.clock().uninstall();
    });

    it('should delete entry', () => {
        service.set('1', '1', '1');
        expect(service.get('1', '1')).toBe('1');

        service.delete('1');
        expect(service.get('1', '1')).toBe(undefined);
    });
});
