import { it, expect, describe, inject, beforeEach, beforeEachProviders } from '@angular/core/testing'; import { SearchResult, LocalStorageCache, CACHE, } from '../../dist/app/core'; import {provide} from '@angular/core'; const testKey = 'test key'; const mockSearchResponse: any = { columns: [ {'name': 'source', 'label': 'Source', 'type': 'text', 'visible': true}, {'name': 'state', 'label': 'State', 'type': 'text', 'visible': true} ], data: [ ['OCC', 'OK'] ] }; describe('Local Storage Cache Service', () => { const mockSearchResult = new SearchResult(mockSearchResponse); beforeEachProviders(() => { return [ LocalStorageCache, provide(CACHE, {useExisting: LocalStorageCache}) ]; }); beforeEach(() => { const cache = new LocalStorageCache(); cache.clear(); }); it('should cache a search query result and retrieve it', inject([LocalStorageCache], (cache: LocalStorageCache) => { const key = cache.set(testKey, mockSearchResult); const data = cache.get(key, SearchResult); expect(data).toEqual(mockSearchResult); }) ); it('should return null if cache key does not exist', inject([LocalStorageCache], (cache: LocalStorageCache) => { const data = cache.get(testKey, SearchResult); expect(data).toBe(null); }) ); it('should delete a search query from cache', inject([LocalStorageCache], (cache: LocalStorageCache) => { const key = cache.set(testKey, mockSearchResult); const confirmation = cache.delete(key); const data = cache.get(key, SearchResult); expect(confirmation).toBe(true); expect(data).toBeNull(); }) ); it('should return false if deleting non-existent key', inject([LocalStorageCache], (cache: LocalStorageCache) => { const confirmation = cache.delete(testKey); expect(confirmation).toBe(false); }) ); });