2020-08-26 15:34:53 +00:00
|
|
|
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
2020-04-28 09:47:35 +00:00
|
|
|
import {
|
|
|
|
newStorage,
|
|
|
|
addIcon,
|
|
|
|
iconExists,
|
|
|
|
getIcon,
|
|
|
|
addIconSet,
|
2020-08-14 09:01:47 +00:00
|
|
|
getStorage,
|
|
|
|
listIcons,
|
2020-12-21 15:15:45 +00:00
|
|
|
} from '../../lib/storage/storage';
|
2021-05-24 10:25:02 +00:00
|
|
|
import type { IconifyIcon, FullIconifyIcon } from '@iconify/utils/lib/icon';
|
2020-04-28 09:47:35 +00:00
|
|
|
|
|
|
|
describe('Testing storage', () => {
|
|
|
|
it('Adding icon', () => {
|
2020-05-29 19:08:45 +00:00
|
|
|
const storage = newStorage('', 'foo');
|
2020-04-28 09:47:35 +00:00
|
|
|
|
|
|
|
// Add one icon
|
|
|
|
addIcon(storage, 'test', {
|
|
|
|
body: '<path d="" />',
|
|
|
|
width: 20,
|
|
|
|
height: 16,
|
|
|
|
});
|
2021-04-24 19:41:54 +00:00
|
|
|
addIcon(storage, 'not-really-missing', {
|
|
|
|
body: '<path d="" />',
|
|
|
|
width: 24,
|
|
|
|
height: 24,
|
|
|
|
});
|
2020-04-28 09:47:35 +00:00
|
|
|
|
|
|
|
// Add another icon with reserved keyword as name
|
|
|
|
addIcon(storage, 'constructor', {
|
|
|
|
body: '<g></g>',
|
|
|
|
width: 24,
|
|
|
|
height: 24,
|
|
|
|
rotate: 1,
|
|
|
|
});
|
|
|
|
|
2021-04-24 19:41:54 +00:00
|
|
|
// Mark 'not-really-missing' as missing
|
|
|
|
storage.missing['not-really-missing'] = Date.now();
|
|
|
|
|
2020-04-28 09:47:35 +00:00
|
|
|
// Add invalid icon
|
2021-05-24 10:25:02 +00:00
|
|
|
addIcon(storage, 'invalid', {} as unknown as IconifyIcon);
|
2020-04-28 09:47:35 +00:00
|
|
|
|
|
|
|
// Should not include 'invalid'
|
2021-09-23 21:27:16 +00:00
|
|
|
expect(Object.keys(storage.icons)).toEqual([
|
2021-04-24 19:41:54 +00:00
|
|
|
'test',
|
|
|
|
'not-really-missing',
|
|
|
|
'constructor',
|
|
|
|
]);
|
2020-04-28 09:47:35 +00:00
|
|
|
|
|
|
|
// Test iconExists
|
2021-09-23 21:27:16 +00:00
|
|
|
expect(iconExists(storage, 'test')).toBe(true);
|
|
|
|
expect(iconExists(storage, 'constructor')).toBe(true);
|
|
|
|
expect(iconExists(storage, 'invalid')).toBe(false);
|
|
|
|
expect(iconExists(storage, 'missing')).toBe(false);
|
|
|
|
expect(iconExists(storage, 'not-really-missing')).toBe(true);
|
2020-04-28 09:47:35 +00:00
|
|
|
|
|
|
|
// Test getIcon
|
|
|
|
let expected: FullIconifyIcon = {
|
|
|
|
body: '<path d="" />',
|
|
|
|
width: 20,
|
|
|
|
height: 16,
|
|
|
|
top: 0,
|
|
|
|
left: 0,
|
|
|
|
hFlip: false,
|
|
|
|
vFlip: false,
|
|
|
|
rotate: 0,
|
|
|
|
};
|
|
|
|
const icon = getIcon(storage, 'test');
|
2021-09-23 21:27:16 +00:00
|
|
|
expect(icon).toEqual(expected);
|
2020-04-28 09:47:35 +00:00
|
|
|
|
|
|
|
// Test icon mutation
|
|
|
|
let thrown = false;
|
|
|
|
try {
|
|
|
|
// @ts-ignore
|
|
|
|
icon.width = 12;
|
|
|
|
} catch (err) {
|
|
|
|
thrown = true;
|
|
|
|
}
|
2021-09-23 21:27:16 +00:00
|
|
|
expect(thrown).toBe(true);
|
2020-04-28 09:47:35 +00:00
|
|
|
|
2021-04-24 19:41:54 +00:00
|
|
|
expected = {
|
|
|
|
body: '<g></g>',
|
|
|
|
width: 24,
|
|
|
|
height: 24,
|
|
|
|
top: 0,
|
|
|
|
left: 0,
|
|
|
|
hFlip: false,
|
|
|
|
vFlip: false,
|
|
|
|
rotate: 1,
|
|
|
|
};
|
2021-09-23 21:27:16 +00:00
|
|
|
expect(getIcon(storage, 'constructor')).toEqual(expected);
|
2021-04-24 19:41:54 +00:00
|
|
|
|
2021-09-23 21:27:16 +00:00
|
|
|
expect(getIcon(storage, 'invalid')).toBeNull();
|
|
|
|
expect(getIcon(storage, 'missing')).toBeNull();
|
2020-04-28 09:47:35 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Adding simple icon set', () => {
|
2020-05-29 19:08:45 +00:00
|
|
|
const storage = newStorage('', 'foo');
|
2020-04-28 09:47:35 +00:00
|
|
|
|
|
|
|
// Add two icons
|
|
|
|
expect(
|
|
|
|
addIconSet(storage, {
|
|
|
|
prefix: 'foo',
|
|
|
|
icons: {
|
|
|
|
icon1: {
|
|
|
|
body: '<path d="icon1" />',
|
|
|
|
width: 20,
|
|
|
|
},
|
|
|
|
icon2: {
|
|
|
|
body: '<path d="icon2" />',
|
|
|
|
width: 24,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
height: 24,
|
|
|
|
})
|
2021-09-23 21:27:16 +00:00
|
|
|
).toBe(true);
|
2020-04-28 09:47:35 +00:00
|
|
|
|
2021-09-23 21:27:16 +00:00
|
|
|
expect(Object.keys(storage.icons)).toEqual(['icon1', 'icon2']);
|
2020-04-28 09:47:35 +00:00
|
|
|
|
|
|
|
// Test iconExists
|
2021-09-23 21:27:16 +00:00
|
|
|
expect(iconExists(storage, 'icon1')).toBe(true);
|
|
|
|
expect(iconExists(storage, 'icon2')).toBe(true);
|
|
|
|
expect(iconExists(storage, 'invalid')).toBe(false);
|
|
|
|
expect(iconExists(storage, 'missing')).toBe(false);
|
2020-04-28 09:47:35 +00:00
|
|
|
|
|
|
|
// Test getIcon
|
|
|
|
let expected: FullIconifyIcon = {
|
|
|
|
body: '<path d="icon1" />',
|
|
|
|
width: 20,
|
|
|
|
height: 24,
|
|
|
|
top: 0,
|
|
|
|
left: 0,
|
|
|
|
hFlip: false,
|
|
|
|
vFlip: false,
|
|
|
|
rotate: 0,
|
|
|
|
};
|
2021-09-23 21:27:16 +00:00
|
|
|
expect(getIcon(storage, 'icon1')).toEqual(expected);
|
2020-04-28 09:47:35 +00:00
|
|
|
expected = {
|
|
|
|
body: '<path d="icon2" />',
|
|
|
|
width: 24,
|
|
|
|
height: 24,
|
|
|
|
top: 0,
|
|
|
|
left: 0,
|
|
|
|
hFlip: false,
|
|
|
|
vFlip: false,
|
|
|
|
rotate: 0,
|
|
|
|
};
|
2021-09-23 21:27:16 +00:00
|
|
|
expect(getIcon(storage, 'icon2')).toEqual(expected);
|
|
|
|
expect(getIcon(storage, 'invalid')).toBeNull();
|
|
|
|
expect(getIcon(storage, 'missing')).toBeNull();
|
2020-04-28 09:47:35 +00:00
|
|
|
});
|
|
|
|
|
2020-04-29 08:15:37 +00:00
|
|
|
it('Icon set with aliases that use transformations', () => {
|
2020-05-29 19:08:45 +00:00
|
|
|
const storage = newStorage('iconify', 'arty-animated');
|
2020-04-29 08:15:37 +00:00
|
|
|
const iconBody =
|
|
|
|
'<g stroke="currentColor" stroke-width="16" stroke-linecap="round" stroke-linejoin="round" fill="none" fill-rule="evenodd"><path d="M40 64l48-48" class="animation-delay-0 animation-duration-10 animate-stroke stroke-length-102"/><path d="M40 64l48 48" class="animation-delay-0 animation-duration-10 animate-stroke stroke-length-102"/></g>';
|
|
|
|
|
|
|
|
expect(
|
|
|
|
addIconSet(storage, {
|
|
|
|
prefix: 'arty-animated',
|
|
|
|
icons: {
|
|
|
|
'16-chevron-left': {
|
|
|
|
body: iconBody,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
aliases: {
|
|
|
|
'16-chevron-right': {
|
|
|
|
parent: '16-chevron-left',
|
|
|
|
hFlip: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
width: 128,
|
|
|
|
height: 128,
|
|
|
|
})
|
2021-09-23 21:27:16 +00:00
|
|
|
).toBe(true);
|
2020-04-29 08:15:37 +00:00
|
|
|
|
2021-09-23 21:27:16 +00:00
|
|
|
expect(Object.keys(storage.icons)).toEqual([
|
2020-04-29 08:15:37 +00:00
|
|
|
'16-chevron-left',
|
|
|
|
'16-chevron-right',
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Test icon
|
|
|
|
let expected: FullIconifyIcon = {
|
|
|
|
body: iconBody,
|
|
|
|
width: 128,
|
|
|
|
height: 128,
|
|
|
|
top: 0,
|
|
|
|
left: 0,
|
|
|
|
hFlip: false,
|
|
|
|
vFlip: false,
|
|
|
|
rotate: 0,
|
|
|
|
};
|
2021-09-23 21:27:16 +00:00
|
|
|
expect(getIcon(storage, '16-chevron-left')).toEqual(expected);
|
2020-04-29 08:15:37 +00:00
|
|
|
|
|
|
|
// Test alias
|
|
|
|
expected = {
|
|
|
|
body: iconBody,
|
|
|
|
width: 128,
|
|
|
|
height: 128,
|
|
|
|
top: 0,
|
|
|
|
left: 0,
|
|
|
|
hFlip: true,
|
|
|
|
vFlip: false,
|
|
|
|
rotate: 0,
|
|
|
|
};
|
2021-09-23 21:27:16 +00:00
|
|
|
expect(getIcon(storage, '16-chevron-right')).toEqual(expected);
|
2020-04-29 08:15:37 +00:00
|
|
|
});
|
2020-08-14 09:01:47 +00:00
|
|
|
|
|
|
|
it('List icons in a global storage', () => {
|
|
|
|
const provider = 'test-provider';
|
|
|
|
const prefix = 'global-storage-test';
|
|
|
|
const storage1 = getStorage('', prefix);
|
|
|
|
const storage2 = getStorage(provider, prefix);
|
|
|
|
|
|
|
|
// List icons
|
2021-09-23 21:27:16 +00:00
|
|
|
expect(listIcons('', prefix)).toEqual([]);
|
|
|
|
expect(listIcons(provider, prefix)).toEqual([]);
|
2020-08-14 09:01:47 +00:00
|
|
|
|
|
|
|
// Add one icon without provider
|
|
|
|
addIcon(storage1, 'test', {
|
|
|
|
body: '<path d="" />',
|
|
|
|
width: 20,
|
|
|
|
height: 16,
|
|
|
|
});
|
|
|
|
|
|
|
|
// List icons
|
2021-09-23 21:27:16 +00:00
|
|
|
expect(listIcons('', prefix)).toEqual([prefix + ':test']);
|
|
|
|
expect(listIcons(provider, prefix)).toEqual([]);
|
2020-08-14 09:01:47 +00:00
|
|
|
|
|
|
|
// Add icon set without provider
|
|
|
|
expect(
|
|
|
|
addIconSet(storage1, {
|
|
|
|
prefix,
|
|
|
|
icons: {
|
|
|
|
'16-chevron-left': {
|
|
|
|
body: '<path d="" />',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
aliases: {
|
|
|
|
'16-chevron-right': {
|
|
|
|
parent: '16-chevron-left',
|
|
|
|
hFlip: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
width: 128,
|
|
|
|
height: 128,
|
|
|
|
})
|
2021-09-23 21:27:16 +00:00
|
|
|
).toBe(true);
|
2020-08-14 09:01:47 +00:00
|
|
|
|
|
|
|
// List icons
|
2021-09-23 21:27:16 +00:00
|
|
|
expect(listIcons('', prefix)).toEqual([
|
2020-08-14 09:01:47 +00:00
|
|
|
prefix + ':test',
|
|
|
|
prefix + ':16-chevron-left',
|
|
|
|
prefix + ':16-chevron-right',
|
|
|
|
]);
|
2021-09-23 21:27:16 +00:00
|
|
|
expect(listIcons(provider, prefix)).toEqual([]);
|
2020-08-14 09:01:47 +00:00
|
|
|
|
|
|
|
// Add one icon with provider
|
|
|
|
addIcon(storage2, 'test2', {
|
|
|
|
body: '<path d="" />',
|
|
|
|
width: 20,
|
|
|
|
height: 16,
|
|
|
|
});
|
|
|
|
|
|
|
|
// List icons
|
2021-09-23 21:27:16 +00:00
|
|
|
expect(listIcons('', prefix)).toEqual([
|
2020-08-14 09:01:47 +00:00
|
|
|
prefix + ':test',
|
|
|
|
prefix + ':16-chevron-left',
|
|
|
|
prefix + ':16-chevron-right',
|
|
|
|
]);
|
2021-09-23 21:27:16 +00:00
|
|
|
expect(listIcons(provider, prefix)).toEqual([
|
2020-08-14 09:01:47 +00:00
|
|
|
'@' + provider + ':' + prefix + ':test2',
|
|
|
|
]);
|
|
|
|
});
|
2020-04-28 09:47:35 +00:00
|
|
|
});
|