2
0
mirror of https://github.com/iconify/iconify.git synced 2024-12-13 22:18:24 +00:00
iconify/packages/core/tests/api/custom-loader-test.ts

582 lines
12 KiB
TypeScript
Raw Normal View History

2024-11-03 15:58:14 +00:00
import { defaultIconProps } from '@iconify/utils';
import { loadIcons, loadIcon } from '../../lib/api/icons';
2024-11-03 17:32:50 +00:00
import {
setCustomIconsLoader,
setCustomIconLoader,
} from '../../lib/api/loaders';
2024-11-03 15:58:14 +00:00
import { listIcons } from '../../lib/storage/storage';
describe('Testing API loadIcons', () => {
let prefixCounter = 0;
function nextPrefix(): string {
prefixCounter++;
return `loader-test-${prefixCounter < 10 ? '0' : ''}${prefixCounter}`;
}
2024-11-03 17:32:50 +00:00
it('Custom async loader for multiple icons with loadIcon', () => {
2024-11-03 15:58:14 +00:00
return new Promise((resolve, reject) => {
const provider = nextPrefix();
const prefix = nextPrefix();
// Set loader
2024-11-03 17:32:50 +00:00
setCustomIconsLoader(
2024-11-03 15:58:14 +00:00
(icons, requestedPrefix, requestedProvider) => {
return new Promise((resolve, reject) => {
try {
// Check params
expect(icons).toEqual(['icon1']);
expect(requestedPrefix).toBe(prefix);
expect(requestedProvider).toBe(provider);
} catch (err) {
reject(err);
return;
}
// Send data
resolve({
prefix,
icons: {
icon1: {
body: '<path d="" />',
},
icon_2: {
body: '<path d="" />',
},
},
});
});
},
prefix,
provider
);
// Load icon
loadIcon(provider + ':' + prefix + ':icon1')
.then((data) => {
try {
// Test response
expect(data).toEqual({
...defaultIconProps,
body: '<path d="" />',
});
// Check storage
expect(listIcons(provider, prefix)).toEqual([
`@${provider}:${prefix}:icon1`,
`@${provider}:${prefix}:icon_2`,
]);
} catch (err) {
reject(err);
return;
}
resolve(true);
})
.catch(reject);
});
});
2024-11-03 17:32:50 +00:00
it('Custom sync loader for multiple icons with loadIcon', () => {
2024-11-03 15:58:14 +00:00
return new Promise((resolve, reject) => {
const provider = nextPrefix();
const prefix = nextPrefix();
// Set loader
2024-11-03 17:32:50 +00:00
setCustomIconsLoader(
2024-11-03 15:58:14 +00:00
(icons, requestedPrefix, requestedProvider) => {
try {
// Check params
expect(icons).toEqual(['Icon_1']);
expect(requestedPrefix).toBe(prefix);
expect(requestedProvider).toBe(provider);
} catch (err) {
reject(err);
return null;
}
// Send data
return {
prefix,
icons: {
// Use name that is not allowed in API
Icon_1: {
body: '<path d="" />',
},
},
};
},
prefix,
provider
);
// Load icon
loadIcon(`${provider}:${prefix}:Icon_1`)
.then((data) => {
try {
// Test response
expect(data).toEqual({
...defaultIconProps,
body: '<path d="" />',
});
// Check storage
expect(listIcons(provider, prefix)).toEqual([
`@${provider}:${prefix}:Icon_1`,
]);
} catch (err) {
reject(err);
return;
}
resolve(true);
})
.catch(reject);
});
});
2024-11-03 17:32:50 +00:00
it('Loader multiple icons with missing icons', () => {
2024-11-03 15:58:14 +00:00
return new Promise((resolve, reject) => {
const provider = nextPrefix();
const prefix1 = nextPrefix();
const prefix2 = nextPrefix();
// Set loaders: one is sync, one is async
2024-11-03 17:32:50 +00:00
setCustomIconsLoader(
2024-11-03 15:58:14 +00:00
(icons, requestedPrefix, requestedProvider) => {
try {
// Check params
expect(icons).toEqual(['icon1']);
expect(requestedPrefix).toBe(prefix1);
expect(requestedProvider).toBe(provider);
} catch (err) {
reject(err);
return null;
}
// Send data
return {
prefix: prefix1,
icons: {
icon1: {
body: '<path d="" />',
},
},
};
},
prefix1,
provider
);
2024-11-03 17:32:50 +00:00
setCustomIconsLoader(
2024-11-03 15:58:14 +00:00
(icons, requestedPrefix, requestedProvider) => {
try {
// Check params
expect(icons).toEqual(['BadIcon', 'Icon_2']);
expect(requestedPrefix).toBe(prefix2);
expect(requestedProvider).toBe(provider);
} catch (err) {
reject(err);
return null;
}
// Send data asynchronously, without 'BadIcon'
return new Promise((resolve) => {
setTimeout(() => {
resolve({
prefix: prefix2,
icons: {
Icon_1: {
body: '<path d="" />',
},
Icon_2: {
body: '<path d="" />',
},
Icon_3: {
body: '<path d="" />',
},
},
});
}, 150);
});
},
prefix2,
provider
);
2024-11-03 17:32:50 +00:00
// Load icons
2024-11-03 15:58:14 +00:00
loadIcons(
[
`${provider}:${prefix1}:icon1`,
`${provider}:${prefix2}:Icon_2`,
`${provider}:${prefix2}:BadIcon`,
],
(loaded, missing, pending) => {
if (pending.length) {
// Could be called before all icons are loaded because of async loader
return;
}
try {
// Test response
expect(loaded).toEqual([
{
provider,
prefix: prefix1,
name: 'icon1',
},
{
provider,
prefix: prefix2,
name: 'Icon_2',
},
]);
expect(missing).toEqual([
{
provider,
prefix: prefix2,
name: 'BadIcon',
},
]);
// Check storage
expect(listIcons(provider, prefix1)).toEqual([
`@${provider}:${prefix1}:icon1`,
]);
expect(listIcons(provider, prefix2)).toEqual([
`@${provider}:${prefix2}:Icon_1`,
`@${provider}:${prefix2}:Icon_2`,
`@${provider}:${prefix2}:Icon_3`,
]);
} catch (err) {
reject(err);
return;
}
resolve(true);
}
);
});
});
2024-11-03 17:32:50 +00:00
it('Custom async loader for one icon with loadIcon', () => {
return new Promise((resolve, reject) => {
const provider = nextPrefix();
const prefix = nextPrefix();
// Set loader
setCustomIconLoader(
(name, requestedPrefix, requestedProvider) => {
return new Promise((resolve, reject) => {
try {
// Check params
expect(name).toBe('icon1');
expect(requestedPrefix).toBe(prefix);
expect(requestedProvider).toBe(provider);
} catch (err) {
reject(err);
return;
}
// Send data
resolve({
body: '<path d="" />',
});
});
},
prefix,
provider
);
// Load icon
loadIcon(provider + ':' + prefix + ':icon1')
.then((data) => {
try {
// Test response
expect(data).toEqual({
...defaultIconProps,
body: '<path d="" />',
});
// Check storage
expect(listIcons(provider, prefix)).toEqual([
`@${provider}:${prefix}:icon1`,
]);
} catch (err) {
reject(err);
return;
}
resolve(true);
})
.catch(reject);
});
});
it('Loader multiple icons with loader for one icon', () => {
return new Promise((resolve, reject) => {
const provider = nextPrefix();
const prefix1 = nextPrefix();
const prefix2 = nextPrefix();
const iconsToTest: Record<string, Set<string>> = {
[prefix1]: new Set(['icon1']),
[prefix2]: new Set(['Icon_2', 'BadIcon']),
};
// Set loaders: one is sync, one is async
setCustomIconLoader(
(requestedName, requestedPrefix, requestedProvider) => {
try {
// Check params
expect(requestedPrefix).toBe(prefix1);
expect(requestedProvider).toBe(provider);
expect(
iconsToTest[prefix1].has(requestedName)
).toBeTruthy();
} catch (err) {
reject(err);
return null;
}
// Send data
return {
body: `<g data-name="${requestedName}" />`,
};
},
prefix1,
provider
);
setCustomIconLoader(
(requestedName, requestedPrefix, requestedProvider) => {
try {
// Check params
expect(requestedPrefix).toBe(prefix2);
expect(requestedProvider).toBe(provider);
expect(
iconsToTest[prefix2].has(requestedName)
).toBeTruthy();
} catch (err) {
reject(err);
return null;
}
// Send data asynchronously, without 'BadIcon'
return new Promise((resolve) => {
setTimeout(() => {
resolve(
requestedName === 'BadIcon'
? null
: {
body: `<g data-name="${requestedName}" />`,
}
);
}, 150);
});
},
prefix2,
provider
);
// Load icons
loadIcons(
[
`${provider}:${prefix1}:icon1`,
`${provider}:${prefix2}:Icon_2`,
`${provider}:${prefix2}:BadIcon`,
],
(loaded, missing, pending) => {
if (pending.length) {
// Could be called before all icons are loaded because multiple async requests
return;
}
try {
// Test response
expect(loaded).toEqual([
{
provider,
prefix: prefix1,
name: 'icon1',
},
{
provider,
prefix: prefix2,
name: 'Icon_2',
},
]);
expect(missing).toEqual([
{
provider,
prefix: prefix2,
name: 'BadIcon',
},
]);
// Check storage
expect(listIcons(provider, prefix1)).toEqual([
`@${provider}:${prefix1}:icon1`,
]);
expect(listIcons(provider, prefix2)).toEqual([
`@${provider}:${prefix2}:Icon_2`,
]);
} catch (err) {
reject(err);
return;
}
resolve(true);
}
);
});
});
it('Loaders for one and multiple icons, requesting one icon', () => {
return new Promise((resolve, reject) => {
const provider = nextPrefix();
const prefix = nextPrefix();
const name = 'TestIcon';
// Set loaders: one is sync, one is async
setCustomIconLoader(
(requestedName, requestedPrefix, requestedProvider) => {
try {
// Check params
expect(requestedPrefix).toBe(prefix);
expect(requestedProvider).toBe(provider);
expect(requestedName).toBe(name);
} catch (err) {
reject(err);
return null;
}
// Send data
return {
body: `<g data-name="${requestedName}" />`,
};
},
prefix,
provider
);
setCustomIconsLoader(
() => {
reject(
new Error(
'Loader for multple icons should not be called'
)
);
return null;
},
prefix,
provider
);
// Load icon
loadIcons(
[`${provider}:${prefix}:${name}`],
(loaded, missing, pending) => {
try {
// Test response
expect(loaded).toEqual([
{
provider,
prefix,
name,
},
]);
expect(missing).toEqual([]);
expect(pending).toEqual([]);
// Check storage
expect(listIcons(provider, prefix)).toEqual([
`@${provider}:${prefix}:${name}`,
]);
} catch (err) {
reject(err);
return;
}
resolve(true);
}
);
});
});
it('Loaders for one and multiple icons, requesting multiple icons', () => {
return new Promise((resolve, reject) => {
const provider = nextPrefix();
const prefix = nextPrefix();
// Set loaders: one is sync, one is async
setCustomIconsLoader(
(requestedNames, requestedPrefix, requestedProvider) => {
try {
// Check params
expect(requestedPrefix).toBe(prefix);
expect(requestedProvider).toBe(provider);
expect(requestedNames).toEqual(['Icon_1', 'Icon_2']);
} catch (err) {
reject(err);
return null;
}
// Send data
return {
prefix,
icons: {
Icon_1: {
body: `<g data-name="Icon_1" />`,
},
Icon_3: {
body: `<g data-name="Icon_3" />`,
},
},
};
},
prefix,
provider
);
setCustomIconLoader(
() => {
reject(
new Error('Loader for one icon should not be called')
);
return null;
},
prefix,
provider
);
// Load icon
loadIcons(
[
`${provider}:${prefix}:Icon_1`,
`${provider}:${prefix}:Icon_2`,
],
(loaded, missing, pending) => {
try {
// Test response
expect(loaded).toEqual([
{
provider,
prefix,
name: 'Icon_1',
},
]);
expect(missing).toEqual([
{
provider,
prefix,
name: 'Icon_2',
},
]);
expect(pending).toEqual([]);
// Check storage
expect(listIcons(provider, prefix)).toEqual([
`@${provider}:${prefix}:Icon_1`,
`@${provider}:${prefix}:Icon_3`,
]);
} catch (err) {
reject(err);
return;
}
resolve(true);
}
);
});
});
2024-11-03 15:58:14 +00:00
});