2
0
mirror of https://github.com/iconify/iconify.git synced 2024-12-12 13:47:49 +00:00

fix(loadIcon): do not attempt to load invalid icons

This commit is contained in:
Vjacheslav Trushkin 2022-09-21 12:16:17 +03:00
parent 3b3fb518c3
commit 33b8510a1c
2 changed files with 90 additions and 1 deletions

View File

@ -256,7 +256,13 @@ export const loadIcon = (
icon: IconifyIconName | string
): Promise<Required<IconifyIcon>> => {
return new Promise((fulfill, reject) => {
const iconObj = typeof icon === 'string' ? stringToIcon(icon) : icon;
const iconObj =
typeof icon === 'string' ? stringToIcon(icon, true) : icon;
if (!iconObj) {
reject(icon);
return;
}
loadIcons([iconObj || icon], (loaded) => {
if (loaded.length && iconObj) {
const data = getIconData(iconObj);

View File

@ -233,6 +233,89 @@ describe('Testing API loadIcons', () => {
expect(isPending({ provider, prefix, name: 'icon1' })).toBe(false);
});
it('Loading icon with bad name', async () => {
const provider = nextPrefix();
const prefix = nextPrefix();
// Set config
addAPIProvider(provider, {
resources: ['https://api1.local', 'https://api2.local'],
});
// Icon loader
const prepareQuery = (
provider: string,
prefix: string,
icons: string[]
): IconifyAPIIconsQueryParams[] => {
const item: IconifyAPIIconsQueryParams = {
type: 'icons',
provider,
prefix,
icons,
};
// Test input and return as one item
const expected: IconifyAPIIconsQueryParams = {
type: 'icons',
provider,
prefix,
icons: ['BadIconName'],
};
expect(item).toEqual(expected);
return [item];
};
const sendQuery = (
host: string,
params: IconifyAPIQueryParams,
callback: QueryModuleResponse
): void => {
expect(params.type).toBe('icons');
// Test input
expect(host).toBe('https://api1.local');
const expected: IconifyAPIQueryParams = {
type: 'icons',
provider,
prefix,
icons: ['BadIconName'],
};
expect(params).toEqual(expected);
// Send data
callback('success', {
prefix,
icons: {
BadIconName: {
body: '<path d="" />',
},
},
});
};
setAPIModule(provider, {
prepare: prepareQuery,
send: sendQuery,
});
// Load icon: should throw an error
let loadedIcon = false;
try {
await loadIcon(provider + ':' + prefix + ':BadIconName');
loadedIcon = true;
} catch {
// Do nothing
}
expect(loadedIcon).toBe(false);
// Test isPending
expect(isPending({ provider, prefix, name: 'BadIconName' })).toBe(
false
);
});
it('Loading one icon twice with Promise', () => {
return new Promise((fulfill, reject) => {
const provider = nextPrefix();