2022-01-09 21:23:02 +00:00
|
|
|
import { promises as fs } from 'fs';
|
2023-02-13 22:06:13 +00:00
|
|
|
import { getCustomIcon } from '../lib/loader/custom';
|
|
|
|
import type { IconifyLoaderOptions } from '../lib/loader/types';
|
2021-12-11 23:26:58 +00:00
|
|
|
|
2022-03-03 23:36:30 +00:00
|
|
|
const fixturesDir = './tests/fixtures';
|
2021-12-11 23:26:58 +00:00
|
|
|
|
|
|
|
describe('Testing getCustomIcon', () => {
|
2022-01-09 20:44:48 +00:00
|
|
|
test('CustomIconLoader', async () => {
|
2022-01-09 21:23:02 +00:00
|
|
|
const svg = await fs.readFile(fixturesDir + '/circle.svg', 'utf8');
|
2022-01-09 20:44:48 +00:00
|
|
|
const result = await getCustomIcon(() => svg, 'a', 'b');
|
2021-12-11 23:26:58 +00:00
|
|
|
expect(svg).toEqual(result);
|
|
|
|
});
|
2022-01-09 20:44:48 +00:00
|
|
|
|
2022-03-18 13:52:53 +00:00
|
|
|
test('CustomIconLoader without xmlns', async () => {
|
|
|
|
const svg =
|
|
|
|
'<svg viewBox="0 0 120 120"><circle cx="60" cy="60" r="50"/></svg>';
|
|
|
|
const result = await getCustomIcon(() => svg, 'a', 'b', {
|
|
|
|
addXmlNs: true,
|
|
|
|
});
|
|
|
|
expect(result).toEqual(
|
|
|
|
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 120 120"><circle cx="60" cy="60" r="50"/></svg>'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-03-21 17:38:18 +00:00
|
|
|
test('CustomIconLoader should apply trim', async () => {
|
|
|
|
const svg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 120 120">
|
|
|
|
<circle cx="60" cy="60" r="50"/>
|
|
|
|
</svg>
|
|
|
|
`;
|
|
|
|
const result = await getCustomIcon(() => svg, 'a', 'b', {
|
|
|
|
customizations: { trimCustomSvg: true },
|
|
|
|
});
|
|
|
|
expect(result).toEqual(
|
|
|
|
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 120 120"><circle cx="60" cy="60" r="50"/></svg>'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-06-17 20:10:54 +00:00
|
|
|
test('CustomIconLoader cleanups svg preface', async () => {
|
|
|
|
const svg = `<?xml version="1.0" standalone="no"?>
|
|
|
|
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
|
|
|
|
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 120 120">
|
|
|
|
<circle cx="60" cy="60" r="50"/>
|
|
|
|
</svg>
|
|
|
|
`;
|
|
|
|
const result = await getCustomIcon(() => svg, 'a', 'b', {
|
|
|
|
customizations: { trimCustomSvg: true },
|
|
|
|
});
|
|
|
|
expect(result).toEqual(
|
|
|
|
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 120 120"><circle cx="60" cy="60" r="50"/></svg>'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-03-17 11:48:49 +00:00
|
|
|
test("CustomIconLoader with transform: scale/width/height shouldn't take effect", async () => {
|
2022-01-09 21:23:02 +00:00
|
|
|
const svg = await fs.readFile(fixturesDir + '/circle.svg', 'utf8');
|
2022-03-17 11:48:49 +00:00
|
|
|
const options: IconifyLoaderOptions = {
|
|
|
|
scale: 2,
|
2022-02-26 14:12:13 +00:00
|
|
|
customizations: {
|
2022-03-17 11:48:49 +00:00
|
|
|
additionalProps: {
|
|
|
|
width: '4em',
|
|
|
|
height: '4em',
|
|
|
|
},
|
2022-03-24 20:16:13 +00:00
|
|
|
transform(svg) {
|
|
|
|
return svg.replace(
|
2023-01-06 12:43:22 +00:00
|
|
|
/<svg\s+/,
|
2022-03-04 16:37:21 +00:00
|
|
|
'<svg width="1em" height="1em" '
|
|
|
|
);
|
2022-02-26 14:12:13 +00:00
|
|
|
},
|
2022-03-04 16:37:21 +00:00
|
|
|
},
|
2022-03-17 11:48:49 +00:00
|
|
|
usedProps: {},
|
|
|
|
};
|
|
|
|
const result = await getCustomIcon(() => svg, 'a', 'b', options);
|
2021-12-11 23:26:58 +00:00
|
|
|
expect(result && result.indexOf('width="1em"') > -1).toBeTruthy();
|
|
|
|
expect(result && result.indexOf('height="1em"') > -1).toBeTruthy();
|
2022-03-18 13:55:47 +00:00
|
|
|
|
2022-03-17 12:14:35 +00:00
|
|
|
expect(options.usedProps).toBeTruthy();
|
2022-03-18 13:55:47 +00:00
|
|
|
|
|
|
|
const usedProps = options.usedProps as Record<string, string>;
|
|
|
|
expect(usedProps).toHaveProperty('width');
|
|
|
|
expect(usedProps).toHaveProperty('height');
|
|
|
|
expect(usedProps.width).toEqual('4em');
|
|
|
|
expect(usedProps.height).toEqual('4em');
|
2021-12-11 23:26:58 +00:00
|
|
|
});
|
2022-01-09 21:23:02 +00:00
|
|
|
|
2023-02-13 23:08:42 +00:00
|
|
|
test('Icon with XML heading', async () => {
|
2022-01-09 21:23:02 +00:00
|
|
|
// Intercept console.warn
|
|
|
|
let warned = false;
|
|
|
|
const warn = console.warn;
|
|
|
|
console.warn = (/*...args*/) => {
|
|
|
|
// warn.apply(this, args);
|
|
|
|
warned = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
const svg = await fs.readFile(fixturesDir + '/1f3eb.svg', 'utf8');
|
|
|
|
const result = await getCustomIcon(() => svg, 'a', 'b');
|
|
|
|
|
|
|
|
// Restore console.warn
|
|
|
|
console.warn = warn;
|
|
|
|
|
2023-02-13 23:08:42 +00:00
|
|
|
expect(result).toEqual(
|
|
|
|
svg.replace(
|
|
|
|
'<?xml version="1.0" encoding="UTF-8" standalone="no"?>',
|
|
|
|
''
|
|
|
|
)
|
|
|
|
);
|
|
|
|
expect(warned).toEqual(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Scale custom icon', async () => {
|
2023-02-14 10:15:30 +00:00
|
|
|
const svg =
|
|
|
|
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path d="M139.61 35.5a12 12 0 0 0-17 0L58.93 98.81l-22.7-22.12a12 12 0 0 0-17 0L3.53 92.41a12 12 0 0 0 0 17l47.59 47.4a12.78 12.78 0 0 0 17.61 0l15.59-15.62L156.52 69a12.09 12.09 0 0 0 .09-17zm0 159.19a12 12 0 0 0-17 0l-63.68 63.72-22.7-22.1a12 12 0 0 0-17 0L3.53 252a12 12 0 0 0 0 17L51 316.5a12.77 12.77 0 0 0 17.6 0l15.7-15.69 72.2-72.22a12 12 0 0 0 .09-16.9zM64 368c-26.49 0-48.59 21.5-48.59 48S37.53 464 64 464a48 48 0 0 0 0-96zm432 16H208a16 16 0 0 0-16 16v32a16 16 0 0 0 16 16h288a16 16 0 0 0 16-16v-32a16 16 0 0 0-16-16zm0-320H208a16 16 0 0 0-16 16v32a16 16 0 0 0 16 16h288a16 16 0 0 0 16-16V80a16 16 0 0 0-16-16zm0 160H208a16 16 0 0 0-16 16v32a16 16 0 0 0 16 16h288a16 16 0 0 0 16-16v-32a16 16 0 0 0-16-16z"/></svg>';
|
|
|
|
|
|
|
|
const options: IconifyLoaderOptions = {
|
|
|
|
scale: 2,
|
|
|
|
};
|
|
|
|
const result = await getCustomIcon(() => svg, 'a', 'b', options);
|
|
|
|
expect(result && result.indexOf(' width="2em"') > -1).toBeTruthy();
|
|
|
|
expect(result && result.indexOf(' height="2em"') > -1).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Scale custom icon with stroke-width', async () => {
|
2023-02-13 23:08:42 +00:00
|
|
|
const svg = `<?xml version="1.0" standalone="no"?>
|
|
|
|
<svg fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" viewBox="0 0 24 24"
|
|
|
|
stroke="currentColor">
|
|
|
|
<path d="M12 14V20M10 12L4 10M14 12L20 10M21 12a9 9 0 11-18 0 9 9 0 0118 0zM14 12a2 2 0 11-4 0 2 2 0 014 0z" />
|
|
|
|
</svg>`;
|
|
|
|
|
|
|
|
const options: IconifyLoaderOptions = {
|
|
|
|
scale: 1.2,
|
|
|
|
};
|
|
|
|
const result = await getCustomIcon(() => svg, 'a', 'b', options);
|
|
|
|
expect(result && result.indexOf(' width="1.2em"') > -1).toBeTruthy();
|
|
|
|
expect(result && result.indexOf(' height="1.2em"') > -1).toBeTruthy();
|
2022-01-09 21:23:02 +00:00
|
|
|
});
|
2023-02-14 10:15:30 +00:00
|
|
|
|
|
|
|
test('Disable scale for custom icon', async () => {
|
|
|
|
const svg =
|
|
|
|
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path d="M139.61 35.5a12 12 0 0 0-17 0L58.93 98.81l-22.7-22.12a12 12 0 0 0-17 0L3.53 92.41a12 12 0 0 0 0 17l47.59 47.4a12.78 12.78 0 0 0 17.61 0l15.59-15.62L156.52 69a12.09 12.09 0 0 0 .09-17zm0 159.19a12 12 0 0 0-17 0l-63.68 63.72-22.7-22.1a12 12 0 0 0-17 0L3.53 252a12 12 0 0 0 0 17L51 316.5a12.77 12.77 0 0 0 17.6 0l15.7-15.69 72.2-72.22a12 12 0 0 0 .09-16.9zM64 368c-26.49 0-48.59 21.5-48.59 48S37.53 464 64 464a48 48 0 0 0 0-96zm432 16H208a16 16 0 0 0-16 16v32a16 16 0 0 0 16 16h288a16 16 0 0 0 16-16v-32a16 16 0 0 0-16-16zm0-320H208a16 16 0 0 0-16 16v32a16 16 0 0 0 16 16h288a16 16 0 0 0 16-16V80a16 16 0 0 0-16-16zm0 160H208a16 16 0 0 0-16 16v32a16 16 0 0 0 16 16h288a16 16 0 0 0 16-16v-32a16 16 0 0 0-16-16z"/></svg>';
|
|
|
|
|
|
|
|
const options: IconifyLoaderOptions = {
|
|
|
|
scale: 0,
|
|
|
|
};
|
|
|
|
const result = await getCustomIcon(() => svg, 'a', 'b', options);
|
|
|
|
expect(result && result.indexOf(' width="') === -1).toBeTruthy();
|
|
|
|
expect(result && result.indexOf(' height="') === -1).toBeTruthy();
|
|
|
|
});
|
2021-12-11 23:26:58 +00:00
|
|
|
});
|