mirror of
https://github.com/iconify/iconify.git
synced 2025-01-07 15:44:05 +00:00
Move browser cache config to separate file
This commit is contained in:
parent
0bd0bae6c2
commit
59bdbe89e0
@ -57,6 +57,10 @@
|
|||||||
"require": "./lib/api/query.cjs",
|
"require": "./lib/api/query.cjs",
|
||||||
"import": "./lib/api/query.mjs"
|
"import": "./lib/api/query.mjs"
|
||||||
},
|
},
|
||||||
|
"./lib/browser-storage/config": {
|
||||||
|
"require": "./lib/browser-storage/config.cjs",
|
||||||
|
"import": "./lib/browser-storage/config.mjs"
|
||||||
|
},
|
||||||
"./lib/browser-storage/functions": {
|
"./lib/browser-storage/functions": {
|
||||||
"require": "./lib/browser-storage/functions.cjs",
|
"require": "./lib/browser-storage/functions.cjs",
|
||||||
"import": "./lib/browser-storage/functions.mjs"
|
"import": "./lib/browser-storage/functions.mjs"
|
||||||
|
7
packages/core/src/browser-storage/config.ts
Normal file
7
packages/core/src/browser-storage/config.ts
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
// Cache version. Bump when structure changes
|
||||||
|
export const browserCacheVersion = 'iconify2';
|
||||||
|
|
||||||
|
// Cache keys
|
||||||
|
export const browserCachePrefix = 'iconify';
|
||||||
|
export const browserCacheCountKey = browserCachePrefix + '-count';
|
||||||
|
export const browserCacheVersionKey = browserCachePrefix + '-version';
|
@ -1,6 +1,12 @@
|
|||||||
import type { IconifyJSON } from '@iconify/types';
|
import type { IconifyJSON } from '@iconify/types';
|
||||||
import type { CacheIcons, LoadIconsCache } from '../cache';
|
import type { CacheIcons, LoadIconsCache } from '../cache';
|
||||||
import { getStorage, addIconSet } from '../storage/storage';
|
import { getStorage, addIconSet } from '../storage/storage';
|
||||||
|
import {
|
||||||
|
browserCacheCountKey,
|
||||||
|
browserCachePrefix,
|
||||||
|
browserCacheVersion,
|
||||||
|
browserCacheVersionKey,
|
||||||
|
} from './config';
|
||||||
|
|
||||||
interface StorageType<T> {
|
interface StorageType<T> {
|
||||||
local: T;
|
local: T;
|
||||||
@ -17,16 +23,6 @@ export interface StoredItem {
|
|||||||
data: IconifyJSON;
|
data: IconifyJSON;
|
||||||
}
|
}
|
||||||
|
|
||||||
// After changing configuration change it in tests/*/fake_cache.ts
|
|
||||||
|
|
||||||
// Cache version. Bump when structure changes
|
|
||||||
const cacheVersion = 'iconify2';
|
|
||||||
|
|
||||||
// Cache keys
|
|
||||||
const cachePrefix = 'iconify';
|
|
||||||
const countKey = cachePrefix + '-count';
|
|
||||||
const versionKey = cachePrefix + '-version';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Cache expiration
|
* Cache expiration
|
||||||
*/
|
*/
|
||||||
@ -107,7 +103,7 @@ function setCount(
|
|||||||
value: number
|
value: number
|
||||||
): boolean {
|
): boolean {
|
||||||
try {
|
try {
|
||||||
storage.setItem(countKey, value.toString());
|
storage.setItem(browserCacheCountKey, value.toString());
|
||||||
count[key] = value;
|
count[key] = value;
|
||||||
return true;
|
return true;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@ -122,7 +118,7 @@ function setCount(
|
|||||||
* @param storage
|
* @param storage
|
||||||
*/
|
*/
|
||||||
function getCount(storage: typeof localStorage): number {
|
function getCount(storage: typeof localStorage): number {
|
||||||
const count = storage.getItem(countKey);
|
const count = storage.getItem(browserCacheCountKey);
|
||||||
if (count) {
|
if (count) {
|
||||||
const total = parseInt(count);
|
const total = parseInt(count);
|
||||||
return total ? total : 0;
|
return total ? total : 0;
|
||||||
@ -141,7 +137,7 @@ function initCache(
|
|||||||
key: keyof StorageConfig
|
key: keyof StorageConfig
|
||||||
): void {
|
): void {
|
||||||
try {
|
try {
|
||||||
storage.setItem(versionKey, cacheVersion);
|
storage.setItem(browserCacheVersionKey, browserCacheVersion);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
//
|
//
|
||||||
}
|
}
|
||||||
@ -157,7 +153,7 @@ function destroyCache(storage: typeof localStorage): void {
|
|||||||
try {
|
try {
|
||||||
const total = getCount(storage);
|
const total = getCount(storage);
|
||||||
for (let i = 0; i < total; i++) {
|
for (let i = 0; i < total; i++) {
|
||||||
storage.removeItem(cachePrefix + i.toString());
|
storage.removeItem(browserCachePrefix + i.toString());
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
//
|
//
|
||||||
@ -185,7 +181,7 @@ export const loadCache: LoadIconsCache = (): void => {
|
|||||||
|
|
||||||
// Get one item from storage
|
// Get one item from storage
|
||||||
const getItem = (index: number): boolean => {
|
const getItem = (index: number): boolean => {
|
||||||
const name = cachePrefix + index.toString();
|
const name = browserCachePrefix + index.toString();
|
||||||
const item = func.getItem(name);
|
const item = func.getItem(name);
|
||||||
|
|
||||||
if (typeof item !== 'string') {
|
if (typeof item !== 'string') {
|
||||||
@ -226,8 +222,8 @@ export const loadCache: LoadIconsCache = (): void => {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// Get version
|
// Get version
|
||||||
const version = func.getItem(versionKey);
|
const version = func.getItem(browserCacheVersionKey);
|
||||||
if (version !== cacheVersion) {
|
if (version !== browserCacheVersion) {
|
||||||
if (version) {
|
if (version) {
|
||||||
// Version is set, but invalid - remove old entries
|
// Version is set, but invalid - remove old entries
|
||||||
destroyCache(func);
|
destroyCache(func);
|
||||||
@ -302,7 +298,10 @@ export const storeCache: CacheIcons = (
|
|||||||
provider,
|
provider,
|
||||||
data,
|
data,
|
||||||
};
|
};
|
||||||
func.setItem(cachePrefix + index.toString(), JSON.stringify(item));
|
func.setItem(
|
||||||
|
browserCachePrefix + index.toString(),
|
||||||
|
JSON.stringify(item)
|
||||||
|
);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -8,14 +8,6 @@ export function nextPrefix(): string {
|
|||||||
return 'fake-storage-' + (prefixCounter++).toString();
|
return 'fake-storage-' + (prefixCounter++).toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cache version. Bump when structure changes
|
|
||||||
export const cacheVersion = 'iconify2';
|
|
||||||
|
|
||||||
// Cache keys
|
|
||||||
export const cachePrefix = 'iconify';
|
|
||||||
export const countKey = cachePrefix + '-count';
|
|
||||||
export const versionKey = cachePrefix + '-version';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Cache expiration
|
* Cache expiration
|
||||||
*/
|
*/
|
||||||
|
26
packages/core/tests/cache/basic-test.ts
vendored
26
packages/core/tests/cache/basic-test.ts
vendored
@ -1,13 +1,11 @@
|
|||||||
import { count, config, loadCache } from '../../lib/browser-storage';
|
import { count, config, loadCache } from '../../lib/browser-storage';
|
||||||
import {
|
import {
|
||||||
nextPrefix,
|
browserCacheCountKey,
|
||||||
createCache,
|
browserCachePrefix,
|
||||||
reset,
|
browserCacheVersion,
|
||||||
cachePrefix,
|
browserCacheVersionKey,
|
||||||
cacheVersion,
|
} from '../../lib/browser-storage/config';
|
||||||
versionKey,
|
import { nextPrefix, createCache, reset } from '../../lib/browser-storage/mock';
|
||||||
countKey,
|
|
||||||
} from '../../lib/browser-storage/mock';
|
|
||||||
|
|
||||||
describe('Testing mocked localStorage', () => {
|
describe('Testing mocked localStorage', () => {
|
||||||
const provider = '';
|
const provider = '';
|
||||||
@ -77,10 +75,10 @@ describe('Testing mocked localStorage', () => {
|
|||||||
const cache = createCache();
|
const cache = createCache();
|
||||||
|
|
||||||
// Add one item
|
// Add one item
|
||||||
cache.setItem(versionKey, cacheVersion);
|
cache.setItem(browserCacheVersionKey, browserCacheVersion);
|
||||||
cache.setItem(countKey, '1');
|
cache.setItem(browserCacheCountKey, '1');
|
||||||
cache.setItem(
|
cache.setItem(
|
||||||
cachePrefix + '0',
|
browserCachePrefix + '0',
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
cached: Date.now(),
|
cached: Date.now(),
|
||||||
provider,
|
provider,
|
||||||
@ -136,10 +134,10 @@ describe('Testing mocked localStorage', () => {
|
|||||||
const cache = createCache();
|
const cache = createCache();
|
||||||
|
|
||||||
// Add one icon set
|
// Add one icon set
|
||||||
cache.setItem(versionKey, cacheVersion);
|
cache.setItem(browserCacheVersionKey, browserCacheVersion);
|
||||||
cache.setItem(countKey, '1');
|
cache.setItem(browserCacheCountKey, '1');
|
||||||
cache.setItem(
|
cache.setItem(
|
||||||
cachePrefix + '0',
|
browserCachePrefix + '0',
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
cached: Date.now(),
|
cached: Date.now(),
|
||||||
provider,
|
provider,
|
||||||
|
66
packages/core/tests/cache/loading-test.ts
vendored
66
packages/core/tests/cache/loading-test.ts
vendored
@ -6,13 +6,15 @@ import {
|
|||||||
nextPrefix,
|
nextPrefix,
|
||||||
createCache,
|
createCache,
|
||||||
reset,
|
reset,
|
||||||
cachePrefix,
|
|
||||||
cacheVersion,
|
|
||||||
versionKey,
|
|
||||||
countKey,
|
|
||||||
hour,
|
hour,
|
||||||
cacheExpiration,
|
cacheExpiration,
|
||||||
} from '../../lib/browser-storage/mock';
|
} from '../../lib/browser-storage/mock';
|
||||||
|
import {
|
||||||
|
browserCacheCountKey,
|
||||||
|
browserCachePrefix,
|
||||||
|
browserCacheVersion,
|
||||||
|
browserCacheVersionKey,
|
||||||
|
} from '../../lib/browser-storage/config';
|
||||||
|
|
||||||
describe('Testing loading from localStorage', () => {
|
describe('Testing loading from localStorage', () => {
|
||||||
const provider = '';
|
const provider = '';
|
||||||
@ -22,8 +24,8 @@ describe('Testing loading from localStorage', () => {
|
|||||||
const cache = createCache();
|
const cache = createCache();
|
||||||
|
|
||||||
// Add one icon set
|
// Add one icon set
|
||||||
cache.setItem(versionKey, cacheVersion);
|
cache.setItem(browserCacheVersionKey, browserCacheVersion);
|
||||||
cache.setItem(countKey, '1');
|
cache.setItem(browserCacheCountKey, '1');
|
||||||
|
|
||||||
const item: StoredItem = {
|
const item: StoredItem = {
|
||||||
cached: Math.floor(Date.now() / hour),
|
cached: Math.floor(Date.now() / hour),
|
||||||
@ -37,7 +39,7 @@ describe('Testing loading from localStorage', () => {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
cache.setItem(cachePrefix + '0', JSON.stringify(item));
|
cache.setItem(browserCachePrefix + '0', JSON.stringify(item));
|
||||||
|
|
||||||
// Set cache
|
// Set cache
|
||||||
reset({
|
reset({
|
||||||
@ -75,8 +77,8 @@ describe('Testing loading from localStorage', () => {
|
|||||||
const cache = createCache();
|
const cache = createCache();
|
||||||
|
|
||||||
// Add one icon set
|
// Add one icon set
|
||||||
cache.setItem(versionKey, cacheVersion);
|
cache.setItem(browserCacheVersionKey, browserCacheVersion);
|
||||||
cache.setItem(countKey, '1');
|
cache.setItem(browserCacheCountKey, '1');
|
||||||
|
|
||||||
const item: StoredItem = {
|
const item: StoredItem = {
|
||||||
cached: Math.floor(Date.now() / hour),
|
cached: Math.floor(Date.now() / hour),
|
||||||
@ -90,7 +92,7 @@ describe('Testing loading from localStorage', () => {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
cache.setItem(cachePrefix + '0', JSON.stringify(item));
|
cache.setItem(browserCachePrefix + '0', JSON.stringify(item));
|
||||||
|
|
||||||
// Set cache
|
// Set cache
|
||||||
reset({
|
reset({
|
||||||
@ -132,8 +134,8 @@ describe('Testing loading from localStorage', () => {
|
|||||||
const cache = createCache();
|
const cache = createCache();
|
||||||
|
|
||||||
// Add one icon set
|
// Add one icon set
|
||||||
cache.setItem(versionKey, cacheVersion);
|
cache.setItem(browserCacheVersionKey, browserCacheVersion);
|
||||||
cache.setItem(countKey, '1');
|
cache.setItem(browserCacheCountKey, '1');
|
||||||
|
|
||||||
const item: StoredItem = {
|
const item: StoredItem = {
|
||||||
// Expiration date
|
// Expiration date
|
||||||
@ -148,7 +150,7 @@ describe('Testing loading from localStorage', () => {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
cache.setItem(cachePrefix + '0', JSON.stringify(item));
|
cache.setItem(browserCachePrefix + '0', JSON.stringify(item));
|
||||||
|
|
||||||
// Set cache
|
// Set cache
|
||||||
reset({
|
reset({
|
||||||
@ -185,10 +187,10 @@ describe('Testing loading from localStorage', () => {
|
|||||||
const cache = createCache();
|
const cache = createCache();
|
||||||
|
|
||||||
// Add one icon set
|
// Add one icon set
|
||||||
cache.setItem(versionKey, cacheVersion);
|
cache.setItem(browserCacheVersionKey, browserCacheVersion);
|
||||||
cache.setItem(countKey, '1');
|
cache.setItem(browserCacheCountKey, '1');
|
||||||
cache.setItem(
|
cache.setItem(
|
||||||
cachePrefix + '0',
|
browserCachePrefix + '0',
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
cached: Math.floor(Date.now() / hour),
|
cached: Math.floor(Date.now() / hour),
|
||||||
provider,
|
provider,
|
||||||
@ -239,8 +241,8 @@ describe('Testing loading from localStorage', () => {
|
|||||||
const cache = createCache();
|
const cache = createCache();
|
||||||
|
|
||||||
// Add one icon set
|
// Add one icon set
|
||||||
cache.setItem(versionKey, cacheVersion);
|
cache.setItem(browserCacheVersionKey, browserCacheVersion);
|
||||||
cache.setItem(countKey, '0'); // Should be at least "1"
|
cache.setItem(browserCacheCountKey, '0'); // Should be at least "1"
|
||||||
|
|
||||||
const item: StoredItem = {
|
const item: StoredItem = {
|
||||||
cached: Math.floor(Date.now() / hour),
|
cached: Math.floor(Date.now() / hour),
|
||||||
@ -254,7 +256,7 @@ describe('Testing loading from localStorage', () => {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
cache.setItem(cachePrefix + '0', JSON.stringify(item));
|
cache.setItem(browserCachePrefix + '0', JSON.stringify(item));
|
||||||
|
|
||||||
// Set cache
|
// Set cache
|
||||||
reset({
|
reset({
|
||||||
@ -291,8 +293,8 @@ describe('Testing loading from localStorage', () => {
|
|||||||
const cache = createCache();
|
const cache = createCache();
|
||||||
|
|
||||||
// Add one icon set
|
// Add one icon set
|
||||||
cache.setItem(versionKey, cacheVersion);
|
cache.setItem(browserCacheVersionKey, browserCacheVersion);
|
||||||
cache.setItem(countKey, '5');
|
cache.setItem(browserCacheCountKey, '5');
|
||||||
|
|
||||||
const item: StoredItem = {
|
const item: StoredItem = {
|
||||||
cached: Math.floor(Date.now() / hour),
|
cached: Math.floor(Date.now() / hour),
|
||||||
@ -306,7 +308,7 @@ describe('Testing loading from localStorage', () => {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
cache.setItem(cachePrefix + '0', JSON.stringify(item));
|
cache.setItem(browserCachePrefix + '0', JSON.stringify(item));
|
||||||
|
|
||||||
// Set cache
|
// Set cache
|
||||||
reset({
|
reset({
|
||||||
@ -343,8 +345,8 @@ describe('Testing loading from localStorage', () => {
|
|||||||
const cache = createCache();
|
const cache = createCache();
|
||||||
|
|
||||||
// Add two icon sets
|
// Add two icon sets
|
||||||
cache.setItem(versionKey, cacheVersion);
|
cache.setItem(browserCacheVersionKey, browserCacheVersion);
|
||||||
cache.setItem(countKey, '5');
|
cache.setItem(browserCacheCountKey, '5');
|
||||||
|
|
||||||
// Missing: 0, 2, 3
|
// Missing: 0, 2, 3
|
||||||
const item1: StoredItem = {
|
const item1: StoredItem = {
|
||||||
@ -372,8 +374,8 @@ describe('Testing loading from localStorage', () => {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
cache.setItem(cachePrefix + '1', JSON.stringify(item1));
|
cache.setItem(browserCachePrefix + '1', JSON.stringify(item1));
|
||||||
cache.setItem(cachePrefix + '4', JSON.stringify(item4));
|
cache.setItem(browserCachePrefix + '4', JSON.stringify(item4));
|
||||||
|
|
||||||
// Set cache
|
// Set cache
|
||||||
reset({
|
reset({
|
||||||
@ -413,11 +415,11 @@ describe('Testing loading from localStorage', () => {
|
|||||||
const cache2 = createCache();
|
const cache2 = createCache();
|
||||||
|
|
||||||
// Add few icon sets
|
// Add few icon sets
|
||||||
cache1.setItem(versionKey, cacheVersion);
|
cache1.setItem(browserCacheVersionKey, browserCacheVersion);
|
||||||
cache2.setItem(versionKey, cacheVersion);
|
cache2.setItem(browserCacheVersionKey, browserCacheVersion);
|
||||||
|
|
||||||
cache1.setItem(countKey, '6');
|
cache1.setItem(browserCacheCountKey, '6');
|
||||||
cache2.setItem(countKey, '3');
|
cache2.setItem(browserCacheCountKey, '3');
|
||||||
|
|
||||||
// Create 5 items
|
// Create 5 items
|
||||||
const icons: IconifyJSON[] = [];
|
const icons: IconifyJSON[] = [];
|
||||||
@ -444,7 +446,7 @@ describe('Testing loading from localStorage', () => {
|
|||||||
// Add items 1,3,5 to localStorage
|
// Add items 1,3,5 to localStorage
|
||||||
[1, 3, 5].forEach((index) => {
|
[1, 3, 5].forEach((index) => {
|
||||||
cache1.setItem(
|
cache1.setItem(
|
||||||
cachePrefix + index.toString(),
|
browserCachePrefix + index.toString(),
|
||||||
JSON.stringify(items[index])
|
JSON.stringify(items[index])
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
@ -452,7 +454,7 @@ describe('Testing loading from localStorage', () => {
|
|||||||
// Add items 0 and 2 to sessionStorage
|
// Add items 0 and 2 to sessionStorage
|
||||||
[0, 2].forEach((index) => {
|
[0, 2].forEach((index) => {
|
||||||
cache2.setItem(
|
cache2.setItem(
|
||||||
cachePrefix + index.toString(),
|
browserCachePrefix + index.toString(),
|
||||||
JSON.stringify(items[index])
|
JSON.stringify(items[index])
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
119
packages/core/tests/cache/saving-test.ts
vendored
119
packages/core/tests/cache/saving-test.ts
vendored
@ -12,13 +12,15 @@ import {
|
|||||||
nextPrefix,
|
nextPrefix,
|
||||||
createCache,
|
createCache,
|
||||||
reset,
|
reset,
|
||||||
cachePrefix,
|
|
||||||
cacheVersion,
|
|
||||||
versionKey,
|
|
||||||
countKey,
|
|
||||||
hour,
|
hour,
|
||||||
cacheExpiration,
|
cacheExpiration,
|
||||||
} from '../../lib/browser-storage/mock';
|
} from '../../lib/browser-storage/mock';
|
||||||
|
import {
|
||||||
|
browserCacheCountKey,
|
||||||
|
browserCachePrefix,
|
||||||
|
browserCacheVersion,
|
||||||
|
browserCacheVersionKey,
|
||||||
|
} from '../../lib/browser-storage/config';
|
||||||
|
|
||||||
describe('Testing saving to localStorage', () => {
|
describe('Testing saving to localStorage', () => {
|
||||||
const provider = '';
|
const provider = '';
|
||||||
@ -73,9 +75,11 @@ describe('Testing saving to localStorage', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Check cache
|
// Check cache
|
||||||
expect(cache.getItem(cachePrefix + '0')).toBe(JSON.stringify(item));
|
expect(cache.getItem(browserCachePrefix + '0')).toBe(
|
||||||
expect(cache.getItem(countKey)).toBe('1');
|
JSON.stringify(item)
|
||||||
expect(cache.getItem(versionKey)).toBe(cacheVersion);
|
);
|
||||||
|
expect(cache.getItem(browserCacheCountKey)).toBe('1');
|
||||||
|
expect(cache.getItem(browserCacheVersionKey)).toBe(browserCacheVersion);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Multiple icon sets', () => {
|
it('Multiple icon sets', () => {
|
||||||
@ -135,10 +139,14 @@ describe('Testing saving to localStorage', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Check cache
|
// Check cache
|
||||||
expect(cache.getItem(cachePrefix + '0')).toBe(JSON.stringify(item0));
|
expect(cache.getItem(browserCachePrefix + '0')).toBe(
|
||||||
expect(cache.getItem(cachePrefix + '1')).toBe(JSON.stringify(item1));
|
JSON.stringify(item0)
|
||||||
expect(cache.getItem(countKey)).toBe('2');
|
);
|
||||||
expect(cache.getItem(versionKey)).toBe(cacheVersion);
|
expect(cache.getItem(browserCachePrefix + '1')).toBe(
|
||||||
|
JSON.stringify(item1)
|
||||||
|
);
|
||||||
|
expect(cache.getItem(browserCacheCountKey)).toBe('2');
|
||||||
|
expect(cache.getItem(browserCacheVersionKey)).toBe(browserCacheVersion);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Adding icon set on unused spot', () => {
|
it('Adding icon set on unused spot', () => {
|
||||||
@ -174,9 +182,9 @@ describe('Testing saving to localStorage', () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Add item
|
// Add item
|
||||||
cache.setItem(versionKey, cacheVersion);
|
cache.setItem(browserCacheVersionKey, browserCacheVersion);
|
||||||
cache.setItem(countKey, '2');
|
cache.setItem(browserCacheCountKey, '2');
|
||||||
cache.setItem(cachePrefix + '1', JSON.stringify(item1));
|
cache.setItem(browserCachePrefix + '1', JSON.stringify(item1));
|
||||||
|
|
||||||
// Set cache
|
// Set cache
|
||||||
reset({
|
reset({
|
||||||
@ -214,10 +222,14 @@ describe('Testing saving to localStorage', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Check cache
|
// Check cache
|
||||||
expect(cache.getItem(cachePrefix + '0')).toBe(JSON.stringify(item0));
|
expect(cache.getItem(browserCachePrefix + '0')).toBe(
|
||||||
expect(cache.getItem(cachePrefix + '1')).toBe(JSON.stringify(item1));
|
JSON.stringify(item0)
|
||||||
expect(cache.getItem(countKey)).toBe('2');
|
);
|
||||||
expect(cache.getItem(versionKey)).toBe(cacheVersion);
|
expect(cache.getItem(browserCachePrefix + '1')).toBe(
|
||||||
|
JSON.stringify(item1)
|
||||||
|
);
|
||||||
|
expect(cache.getItem(browserCacheCountKey)).toBe('2');
|
||||||
|
expect(cache.getItem(browserCacheVersionKey)).toBe(browserCacheVersion);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Adding multiple icon sets to existing data', () => {
|
it('Adding multiple icon sets to existing data', () => {
|
||||||
@ -257,12 +269,15 @@ describe('Testing saving to localStorage', () => {
|
|||||||
|
|
||||||
// Skip items 1, 5, 9+
|
// Skip items 1, 5, 9+
|
||||||
if (i !== 1 && i !== 5 && i < 9) {
|
if (i !== 1 && i !== 5 && i < 9) {
|
||||||
cache.setItem(cachePrefix + i.toString(), JSON.stringify(item));
|
cache.setItem(
|
||||||
|
browserCachePrefix + i.toString(),
|
||||||
|
JSON.stringify(item)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cache.setItem(versionKey, cacheVersion);
|
cache.setItem(browserCacheVersionKey, browserCacheVersion);
|
||||||
cache.setItem(countKey, '10');
|
cache.setItem(browserCacheCountKey, '10');
|
||||||
|
|
||||||
// Set cache
|
// Set cache
|
||||||
reset({
|
reset({
|
||||||
@ -287,11 +302,11 @@ describe('Testing saving to localStorage', () => {
|
|||||||
// reverse order, 9 should not be there because it is last item
|
// reverse order, 9 should not be there because it is last item
|
||||||
session: [5, 4, 2, 1],
|
session: [5, 4, 2, 1],
|
||||||
});
|
});
|
||||||
expect(cache.getItem(countKey)).toBe('9');
|
expect(cache.getItem(browserCacheCountKey)).toBe('9');
|
||||||
|
|
||||||
// Check cached items
|
// Check cached items
|
||||||
[0, 3, 6, 7, 8].forEach((index) => {
|
[0, 3, 6, 7, 8].forEach((index) => {
|
||||||
expect(cache.getItem(cachePrefix + index.toString())).toBe(
|
expect(cache.getItem(browserCachePrefix + index.toString())).toBe(
|
||||||
JSON.stringify(items[index])
|
JSON.stringify(items[index])
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
@ -299,7 +314,9 @@ describe('Testing saving to localStorage', () => {
|
|||||||
// Check expired items - should have been deleted
|
// Check expired items - should have been deleted
|
||||||
// Also check items that weren't supposed to be added
|
// Also check items that weren't supposed to be added
|
||||||
[2, 4, 1, 5, 9, 10, 11, 12, 13].forEach((index) => {
|
[2, 4, 1, 5, 9, 10, 11, 12, 13].forEach((index) => {
|
||||||
expect(cache.getItem(cachePrefix + index.toString())).toBeNull();
|
expect(
|
||||||
|
cache.getItem(browserCachePrefix + index.toString())
|
||||||
|
).toBeNull();
|
||||||
});
|
});
|
||||||
|
|
||||||
// Add item 5
|
// Add item 5
|
||||||
@ -312,7 +329,7 @@ describe('Testing saving to localStorage', () => {
|
|||||||
local: [],
|
local: [],
|
||||||
session: [4, 2, 1],
|
session: [4, 2, 1],
|
||||||
});
|
});
|
||||||
expect(cache.getItem(countKey)).toBe('9');
|
expect(cache.getItem(browserCacheCountKey)).toBe('9');
|
||||||
|
|
||||||
// Add items 4, 2, 1
|
// Add items 4, 2, 1
|
||||||
const list = [4, 2, 1];
|
const list = [4, 2, 1];
|
||||||
@ -327,7 +344,7 @@ describe('Testing saving to localStorage', () => {
|
|||||||
local: [],
|
local: [],
|
||||||
session: list,
|
session: list,
|
||||||
});
|
});
|
||||||
expect(cache.getItem(countKey)).toBe('9');
|
expect(cache.getItem(browserCacheCountKey)).toBe('9');
|
||||||
});
|
});
|
||||||
|
|
||||||
// Add item 10
|
// Add item 10
|
||||||
@ -340,7 +357,7 @@ describe('Testing saving to localStorage', () => {
|
|||||||
local: [],
|
local: [],
|
||||||
session: [],
|
session: [],
|
||||||
});
|
});
|
||||||
expect(cache.getItem(countKey)).toBe('10');
|
expect(cache.getItem(browserCacheCountKey)).toBe('10');
|
||||||
|
|
||||||
// Add item 11
|
// Add item 11
|
||||||
storeCache(provider, icons[11]);
|
storeCache(provider, icons[11]);
|
||||||
@ -352,7 +369,7 @@ describe('Testing saving to localStorage', () => {
|
|||||||
local: [],
|
local: [],
|
||||||
session: [],
|
session: [],
|
||||||
});
|
});
|
||||||
expect(cache.getItem(countKey)).toBe('11');
|
expect(cache.getItem(browserCacheCountKey)).toBe('11');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Overwrite outdated data', () => {
|
it('Overwrite outdated data', () => {
|
||||||
@ -360,11 +377,11 @@ describe('Testing saving to localStorage', () => {
|
|||||||
const cache = createCache();
|
const cache = createCache();
|
||||||
|
|
||||||
// Add data in old format
|
// Add data in old format
|
||||||
cache.setItem(versionKey, '1.0.6');
|
cache.setItem(browserCacheVersionKey, '1.0.6');
|
||||||
cache.setItem(countKey, '3');
|
cache.setItem(browserCacheCountKey, '3');
|
||||||
for (let i = 0; i < 3; i++) {
|
for (let i = 0; i < 3; i++) {
|
||||||
cache.setItem(
|
cache.setItem(
|
||||||
cachePrefix + i.toString(),
|
browserCachePrefix + i.toString(),
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
prefix: prefix,
|
prefix: prefix,
|
||||||
icons: {
|
icons: {
|
||||||
@ -438,9 +455,11 @@ describe('Testing saving to localStorage', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Check cache
|
// Check cache
|
||||||
expect(cache.getItem(cachePrefix + '0')).toBe(JSON.stringify(item));
|
expect(cache.getItem(browserCachePrefix + '0')).toBe(
|
||||||
expect(cache.getItem(countKey)).toBe('1');
|
JSON.stringify(item)
|
||||||
expect(cache.getItem(versionKey)).toBe(cacheVersion);
|
);
|
||||||
|
expect(cache.getItem(browserCacheCountKey)).toBe('1');
|
||||||
|
expect(cache.getItem(browserCacheVersionKey)).toBe(browserCacheVersion);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Using both storage options', () => {
|
it('Using both storage options', () => {
|
||||||
@ -449,8 +468,8 @@ describe('Testing saving to localStorage', () => {
|
|||||||
const cache2 = createCache();
|
const cache2 = createCache();
|
||||||
|
|
||||||
// Add icon sets to localStorage
|
// Add icon sets to localStorage
|
||||||
cache1.setItem(versionKey, cacheVersion);
|
cache1.setItem(browserCacheVersionKey, browserCacheVersion);
|
||||||
cache1.setItem(countKey, '3');
|
cache1.setItem(browserCacheCountKey, '3');
|
||||||
[0, 1, 2].forEach((index) => {
|
[0, 1, 2].forEach((index) => {
|
||||||
const icon: IconifyJSON = {
|
const icon: IconifyJSON = {
|
||||||
prefix: prefix,
|
prefix: prefix,
|
||||||
@ -466,14 +485,14 @@ describe('Testing saving to localStorage', () => {
|
|||||||
data: icon,
|
data: icon,
|
||||||
};
|
};
|
||||||
cache1.setItem(
|
cache1.setItem(
|
||||||
cachePrefix + index.toString(),
|
browserCachePrefix + index.toString(),
|
||||||
JSON.stringify(item)
|
JSON.stringify(item)
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Add icon sets to sessionStorage
|
// Add icon sets to sessionStorage
|
||||||
cache2.setItem(versionKey, cacheVersion);
|
cache2.setItem(browserCacheVersionKey, browserCacheVersion);
|
||||||
cache2.setItem(countKey, '4');
|
cache2.setItem(browserCacheCountKey, '4');
|
||||||
[0, 1, 2, 3].forEach((index) => {
|
[0, 1, 2, 3].forEach((index) => {
|
||||||
const icon: IconifyJSON = {
|
const icon: IconifyJSON = {
|
||||||
prefix: prefix,
|
prefix: prefix,
|
||||||
@ -489,7 +508,7 @@ describe('Testing saving to localStorage', () => {
|
|||||||
data: icon,
|
data: icon,
|
||||||
};
|
};
|
||||||
cache2.setItem(
|
cache2.setItem(
|
||||||
cachePrefix + index.toString(),
|
browserCachePrefix + index.toString(),
|
||||||
JSON.stringify(item)
|
JSON.stringify(item)
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
@ -553,7 +572,9 @@ describe('Testing saving to localStorage', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Check cache
|
// Check cache
|
||||||
expect(cache1.getItem(cachePrefix + '3')).toBe(JSON.stringify(item));
|
expect(cache1.getItem(browserCachePrefix + '3')).toBe(
|
||||||
|
JSON.stringify(item)
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Using both storage options, but localStorage is read only', () => {
|
it('Using both storage options, but localStorage is read only', () => {
|
||||||
@ -562,8 +583,8 @@ describe('Testing saving to localStorage', () => {
|
|||||||
const cache2 = createCache();
|
const cache2 = createCache();
|
||||||
|
|
||||||
// Add icon sets to localStorage
|
// Add icon sets to localStorage
|
||||||
cache1.setItem(versionKey, cacheVersion);
|
cache1.setItem(browserCacheVersionKey, browserCacheVersion);
|
||||||
cache1.setItem(countKey, '3');
|
cache1.setItem(browserCacheCountKey, '3');
|
||||||
[0, 1, 2].forEach((index) => {
|
[0, 1, 2].forEach((index) => {
|
||||||
const icon: IconifyJSON = {
|
const icon: IconifyJSON = {
|
||||||
prefix: prefix,
|
prefix: prefix,
|
||||||
@ -579,14 +600,14 @@ describe('Testing saving to localStorage', () => {
|
|||||||
data: icon,
|
data: icon,
|
||||||
};
|
};
|
||||||
cache1.setItem(
|
cache1.setItem(
|
||||||
cachePrefix + index.toString(),
|
browserCachePrefix + index.toString(),
|
||||||
JSON.stringify(item)
|
JSON.stringify(item)
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Add icon sets to sessionStorage
|
// Add icon sets to sessionStorage
|
||||||
cache2.setItem(versionKey, cacheVersion);
|
cache2.setItem(browserCacheVersionKey, browserCacheVersion);
|
||||||
cache2.setItem(countKey, '4');
|
cache2.setItem(browserCacheCountKey, '4');
|
||||||
[0, 1, 2, 3].forEach((index) => {
|
[0, 1, 2, 3].forEach((index) => {
|
||||||
const icon: IconifyJSON = {
|
const icon: IconifyJSON = {
|
||||||
prefix: prefix,
|
prefix: prefix,
|
||||||
@ -602,7 +623,7 @@ describe('Testing saving to localStorage', () => {
|
|||||||
data: icon,
|
data: icon,
|
||||||
};
|
};
|
||||||
cache2.setItem(
|
cache2.setItem(
|
||||||
cachePrefix + index.toString(),
|
browserCachePrefix + index.toString(),
|
||||||
JSON.stringify(item)
|
JSON.stringify(item)
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
@ -669,6 +690,8 @@ describe('Testing saving to localStorage', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Check cache
|
// Check cache
|
||||||
expect(cache2.getItem(cachePrefix + '4')).toBe(JSON.stringify(item));
|
expect(cache2.getItem(browserCachePrefix + '4')).toBe(
|
||||||
|
JSON.stringify(item)
|
||||||
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user