2
0
mirror of https://github.com/iconify/iconify.git synced 2024-10-23 17:12:03 +00:00
iconify/packages/core/tests/cache/loading-test.ts

536 lines
12 KiB
TypeScript
Raw Normal View History

2020-12-25 19:03:15 +00:00
import type { IconifyJSON } from '@iconify/types';
2022-06-28 18:16:08 +00:00
import type { BrowserStorageItem } 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';
2022-06-28 18:16:08 +00:00
import { getStorage, iconExists } from '../../lib/storage/storage';
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 loading from localStorage', () => {
const provider = '';
2020-04-28 09:47:35 +00:00
it('Valid icon set', () => {
const prefix = nextPrefix();
const cache = createCache();
// 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
const icons = getStorage(provider, prefix);
expect(iconExists(icons, '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(iconExists(icons, '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
});
});
it('Different provider', () => {
const provider = nextPrefix();
const prefix = nextPrefix();
const cache = createCache();
// 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,
icons: {
foo: {
body: '<g></g>',
},
},
},
};
cache.setItem(browserCachePrefix + '0', JSON.stringify(item));
// Set cache
reset({
localStorage: cache,
});
// Check icon storage
const icons = getStorage(provider, prefix);
expect(iconExists(icons, 'foo')).toBe(false);
// Check default provider
const icons2 = getStorage('', prefix);
expect(iconExists(icons2, 'foo')).toBe(false);
// Load localStorage
2022-06-28 20:40:20 +00:00
initBrowserStorage();
// Icon should exist now
expect(iconExists(icons, 'foo')).toBe(true);
expect(iconExists(icons2, '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(),
});
});
2020-04-28 09:47:35 +00:00
it('Expired icon set', () => {
const prefix = nextPrefix();
const cache = createCache();
// 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,
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 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
const icons = getStorage(provider, prefix);
expect(iconExists(icons, '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(iconExists(icons, '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
);
2020-04-28 09:47:35 +00:00
});
it('Bad icon set', () => {
const prefix = nextPrefix();
const cache = createCache();
// 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,
},
},
},
})
);
// 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
const icons = getStorage(provider, prefix);
expect(iconExists(icons, '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(iconExists(icons, '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
);
2020-04-28 09:47:35 +00:00
});
it('Wrong counter', () => {
const prefix = nextPrefix();
const cache = createCache();
// 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
const icons = getStorage(provider, prefix);
expect(iconExists(icons, '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(iconExists(icons, '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();
// 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
const icons = getStorage(provider, prefix);
expect(iconExists(icons, '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(iconExists(icons, '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();
// 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
const icons = getStorage(provider, prefix);
expect(iconExists(icons, 'foo1')).toBe(false);
expect(iconExists(icons, '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(iconExists(icons, 'foo1')).toBe(true);
expect(iconExists(icons, '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();
// 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
const iconsStorage = getStorage(provider, prefix);
2020-04-28 09:47:35 +00:00
for (let i = 0; i < 6; i++) {
2022-03-16 21:30:16 +00:00
expect(iconExists(iconsStorage, '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++) {
2022-03-16 21:30:16 +00:00
expect(iconExists(iconsStorage, '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
});
});
});