2020-08-26 15:34:53 +00:00
|
|
|
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
2020-04-28 09:47:35 +00:00
|
|
|
import 'mocha';
|
|
|
|
import { expect } from 'chai';
|
|
|
|
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-04-24 19:41:54 +00:00
|
|
|
expect(Object.keys(storage.icons)).to.be.eql([
|
|
|
|
'test',
|
|
|
|
'not-really-missing',
|
|
|
|
'constructor',
|
|
|
|
]);
|
2020-04-28 09:47:35 +00:00
|
|
|
|
|
|
|
// Test iconExists
|
|
|
|
expect(iconExists(storage, 'test')).to.be.equal(true);
|
|
|
|
expect(iconExists(storage, 'constructor')).to.be.equal(true);
|
|
|
|
expect(iconExists(storage, 'invalid')).to.be.equal(false);
|
|
|
|
expect(iconExists(storage, 'missing')).to.be.equal(false);
|
2021-04-24 19:41:54 +00:00
|
|
|
expect(iconExists(storage, 'not-really-missing')).to.be.equal(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');
|
|
|
|
expect(icon).to.be.eql(expected);
|
|
|
|
|
|
|
|
// Test icon mutation
|
|
|
|
let thrown = false;
|
|
|
|
try {
|
|
|
|
// @ts-ignore
|
|
|
|
icon.width = 12;
|
|
|
|
} catch (err) {
|
|
|
|
thrown = true;
|
|
|
|
}
|
|
|
|
expect(thrown).to.be.equal(true);
|
|
|
|
|
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,
|
|
|
|
};
|
2020-04-28 09:47:35 +00:00
|
|
|
expect(getIcon(storage, 'constructor')).to.be.eql(expected);
|
2021-04-24 19:41:54 +00:00
|
|
|
|
2020-04-28 09:47:35 +00:00
|
|
|
expect(getIcon(storage, 'invalid')).to.be.equal(null);
|
|
|
|
expect(getIcon(storage, 'missing')).to.be.equal(null);
|
|
|
|
});
|
|
|
|
|
|
|
|
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,
|
|
|
|
})
|
|
|
|
).to.be.equal(true);
|
|
|
|
|
|
|
|
expect(Object.keys(storage.icons)).to.be.eql(['icon1', 'icon2']);
|
|
|
|
|
|
|
|
// Test iconExists
|
|
|
|
expect(iconExists(storage, 'icon1')).to.be.equal(true);
|
|
|
|
expect(iconExists(storage, 'icon2')).to.be.equal(true);
|
|
|
|
expect(iconExists(storage, 'invalid')).to.be.equal(false);
|
|
|
|
expect(iconExists(storage, 'missing')).to.be.equal(false);
|
|
|
|
|
|
|
|
// Test getIcon
|
|
|
|
let expected: FullIconifyIcon = {
|
|
|
|
body: '<path d="icon1" />',
|
|
|
|
width: 20,
|
|
|
|
height: 24,
|
|
|
|
top: 0,
|
|
|
|
left: 0,
|
|
|
|
hFlip: false,
|
|
|
|
vFlip: false,
|
|
|
|
rotate: 0,
|
|
|
|
};
|
|
|
|
expect(getIcon(storage, 'icon1')).to.be.eql(expected);
|
|
|
|
expected = {
|
|
|
|
body: '<path d="icon2" />',
|
|
|
|
width: 24,
|
|
|
|
height: 24,
|
|
|
|
top: 0,
|
|
|
|
left: 0,
|
|
|
|
hFlip: false,
|
|
|
|
vFlip: false,
|
|
|
|
rotate: 0,
|
|
|
|
};
|
|
|
|
expect(getIcon(storage, 'icon2')).to.be.eql(expected);
|
|
|
|
expect(getIcon(storage, 'invalid')).to.be.equal(null);
|
|
|
|
expect(getIcon(storage, 'missing')).to.be.equal(null);
|
|
|
|
});
|
|
|
|
|
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,
|
|
|
|
})
|
|
|
|
).to.be.equal(true);
|
|
|
|
|
|
|
|
expect(Object.keys(storage.icons)).to.be.eql([
|
|
|
|
'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,
|
|
|
|
};
|
|
|
|
expect(getIcon(storage, '16-chevron-left')).to.be.eql(expected);
|
|
|
|
|
|
|
|
// Test alias
|
|
|
|
expected = {
|
|
|
|
body: iconBody,
|
|
|
|
width: 128,
|
|
|
|
height: 128,
|
|
|
|
top: 0,
|
|
|
|
left: 0,
|
|
|
|
hFlip: true,
|
|
|
|
vFlip: false,
|
|
|
|
rotate: 0,
|
|
|
|
};
|
|
|
|
expect(getIcon(storage, '16-chevron-right')).to.be.eql(expected);
|
|
|
|
});
|
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
|
|
|
|
expect(listIcons('', prefix)).to.be.eql([]);
|
|
|
|
expect(listIcons(provider, prefix)).to.be.eql([]);
|
|
|
|
|
|
|
|
// Add one icon without provider
|
|
|
|
addIcon(storage1, 'test', {
|
|
|
|
body: '<path d="" />',
|
|
|
|
width: 20,
|
|
|
|
height: 16,
|
|
|
|
});
|
|
|
|
|
|
|
|
// List icons
|
|
|
|
expect(listIcons('', prefix)).to.be.eql([prefix + ':test']);
|
|
|
|
expect(listIcons(provider, prefix)).to.be.eql([]);
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
})
|
|
|
|
).to.be.equal(true);
|
|
|
|
|
|
|
|
// List icons
|
|
|
|
expect(listIcons('', prefix)).to.be.eql([
|
|
|
|
prefix + ':test',
|
|
|
|
prefix + ':16-chevron-left',
|
|
|
|
prefix + ':16-chevron-right',
|
|
|
|
]);
|
|
|
|
expect(listIcons(provider, prefix)).to.be.eql([]);
|
|
|
|
|
|
|
|
// Add one icon with provider
|
|
|
|
addIcon(storage2, 'test2', {
|
|
|
|
body: '<path d="" />',
|
|
|
|
width: 20,
|
|
|
|
height: 16,
|
|
|
|
});
|
|
|
|
|
|
|
|
// List icons
|
|
|
|
expect(listIcons('', prefix)).to.be.eql([
|
|
|
|
prefix + ':test',
|
|
|
|
prefix + ':16-chevron-left',
|
|
|
|
prefix + ':16-chevron-right',
|
|
|
|
]);
|
|
|
|
expect(listIcons(provider, prefix)).to.be.eql([
|
|
|
|
'@' + provider + ':' + prefix + ':test2',
|
|
|
|
]);
|
|
|
|
});
|
2020-04-28 09:47:35 +00:00
|
|
|
});
|