2020-12-25 19:03:15 +00:00
|
|
|
import type { IconifyJSON } from '@iconify/types';
|
2022-06-29 13:11:16 +00:00
|
|
|
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';
|
2023-07-04 16:45:04 +00:00
|
|
|
import { getStorage, iconInStorage } from '../../lib/storage/storage';
|
2022-06-28 18:16:08 +00:00
|
|
|
import { nextPrefix, createCache, reset } from '../../lib/browser-storage/mock';
|
2022-06-28 16:12:45 +00:00
|
|
|
import {
|
|
|
|
browserCacheCountKey,
|
|
|
|
browserCachePrefix,
|
|
|
|
browserCacheVersion,
|
|
|
|
browserCacheVersionKey,
|
2022-06-28 18:16:08 +00:00
|
|
|
browserStorageHour,
|
|
|
|
browserStorageCacheExpiration,
|
2022-06-28 16:12:45 +00:00
|
|
|
} from '../../lib/browser-storage/config';
|
2022-06-29 13:11:16 +00:00
|
|
|
import { getStoredItem } from '../../lib/browser-storage/item';
|
2020-04-28 09:47:35 +00:00
|
|
|
|
|
|
|
describe('Testing loading from localStorage', () => {
|
2020-05-29 19:08:45 +00:00
|
|
|
const provider = '';
|
|
|
|
|
2020-04-28 09:47:35 +00:00
|
|
|
it('Valid icon set', () => {
|
|
|
|
const prefix = nextPrefix();
|
|
|
|
const cache = createCache();
|
2022-06-29 13:11:16 +00:00
|
|
|
const storage = getStorage(provider, prefix) as IconStorageWithCache;
|
2020-04-28 09:47:35 +00:00
|
|
|
|
|
|
|
// Add one icon set
|
2022-06-28 16:12:45 +00:00
|
|
|
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),
|
2020-05-29 19:08:45 +00:00
|
|
|
provider,
|
2020-04-28 09:47:35 +00:00
|
|
|
data: {
|
|
|
|
prefix: prefix,
|
|
|
|
icons: {
|
|
|
|
foo: {
|
|
|
|
body: '<g></g>',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
2022-06-28 16:12:45 +00:00
|
|
|
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
|
2023-07-04 16:45:04 +00:00
|
|
|
expect(iconInStorage(storage, 'foo')).toBe(false);
|
2022-06-29 13:11:16 +00:00
|
|
|
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
|
|
|
|
2022-06-29 13:11:16 +00:00
|
|
|
// Icon should exist now and lastModified should be set
|
2023-07-04 16:45:04 +00:00
|
|
|
expect(iconInStorage(storage, 'foo')).toBe(true);
|
2022-06-29 13:11:16 +00:00
|
|
|
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
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-29 19:08:45 +00:00
|
|
|
it('Different provider', () => {
|
|
|
|
const provider = nextPrefix();
|
|
|
|
const prefix = nextPrefix();
|
|
|
|
const cache = createCache();
|
2022-06-29 13:11:16 +00:00
|
|
|
const storage = getStorage(provider, prefix) as IconStorageWithCache;
|
|
|
|
const defaultStorage = getStorage('', prefix) as IconStorageWithCache;
|
|
|
|
const lastModified = 12345;
|
2020-05-29 19:08:45 +00:00
|
|
|
|
|
|
|
// Add one icon set
|
2022-06-28 16:12:45 +00:00
|
|
|
cache.setItem(browserCacheVersionKey, browserCacheVersion);
|
|
|
|
cache.setItem(browserCacheCountKey, '1');
|
2020-05-29 19:08:45 +00:00
|
|
|
|
2022-06-28 18:16:08 +00:00
|
|
|
const item: BrowserStorageItem = {
|
|
|
|
cached: Math.floor(Date.now() / browserStorageHour),
|
2020-05-29 19:08:45 +00:00
|
|
|
provider,
|
|
|
|
data: {
|
|
|
|
prefix: prefix,
|
2022-06-29 13:11:16 +00:00
|
|
|
lastModified,
|
2020-05-29 19:08:45 +00:00
|
|
|
icons: {
|
|
|
|
foo: {
|
|
|
|
body: '<g></g>',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
2022-06-28 16:12:45 +00:00
|
|
|
cache.setItem(browserCachePrefix + '0', JSON.stringify(item));
|
2022-06-29 13:11:16 +00:00
|
|
|
expect(getStoredItem(cache, browserCachePrefix + '0')).toBeTruthy();
|
2020-05-29 19:08:45 +00:00
|
|
|
|
|
|
|
// Set cache
|
|
|
|
reset({
|
|
|
|
localStorage: cache,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Check icon storage
|
2023-07-04 16:45:04 +00:00
|
|
|
expect(iconInStorage(storage, 'foo')).toBe(false);
|
2022-06-29 13:11:16 +00:00
|
|
|
expect(storage.lastModifiedCached).toBeUndefined();
|
2020-05-29 19:08:45 +00:00
|
|
|
|
|
|
|
// Check default provider
|
2023-07-04 16:45:04 +00:00
|
|
|
expect(iconInStorage(defaultStorage, 'foo')).toBe(false);
|
2022-06-29 13:11:16 +00:00
|
|
|
expect(defaultStorage.lastModifiedCached).toBeUndefined();
|
2020-05-29 19:08:45 +00:00
|
|
|
|
|
|
|
// Load localStorage
|
2022-06-28 20:40:20 +00:00
|
|
|
initBrowserStorage();
|
2020-05-29 19:08:45 +00:00
|
|
|
|
|
|
|
// Icon should exist now
|
2023-07-04 16:45:04 +00:00
|
|
|
expect(iconInStorage(storage, 'foo')).toBe(true);
|
|
|
|
expect(iconInStorage(defaultStorage, 'foo')).toBe(false);
|
2020-05-29 19:08:45 +00:00
|
|
|
|
|
|
|
// Check data
|
2022-06-28 18:16:08 +00:00
|
|
|
expect(browserStorageConfig).toEqual({
|
2020-05-29 19:08:45 +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-05-29 19:08:45 +00:00
|
|
|
});
|
2022-06-29 13:11:16 +00:00
|
|
|
|
|
|
|
expect(storage.lastModifiedCached).toBe(lastModified);
|
|
|
|
expect(defaultStorage.lastModifiedCached).toBeUndefined();
|
2020-05-29 19:08:45 +00:00
|
|
|
});
|
|
|
|
|
2020-04-28 09:47:35 +00:00
|
|
|
it('Expired icon set', () => {
|
|
|
|
const prefix = nextPrefix();
|
|
|
|
const cache = createCache();
|
2022-06-29 13:11:16 +00:00
|
|
|
const storage = getStorage(provider, prefix) as IconStorageWithCache;
|
|
|
|
const lastModified = 12345;
|
2020-04-28 09:47:35 +00:00
|
|
|
|
|
|
|
// Add one icon set
|
2022-06-28 16:12:45 +00:00
|
|
|
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,
|
2020-05-29 19:08:45 +00:00
|
|
|
provider,
|
2020-04-28 09:47:35 +00:00
|
|
|
data: {
|
|
|
|
prefix: prefix,
|
2022-06-29 13:11:16 +00:00
|
|
|
lastModified,
|
2020-04-28 09:47:35 +00:00
|
|
|
icons: {
|
|
|
|
foo: {
|
|
|
|
body: '<g></g>',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
2022-06-28 16:12:45 +00:00
|
|
|
cache.setItem(browserCachePrefix + '0', JSON.stringify(item));
|
2022-06-29 13:11:16 +00:00
|
|
|
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
|
2023-07-04 16:45:04 +00:00
|
|
|
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
|
2023-07-04 16:45:04 +00:00
|
|
|
expect(iconInStorage(storage, 'foo')).toBe(false);
|
2022-06-29 13:11:16 +00:00
|
|
|
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();
|
2022-06-29 13:11:16 +00:00
|
|
|
const storage = getStorage(provider, prefix);
|
2020-04-28 09:47:35 +00:00
|
|
|
|
|
|
|
// Add one icon set
|
2022-06-28 16:12:45 +00:00
|
|
|
cache.setItem(browserCacheVersionKey, browserCacheVersion);
|
|
|
|
cache.setItem(browserCacheCountKey, '1');
|
2020-04-28 09:47:35 +00:00
|
|
|
cache.setItem(
|
2022-06-28 16:12:45 +00:00
|
|
|
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),
|
2020-05-29 19:08:45 +00:00
|
|
|
provider,
|
2020-04-28 09:47:35 +00:00
|
|
|
data: {
|
|
|
|
prefix: prefix,
|
|
|
|
icons: {
|
|
|
|
foo: {
|
|
|
|
// Missing 'body' property
|
|
|
|
width: 20,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
);
|
2022-06-29 13:11:16 +00:00
|
|
|
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
|
2023-07-04 16:45:04 +00:00
|
|
|
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
|
2023-07-04 16:45:04 +00:00
|
|
|
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
|
|
|
|
);
|
2022-06-29 13:11:16 +00:00
|
|
|
|
|
|
|
// 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();
|
2022-06-29 13:11:16 +00:00
|
|
|
const storage = getStorage(provider, prefix);
|
2020-04-28 09:47:35 +00:00
|
|
|
|
|
|
|
// Add one icon set
|
2022-06-28 16:12:45 +00:00
|
|
|
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),
|
2020-05-29 19:08:45 +00:00
|
|
|
provider,
|
2020-04-28 09:47:35 +00:00
|
|
|
data: {
|
|
|
|
prefix: prefix,
|
|
|
|
icons: {
|
|
|
|
foo: {
|
|
|
|
body: '<g></g>',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
2022-06-28 16:12:45 +00:00
|
|
|
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
|
2023-07-04 16:45:04 +00:00
|
|
|
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
|
2023-07-04 16:45:04 +00:00
|
|
|
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();
|
2022-06-29 13:11:16 +00:00
|
|
|
const storage = getStorage(provider, prefix);
|
2020-04-28 09:47:35 +00:00
|
|
|
|
|
|
|
// Add one icon set
|
2022-06-28 16:12:45 +00:00
|
|
|
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),
|
2020-05-29 19:08:45 +00:00
|
|
|
provider,
|
2020-04-28 09:47:35 +00:00
|
|
|
data: {
|
|
|
|
prefix: prefix,
|
|
|
|
icons: {
|
|
|
|
foo: {
|
|
|
|
body: '<g></g>',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
2022-06-28 16:12:45 +00:00
|
|
|
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
|
2023-07-04 16:45:04 +00:00
|
|
|
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
|
2023-07-04 16:45:04 +00:00
|
|
|
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();
|
2022-06-29 13:11:16 +00:00
|
|
|
const storage = getStorage(provider, prefix);
|
2020-04-28 09:47:35 +00:00
|
|
|
|
|
|
|
// Add two icon sets
|
2022-06-28 16:12:45 +00:00
|
|
|
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),
|
2020-05-29 19:08:45 +00:00
|
|
|
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),
|
2020-05-29 19:08:45 +00:00
|
|
|
provider,
|
2020-04-28 09:47:35 +00:00
|
|
|
data: {
|
|
|
|
prefix: prefix,
|
|
|
|
icons: {
|
|
|
|
foo4: {
|
|
|
|
body: '<g></g>',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2022-06-28 16:12:45 +00:00
|
|
|
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
|
2023-07-04 16:45:04 +00:00
|
|
|
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
|
2023-07-04 16:45:04 +00:00
|
|
|
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();
|
2022-06-29 13:11:16 +00:00
|
|
|
const storage = getStorage(provider, prefix);
|
2020-04-28 09:47:35 +00:00
|
|
|
|
|
|
|
// Add few icon sets
|
2022-06-28 16:12:45 +00:00
|
|
|
cache1.setItem(browserCacheVersionKey, browserCacheVersion);
|
|
|
|
cache2.setItem(browserCacheVersionKey, browserCacheVersion);
|
2020-04-28 09:47:35 +00:00
|
|
|
|
2022-06-28 16:12:45 +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),
|
2020-05-29 19:08:45 +00:00
|
|
|
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(
|
2022-06-28 16:12:45 +00:00
|
|
|
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(
|
2022-06-28 16:12:45 +00:00
|
|
|
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++) {
|
2023-07-04 16:45:04 +00:00
|
|
|
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++) {
|
2023-07-04 16:45:04 +00:00
|
|
|
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
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|