2
0
mirror of https://github.com/iconify/iconify.git synced 2024-12-14 06:28:24 +00:00
iconify/packages/core/tests/cache/saving-test.ts

882 lines
21 KiB
TypeScript
Raw Normal View History

2020-12-25 19:03:15 +00:00
import type { IconifyJSON } from '@iconify/types';
import type {
BrowserStorageItem,
IconStorageWithCache,
} from '../../lib/browser-storage/types';
import { storeInBrowserStorage } from '../../lib/browser-storage/store';
2022-06-28 20:40:20 +00:00
import { initBrowserStorage } from '../../lib/browser-storage';
2020-04-28 09:47:35 +00:00
import {
2022-06-28 18:16:08 +00:00
browserStorageConfig,
browserStorageEmptyItems,
} from '../../lib/browser-storage/data';
2022-06-29 09:30:13 +00:00
import { getBrowserStorageItemsCount } from '../../lib/browser-storage/count';
import { getBrowserStorage } from '../../lib/browser-storage/global';
import { getStorage, iconInStorage } from '../../lib/storage/storage';
2022-06-28 18:16:08 +00:00
import { nextPrefix, createCache, reset } from '../../lib/browser-storage/mock';
import {
browserCacheCountKey,
browserCachePrefix,
browserCacheVersion,
browserCacheVersionKey,
2022-06-28 18:16:08 +00:00
browserStorageHour,
browserStorageCacheExpiration,
} from '../../lib/browser-storage/config';
2020-04-28 09:47:35 +00:00
describe('Testing saving to localStorage', () => {
const provider = '';
2020-04-28 09:47:35 +00:00
it('One icon set', () => {
const prefix = nextPrefix();
const cache = createCache();
const storage = getStorage(provider, prefix) as IconStorageWithCache;
2020-04-28 09:47:35 +00:00
// Add one icon set
const icon: IconifyJSON = {
prefix: prefix,
icons: {
foo: {
body: '<g></g>',
},
},
};
2022-06-28 18:16:08 +00:00
const item: BrowserStorageItem = {
cached: Math.floor(Date.now() / browserStorageHour),
provider,
2020-04-28 09:47:35 +00:00
data: icon,
};
// Set cache
reset({
localStorage: cache,
});
// Check icon storage
expect(iconInStorage(storage, 'foo')).toBe(false);
expect(storage.lastModifiedCached).toBeUndefined();
2022-06-29 09:30:13 +00:00
// Counter should be 0
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
expect(getBrowserStorageItemsCount(getBrowserStorage('local')!)).toBe(
0
);
2020-04-28 09:47:35 +00:00
// Save item
2022-06-29 09:30:13 +00:00
storeInBrowserStorage(storage, icon);
2020-04-28 09:47:35 +00:00
// Storing in cache should not add item to storage
expect(iconInStorage(storage, 'foo')).toBe(false);
2020-04-28 09:47:35 +00:00
// lastModified is missing, so should not have updated
expect(storage.lastModifiedCached).toBeUndefined();
2020-04-28 09:47:35 +00:00
// Check data that should have been updated because storeCache()
// should call load function before first execution
2022-06-28 18:16:08 +00:00
expect(browserStorageConfig).toEqual({
2020-04-28 09:47:35 +00:00
local: true,
session: false,
});
2022-06-28 18:16:08 +00:00
expect(browserStorageEmptyItems).toEqual({
2022-06-29 09:30:13 +00:00
local: new Set(),
session: new Set(),
2020-04-28 09:47:35 +00:00
});
// Check cache
expect(cache.getItem(browserCachePrefix + '0')).toBe(
JSON.stringify(item)
);
expect(cache.getItem(browserCacheCountKey)).toBe('1');
expect(cache.getItem(browserCacheVersionKey)).toBe(browserCacheVersion);
2022-06-29 09:30:13 +00:00
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
expect(getBrowserStorageItemsCount(getBrowserStorage('local')!)).toBe(
1
);
2020-04-28 09:47:35 +00:00
});
it('Multiple icon sets', () => {
const prefix = nextPrefix();
const cache = createCache();
const storage = getStorage(provider, prefix) as IconStorageWithCache;
const lastModified = 12345;
2020-04-28 09:47:35 +00:00
// Add icon sets
const icon0: IconifyJSON = {
prefix: prefix,
lastModified,
2020-04-28 09:47:35 +00:00
icons: {
foo0: {
body: '<g></g>',
},
},
};
2022-06-28 18:16:08 +00:00
const item0: BrowserStorageItem = {
cached: Math.floor(Date.now() / browserStorageHour),
provider,
2020-04-28 09:47:35 +00:00
data: icon0,
};
const icon1: IconifyJSON = {
prefix: prefix,
lastModified,
2020-04-28 09:47:35 +00:00
icons: {
foo: {
body: '<g></g>',
},
},
};
2022-06-28 18:16:08 +00:00
const item1: BrowserStorageItem = {
cached: Math.floor(Date.now() / browserStorageHour),
provider,
2020-04-28 09:47:35 +00:00
data: icon1,
};
// Set cache
reset({
localStorage: cache,
});
2022-06-29 09:30:13 +00:00
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
expect(getBrowserStorageItemsCount(getBrowserStorage('local')!)).toBe(
0
);
expect(storage.lastModifiedCached).toBeUndefined();
2022-06-29 09:30:13 +00:00
2020-04-28 09:47:35 +00:00
// Save items
2022-06-29 09:30:13 +00:00
storeInBrowserStorage(storage, icon0);
storeInBrowserStorage(storage, icon1);
2020-04-28 09:47:35 +00:00
// Check data that should have been updated because storeCache()
// should call load function before first execution
2022-06-28 18:16:08 +00:00
expect(browserStorageConfig).toEqual({
2020-04-28 09:47:35 +00:00
local: true,
session: false,
});
2022-06-28 18:16:08 +00:00
expect(browserStorageEmptyItems).toEqual({
2022-06-29 09:30:13 +00:00
local: new Set(),
session: new Set(),
2020-04-28 09:47:35 +00:00
});
// lastModified should be set
expect(storage.lastModifiedCached).toBe(lastModified);
2022-06-29 09:30:13 +00:00
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
expect(getBrowserStorageItemsCount(getBrowserStorage('local')!)).toBe(
2
);
2020-04-28 09:47:35 +00:00
// Check cache
expect(cache.getItem(browserCachePrefix + '0')).toBe(
JSON.stringify(item0)
);
expect(cache.getItem(browserCachePrefix + '1')).toBe(
JSON.stringify(item1)
);
expect(cache.getItem(browserCacheCountKey)).toBe('2');
expect(cache.getItem(browserCacheVersionKey)).toBe(browserCacheVersion);
2020-04-28 09:47:35 +00:00
});
it('Multiple icon sets, first is outdated', () => {
const prefix = nextPrefix();
const cache = createCache();
const storage = getStorage(provider, prefix) as IconStorageWithCache;
const lastModified1 = 1234;
const lastModified2 = 12345;
// Add icon sets
const icon0: IconifyJSON = {
prefix: prefix,
lastModified: lastModified1,
icons: {
foo0: {
body: '<g></g>',
},
},
};
// lastModified is newer than first entry: first entry should be deleted
const icon1: IconifyJSON = {
prefix: prefix,
lastModified: lastModified2,
icons: {
foo: {
body: '<g></g>',
},
},
};
const item1: BrowserStorageItem = {
cached: Math.floor(Date.now() / browserStorageHour),
provider,
data: icon1,
};
// Set cache
reset({
localStorage: cache,
});
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
expect(getBrowserStorageItemsCount(getBrowserStorage('local')!)).toBe(
0
);
expect(storage.lastModifiedCached).toBeUndefined();
// Save items
storeInBrowserStorage(storage, icon0);
storeInBrowserStorage(storage, icon1);
// Check data that should have been updated because storeCache()
// should call load function before first execution
expect(browserStorageConfig).toEqual({
local: true,
session: false,
});
expect(browserStorageEmptyItems).toEqual({
local: new Set(),
session: new Set(),
});
// lastModified should be set to max value
expect(storage.lastModifiedCached).toBe(lastModified2);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
expect(getBrowserStorageItemsCount(getBrowserStorage('local')!)).toBe(
1
);
// Check cache
expect(cache.getItem(browserCachePrefix + '0')).toBe(
// Second item!
JSON.stringify(item1)
);
expect(cache.getItem(browserCachePrefix + '1')).toBeFalsy();
expect(cache.getItem(browserCacheCountKey)).toBe('1');
expect(cache.getItem(browserCacheVersionKey)).toBe(browserCacheVersion);
});
it('Multiple icon sets, second set is outdated', () => {
const prefix = nextPrefix();
const cache = createCache();
const storage = getStorage(provider, prefix) as IconStorageWithCache;
const lastModified1 = 12345;
const lastModified2 = 1234;
// Add icon sets
const icon0: IconifyJSON = {
prefix: prefix,
lastModified: lastModified1,
icons: {
foo0: {
body: '<g></g>',
},
},
};
const item0: BrowserStorageItem = {
cached: Math.floor(Date.now() / browserStorageHour),
provider,
data: icon0,
};
// Icon set with lastModified lower than previous entry should not be stored
const icon1: IconifyJSON = {
prefix: prefix,
lastModified: lastModified2,
icons: {
foo: {
body: '<g></g>',
},
},
};
// Set cache
reset({
localStorage: cache,
});
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
expect(getBrowserStorageItemsCount(getBrowserStorage('local')!)).toBe(
0
);
expect(storage.lastModifiedCached).toBeUndefined();
// Save items
storeInBrowserStorage(storage, icon0);
storeInBrowserStorage(storage, icon1);
// Check data that should have been updated because storeCache()
// should call load function before first execution
expect(browserStorageConfig).toEqual({
local: true,
session: false,
});
expect(browserStorageEmptyItems).toEqual({
local: new Set(),
session: new Set(),
});
// lastModified should be set to maximum value
expect(storage.lastModifiedCached).toBe(lastModified1);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
expect(getBrowserStorageItemsCount(getBrowserStorage('local')!)).toBe(
1
);
// Check cache
expect(cache.getItem(browserCachePrefix + '0')).toBe(
JSON.stringify(item0)
);
expect(cache.getItem(browserCachePrefix + '1')).toBeFalsy();
expect(cache.getItem(browserCacheCountKey)).toBe('1');
expect(cache.getItem(browserCacheVersionKey)).toBe(browserCacheVersion);
});
2020-04-28 09:47:35 +00:00
it('Adding icon set on unused spot', () => {
const prefix = nextPrefix();
const cache = createCache();
2022-06-29 09:30:13 +00:00
const storage = getStorage(provider, prefix);
2020-04-28 09:47:35 +00:00
// Add icon sets
const icon0: IconifyJSON = {
prefix: prefix,
icons: {
foo0: {
body: '<g></g>',
},
},
};
2022-06-28 18:16:08 +00:00
const item0: BrowserStorageItem = {
cached: Math.floor(Date.now() / browserStorageHour),
provider,
2020-04-28 09:47:35 +00:00
data: icon0,
};
const icon1: IconifyJSON = {
prefix: prefix,
icons: {
foo: {
body: '<g></g>',
},
},
};
2022-06-28 18:16:08 +00:00
const item1: BrowserStorageItem = {
cached: Math.floor(Date.now() / browserStorageHour),
provider,
2020-04-28 09:47:35 +00:00
data: icon1,
};
// Add item
cache.setItem(browserCacheVersionKey, browserCacheVersion);
cache.setItem(browserCacheCountKey, '2');
cache.setItem(browserCachePrefix + '1', JSON.stringify(item1));
2020-04-28 09:47:35 +00:00
// Set cache
reset({
localStorage: cache,
});
// Load data
2022-06-28 20:40:20 +00:00
initBrowserStorage();
2020-04-28 09:47:35 +00:00
// Check data
2022-06-28 18:16:08 +00:00
expect(browserStorageConfig).toEqual({
2020-04-28 09:47:35 +00:00
local: true,
session: false,
});
2022-06-28 18:16:08 +00:00
expect(browserStorageEmptyItems).toEqual({
2022-06-29 09:30:13 +00:00
local: new Set([0]),
session: new Set(),
2020-04-28 09:47:35 +00:00
});
2022-06-29 09:30:13 +00:00
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
expect(getBrowserStorageItemsCount(getBrowserStorage('local')!)).toBe(
2
);
2020-04-28 09:47:35 +00:00
// Save items
2022-06-29 09:30:13 +00:00
storeInBrowserStorage(storage, icon0);
2020-04-28 09:47:35 +00:00
// Check data
2022-06-28 18:16:08 +00:00
expect(browserStorageEmptyItems).toEqual({
2022-06-29 09:30:13 +00:00
local: new Set(),
session: new Set(),
2020-04-28 09:47:35 +00:00
});
2022-06-29 09:30:13 +00:00
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
expect(getBrowserStorageItemsCount(getBrowserStorage('local')!)).toBe(
2
);
2020-04-28 09:47:35 +00:00
// Check cache
expect(cache.getItem(browserCachePrefix + '0')).toBe(
JSON.stringify(item0)
);
expect(cache.getItem(browserCachePrefix + '1')).toBe(
JSON.stringify(item1)
);
expect(cache.getItem(browserCacheCountKey)).toBe('2');
expect(cache.getItem(browserCacheVersionKey)).toBe(browserCacheVersion);
2020-04-28 09:47:35 +00:00
});
it('Adding multiple icon sets to existing data', () => {
const prefix = nextPrefix();
const cache = createCache();
2022-06-29 09:30:13 +00:00
const storage = getStorage(provider, prefix);
2020-04-28 09:47:35 +00:00
// Add icon sets
const icons: IconifyJSON[] = [];
2022-06-28 18:16:08 +00:00
const items: BrowserStorageItem[] = [];
2020-04-28 09:47:35 +00:00
for (let i = 0; i < 12; i++) {
const icon: IconifyJSON = {
prefix: prefix,
icons: {
2022-03-16 21:30:16 +00:00
['foo' + i.toString()]: {
2020-04-28 09:47:35 +00:00
body: '<g></g>',
},
},
};
2022-06-28 18:16:08 +00:00
const item: BrowserStorageItem = {
cached: Math.floor(Date.now() / browserStorageHour),
provider,
2020-04-28 09:47:35 +00:00
data: icon,
};
// Make items 2 and 4 expire
if (i === 2 || i === 4) {
2022-06-28 18:16:08 +00:00
item.cached -= browserStorageCacheExpiration + 1;
2020-04-28 09:47:35 +00:00
}
// Change expiration for items 6 and 8 to almost expire
if (i === 6 || i === 8) {
2022-06-28 18:16:08 +00:00
item.cached -= browserStorageCacheExpiration - 1;
2020-04-28 09:47:35 +00:00
}
icons.push(icon);
items.push(item);
// Skip items 1, 5, 9+
if (i !== 1 && i !== 5 && i < 9) {
cache.setItem(
browserCachePrefix + i.toString(),
JSON.stringify(item)
);
2020-04-28 09:47:35 +00:00
}
}
cache.setItem(browserCacheVersionKey, browserCacheVersion);
cache.setItem(browserCacheCountKey, '10');
2020-04-28 09:47:35 +00:00
// Set cache
reset({
sessionStorage: cache,
});
// Load data
2022-06-28 20:40:20 +00:00
initBrowserStorage();
2020-04-28 09:47:35 +00:00
// Check data
2022-06-28 18:16:08 +00:00
expect(browserStorageConfig).toEqual({
2020-04-28 09:47:35 +00:00
local: false,
session: true,
});
2022-06-29 09:30:13 +00:00
// Counter should have changed to 9 after validation because last item is missing
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
expect(getBrowserStorageItemsCount(getBrowserStorage('session')!)).toBe(
9
);
2022-06-28 18:16:08 +00:00
expect(browserStorageEmptyItems).toEqual({
2022-06-29 09:30:13 +00:00
local: new Set(),
2020-04-28 09:47:35 +00:00
// mix of expired and skipped items
// reverse order, 9 should not be there because it is last item
2022-06-29 09:30:13 +00:00
session: new Set([5, 4, 2, 1]),
2020-04-28 09:47:35 +00:00
});
expect(cache.getItem(browserCacheCountKey)).toBe('9');
2020-04-28 09:47:35 +00:00
// Check cached items
[0, 3, 6, 7, 8].forEach((index) => {
expect(cache.getItem(browserCachePrefix + index.toString())).toBe(
JSON.stringify(items[index])
2020-04-28 09:47:35 +00:00
);
});
// Check expired items - should have been deleted
// Also check items that weren't supposed to be added
[2, 4, 1, 5, 9, 10, 11, 12, 13].forEach((index) => {
expect(
cache.getItem(browserCachePrefix + index.toString())
).toBeNull();
2020-04-28 09:47:35 +00:00
});
// Add item 5
2022-06-29 09:30:13 +00:00
storeInBrowserStorage(storage, icons[5]);
2022-06-28 18:16:08 +00:00
expect(browserStorageEmptyItems).toEqual({
2022-06-29 09:30:13 +00:00
local: new Set(),
session: new Set([4, 2, 1]),
2020-04-28 09:47:35 +00:00
});
expect(cache.getItem(browserCacheCountKey)).toBe('9');
2020-04-28 09:47:35 +00:00
// Add items 4, 2, 1
const list = [4, 2, 1];
list.slice(0).forEach((index) => {
expect(list.shift()).toBe(index);
2022-06-29 09:30:13 +00:00
storeInBrowserStorage(storage, icons[index]);
2022-06-28 18:16:08 +00:00
expect(browserStorageEmptyItems).toEqual({
2022-06-29 09:30:13 +00:00
local: new Set(),
session: new Set(list),
2020-04-28 09:47:35 +00:00
});
expect(cache.getItem(browserCacheCountKey)).toBe('9');
2020-04-28 09:47:35 +00:00
});
// Add item 10
2022-06-29 09:30:13 +00:00
storeInBrowserStorage(storage, icons[10]);
2022-06-28 18:16:08 +00:00
expect(browserStorageEmptyItems).toEqual({
2022-06-29 09:30:13 +00:00
local: new Set(),
session: new Set(),
2020-04-28 09:47:35 +00:00
});
expect(cache.getItem(browserCacheCountKey)).toBe('10');
2020-04-28 09:47:35 +00:00
// Add item 11
2022-06-29 09:30:13 +00:00
storeInBrowserStorage(storage, icons[11]);
2022-06-28 18:16:08 +00:00
expect(browserStorageEmptyItems).toEqual({
2022-06-29 09:30:13 +00:00
local: new Set(),
session: new Set(),
2020-04-28 09:47:35 +00:00
});
expect(cache.getItem(browserCacheCountKey)).toBe('11');
2020-04-28 09:47:35 +00:00
});
it('Overwrite outdated data', () => {
const prefix = nextPrefix();
const cache = createCache();
2022-06-29 09:30:13 +00:00
const storage = getStorage(provider, prefix);
2020-04-28 09:47:35 +00:00
// Add data in old format
cache.setItem(browserCacheVersionKey, '1.0.6');
cache.setItem(browserCacheCountKey, '3');
2020-04-28 09:47:35 +00:00
for (let i = 0; i < 3; i++) {
cache.setItem(
browserCachePrefix + i.toString(),
2020-04-28 09:47:35 +00:00
JSON.stringify({
prefix: prefix,
icons: {
2022-03-16 21:30:16 +00:00
['foo' + i.toString()]: {
2020-04-28 09:47:35 +00:00
body: '<g></g>',
},
},
})
);
}
// Set cache
reset({
localStorage: cache,
});
// Check icon storage
expect(iconInStorage(storage, 'foo1')).toBe(false);
2020-04-28 09:47:35 +00:00
// Load cache
2022-06-28 20:40:20 +00:00
initBrowserStorage();
2020-04-28 09:47:35 +00:00
2022-06-28 18:16:08 +00:00
expect(browserStorageConfig).toEqual({
2020-04-28 09:47:35 +00:00
local: true,
session: false,
});
2022-06-28 18:16:08 +00:00
expect(browserStorageEmptyItems).toEqual({
2022-06-29 09:30:13 +00:00
local: new Set(),
session: new Set(),
2020-04-28 09:47:35 +00:00
});
// Add one icon set
const icon: IconifyJSON = {
prefix: prefix,
icons: {
foo: {
body: '<g></g>',
},
},
};
2022-06-28 18:16:08 +00:00
const item: BrowserStorageItem = {
cached: Math.floor(Date.now() / browserStorageHour),
provider,
2020-04-28 09:47:35 +00:00
data: icon,
};
// Save item
2022-06-29 09:30:13 +00:00
storeInBrowserStorage(storage, icon);
2020-04-28 09:47:35 +00:00
// Storing in cache should not add item to storage
expect(iconInStorage(storage, 'foo')).toBe(false);
2020-04-28 09:47:35 +00:00
// Check data that should have been updated because storeCache()
// should call load function before first execution
2022-06-28 18:16:08 +00:00
expect(browserStorageConfig).toEqual({
2020-04-28 09:47:35 +00:00
local: true,
session: false,
});
2022-06-28 18:16:08 +00:00
expect(browserStorageEmptyItems).toEqual({
2022-06-29 09:30:13 +00:00
local: new Set(),
session: new Set(),
2020-04-28 09:47:35 +00:00
});
// Check cache
expect(cache.getItem(browserCachePrefix + '0')).toBe(
JSON.stringify(item)
);
expect(cache.getItem(browserCacheCountKey)).toBe('1');
expect(cache.getItem(browserCacheVersionKey)).toBe(browserCacheVersion);
2020-04-28 09:47:35 +00:00
});
it('Using both storage options', () => {
const prefix = nextPrefix();
const cache1 = createCache();
const cache2 = createCache();
2022-06-29 09:30:13 +00:00
const storage = getStorage(provider, prefix);
2020-04-28 09:47:35 +00:00
// Add icon sets to localStorage
cache1.setItem(browserCacheVersionKey, browserCacheVersion);
cache1.setItem(browserCacheCountKey, '3');
2020-04-28 09:47:35 +00:00
[0, 1, 2].forEach((index) => {
const icon: IconifyJSON = {
prefix: prefix,
icons: {
2022-03-16 21:30:16 +00:00
['foo' + index.toString()]: {
2020-04-28 09:47:35 +00:00
body: '<g></g>',
},
},
};
2022-06-28 18:16:08 +00:00
const item: BrowserStorageItem = {
cached: Math.floor(Date.now() / browserStorageHour),
provider,
2020-04-28 09:47:35 +00:00
data: icon,
};
2022-03-16 21:30:16 +00:00
cache1.setItem(
browserCachePrefix + index.toString(),
2022-03-16 21:30:16 +00:00
JSON.stringify(item)
);
2020-04-28 09:47:35 +00:00
});
// Add icon sets to sessionStorage
cache2.setItem(browserCacheVersionKey, browserCacheVersion);
cache2.setItem(browserCacheCountKey, '4');
2020-04-28 09:47:35 +00:00
[0, 1, 2, 3].forEach((index) => {
const icon: IconifyJSON = {
prefix: prefix,
icons: {
2022-03-16 21:30:16 +00:00
['bar' + index.toString()]: {
2020-04-28 09:47:35 +00:00
body: '<g></g>',
},
},
};
2022-06-28 18:16:08 +00:00
const item: BrowserStorageItem = {
cached: Math.floor(Date.now() / browserStorageHour),
provider,
2020-04-28 09:47:35 +00:00
data: icon,
};
2022-03-16 21:30:16 +00:00
cache2.setItem(
browserCachePrefix + index.toString(),
2022-03-16 21:30:16 +00:00
JSON.stringify(item)
);
2020-04-28 09:47:35 +00:00
});
// Set cache
reset({
localStorage: cache1,
sessionStorage: cache2,
});
// Load data
2022-06-28 20:40:20 +00:00
initBrowserStorage();
2020-04-28 09:47:35 +00:00
// Check data
2022-06-28 18:16:08 +00:00
expect(browserStorageConfig).toEqual({
2020-04-28 09:47:35 +00:00
local: true,
session: true,
});
2022-06-29 09:30:13 +00:00
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
expect(getBrowserStorageItemsCount(getBrowserStorage('local')!)).toBe(
3
);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
expect(getBrowserStorageItemsCount(getBrowserStorage('session')!)).toBe(
4
);
2022-06-28 18:16:08 +00:00
expect(browserStorageEmptyItems).toEqual({
2022-06-29 09:30:13 +00:00
local: new Set(),
session: new Set(),
2020-04-28 09:47:35 +00:00
});
// Check icon storage
2022-06-29 09:30:13 +00:00
for (let i = 0; i < 3; i++) {
expect(iconInStorage(storage, 'foo' + i.toString())).toBe(true);
2020-04-28 09:47:35 +00:00
}
2022-06-29 09:30:13 +00:00
for (let i = 0; i < 4; i++) {
expect(iconInStorage(storage, 'bar' + i.toString())).toBe(true);
2020-04-28 09:47:35 +00:00
}
// Add new item to localStorage
const icon: IconifyJSON = {
prefix: prefix,
icons: {
'new-icon': {
body: '<g></g>',
},
},
};
2022-06-28 18:16:08 +00:00
const item: BrowserStorageItem = {
cached: Math.floor(Date.now() / browserStorageHour),
provider,
2020-04-28 09:47:35 +00:00
data: icon,
};
2022-06-29 09:30:13 +00:00
storeInBrowserStorage(storage, icon);
2020-04-28 09:47:35 +00:00
// Check data
2022-06-28 18:16:08 +00:00
expect(browserStorageEmptyItems).toEqual({
2022-06-29 09:30:13 +00:00
local: new Set(),
session: new Set(),
2020-04-28 09:47:35 +00:00
});
2022-06-29 09:30:13 +00:00
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
expect(getBrowserStorageItemsCount(getBrowserStorage('local')!)).toBe(
4 // +1
);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
expect(getBrowserStorageItemsCount(getBrowserStorage('session')!)).toBe(
4
);
2020-04-28 09:47:35 +00:00
// Check cache
expect(cache1.getItem(browserCachePrefix + '3')).toBe(
JSON.stringify(item)
);
2020-04-28 09:47:35 +00:00
});
it('Using both storage options, but localStorage is read only', () => {
const prefix = nextPrefix();
const cache1 = createCache();
const cache2 = createCache();
2022-06-29 09:30:13 +00:00
const storage = getStorage(provider, prefix);
2020-04-28 09:47:35 +00:00
// Add icon sets to localStorage
cache1.setItem(browserCacheVersionKey, browserCacheVersion);
cache1.setItem(browserCacheCountKey, '3');
2020-04-28 09:47:35 +00:00
[0, 1, 2].forEach((index) => {
const icon: IconifyJSON = {
prefix: prefix,
icons: {
2022-03-16 21:30:16 +00:00
['foo' + index.toString()]: {
2020-04-28 09:47:35 +00:00
body: '<g></g>',
},
},
};
2022-06-28 18:16:08 +00:00
const item: BrowserStorageItem = {
cached: Math.floor(Date.now() / browserStorageHour),
provider,
2020-04-28 09:47:35 +00:00
data: icon,
};
2022-03-16 21:30:16 +00:00
cache1.setItem(
browserCachePrefix + index.toString(),
2022-03-16 21:30:16 +00:00
JSON.stringify(item)
);
2020-04-28 09:47:35 +00:00
});
// Add icon sets to sessionStorage
cache2.setItem(browserCacheVersionKey, browserCacheVersion);
cache2.setItem(browserCacheCountKey, '4');
2020-04-28 09:47:35 +00:00
[0, 1, 2, 3].forEach((index) => {
const icon: IconifyJSON = {
prefix: prefix,
icons: {
2022-03-16 21:30:16 +00:00
['bar' + index.toString()]: {
2020-04-28 09:47:35 +00:00
body: '<g></g>',
},
},
};
2022-06-28 18:16:08 +00:00
const item: BrowserStorageItem = {
cached: Math.floor(Date.now() / browserStorageHour),
provider,
2020-04-28 09:47:35 +00:00
data: icon,
};
2022-03-16 21:30:16 +00:00
cache2.setItem(
browserCachePrefix + index.toString(),
2022-03-16 21:30:16 +00:00
JSON.stringify(item)
);
2020-04-28 09:47:35 +00:00
});
// Set cache
reset({
localStorage: cache1,
sessionStorage: cache2,
});
// Load data
2022-06-28 20:40:20 +00:00
initBrowserStorage();
2020-04-28 09:47:35 +00:00
// Check data
2022-06-28 18:16:08 +00:00
expect(browserStorageConfig).toEqual({
2020-04-28 09:47:35 +00:00
local: true,
session: true,
});
2022-06-28 18:16:08 +00:00
expect(browserStorageEmptyItems).toEqual({
2022-06-29 09:30:13 +00:00
local: new Set(),
session: new Set(),
2020-04-28 09:47:35 +00:00
});
2022-06-29 09:30:13 +00:00
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
expect(getBrowserStorageItemsCount(getBrowserStorage('local')!)).toBe(
3
);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
expect(getBrowserStorageItemsCount(getBrowserStorage('session')!)).toBe(
4
);
2020-04-28 09:47:35 +00:00
// Check icon storage
2022-06-29 09:30:13 +00:00
for (let i = 0; i < 3; i++) {
expect(iconInStorage(storage, 'foo' + i.toString())).toBe(true);
2020-04-28 09:47:35 +00:00
}
2022-06-29 09:30:13 +00:00
for (let i = 0; i < 4; i++) {
expect(iconInStorage(storage, 'bar' + i.toString())).toBe(true);
2020-04-28 09:47:35 +00:00
}
// Set localStorage to read-only
cache1.canWrite = false;
// Add new item to localStorage
const icon: IconifyJSON = {
prefix: prefix,
icons: {
'new-icon': {
body: '<g></g>',
},
},
};
2022-06-28 18:16:08 +00:00
const item: BrowserStorageItem = {
cached: Math.floor(Date.now() / browserStorageHour),
provider,
2020-04-28 09:47:35 +00:00
data: icon,
};
2022-06-29 09:30:13 +00:00
storeInBrowserStorage(storage, icon);
2020-04-28 09:47:35 +00:00
// Check data
2022-06-28 18:16:08 +00:00
expect(browserStorageEmptyItems).toEqual({
2022-06-29 09:30:13 +00:00
local: new Set(),
session: new Set(),
2020-04-28 09:47:35 +00:00
});
2022-06-29 09:30:13 +00:00
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
expect(getBrowserStorageItemsCount(getBrowserStorage('local')!)).toBe(
3
);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
expect(getBrowserStorageItemsCount(getBrowserStorage('session')!)).toBe(
5 // +1
);
2020-04-28 09:47:35 +00:00
// Check cache
expect(cache2.getItem(browserCachePrefix + '4')).toBe(
JSON.stringify(item)
);
2020-04-28 09:47:35 +00:00
});
});