2021-09-23 21:27:16 +00:00
|
|
|
import { count, config, loadCache } from '@iconify/core/lib/browser-storage';
|
2020-04-28 09:47:35 +00:00
|
|
|
import {
|
|
|
|
nextPrefix,
|
|
|
|
createCache,
|
|
|
|
reset,
|
|
|
|
cachePrefix,
|
|
|
|
cacheVersion,
|
|
|
|
versionKey,
|
|
|
|
countKey,
|
2021-09-23 21:27:16 +00:00
|
|
|
} from './fakeCache.mjs';
|
2020-04-28 09:47:35 +00:00
|
|
|
|
|
|
|
describe('Testing mocked localStorage', () => {
|
2020-05-29 19:08:45 +00:00
|
|
|
const provider = '';
|
|
|
|
|
2020-04-28 09:47:35 +00:00
|
|
|
it('No usable cache', () => {
|
|
|
|
reset({});
|
|
|
|
|
|
|
|
// Config before tests
|
2021-09-23 21:27:16 +00:00
|
|
|
expect(config).toEqual({
|
2020-04-28 09:47:35 +00:00
|
|
|
local: true,
|
|
|
|
session: true,
|
|
|
|
});
|
2021-09-23 21:27:16 +00:00
|
|
|
expect(count).toEqual({
|
2020-04-28 09:47:35 +00:00
|
|
|
local: 0,
|
|
|
|
session: 0,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Attempt to load
|
|
|
|
loadCache();
|
|
|
|
|
|
|
|
// Everything should be disabled
|
2021-09-23 21:27:16 +00:00
|
|
|
expect(config).toEqual({
|
2020-04-28 09:47:35 +00:00
|
|
|
local: false,
|
|
|
|
session: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Nothing should have loaded
|
2021-09-23 21:27:16 +00:00
|
|
|
expect(count).toEqual({
|
2020-04-28 09:47:35 +00:00
|
|
|
local: 0,
|
|
|
|
session: 0,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Empty localStorage', () => {
|
|
|
|
reset({
|
|
|
|
localStorage: createCache(),
|
|
|
|
});
|
|
|
|
|
|
|
|
// Config before tests
|
2021-09-23 21:27:16 +00:00
|
|
|
expect(config).toEqual({
|
2020-04-28 09:47:35 +00:00
|
|
|
local: true,
|
|
|
|
session: true,
|
|
|
|
});
|
2021-09-23 21:27:16 +00:00
|
|
|
expect(count).toEqual({
|
2020-04-28 09:47:35 +00:00
|
|
|
local: 0,
|
|
|
|
session: 0,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Attempt to load
|
|
|
|
loadCache();
|
|
|
|
|
|
|
|
// sessionStorage should be disabled
|
2021-09-23 21:27:16 +00:00
|
|
|
expect(config).toEqual({
|
2020-04-28 09:47:35 +00:00
|
|
|
local: true,
|
|
|
|
session: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Nothing should have loaded
|
2021-09-23 21:27:16 +00:00
|
|
|
expect(count).toEqual({
|
2020-04-28 09:47:35 +00:00
|
|
|
local: 0,
|
|
|
|
session: 0,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Restricted localStorage', () => {
|
|
|
|
const prefix = nextPrefix();
|
|
|
|
const cache = createCache();
|
|
|
|
|
|
|
|
// Add one item
|
|
|
|
cache.setItem(versionKey, cacheVersion);
|
|
|
|
cache.setItem(countKey, '1');
|
|
|
|
cache.setItem(
|
|
|
|
cachePrefix + '0',
|
|
|
|
JSON.stringify({
|
|
|
|
cached: Date.now(),
|
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>',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
// Prevent reading and writing
|
|
|
|
cache.canRead = false;
|
|
|
|
cache.canWrite = false;
|
|
|
|
|
|
|
|
// Set cache and test it
|
|
|
|
reset({
|
|
|
|
localStorage: cache,
|
|
|
|
sessionStorage: cache,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Config before tests
|
2021-09-23 21:27:16 +00:00
|
|
|
expect(config).toEqual({
|
2020-04-28 09:47:35 +00:00
|
|
|
local: true,
|
|
|
|
session: true,
|
|
|
|
});
|
2021-09-23 21:27:16 +00:00
|
|
|
expect(count).toEqual({
|
2020-04-28 09:47:35 +00:00
|
|
|
local: 0,
|
|
|
|
session: 0,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Attempt to load
|
|
|
|
loadCache();
|
|
|
|
|
|
|
|
// Everything should be disabled because read-only mock throws errors
|
2021-09-23 21:27:16 +00:00
|
|
|
expect(config).toEqual({
|
2020-04-28 09:47:35 +00:00
|
|
|
local: false,
|
|
|
|
session: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Nothing should have loaded
|
2021-09-23 21:27:16 +00:00
|
|
|
expect(count).toEqual({
|
2020-04-28 09:47:35 +00:00
|
|
|
local: 0,
|
|
|
|
session: 0,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('localStorage with one item', () => {
|
|
|
|
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: Date.now(),
|
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>',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
// Set cache and test it
|
|
|
|
reset({
|
|
|
|
localStorage: cache,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Config before tests
|
2021-09-23 21:27:16 +00:00
|
|
|
expect(config).toEqual({
|
2020-04-28 09:47:35 +00:00
|
|
|
local: true,
|
|
|
|
session: true,
|
|
|
|
});
|
2021-09-23 21:27:16 +00:00
|
|
|
expect(count).toEqual({
|
2020-04-28 09:47:35 +00:00
|
|
|
local: 0,
|
|
|
|
session: 0,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Attempt to load
|
|
|
|
loadCache();
|
|
|
|
|
|
|
|
// sessionStorage should be disabled
|
2021-09-23 21:27:16 +00:00
|
|
|
expect(config).toEqual({
|
2020-04-28 09:47:35 +00:00
|
|
|
local: true,
|
|
|
|
session: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
// One item should be in localStorage
|
2021-09-23 21:27:16 +00:00
|
|
|
expect(count).toEqual({
|
2020-04-28 09:47:35 +00:00
|
|
|
local: 1,
|
|
|
|
session: 0,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('localStorage and sessionStorage', () => {
|
|
|
|
reset({
|
|
|
|
localStorage: createCache(),
|
|
|
|
sessionStorage: createCache(),
|
|
|
|
});
|
|
|
|
|
|
|
|
// Config before tests
|
2021-09-23 21:27:16 +00:00
|
|
|
expect(config).toEqual({
|
2020-04-28 09:47:35 +00:00
|
|
|
local: true,
|
|
|
|
session: true,
|
|
|
|
});
|
2021-09-23 21:27:16 +00:00
|
|
|
expect(count).toEqual({
|
2020-04-28 09:47:35 +00:00
|
|
|
local: 0,
|
|
|
|
session: 0,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Attempt to load
|
|
|
|
loadCache();
|
|
|
|
|
|
|
|
// Everything should be working
|
2021-09-23 21:27:16 +00:00
|
|
|
expect(config).toEqual({
|
2020-04-28 09:47:35 +00:00
|
|
|
local: true,
|
|
|
|
session: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Empty storage
|
2021-09-23 21:27:16 +00:00
|
|
|
expect(count).toEqual({
|
2020-04-28 09:47:35 +00:00
|
|
|
local: 0,
|
|
|
|
session: 0,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|