diff --git a/packages/core/package.json b/packages/core/package.json index 696a2e7..a1c00be 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -61,6 +61,10 @@ "require": "./lib/browser-storage/config.cjs", "import": "./lib/browser-storage/config.mjs" }, + "./lib/browser-storage/data": { + "require": "./lib/browser-storage/data.cjs", + "import": "./lib/browser-storage/data.mjs" + }, "./lib/browser-storage/functions": { "require": "./lib/browser-storage/functions.cjs", "import": "./lib/browser-storage/functions.mjs" @@ -77,6 +81,10 @@ "require": "./lib/browser-storage/mock.cjs", "import": "./lib/browser-storage/mock.mjs" }, + "./lib/browser-storage/types": { + "require": "./lib/browser-storage/types.cjs", + "import": "./lib/browser-storage/types.mjs" + }, "./lib/builder/functions": { "require": "./lib/builder/functions.cjs", "import": "./lib/builder/functions.mjs" diff --git a/packages/core/src/browser-storage/config.ts b/packages/core/src/browser-storage/config.ts index de42d24..337840a 100644 --- a/packages/core/src/browser-storage/config.ts +++ b/packages/core/src/browser-storage/config.ts @@ -5,3 +5,7 @@ export const browserCacheVersion = 'iconify2'; export const browserCachePrefix = 'iconify'; export const browserCacheCountKey = browserCachePrefix + '-count'; export const browserCacheVersionKey = browserCachePrefix + '-version'; + +// Cache expiration +export const browserStorageHour = 3600000; +export const browserStorageCacheExpiration = 168; // In hours diff --git a/packages/core/src/browser-storage/data.ts b/packages/core/src/browser-storage/data.ts new file mode 100644 index 0000000..618ac65 --- /dev/null +++ b/packages/core/src/browser-storage/data.ts @@ -0,0 +1,29 @@ +import type { + BrowserStorageConfig, + BrowserStorageCount, + BrowserStorageEmptyList, +} from './types'; + +/** + * Storage configuration + */ +export const browserStorageConfig: BrowserStorageConfig = { + local: true, + session: true, +}; + +/** + * Items counter + */ +export const browserStorageItemsCount: BrowserStorageCount = { + local: 0, + session: 0, +}; + +/** + * List of empty items + */ +export const browserStorageEmptyItems: BrowserStorageEmptyList = { + local: [], + session: [], +}; diff --git a/packages/core/src/browser-storage/functions.ts b/packages/core/src/browser-storage/functions.ts index 3b8921d..5eaa65d 100644 --- a/packages/core/src/browser-storage/functions.ts +++ b/packages/core/src/browser-storage/functions.ts @@ -1,4 +1,4 @@ -import { config } from './index'; +import { browserStorageConfig } from './data'; /** * Cache types @@ -15,12 +15,13 @@ export function toggleBrowserCache( switch (storage) { case 'local': case 'session': - config[storage] = value; + browserStorageConfig[storage] = value; break; case 'all': - for (const key in config) { - config[key as keyof typeof config] = value; + for (const key in browserStorageConfig) { + browserStorageConfig[key as keyof typeof browserStorageConfig] = + value; } break; } diff --git a/packages/core/src/browser-storage/index.ts b/packages/core/src/browser-storage/index.ts index 9b6a001..5b8aed6 100644 --- a/packages/core/src/browser-storage/index.ts +++ b/packages/core/src/browser-storage/index.ts @@ -6,58 +6,21 @@ import { browserCachePrefix, browserCacheVersion, browserCacheVersionKey, + browserStorageCacheExpiration, + browserStorageHour, } from './config'; - -interface StorageType { - local: T; - session: T; -} - -type StorageConfig = StorageType; -type StorageCount = StorageType; -type StorageEmptyList = StorageType; - -export interface StoredItem { - cached: number; - provider: string; - data: IconifyJSON; -} - -/** - * Cache expiration - */ -const hour = 3600000; -const cacheExpiration = 168; // In hours - -/** - * Storage configuration - */ -export const config: StorageConfig = { - local: true, - session: true, -}; +import { + browserStorageConfig, + browserStorageEmptyItems, + browserStorageItemsCount, +} from './data'; +import type { BrowserStorageConfig, BrowserStorageItem } from './types'; /** * Flag to check if storage has been loaded */ let loaded = false; -/** - * Items counter - */ -export const count: StorageCount = { - local: 0, - session: 0, -}; - -/** - * List of empty items - */ -export const emptyList: StorageEmptyList = { - local: [], - session: [], -}; - /** * Fake window for unit testing */ @@ -75,7 +38,9 @@ export function mock(fakeWindow: FakeWindow): void { * * @param key */ -function getGlobal(key: keyof StorageConfig): typeof localStorage | null { +function getGlobal( + key: keyof BrowserStorageConfig +): typeof localStorage | null { const attr = key + 'Storage'; try { if ( @@ -90,7 +55,7 @@ function getGlobal(key: keyof StorageConfig): typeof localStorage | null { } // Failed - mark as disabled - config[key] = false; + browserStorageConfig[key] = false; return null; } @@ -99,12 +64,12 @@ function getGlobal(key: keyof StorageConfig): typeof localStorage | null { */ function setCount( storage: typeof localStorage, - key: keyof StorageConfig, + key: keyof BrowserStorageConfig, value: number ): boolean { try { storage.setItem(browserCacheCountKey, value.toString()); - count[key] = value; + browserStorageItemsCount[key] = value; return true; } catch (err) { // @@ -134,7 +99,7 @@ function getCount(storage: typeof localStorage): number { */ function initCache( storage: typeof localStorage, - key: keyof StorageConfig + key: keyof BrowserStorageConfig ): void { try { storage.setItem(browserCacheVersionKey, browserCacheVersion); @@ -170,10 +135,12 @@ export const loadCache: LoadIconsCache = (): void => { loaded = true; // Minimum time - const minTime = Math.floor(Date.now() / hour) - cacheExpiration; + const minTime = + Math.floor(Date.now() / browserStorageHour) - + browserStorageCacheExpiration; // Load data from storage - function load(key: keyof StorageConfig): void { + function load(key: keyof BrowserStorageConfig): void { const func = getGlobal(key); if (!func) { return; @@ -193,7 +160,7 @@ export const loadCache: LoadIconsCache = (): void => { let valid = true; try { // Parse, check time stamp - const data = JSON.parse(item) as StoredItem; + const data = JSON.parse(item) as BrowserStorageItem; if ( typeof data !== 'object' || typeof data.cached !== 'number' || @@ -243,7 +210,7 @@ export const loadCache: LoadIconsCache = (): void => { total--; } else { // Mark as empty - emptyList[key].push(i); + browserStorageEmptyItems[key].push(i); } } } @@ -255,8 +222,8 @@ export const loadCache: LoadIconsCache = (): void => { } } - for (const key in config) { - load(key as keyof StorageConfig); + for (const key in browserStorageConfig) { + load(key as keyof BrowserStorageConfig); } }; @@ -271,8 +238,8 @@ export const storeCache: CacheIcons = ( loadCache(); } - function store(key: keyof StorageConfig): boolean { - if (!config[key]) { + function store(key: keyof BrowserStorageConfig): boolean { + if (!browserStorageConfig[key]) { return false; } @@ -282,10 +249,10 @@ export const storeCache: CacheIcons = ( } // Get item index - let index = emptyList[key].shift(); + let index = browserStorageEmptyItems[key].shift(); if (index === void 0) { // Create new index - index = count[key]; + index = browserStorageItemsCount[key]; if (!setCount(func, key, index + 1)) { return false; } @@ -293,8 +260,8 @@ export const storeCache: CacheIcons = ( // Create and save item try { - const item: StoredItem = { - cached: Math.floor(Date.now() / hour), + const item: BrowserStorageItem = { + cached: Math.floor(Date.now() / browserStorageHour), provider, data, }; diff --git a/packages/core/src/browser-storage/mock.ts b/packages/core/src/browser-storage/mock.ts index 5c550e2..6ef0014 100644 --- a/packages/core/src/browser-storage/mock.ts +++ b/packages/core/src/browser-storage/mock.ts @@ -1,4 +1,9 @@ -import { mock, count, config, emptyList } from './index'; +import { mock } from './index'; +import { + browserStorageItemsCount, + browserStorageConfig, + browserStorageEmptyItems, +} from './data'; /** * Get next icon set prefix for testing @@ -8,12 +13,6 @@ export function nextPrefix(): string { return 'fake-storage-' + (prefixCounter++).toString(); } -/** - * Cache expiration - */ -export const hour = 3600000; -export const cacheExpiration = 168; // In hours - /** * Storage class */ @@ -97,10 +96,10 @@ export function reset(fakeWindow: Record): void { mock(fakeWindow); // Reset all data - for (const key in config) { - const attr = key as unknown as keyof typeof config; - config[attr] = true; - count[attr] = 0; - emptyList[attr] = []; + for (const key in browserStorageConfig) { + const attr = key as unknown as keyof typeof browserStorageConfig; + browserStorageConfig[attr] = true; + browserStorageItemsCount[attr] = 0; + browserStorageEmptyItems[attr] = []; } } diff --git a/packages/core/src/browser-storage/types.ts b/packages/core/src/browser-storage/types.ts new file mode 100644 index 0000000..f56182a --- /dev/null +++ b/packages/core/src/browser-storage/types.ts @@ -0,0 +1,23 @@ +import type { IconifyJSON } from '@iconify/types'; + +// Mixin for config for various types +export interface BrowserStorageType { + local: T; + session: T; +} + +// Config +export type BrowserStorageConfig = BrowserStorageType; + +// Number of items +export type BrowserStorageCount = BrowserStorageType; + +// List of empty items, for use later +export type BrowserStorageEmptyList = BrowserStorageType; + +// Stored item +export interface BrowserStorageItem { + cached: number; + provider: string; + data: IconifyJSON; +} diff --git a/packages/core/tests/cache/basic-test.ts b/packages/core/tests/cache/basic-test.ts index ab757f5..dc41748 100644 --- a/packages/core/tests/cache/basic-test.ts +++ b/packages/core/tests/cache/basic-test.ts @@ -1,4 +1,8 @@ -import { count, config, loadCache } from '../../lib/browser-storage'; +import { loadCache } from '../../lib/browser-storage'; +import { + browserStorageItemsCount, + browserStorageConfig, +} from '../../lib/browser-storage/data'; import { browserCacheCountKey, browserCachePrefix, @@ -14,11 +18,11 @@ describe('Testing mocked localStorage', () => { reset({}); // Config before tests - expect(config).toEqual({ + expect(browserStorageConfig).toEqual({ local: true, session: true, }); - expect(count).toEqual({ + expect(browserStorageItemsCount).toEqual({ local: 0, session: 0, }); @@ -27,13 +31,13 @@ describe('Testing mocked localStorage', () => { loadCache(); // Everything should be disabled - expect(config).toEqual({ + expect(browserStorageConfig).toEqual({ local: false, session: false, }); // Nothing should have loaded - expect(count).toEqual({ + expect(browserStorageItemsCount).toEqual({ local: 0, session: 0, }); @@ -45,11 +49,11 @@ describe('Testing mocked localStorage', () => { }); // Config before tests - expect(config).toEqual({ + expect(browserStorageConfig).toEqual({ local: true, session: true, }); - expect(count).toEqual({ + expect(browserStorageItemsCount).toEqual({ local: 0, session: 0, }); @@ -58,13 +62,13 @@ describe('Testing mocked localStorage', () => { loadCache(); // sessionStorage should be disabled - expect(config).toEqual({ + expect(browserStorageConfig).toEqual({ local: true, session: false, }); // Nothing should have loaded - expect(count).toEqual({ + expect(browserStorageItemsCount).toEqual({ local: 0, session: 0, }); @@ -104,11 +108,11 @@ describe('Testing mocked localStorage', () => { }); // Config before tests - expect(config).toEqual({ + expect(browserStorageConfig).toEqual({ local: true, session: true, }); - expect(count).toEqual({ + expect(browserStorageItemsCount).toEqual({ local: 0, session: 0, }); @@ -117,13 +121,13 @@ describe('Testing mocked localStorage', () => { loadCache(); // Everything should be disabled because read-only mock throws errors - expect(config).toEqual({ + expect(browserStorageConfig).toEqual({ local: false, session: false, }); // Nothing should have loaded - expect(count).toEqual({ + expect(browserStorageItemsCount).toEqual({ local: 0, session: 0, }); @@ -158,11 +162,11 @@ describe('Testing mocked localStorage', () => { }); // Config before tests - expect(config).toEqual({ + expect(browserStorageConfig).toEqual({ local: true, session: true, }); - expect(count).toEqual({ + expect(browserStorageItemsCount).toEqual({ local: 0, session: 0, }); @@ -171,13 +175,13 @@ describe('Testing mocked localStorage', () => { loadCache(); // sessionStorage should be disabled - expect(config).toEqual({ + expect(browserStorageConfig).toEqual({ local: true, session: false, }); // One item should be in localStorage - expect(count).toEqual({ + expect(browserStorageItemsCount).toEqual({ local: 1, session: 0, }); @@ -190,11 +194,11 @@ describe('Testing mocked localStorage', () => { }); // Config before tests - expect(config).toEqual({ + expect(browserStorageConfig).toEqual({ local: true, session: true, }); - expect(count).toEqual({ + expect(browserStorageItemsCount).toEqual({ local: 0, session: 0, }); @@ -203,13 +207,13 @@ describe('Testing mocked localStorage', () => { loadCache(); // Everything should be working - expect(config).toEqual({ + expect(browserStorageConfig).toEqual({ local: true, session: true, }); // Empty storage - expect(count).toEqual({ + expect(browserStorageItemsCount).toEqual({ local: 0, session: 0, }); diff --git a/packages/core/tests/cache/loading-test.ts b/packages/core/tests/cache/loading-test.ts index 6952d59..949ee90 100644 --- a/packages/core/tests/cache/loading-test.ts +++ b/packages/core/tests/cache/loading-test.ts @@ -1,19 +1,20 @@ import type { IconifyJSON } from '@iconify/types'; -import type { StoredItem } from '../../lib/browser-storage'; -import { loadCache, count, config, emptyList } from '../../lib/browser-storage'; -import { getStorage, iconExists } from '../../lib/storage/storage'; +import type { BrowserStorageItem } from '../../lib/browser-storage/types'; +import { loadCache } from '../../lib/browser-storage'; import { - nextPrefix, - createCache, - reset, - hour, - cacheExpiration, -} from '../../lib/browser-storage/mock'; + browserStorageItemsCount, + browserStorageConfig, + browserStorageEmptyItems, +} from '../../lib/browser-storage/data'; +import { getStorage, iconExists } from '../../lib/storage/storage'; +import { nextPrefix, createCache, reset } from '../../lib/browser-storage/mock'; import { browserCacheCountKey, browserCachePrefix, browserCacheVersion, browserCacheVersionKey, + browserStorageHour, + browserStorageCacheExpiration, } from '../../lib/browser-storage/config'; describe('Testing loading from localStorage', () => { @@ -27,8 +28,8 @@ describe('Testing loading from localStorage', () => { cache.setItem(browserCacheVersionKey, browserCacheVersion); cache.setItem(browserCacheCountKey, '1'); - const item: StoredItem = { - cached: Math.floor(Date.now() / hour), + const item: BrowserStorageItem = { + cached: Math.floor(Date.now() / browserStorageHour), provider, data: { prefix: prefix, @@ -57,15 +58,15 @@ describe('Testing loading from localStorage', () => { expect(iconExists(icons, 'foo')).toBe(true); // Check data - expect(config).toEqual({ + expect(browserStorageConfig).toEqual({ local: true, session: false, }); - expect(count).toEqual({ + expect(browserStorageItemsCount).toEqual({ local: 1, session: 0, }); - expect(emptyList).toEqual({ + expect(browserStorageEmptyItems).toEqual({ local: [], session: [], }); @@ -80,8 +81,8 @@ describe('Testing loading from localStorage', () => { cache.setItem(browserCacheVersionKey, browserCacheVersion); cache.setItem(browserCacheCountKey, '1'); - const item: StoredItem = { - cached: Math.floor(Date.now() / hour), + const item: BrowserStorageItem = { + cached: Math.floor(Date.now() / browserStorageHour), provider, data: { prefix: prefix, @@ -115,15 +116,15 @@ describe('Testing loading from localStorage', () => { expect(iconExists(icons2, 'foo')).toBe(false); // Check data - expect(config).toEqual({ + expect(browserStorageConfig).toEqual({ local: true, session: false, }); - expect(count).toEqual({ + expect(browserStorageItemsCount).toEqual({ local: 1, session: 0, }); - expect(emptyList).toEqual({ + expect(browserStorageEmptyItems).toEqual({ local: [], session: [], }); @@ -137,9 +138,12 @@ describe('Testing loading from localStorage', () => { cache.setItem(browserCacheVersionKey, browserCacheVersion); cache.setItem(browserCacheCountKey, '1'); - const item: StoredItem = { + const item: BrowserStorageItem = { // Expiration date - cached: Math.floor(Date.now() / hour) - cacheExpiration - 1, + cached: + Math.floor(Date.now() / browserStorageHour) - + browserStorageCacheExpiration - + 1, provider, data: { prefix: prefix, @@ -168,15 +172,15 @@ describe('Testing loading from localStorage', () => { expect(iconExists(icons, 'foo')).toBe(false); // Check data - expect(config).toEqual({ + expect(browserStorageConfig).toEqual({ local: true, session: false, }); - expect(count).toEqual({ + expect(browserStorageItemsCount).toEqual({ local: 0, session: 0, }); - expect(emptyList).toEqual({ + expect(browserStorageEmptyItems).toEqual({ local: [], session: [], }); @@ -192,7 +196,7 @@ describe('Testing loading from localStorage', () => { cache.setItem( browserCachePrefix + '0', JSON.stringify({ - cached: Math.floor(Date.now() / hour), + cached: Math.floor(Date.now() / browserStorageHour), provider, data: { prefix: prefix, @@ -222,15 +226,15 @@ describe('Testing loading from localStorage', () => { expect(iconExists(icons, 'foo')).toBe(false); // Check data - expect(config).toEqual({ + expect(browserStorageConfig).toEqual({ local: true, session: false, }); - expect(count).toEqual({ + expect(browserStorageItemsCount).toEqual({ local: 0, session: 0, }); - expect(emptyList).toEqual({ + expect(browserStorageEmptyItems).toEqual({ local: [], session: [], }); @@ -244,8 +248,8 @@ describe('Testing loading from localStorage', () => { cache.setItem(browserCacheVersionKey, browserCacheVersion); cache.setItem(browserCacheCountKey, '0'); // Should be at least "1" - const item: StoredItem = { - cached: Math.floor(Date.now() / hour), + const item: BrowserStorageItem = { + cached: Math.floor(Date.now() / browserStorageHour), provider, data: { prefix: prefix, @@ -274,15 +278,15 @@ describe('Testing loading from localStorage', () => { expect(iconExists(icons, 'foo')).toBe(false); // Check data - expect(config).toEqual({ + expect(browserStorageConfig).toEqual({ local: true, session: false, }); - expect(count).toEqual({ + expect(browserStorageItemsCount).toEqual({ local: 0, session: 0, }); - expect(emptyList).toEqual({ + expect(browserStorageEmptyItems).toEqual({ local: [], session: [], }); @@ -296,8 +300,8 @@ describe('Testing loading from localStorage', () => { cache.setItem(browserCacheVersionKey, browserCacheVersion); cache.setItem(browserCacheCountKey, '5'); - const item: StoredItem = { - cached: Math.floor(Date.now() / hour), + const item: BrowserStorageItem = { + cached: Math.floor(Date.now() / browserStorageHour), provider, data: { prefix: prefix, @@ -326,15 +330,15 @@ describe('Testing loading from localStorage', () => { expect(iconExists(icons, 'foo')).toBe(true); // Check data - expect(config).toEqual({ + expect(browserStorageConfig).toEqual({ local: true, session: false, }); - expect(count).toEqual({ + expect(browserStorageItemsCount).toEqual({ local: 1, session: 0, }); - expect(emptyList).toEqual({ + expect(browserStorageEmptyItems).toEqual({ local: [], session: [], }); @@ -349,8 +353,8 @@ describe('Testing loading from localStorage', () => { cache.setItem(browserCacheCountKey, '5'); // Missing: 0, 2, 3 - const item1: StoredItem = { - cached: Math.floor(Date.now() / hour), + const item1: BrowserStorageItem = { + cached: Math.floor(Date.now() / browserStorageHour), provider, data: { prefix: prefix, @@ -361,8 +365,8 @@ describe('Testing loading from localStorage', () => { }, }, }; - const item4: StoredItem = { - cached: Math.floor(Date.now() / hour), + const item4: BrowserStorageItem = { + cached: Math.floor(Date.now() / browserStorageHour), provider, data: { prefix: prefix, @@ -395,15 +399,15 @@ describe('Testing loading from localStorage', () => { expect(iconExists(icons, 'foo4')).toBe(true); // Check data - expect(config).toEqual({ + expect(browserStorageConfig).toEqual({ local: true, session: false, }); - expect(count).toEqual({ + expect(browserStorageItemsCount).toEqual({ local: 5, session: 0, }); - expect(emptyList).toEqual({ + expect(browserStorageEmptyItems).toEqual({ local: [3, 2, 0], // reserse order session: [], }); @@ -423,7 +427,7 @@ describe('Testing loading from localStorage', () => { // Create 5 items const icons: IconifyJSON[] = []; - const items: StoredItem[] = []; + const items: BrowserStorageItem[] = []; for (let i = 0; i < 6; i++) { const icon: IconifyJSON = { @@ -434,8 +438,8 @@ describe('Testing loading from localStorage', () => { }, }, }; - const item: StoredItem = { - cached: Math.floor(Date.now() / hour), + const item: BrowserStorageItem = { + cached: Math.floor(Date.now() / browserStorageHour), provider, data: icon, }; @@ -482,15 +486,15 @@ describe('Testing loading from localStorage', () => { } // Check data - expect(config).toEqual({ + expect(browserStorageConfig).toEqual({ local: true, session: true, }); - expect(count).toEqual({ + expect(browserStorageItemsCount).toEqual({ local: 6, session: 3, }); - expect(emptyList).toEqual({ + expect(browserStorageEmptyItems).toEqual({ local: [4, 2, 0], session: [1], }); diff --git a/packages/core/tests/cache/saving-test.ts b/packages/core/tests/cache/saving-test.ts index fcdef12..d30de3a 100644 --- a/packages/core/tests/cache/saving-test.ts +++ b/packages/core/tests/cache/saving-test.ts @@ -1,25 +1,20 @@ import type { IconifyJSON } from '@iconify/types'; -import type { StoredItem } from '../../lib/browser-storage'; +import type { BrowserStorageItem } from '../../lib/browser-storage/types'; +import { loadCache, storeCache } from '../../lib/browser-storage'; import { - loadCache, - storeCache, - count, - config, - emptyList, -} from '../../lib/browser-storage'; + browserStorageItemsCount, + browserStorageConfig, + browserStorageEmptyItems, +} from '../../lib/browser-storage/data'; import { getStorage, iconExists } from '../../lib/storage/storage'; -import { - nextPrefix, - createCache, - reset, - hour, - cacheExpiration, -} from '../../lib/browser-storage/mock'; +import { nextPrefix, createCache, reset } from '../../lib/browser-storage/mock'; import { browserCacheCountKey, browserCachePrefix, browserCacheVersion, browserCacheVersionKey, + browserStorageHour, + browserStorageCacheExpiration, } from '../../lib/browser-storage/config'; describe('Testing saving to localStorage', () => { @@ -38,8 +33,8 @@ describe('Testing saving to localStorage', () => { }, }, }; - const item: StoredItem = { - cached: Math.floor(Date.now() / hour), + const item: BrowserStorageItem = { + cached: Math.floor(Date.now() / browserStorageHour), provider, data: icon, }; @@ -61,15 +56,15 @@ describe('Testing saving to localStorage', () => { // Check data that should have been updated because storeCache() // should call load function before first execution - expect(config).toEqual({ + expect(browserStorageConfig).toEqual({ local: true, session: false, }); - expect(count).toEqual({ + expect(browserStorageItemsCount).toEqual({ local: 1, session: 0, }); - expect(emptyList).toEqual({ + expect(browserStorageEmptyItems).toEqual({ local: [], session: [], }); @@ -95,8 +90,8 @@ describe('Testing saving to localStorage', () => { }, }, }; - const item0: StoredItem = { - cached: Math.floor(Date.now() / hour), + const item0: BrowserStorageItem = { + cached: Math.floor(Date.now() / browserStorageHour), provider, data: icon0, }; @@ -108,8 +103,8 @@ describe('Testing saving to localStorage', () => { }, }, }; - const item1: StoredItem = { - cached: Math.floor(Date.now() / hour), + const item1: BrowserStorageItem = { + cached: Math.floor(Date.now() / browserStorageHour), provider, data: icon1, }; @@ -125,15 +120,15 @@ describe('Testing saving to localStorage', () => { // Check data that should have been updated because storeCache() // should call load function before first execution - expect(config).toEqual({ + expect(browserStorageConfig).toEqual({ local: true, session: false, }); - expect(count).toEqual({ + expect(browserStorageItemsCount).toEqual({ local: 2, session: 0, }); - expect(emptyList).toEqual({ + expect(browserStorageEmptyItems).toEqual({ local: [], session: [], }); @@ -162,8 +157,8 @@ describe('Testing saving to localStorage', () => { }, }, }; - const item0: StoredItem = { - cached: Math.floor(Date.now() / hour), + const item0: BrowserStorageItem = { + cached: Math.floor(Date.now() / browserStorageHour), provider, data: icon0, }; @@ -175,8 +170,8 @@ describe('Testing saving to localStorage', () => { }, }, }; - const item1: StoredItem = { - cached: Math.floor(Date.now() / hour), + const item1: BrowserStorageItem = { + cached: Math.floor(Date.now() / browserStorageHour), provider, data: icon1, }; @@ -195,15 +190,15 @@ describe('Testing saving to localStorage', () => { loadCache(); // Check data - expect(config).toEqual({ + expect(browserStorageConfig).toEqual({ local: true, session: false, }); - expect(count).toEqual({ + expect(browserStorageItemsCount).toEqual({ local: 2, session: 0, }); - expect(emptyList).toEqual({ + expect(browserStorageEmptyItems).toEqual({ local: [0], session: [], }); @@ -212,11 +207,11 @@ describe('Testing saving to localStorage', () => { storeCache(provider, icon0); // Check data - expect(count).toEqual({ + expect(browserStorageItemsCount).toEqual({ local: 2, session: 0, }); - expect(emptyList).toEqual({ + expect(browserStorageEmptyItems).toEqual({ local: [], session: [], }); @@ -238,7 +233,7 @@ describe('Testing saving to localStorage', () => { // Add icon sets const icons: IconifyJSON[] = []; - const items: StoredItem[] = []; + const items: BrowserStorageItem[] = []; for (let i = 0; i < 12; i++) { const icon: IconifyJSON = { prefix: prefix, @@ -248,20 +243,20 @@ describe('Testing saving to localStorage', () => { }, }, }; - const item: StoredItem = { - cached: Math.floor(Date.now() / hour), + const item: BrowserStorageItem = { + cached: Math.floor(Date.now() / browserStorageHour), provider, data: icon, }; // Make items 2 and 4 expire if (i === 2 || i === 4) { - item.cached -= cacheExpiration + 1; + item.cached -= browserStorageCacheExpiration + 1; } // Change expiration for items 6 and 8 to almost expire if (i === 6 || i === 8) { - item.cached -= cacheExpiration - 1; + item.cached -= browserStorageCacheExpiration - 1; } icons.push(icon); @@ -288,15 +283,15 @@ describe('Testing saving to localStorage', () => { loadCache(); // Check data - expect(config).toEqual({ + expect(browserStorageConfig).toEqual({ local: false, session: true, }); - expect(count).toEqual({ + expect(browserStorageItemsCount).toEqual({ local: 0, session: 9, // item 9 was missing }); - expect(emptyList).toEqual({ + expect(browserStorageEmptyItems).toEqual({ local: [], // mix of expired and skipped items // reverse order, 9 should not be there because it is last item @@ -321,11 +316,11 @@ describe('Testing saving to localStorage', () => { // Add item 5 storeCache(provider, icons[5]); - expect(count).toEqual({ + expect(browserStorageItemsCount).toEqual({ local: 0, session: 9, }); - expect(emptyList).toEqual({ + expect(browserStorageEmptyItems).toEqual({ local: [], session: [4, 2, 1], }); @@ -336,11 +331,11 @@ describe('Testing saving to localStorage', () => { list.slice(0).forEach((index) => { expect(list.shift()).toBe(index); storeCache(provider, icons[index]); - expect(count).toEqual({ + expect(browserStorageItemsCount).toEqual({ local: 0, session: 9, }); - expect(emptyList).toEqual({ + expect(browserStorageEmptyItems).toEqual({ local: [], session: list, }); @@ -349,11 +344,11 @@ describe('Testing saving to localStorage', () => { // Add item 10 storeCache(provider, icons[10]); - expect(count).toEqual({ + expect(browserStorageItemsCount).toEqual({ local: 0, session: 10, }); - expect(emptyList).toEqual({ + expect(browserStorageEmptyItems).toEqual({ local: [], session: [], }); @@ -361,11 +356,11 @@ describe('Testing saving to localStorage', () => { // Add item 11 storeCache(provider, icons[11]); - expect(count).toEqual({ + expect(browserStorageItemsCount).toEqual({ local: 0, session: 11, }); - expect(emptyList).toEqual({ + expect(browserStorageEmptyItems).toEqual({ local: [], session: [], }); @@ -405,15 +400,15 @@ describe('Testing saving to localStorage', () => { // Load cache loadCache(); - expect(config).toEqual({ + expect(browserStorageConfig).toEqual({ local: true, session: false, }); - expect(count).toEqual({ + expect(browserStorageItemsCount).toEqual({ local: 0, session: 0, }); - expect(emptyList).toEqual({ + expect(browserStorageEmptyItems).toEqual({ local: [], session: [], }); @@ -427,8 +422,8 @@ describe('Testing saving to localStorage', () => { }, }, }; - const item: StoredItem = { - cached: Math.floor(Date.now() / hour), + const item: BrowserStorageItem = { + cached: Math.floor(Date.now() / browserStorageHour), provider, data: icon, }; @@ -441,15 +436,15 @@ describe('Testing saving to localStorage', () => { // Check data that should have been updated because storeCache() // should call load function before first execution - expect(config).toEqual({ + expect(browserStorageConfig).toEqual({ local: true, session: false, }); - expect(count).toEqual({ + expect(browserStorageItemsCount).toEqual({ local: 1, session: 0, }); - expect(emptyList).toEqual({ + expect(browserStorageEmptyItems).toEqual({ local: [], session: [], }); @@ -479,8 +474,8 @@ describe('Testing saving to localStorage', () => { }, }, }; - const item: StoredItem = { - cached: Math.floor(Date.now() / hour), + const item: BrowserStorageItem = { + cached: Math.floor(Date.now() / browserStorageHour), provider, data: icon, }; @@ -502,8 +497,8 @@ describe('Testing saving to localStorage', () => { }, }, }; - const item: StoredItem = { - cached: Math.floor(Date.now() / hour), + const item: BrowserStorageItem = { + cached: Math.floor(Date.now() / browserStorageHour), provider, data: icon, }; @@ -523,25 +518,25 @@ describe('Testing saving to localStorage', () => { loadCache(); // Check data - expect(config).toEqual({ + expect(browserStorageConfig).toEqual({ local: true, session: true, }); - expect(count).toEqual({ + expect(browserStorageItemsCount).toEqual({ local: 3, session: 4, }); - expect(emptyList).toEqual({ + expect(browserStorageEmptyItems).toEqual({ local: [], session: [], }); // Check icon storage const iconsStorage = getStorage(provider, prefix); - for (let i = 0; i < count.local; i++) { + for (let i = 0; i < browserStorageItemsCount.local; i++) { expect(iconExists(iconsStorage, 'foo' + i.toString())).toBe(true); } - for (let i = 0; i < count.session; i++) { + for (let i = 0; i < browserStorageItemsCount.session; i++) { expect(iconExists(iconsStorage, 'bar' + i.toString())).toBe(true); } @@ -554,19 +549,19 @@ describe('Testing saving to localStorage', () => { }, }, }; - const item: StoredItem = { - cached: Math.floor(Date.now() / hour), + const item: BrowserStorageItem = { + cached: Math.floor(Date.now() / browserStorageHour), provider, data: icon, }; storeCache(provider, icon); // Check data - expect(count).toEqual({ + expect(browserStorageItemsCount).toEqual({ local: 4, // +1 session: 4, }); - expect(emptyList).toEqual({ + expect(browserStorageEmptyItems).toEqual({ local: [], session: [], }); @@ -594,8 +589,8 @@ describe('Testing saving to localStorage', () => { }, }, }; - const item: StoredItem = { - cached: Math.floor(Date.now() / hour), + const item: BrowserStorageItem = { + cached: Math.floor(Date.now() / browserStorageHour), provider, data: icon, }; @@ -617,8 +612,8 @@ describe('Testing saving to localStorage', () => { }, }, }; - const item: StoredItem = { - cached: Math.floor(Date.now() / hour), + const item: BrowserStorageItem = { + cached: Math.floor(Date.now() / browserStorageHour), provider, data: icon, }; @@ -638,25 +633,25 @@ describe('Testing saving to localStorage', () => { loadCache(); // Check data - expect(config).toEqual({ + expect(browserStorageConfig).toEqual({ local: true, session: true, }); - expect(count).toEqual({ + expect(browserStorageItemsCount).toEqual({ local: 3, session: 4, }); - expect(emptyList).toEqual({ + expect(browserStorageEmptyItems).toEqual({ local: [], session: [], }); // Check icon storage const iconsStorage = getStorage(provider, prefix); - for (let i = 0; i < count.local; i++) { + for (let i = 0; i < browserStorageItemsCount.local; i++) { expect(iconExists(iconsStorage, 'foo' + i.toString())).toBe(true); } - for (let i = 0; i < count.session; i++) { + for (let i = 0; i < browserStorageItemsCount.session; i++) { expect(iconExists(iconsStorage, 'bar' + i.toString())).toBe(true); } @@ -672,19 +667,19 @@ describe('Testing saving to localStorage', () => { }, }, }; - const item: StoredItem = { - cached: Math.floor(Date.now() / hour), + const item: BrowserStorageItem = { + cached: Math.floor(Date.now() / browserStorageHour), provider, data: icon, }; storeCache(provider, icon); // Check data - expect(count).toEqual({ + expect(browserStorageItemsCount).toEqual({ local: 3, session: 5, }); - expect(emptyList).toEqual({ + expect(browserStorageEmptyItems).toEqual({ local: [], session: [], });