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

556 lines
13 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';
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';
import { getStoredItem } from '../../lib/browser-storage/item';
2020-04-28 09:47:35 +00:00
describe('Testing loading from localStorage', () => {
const provider = '';
2020-04-28 09:47:35 +00:00
it('Valid 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
cache.setItem(browserCacheVersionKey, browserCacheVersion);
cache.setItem(browserCacheCountKey, '1');
2020-04-28 09:47:35 +00:00
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: {
prefix: prefix,
icons: {
foo: {
body: '<g></g>',
},
},
},
};
cache.setItem(browserCachePrefix + '0', JSON.stringify(item));
2020-04-28 09:47:35 +00:00
// Set cache
reset({
localStorage: cache,
});
2022-06-29 09:30:13 +00:00
// Only locaStorage should be available
expect(getBrowserStorage('local')).toBeDefined();
expect(getBrowserStorage('session')).toBeUndefined();
// 1 icon
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
expect(getBrowserStorageItemsCount(getBrowserStorage('local')!)).toBe(
1
);
2020-04-28 09:47:35 +00:00
// Check icon storage
expect(iconInStorage(storage, 'foo')).toBe(false);
expect(storage.lastModifiedCached).toBeUndefined();
2020-04-28 09:47:35 +00:00
// Load localStorage
2022-06-28 20:40:20 +00:00
initBrowserStorage();
2020-04-28 09:47:35 +00:00
// Icon should exist now and lastModified should be set
expect(iconInStorage(storage, 'foo')).toBe(true);
expect(storage.lastModifiedCached).toBe(-1);
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(),
session: new Set(),
2020-04-28 09:47:35 +00:00
});
});
it('Different provider', () => {
const provider = nextPrefix();
const prefix = nextPrefix();
const cache = createCache();
const storage = getStorage(provider, prefix) as IconStorageWithCache;
const defaultStorage = getStorage('', prefix) as IconStorageWithCache;
const lastModified = 12345;
// Add one icon set
cache.setItem(browserCacheVersionKey, browserCacheVersion);
cache.setItem(browserCacheCountKey, '1');
2022-06-28 18:16:08 +00:00
const item: BrowserStorageItem = {
cached: Math.floor(Date.now() / browserStorageHour),
provider,
data: {
prefix: prefix,
lastModified,
icons: {
foo: {
body: '<g></g>',
},
},
},
};
cache.setItem(browserCachePrefix + '0', JSON.stringify(item));
expect(getStoredItem(cache, browserCachePrefix + '0')).toBeTruthy();
// Set cache
reset({
localStorage: cache,
});
// Check icon storage
expect(iconInStorage(storage, 'foo')).toBe(false);
expect(storage.lastModifiedCached).toBeUndefined();
// Check default provider
expect(iconInStorage(defaultStorage, 'foo')).toBe(false);
expect(defaultStorage.lastModifiedCached).toBeUndefined();
// Load localStorage
2022-06-28 20:40:20 +00:00
initBrowserStorage();
// Icon should exist now
expect(iconInStorage(storage, 'foo')).toBe(true);
expect(iconInStorage(defaultStorage, 'foo')).toBe(false);
// Check data
2022-06-28 18:16:08 +00:00
expect(browserStorageConfig).toEqual({
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(),
});
expect(storage.lastModifiedCached).toBe(lastModified);
expect(defaultStorage.lastModifiedCached).toBeUndefined();
});
2020-04-28 09:47:35 +00:00
it('Expired icon set', () => {
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 one icon set
cache.setItem(browserCacheVersionKey, browserCacheVersion);
cache.setItem(browserCacheCountKey, '1');
2020-04-28 09:47:35 +00:00
2022-06-28 18:16:08 +00:00
const item: BrowserStorageItem = {
2020-04-28 09:47:35 +00:00
// Expiration date
2022-06-28 18:16:08 +00:00
cached:
Math.floor(Date.now() / browserStorageHour) -
browserStorageCacheExpiration -
1,
provider,
2020-04-28 09:47:35 +00:00
data: {
prefix: prefix,
lastModified,
2020-04-28 09:47:35 +00:00
icons: {
foo: {
body: '<g></g>',
},
},
},
};
cache.setItem(browserCachePrefix + '0', JSON.stringify(item));
expect(getStoredItem(cache, browserCachePrefix + '0')).toBeTruthy();
2020-04-28 09:47:35 +00:00
// Set cache
reset({
localStorage: cache,
});
2022-06-29 09:30:13 +00:00
// Counter should be 1 before parsing it
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
expect(getBrowserStorageItemsCount(getBrowserStorage('local')!)).toBe(
1
);
2020-04-28 09:47:35 +00:00
// Check icon storage
expect(iconInStorage(storage, 'foo')).toBe(false);
2020-04-28 09:47:35 +00:00
// Load localStorage
2022-06-28 20:40:20 +00:00
initBrowserStorage();
2020-04-28 09:47:35 +00:00
// Icon should not have loaded
expect(iconInStorage(storage, 'foo')).toBe(false);
expect(storage.lastModifiedCached).toBeUndefined();
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(),
session: new Set(),
2020-04-28 09:47:35 +00:00
});
2022-06-29 09:30:13 +00:00
// Counter should have changed to 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
});
it('Bad icon set', () => {
const prefix = nextPrefix();
const cache = createCache();
const storage = getStorage(provider, prefix);
2020-04-28 09:47:35 +00:00
// Add one icon set
cache.setItem(browserCacheVersionKey, browserCacheVersion);
cache.setItem(browserCacheCountKey, '1');
2020-04-28 09:47:35 +00:00
cache.setItem(
browserCachePrefix + '0',
2020-04-28 09:47:35 +00:00
JSON.stringify({
2022-06-28 18:16:08 +00:00
cached: Math.floor(Date.now() / browserStorageHour),
provider,
2020-04-28 09:47:35 +00:00
data: {
prefix: prefix,
icons: {
foo: {
// Missing 'body' property
width: 20,
},
},
},
})
);
expect(getStoredItem(cache, browserCachePrefix + '0')).toBeTruthy();
2020-04-28 09:47:35 +00:00
// Set cache
reset({
localStorage: cache,
});
2022-06-29 09:30:13 +00:00
// Counter should be 1 before parsing it
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
expect(getBrowserStorageItemsCount(getBrowserStorage('local')!)).toBe(
1
);
2020-04-28 09:47:35 +00:00
// Check icon storage
expect(iconInStorage(storage, 'foo')).toBe(false);
2020-04-28 09:47:35 +00:00
// Load localStorage
2022-06-28 20:40:20 +00:00
initBrowserStorage();
2020-04-28 09:47:35 +00:00
// Icon should not have loaded
expect(iconInStorage(storage, 'foo')).toBe(false);
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(),
session: new Set(),
2020-04-28 09:47:35 +00:00
});
2022-06-29 09:30:13 +00:00
// Counter should have changed to 0
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
expect(getBrowserStorageItemsCount(getBrowserStorage('local')!)).toBe(
0
);
// Item should have been deleted
expect(getStoredItem(cache, browserCachePrefix + '0')).toBeFalsy();
2020-04-28 09:47:35 +00:00
});
it('Wrong counter', () => {
const prefix = nextPrefix();
const cache = createCache();
const storage = getStorage(provider, prefix);
2020-04-28 09:47:35 +00:00
// Add one icon set
cache.setItem(browserCacheVersionKey, browserCacheVersion);
cache.setItem(browserCacheCountKey, '0'); // Should be at least "1"
2020-04-28 09:47:35 +00:00
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: {
prefix: prefix,
icons: {
foo: {
body: '<g></g>',
},
},
},
};
cache.setItem(browserCachePrefix + '0', JSON.stringify(item));
2020-04-28 09:47:35 +00:00
// Set cache
reset({
localStorage: cache,
});
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
// Check icon storage
expect(iconInStorage(storage, 'foo')).toBe(false);
2020-04-28 09:47:35 +00:00
// Load localStorage
2022-06-28 20:40:20 +00:00
initBrowserStorage();
2020-04-28 09:47:35 +00:00
// Icon should not have loaded
expect(iconInStorage(storage, 'foo')).toBe(false);
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(),
session: new Set(),
2020-04-28 09:47:35 +00:00
});
});
it('Missing entries at the end', () => {
const prefix = nextPrefix();
const cache = createCache();
const storage = getStorage(provider, prefix);
2020-04-28 09:47:35 +00:00
// Add one icon set
cache.setItem(browserCacheVersionKey, browserCacheVersion);
cache.setItem(browserCacheCountKey, '5');
2020-04-28 09:47:35 +00:00
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: {
prefix: prefix,
icons: {
foo: {
body: '<g></g>',
},
},
},
};
cache.setItem(browserCachePrefix + '0', JSON.stringify(item));
2020-04-28 09:47:35 +00:00
// Set cache
reset({
localStorage: cache,
});
2022-06-29 09:30:13 +00:00
// Counter should be 5 before validation
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
expect(getBrowserStorageItemsCount(getBrowserStorage('local')!)).toBe(
5
);
2020-04-28 09:47:35 +00:00
// Check icon storage
expect(iconInStorage(storage, 'foo')).toBe(false);
2020-04-28 09:47:35 +00:00
// Load localStorage
2022-06-28 20:40:20 +00:00
initBrowserStorage();
2020-04-28 09:47:35 +00:00
// Icon should exist now
expect(iconInStorage(storage, 'foo')).toBe(true);
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(),
session: new Set(),
2020-04-28 09:47:35 +00:00
});
2022-06-29 09:30:13 +00:00
// Counter should be 1 after validation
// 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('Missing entries', () => {
const prefix = nextPrefix();
const cache = createCache();
const storage = getStorage(provider, prefix);
2020-04-28 09:47:35 +00:00
// Add two icon sets
cache.setItem(browserCacheVersionKey, browserCacheVersion);
cache.setItem(browserCacheCountKey, '5');
2020-04-28 09:47:35 +00:00
// Missing: 0, 2, 3
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: {
prefix: prefix,
icons: {
foo1: {
body: '<g></g>',
},
},
},
};
2022-06-28 18:16:08 +00:00
const item4: BrowserStorageItem = {
cached: Math.floor(Date.now() / browserStorageHour),
provider,
2020-04-28 09:47:35 +00:00
data: {
prefix: prefix,
icons: {
foo4: {
body: '<g></g>',
},
},
},
};
cache.setItem(browserCachePrefix + '1', JSON.stringify(item1));
cache.setItem(browserCachePrefix + '4', JSON.stringify(item4));
2020-04-28 09:47:35 +00:00
// Set cache
reset({
localStorage: cache,
});
2022-06-29 09:30:13 +00:00
// Counter should be 5 before validation
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
expect(getBrowserStorageItemsCount(getBrowserStorage('local')!)).toBe(
5
);
2020-04-28 09:47:35 +00:00
// Check icon storage
expect(iconInStorage(storage, 'foo1')).toBe(false);
expect(iconInStorage(storage, 'foo4')).toBe(false);
2020-04-28 09:47:35 +00:00
// Load localStorage
2022-06-28 20:40:20 +00:00
initBrowserStorage();
2020-04-28 09:47:35 +00:00
// Icons should exist now
expect(iconInStorage(storage, 'foo1')).toBe(true);
expect(iconInStorage(storage, 'foo4')).toBe(true);
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([3, 2, 0]), // reserse order
session: new Set(),
2020-04-28 09:47:35 +00:00
});
2022-06-29 09:30:13 +00:00
// Counter should be 5 after validation
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
expect(getBrowserStorageItemsCount(getBrowserStorage('local')!)).toBe(
5
);
2020-04-28 09:47:35 +00:00
});
it('Using both storage options', () => {
const prefix = nextPrefix();
const cache1 = createCache();
const cache2 = createCache();
const storage = getStorage(provider, prefix);
2020-04-28 09:47:35 +00:00
// Add few icon sets
cache1.setItem(browserCacheVersionKey, browserCacheVersion);
cache2.setItem(browserCacheVersionKey, browserCacheVersion);
2020-04-28 09:47:35 +00:00
cache1.setItem(browserCacheCountKey, '6');
cache2.setItem(browserCacheCountKey, '3');
2020-04-28 09:47:35 +00:00
// Create 5 items
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 < 6; 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,
};
icons.push(icon);
items.push(item);
}
// Add items 1,3,5 to localStorage
[1, 3, 5].forEach((index) => {
2022-03-16 21:30:16 +00:00
cache1.setItem(
browserCachePrefix + index.toString(),
2022-03-16 21:30:16 +00:00
JSON.stringify(items[index])
);
2020-04-28 09:47:35 +00:00
});
// Add items 0 and 2 to sessionStorage
[0, 2].forEach((index) => {
2022-03-16 21:30:16 +00:00
cache2.setItem(
browserCachePrefix + index.toString(),
2022-03-16 21:30:16 +00:00
JSON.stringify(items[index])
);
2020-04-28 09:47:35 +00:00
});
// Set cache
reset({
localStorage: cache1,
sessionStorage: cache2,
});
// Check icon storage
for (let i = 0; i < 6; i++) {
expect(iconInStorage(storage, 'foo' + i.toString())).toBe(false);
2020-04-28 09:47:35 +00:00
}
// Load localStorage
2022-06-28 20:40:20 +00:00
initBrowserStorage();
2020-04-28 09:47:35 +00:00
// Icons should exist now, except for number 4
for (let i = 0; i < 6; i++) {
expect(iconInStorage(storage, 'foo' + i.toString())).toBe(i !== 4);
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([4, 2, 0]),
session: new Set([1]),
2020-04-28 09:47:35 +00:00
});
});
});