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

218 lines
4.9 KiB
TypeScript
Raw Normal View History

2022-06-28 20:40:20 +00:00
import { initBrowserStorage } from '../../lib/browser-storage';
2022-06-29 09:30:13 +00:00
import { browserStorageConfig } from '../../lib/browser-storage/data';
2020-04-28 09:47:35 +00:00
import {
browserCacheCountKey,
browserCachePrefix,
browserCacheVersion,
browserCacheVersionKey,
} from '../../lib/browser-storage/config';
2022-06-29 09:30:13 +00:00
import { getBrowserStorageItemsCount } from '../../lib/browser-storage/count';
import { getBrowserStorage } from '../../lib/browser-storage/global';
import { nextPrefix, createCache, reset } from '../../lib/browser-storage/mock';
2020-04-28 09:47:35 +00:00
describe('Testing mocked localStorage', () => {
const provider = '';
2020-04-28 09:47:35 +00:00
it('No usable cache', () => {
reset({});
// Config before tests
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-29 09:30:13 +00:00
// No storage available
expect(getBrowserStorage('local')).toBeUndefined();
expect(getBrowserStorage('session')).toBeUndefined();
2020-04-28 09:47:35 +00:00
// Attempt to load
2022-06-28 20:40:20 +00:00
initBrowserStorage();
2020-04-28 09:47:35 +00:00
// Everything should be disabled
2022-06-28 18:16:08 +00:00
expect(browserStorageConfig).toEqual({
2020-04-28 09:47:35 +00:00
local: false,
session: false,
});
});
it('Empty localStorage', () => {
reset({
localStorage: createCache(),
});
// Config before tests
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-29 09:30:13 +00:00
// Only locaStorage should be available
expect(getBrowserStorage('local')).toBeDefined();
expect(getBrowserStorage('session')).toBeUndefined();
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
expect(getBrowserStorageItemsCount(getBrowserStorage('local')!)).toBe(
0
);
2020-04-28 09:47:35 +00:00
// Attempt to load
2022-06-28 20:40:20 +00:00
initBrowserStorage();
2020-04-28 09:47:35 +00:00
// sessionStorage should be disabled
2022-06-28 18:16:08 +00:00
expect(browserStorageConfig).toEqual({
2020-04-28 09:47:35 +00:00
local: true,
session: false,
});
// Nothing should have loaded
2022-06-29 09:30:13 +00:00
// 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('Restricted localStorage', () => {
const prefix = nextPrefix();
const cache = createCache();
// Add one item
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({
cached: Date.now(),
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
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-29 09:30:13 +00:00
// Storage should not be available
expect(getBrowserStorage('local')).toBeUndefined();
expect(getBrowserStorage('session')).toBeUndefined();
2020-04-28 09:47:35 +00:00
// Attempt to load
2022-06-28 20:40:20 +00:00
initBrowserStorage();
2020-04-28 09:47:35 +00:00
// Everything should be disabled because read-only mock throws errors
2022-06-28 18:16:08 +00:00
expect(browserStorageConfig).toEqual({
2020-04-28 09:47:35 +00:00
local: false,
session: false,
});
});
it('localStorage with one item', () => {
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({
cached: Date.now(),
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
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-29 09:30:13 +00:00
// localStorage should be available
expect(getBrowserStorage('local')).toBeDefined();
expect(getBrowserStorage('session')).toBeUndefined();
2020-04-28 09:47:35 +00:00
// Attempt to load
2022-06-28 20:40:20 +00:00
initBrowserStorage();
2020-04-28 09:47:35 +00:00
// sessionStorage should be disabled
2022-06-28 18:16:08 +00:00
expect(browserStorageConfig).toEqual({
2020-04-28 09:47:35 +00:00
local: true,
session: false,
});
// One item should be in localStorage
2022-06-29 09:30:13 +00:00
// 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('localStorage and sessionStorage', () => {
reset({
localStorage: createCache(),
sessionStorage: createCache(),
});
// Config before tests
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-29 09:30:13 +00:00
// Storage should be available
expect(getBrowserStorage('local')).toBeDefined();
expect(getBrowserStorage('session')).toBeDefined();
// Storage should be empty
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
expect(getBrowserStorageItemsCount(getBrowserStorage('local')!)).toBe(
0
);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
expect(getBrowserStorageItemsCount(getBrowserStorage('session')!)).toBe(
0
);
2020-04-28 09:47:35 +00:00
// Attempt to load
2022-06-28 20:40:20 +00:00
initBrowserStorage();
2020-04-28 09:47:35 +00:00
// Everything should be working
2022-06-28 18:16:08 +00:00
expect(browserStorageConfig).toEqual({
2020-04-28 09:47:35 +00:00
local: true,
session: true,
});
});
});