2
0
mirror of https://github.com/iconify/iconify.git synced 2024-12-13 22:18:24 +00:00
iconify/packages/core/spec/cache/loadingSpec.mjs

492 lines
9.1 KiB
JavaScript
Raw Normal View History

2020-04-28 09:47:35 +00:00
import {
loadCache,
count,
config,
emptyList,
} from '@iconify/core/lib/browser-storage';
import { getStorage, iconExists } from '@iconify/core/lib/storage/storage';
2020-04-28 09:47:35 +00:00
import {
nextPrefix,
createCache,
reset,
cachePrefix,
cacheVersion,
versionKey,
countKey,
hour,
cacheExpiration,
} from './fakeCache.mjs';
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(versionKey, cacheVersion);
cache.setItem(countKey, '1');
const item = {
2020-04-28 09:47:35 +00:00
cached: Math.floor(Date.now() / hour),
provider,
2020-04-28 09:47:35 +00:00
data: {
prefix: prefix,
icons: {
foo: {
body: '<g></g>',
},
},
},
};
cache.setItem(cachePrefix + '0', JSON.stringify(item));
// Set cache
reset({
localStorage: cache,
});
// Check icon storage
const icons = getStorage(provider, prefix);
expect(iconExists(icons, 'foo')).toBe(false);
2020-04-28 09:47:35 +00:00
// Load localStorage
loadCache();
// Icon should exist now
expect(iconExists(icons, 'foo')).toBe(true);
2020-04-28 09:47:35 +00:00
// Check data
expect(config).toEqual({
2020-04-28 09:47:35 +00:00
local: true,
session: false,
});
expect(count).toEqual({
2020-04-28 09:47:35 +00:00
local: 1,
session: 0,
});
expect(emptyList).toEqual({
2020-04-28 09:47:35 +00:00
local: [],
session: [],
});
});
it('Different provider', () => {
const provider = nextPrefix();
const prefix = nextPrefix();
const cache = createCache();
// Add one icon set
cache.setItem(versionKey, cacheVersion);
cache.setItem(countKey, '1');
const item = {
cached: Math.floor(Date.now() / hour),
provider,
data: {
prefix: prefix,
icons: {
foo: {
body: '<g></g>',
},
},
},
};
cache.setItem(cachePrefix + '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
loadCache();
// Icon should exist now
expect(iconExists(icons, 'foo')).toBe(true);
expect(iconExists(icons2, 'foo')).toBe(false);
// Check data
expect(config).toEqual({
local: true,
session: false,
});
expect(count).toEqual({
local: 1,
session: 0,
});
expect(emptyList).toEqual({
local: [],
session: [],
});
});
2020-04-28 09:47:35 +00:00
it('Expired icon set', () => {
const prefix = nextPrefix();
const cache = createCache();
// Add one icon set
cache.setItem(versionKey, cacheVersion);
cache.setItem(countKey, '1');
const item = {
2020-04-28 09:47:35 +00:00
// Expiration date
cached: Math.floor(Date.now() / hour) - cacheExpiration - 1,
provider,
2020-04-28 09:47:35 +00:00
data: {
prefix: prefix,
icons: {
foo: {
body: '<g></g>',
},
},
},
};
cache.setItem(cachePrefix + '0', JSON.stringify(item));
// Set cache
reset({
localStorage: cache,
});
// Check icon storage
const icons = getStorage(provider, prefix);
expect(iconExists(icons, 'foo')).toBe(false);
2020-04-28 09:47:35 +00:00
// Load localStorage
loadCache();
// Icon should not have loaded
expect(iconExists(icons, 'foo')).toBe(false);
2020-04-28 09:47:35 +00:00
// Check data
expect(config).toEqual({
2020-04-28 09:47:35 +00:00
local: true,
session: false,
});
expect(count).toEqual({
2020-04-28 09:47:35 +00:00
local: 0,
session: 0,
});
expect(emptyList).toEqual({
2020-04-28 09:47:35 +00:00
local: [],
session: [],
});
});
it('Bad icon set', () => {
const prefix = nextPrefix();
const cache = createCache();
// Add one icon set
cache.setItem(versionKey, cacheVersion);
cache.setItem(countKey, '1');
cache.setItem(
cachePrefix + '0',
JSON.stringify({
cached: Math.floor(Date.now() / hour),
provider,
2020-04-28 09:47:35 +00:00
data: {
prefix: prefix,
icons: {
foo: {
// Missing 'body' property
width: 20,
},
},
},
})
);
// Set cache
reset({
localStorage: cache,
});
// Check icon storage
const icons = getStorage(provider, prefix);
expect(iconExists(icons, 'foo')).toBe(false);
2020-04-28 09:47:35 +00:00
// Load localStorage
loadCache();
// Icon should not have loaded
expect(iconExists(icons, 'foo')).toBe(false);
2020-04-28 09:47:35 +00:00
// Check data
expect(config).toEqual({
2020-04-28 09:47:35 +00:00
local: true,
session: false,
});
expect(count).toEqual({
2020-04-28 09:47:35 +00:00
local: 0,
session: 0,
});
expect(emptyList).toEqual({
2020-04-28 09:47:35 +00:00
local: [],
session: [],
});
});
it('Wrong counter', () => {
const prefix = nextPrefix();
const cache = createCache();
// Add one icon set
cache.setItem(versionKey, cacheVersion);
cache.setItem(countKey, '0'); // Should be at least "1"
const item = {
2020-04-28 09:47:35 +00:00
cached: Math.floor(Date.now() / hour),
provider,
2020-04-28 09:47:35 +00:00
data: {
prefix: prefix,
icons: {
foo: {
body: '<g></g>',
},
},
},
};
cache.setItem(cachePrefix + '0', JSON.stringify(item));
// Set cache
reset({
localStorage: cache,
});
// Check icon storage
const icons = getStorage(provider, prefix);
expect(iconExists(icons, 'foo')).toBe(false);
2020-04-28 09:47:35 +00:00
// Load localStorage
loadCache();
// Icon should not have loaded
expect(iconExists(icons, 'foo')).toBe(false);
2020-04-28 09:47:35 +00:00
// Check data
expect(config).toEqual({
2020-04-28 09:47:35 +00:00
local: true,
session: false,
});
expect(count).toEqual({
2020-04-28 09:47:35 +00:00
local: 0,
session: 0,
});
expect(emptyList).toEqual({
2020-04-28 09:47:35 +00:00
local: [],
session: [],
});
});
it('Missing entries at the end', () => {
const prefix = nextPrefix();
const cache = createCache();
// Add one icon set
cache.setItem(versionKey, cacheVersion);
cache.setItem(countKey, '5');
const item = {
2020-04-28 09:47:35 +00:00
cached: Math.floor(Date.now() / hour),
provider,
2020-04-28 09:47:35 +00:00
data: {
prefix: prefix,
icons: {
foo: {
body: '<g></g>',
},
},
},
};
cache.setItem(cachePrefix + '0', JSON.stringify(item));
// Set cache
reset({
localStorage: cache,
});
// Check icon storage
const icons = getStorage(provider, prefix);
expect(iconExists(icons, 'foo')).toBe(false);
2020-04-28 09:47:35 +00:00
// Load localStorage
loadCache();
// Icon should exist now
expect(iconExists(icons, 'foo')).toBe(true);
2020-04-28 09:47:35 +00:00
// Check data
expect(config).toEqual({
2020-04-28 09:47:35 +00:00
local: true,
session: false,
});
expect(count).toEqual({
2020-04-28 09:47:35 +00:00
local: 1,
session: 0,
});
expect(emptyList).toEqual({
2020-04-28 09:47:35 +00:00
local: [],
session: [],
});
});
it('Missing entries', () => {
const prefix = nextPrefix();
const cache = createCache();
// Add two icon sets
cache.setItem(versionKey, cacheVersion);
cache.setItem(countKey, '5');
// Missing: 0, 2, 3
const item1 = {
2020-04-28 09:47:35 +00:00
cached: Math.floor(Date.now() / hour),
provider,
2020-04-28 09:47:35 +00:00
data: {
prefix: prefix,
icons: {
foo1: {
body: '<g></g>',
},
},
},
};
const item4 = {
2020-04-28 09:47:35 +00:00
cached: Math.floor(Date.now() / hour),
provider,
2020-04-28 09:47:35 +00:00
data: {
prefix: prefix,
icons: {
foo4: {
body: '<g></g>',
},
},
},
};
cache.setItem(cachePrefix + '1', JSON.stringify(item1));
cache.setItem(cachePrefix + '4', JSON.stringify(item4));
// Set cache
reset({
localStorage: cache,
});
// 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
loadCache();
// 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
expect(config).toEqual({
2020-04-28 09:47:35 +00:00
local: true,
session: false,
});
expect(count).toEqual({
2020-04-28 09:47:35 +00:00
local: 5,
session: 0,
});
expect(emptyList).toEqual({
2020-04-28 09:47:35 +00:00
local: [3, 2, 0], // reserse order
session: [],
});
});
it('Using both storage options', () => {
const prefix = nextPrefix();
const cache1 = createCache();
const cache2 = createCache();
// Add few icon sets
cache1.setItem(versionKey, cacheVersion);
cache2.setItem(versionKey, cacheVersion);
cache1.setItem(countKey, '6');
cache2.setItem(countKey, '3');
// Create 5 items
const icons = [];
const items = [];
2020-04-28 09:47:35 +00:00
for (let i = 0; i < 6; i++) {
const icon = {
2020-04-28 09:47:35 +00:00
prefix: prefix,
icons: {
['foo' + i]: {
body: '<g></g>',
},
},
};
const item = {
2020-04-28 09:47:35 +00:00
cached: Math.floor(Date.now() / hour),
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) => {
cache1.setItem(cachePrefix + index, JSON.stringify(items[index]));
});
// Add items 0 and 2 to sessionStorage
[0, 2].forEach((index) => {
cache2.setItem(cachePrefix + index, JSON.stringify(items[index]));
});
// 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++) {
expect(iconExists(iconsStorage, 'foo' + i)).toBe(false);
2020-04-28 09:47:35 +00:00
}
// Load localStorage
loadCache();
// Icons should exist now, except for number 4
for (let i = 0; i < 6; i++) {
expect(iconExists(iconsStorage, 'foo' + i)).toBe(i !== 4);
2020-04-28 09:47:35 +00:00
}
// Check data
expect(config).toEqual({
2020-04-28 09:47:35 +00:00
local: true,
session: true,
});
expect(count).toEqual({
2020-04-28 09:47:35 +00:00
local: 6,
session: 3,
});
expect(emptyList).toEqual({
2020-04-28 09:47:35 +00:00
local: [4, 2, 0],
session: [1],
});
});
});