2022-02-26 14:12:13 +00:00
|
|
|
import { promises as fs } from 'fs';
|
2022-03-03 23:36:30 +00:00
|
|
|
import type { CustomIconLoader } from '../lib';
|
|
|
|
import { loadIcon } from '../lib';
|
2022-02-26 14:12:13 +00:00
|
|
|
|
2022-03-03 23:36:30 +00:00
|
|
|
const fixturesDir = './tests/fixtures';
|
2022-02-26 14:12:13 +00:00
|
|
|
|
2022-03-04 16:37:21 +00:00
|
|
|
const loader: CustomIconLoader = async (name) => {
|
2022-02-26 14:12:13 +00:00
|
|
|
return await fs.readFile(`${fixturesDir}/${name}.svg`, 'utf8');
|
2022-03-04 16:37:21 +00:00
|
|
|
};
|
2022-02-26 14:12:13 +00:00
|
|
|
|
|
|
|
describe('Testing loadIcon', () => {
|
|
|
|
test('CustomCollection', async () => {
|
|
|
|
const svg = await loader('circle');
|
2022-02-26 16:20:57 +00:00
|
|
|
expect(svg).toBeTruthy();
|
2022-02-26 14:12:13 +00:00
|
|
|
const result = await loadIcon('a', 'circle', {
|
|
|
|
customCollections: {
|
2022-03-04 16:37:21 +00:00
|
|
|
a: {
|
|
|
|
circle: svg as string,
|
2022-02-26 14:12:13 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2022-02-26 16:20:57 +00:00
|
|
|
expect(result).toBeTruthy();
|
2022-02-26 14:12:13 +00:00
|
|
|
expect(svg).toEqual(result);
|
|
|
|
});
|
|
|
|
|
2022-02-27 18:55:06 +00:00
|
|
|
test('CustomCollection using dynamic import', async () => {
|
2022-06-17 20:10:54 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
2022-03-14 14:39:40 +00:00
|
|
|
// @ts-ignore
|
2022-02-27 18:55:06 +00:00
|
|
|
const result = await loadIcon('flat-color-icons', 'up-right', {
|
|
|
|
customCollections: {
|
2022-03-14 14:39:40 +00:00
|
|
|
'flat-color-icons': () =>
|
|
|
|
import(
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-ignore
|
|
|
|
'@iconify-json/flat-color-icons/icons.json'
|
|
|
|
).then((i) => i?.default),
|
2022-02-27 18:55:06 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
expect(result).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
2022-02-26 14:12:13 +00:00
|
|
|
test('CustomCollection with transform', async () => {
|
2022-02-26 16:20:57 +00:00
|
|
|
const svg = await loader('circle');
|
|
|
|
expect(svg).toBeTruthy();
|
2022-02-26 14:12:13 +00:00
|
|
|
const result = await loadIcon('a', 'circle', {
|
|
|
|
customCollections: {
|
2022-03-04 16:37:21 +00:00
|
|
|
a: {
|
|
|
|
circle: svg as string,
|
2022-02-26 14:12:13 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
customizations: {
|
2022-03-24 20:16:13 +00:00
|
|
|
transform(svg) {
|
|
|
|
return svg.replace(
|
2022-03-04 16:37:21 +00:00
|
|
|
'<svg ',
|
|
|
|
'<svg width="1em" height="1em" '
|
|
|
|
);
|
2022-02-26 14:12:13 +00:00
|
|
|
},
|
2022-02-26 16:20:57 +00:00
|
|
|
},
|
2022-02-26 14:12:13 +00:00
|
|
|
});
|
2022-02-26 16:20:57 +00:00
|
|
|
expect(result).toBeTruthy();
|
2022-02-26 14:12:13 +00:00
|
|
|
expect(result && result.indexOf('width="1em"') > -1).toBeTruthy();
|
|
|
|
expect(result && result.indexOf('height="1em"') > -1).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
2022-06-17 20:26:55 +00:00
|
|
|
test('CustomCollection Icon with XML heading', async () => {
|
2022-02-26 16:20:57 +00:00
|
|
|
const svg = await loader('1f3eb');
|
|
|
|
expect(svg).toBeTruthy();
|
2022-02-26 14:12:13 +00:00
|
|
|
// Intercept console.warn
|
|
|
|
let warned = false;
|
|
|
|
const warn = console.warn;
|
|
|
|
console.warn = (/*...args*/) => {
|
|
|
|
// warn.apply(this, args);
|
|
|
|
warned = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
const result = await loadIcon('a', '1f3eb', {
|
|
|
|
customCollections: {
|
2022-03-04 16:37:21 +00:00
|
|
|
a: {
|
2022-02-26 14:12:13 +00:00
|
|
|
'1f3eb': svg as string,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
// Restore console.warn
|
|
|
|
console.warn = warn;
|
|
|
|
|
2022-06-17 20:26:55 +00:00
|
|
|
expect(svg).not.toEqual(result);
|
|
|
|
expect(
|
|
|
|
svg?.replace(
|
|
|
|
'<?xml version="1.0" encoding="UTF-8" standalone="no"?>',
|
|
|
|
''
|
|
|
|
)
|
|
|
|
).toEqual(result);
|
|
|
|
|
|
|
|
// warning should not longer be used
|
|
|
|
expect(warned).toEqual(false);
|
2022-02-26 14:12:13 +00:00
|
|
|
});
|
|
|
|
});
|