2021-05-24 14:02:00 +00:00
|
|
|
import type { IconifyIconBuildResult } from '../lib/svg/build';
|
|
|
|
import { iconToSVG } from '../lib/svg/build';
|
2022-07-01 19:12:18 +00:00
|
|
|
import type { IconifyIcon } from '../lib/icon/defaults';
|
|
|
|
import type { IconifyIconCustomisations } from '../lib/customisations/defaults';
|
2022-04-10 07:45:05 +00:00
|
|
|
import { iconToHTML } from '../lib/svg/html';
|
2020-04-28 09:47:35 +00:00
|
|
|
|
|
|
|
describe('Testing iconToSVG', () => {
|
2021-09-20 20:53:49 +00:00
|
|
|
test('Empty icon', () => {
|
2022-07-01 19:12:18 +00:00
|
|
|
const icon: IconifyIcon = { body: '' };
|
2020-04-28 09:47:35 +00:00
|
|
|
const expected: IconifyIconBuildResult = {
|
|
|
|
attributes: {
|
|
|
|
width: '1em',
|
|
|
|
height: '1em',
|
|
|
|
viewBox: '0 0 16 16',
|
|
|
|
},
|
|
|
|
body: '',
|
|
|
|
};
|
|
|
|
|
2022-07-01 19:12:18 +00:00
|
|
|
const result = iconToSVG(icon);
|
2021-09-20 20:53:49 +00:00
|
|
|
expect(result).toEqual(expected);
|
2022-04-10 07:45:05 +00:00
|
|
|
|
|
|
|
// Test HTML
|
|
|
|
const html = iconToHTML(result.body, result.attributes);
|
|
|
|
expect(html).toBe(
|
2022-04-30 20:12:34 +00:00
|
|
|
'<svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" viewBox="0 0 16 16"></svg>'
|
2022-04-10 07:45:05 +00:00
|
|
|
);
|
2020-04-28 09:47:35 +00:00
|
|
|
});
|
|
|
|
|
2022-06-19 14:12:26 +00:00
|
|
|
test('Auto size, body', () => {
|
2022-07-01 19:12:18 +00:00
|
|
|
const custom: IconifyIconCustomisations = {
|
2022-06-20 20:43:01 +00:00
|
|
|
height: 'auto',
|
|
|
|
};
|
2022-07-01 19:12:18 +00:00
|
|
|
const icon: IconifyIcon = {
|
2020-04-28 09:47:35 +00:00
|
|
|
body: '<path d="" />',
|
2022-06-19 14:12:26 +00:00
|
|
|
};
|
2020-04-28 09:47:35 +00:00
|
|
|
const expected: IconifyIconBuildResult = {
|
|
|
|
attributes: {
|
|
|
|
width: '16',
|
|
|
|
height: '16',
|
|
|
|
viewBox: '0 0 16 16',
|
|
|
|
},
|
|
|
|
body: '<path d="" />',
|
|
|
|
};
|
|
|
|
|
|
|
|
const result = iconToSVG(icon, custom);
|
2021-09-20 20:53:49 +00:00
|
|
|
expect(result).toEqual(expected);
|
2022-04-10 07:45:05 +00:00
|
|
|
|
|
|
|
// Test HTML
|
|
|
|
const htmlProps: Record<string, string> = {
|
|
|
|
'aria-hidden': 'true',
|
|
|
|
'role': 'img',
|
|
|
|
...result.attributes,
|
|
|
|
};
|
|
|
|
const html = iconToHTML(result.body, htmlProps);
|
|
|
|
expect(html).toBe(
|
2022-06-19 14:12:26 +00:00
|
|
|
'<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" width="16" height="16" viewBox="0 0 16 16"><path d="" /></svg>'
|
2022-04-10 07:45:05 +00:00
|
|
|
);
|
2020-04-28 09:47:35 +00:00
|
|
|
});
|
|
|
|
|
2022-06-19 14:12:26 +00:00
|
|
|
test('Auto size, body', () => {
|
2022-07-01 19:12:18 +00:00
|
|
|
const custom: IconifyIconCustomisations = {
|
2022-06-20 20:43:01 +00:00
|
|
|
height: 'auto',
|
|
|
|
};
|
2022-07-01 19:12:18 +00:00
|
|
|
const icon: IconifyIcon = {
|
2020-04-28 09:47:35 +00:00
|
|
|
body: '<path d="" />',
|
2022-06-19 14:12:26 +00:00
|
|
|
};
|
2020-04-28 09:47:35 +00:00
|
|
|
const expected: IconifyIconBuildResult = {
|
|
|
|
attributes: {
|
|
|
|
width: '16',
|
|
|
|
height: '16',
|
|
|
|
viewBox: '0 0 16 16',
|
|
|
|
},
|
|
|
|
body: '<path d="" />',
|
|
|
|
};
|
|
|
|
|
|
|
|
const result = iconToSVG(icon, custom);
|
2021-09-20 20:53:49 +00:00
|
|
|
expect(result).toEqual(expected);
|
2020-04-28 09:47:35 +00:00
|
|
|
});
|
|
|
|
|
2022-04-30 20:12:34 +00:00
|
|
|
test('Custom size', () => {
|
2022-07-01 19:12:18 +00:00
|
|
|
const custom: IconifyIconCustomisations = {
|
2022-06-20 20:43:01 +00:00
|
|
|
height: 'auto',
|
|
|
|
};
|
2022-07-01 19:12:18 +00:00
|
|
|
const icon: IconifyIcon = {
|
2020-04-28 09:47:35 +00:00
|
|
|
width: 20,
|
|
|
|
height: 16,
|
|
|
|
body: '<path d="..." />',
|
2022-06-19 14:12:26 +00:00
|
|
|
};
|
2020-04-28 09:47:35 +00:00
|
|
|
const expected: IconifyIconBuildResult = {
|
|
|
|
attributes: {
|
|
|
|
width: '20',
|
|
|
|
height: '16',
|
|
|
|
viewBox: '0 0 20 16',
|
|
|
|
},
|
|
|
|
body: '<path d="..." />',
|
|
|
|
};
|
|
|
|
|
|
|
|
const result = iconToSVG(icon, custom);
|
2021-09-20 20:53:49 +00:00
|
|
|
expect(result).toEqual(expected);
|
2020-04-28 09:47:35 +00:00
|
|
|
});
|
|
|
|
|
2022-04-30 20:12:34 +00:00
|
|
|
test('Rotation', () => {
|
2022-07-01 19:12:18 +00:00
|
|
|
const custom: IconifyIconCustomisations = {
|
2022-06-20 20:43:01 +00:00
|
|
|
height: '40px',
|
|
|
|
rotate: 1,
|
|
|
|
};
|
2022-07-01 19:12:18 +00:00
|
|
|
const icon: IconifyIcon = {
|
2020-04-28 09:47:35 +00:00
|
|
|
width: 20,
|
|
|
|
height: 16,
|
|
|
|
body: '<path d="..." />',
|
2022-06-19 14:12:26 +00:00
|
|
|
};
|
2020-04-28 09:47:35 +00:00
|
|
|
const expected: IconifyIconBuildResult = {
|
|
|
|
attributes: {
|
|
|
|
width: '32px',
|
|
|
|
height: '40px',
|
|
|
|
viewBox: '0 0 16 20',
|
|
|
|
},
|
|
|
|
body: '<g transform="rotate(90 8 8)"><path d="..." /></g>',
|
|
|
|
};
|
|
|
|
|
|
|
|
const result = iconToSVG(icon, custom);
|
2021-09-20 20:53:49 +00:00
|
|
|
expect(result).toEqual(expected);
|
2020-04-28 09:47:35 +00:00
|
|
|
});
|
|
|
|
|
2021-09-20 20:53:49 +00:00
|
|
|
test('Negative rotation', () => {
|
2022-07-01 19:12:18 +00:00
|
|
|
const custom: IconifyIconCustomisations = {
|
2022-06-20 20:43:01 +00:00
|
|
|
height: '40px',
|
|
|
|
rotate: -1,
|
|
|
|
};
|
2022-07-01 19:12:18 +00:00
|
|
|
const icon: IconifyIcon = {
|
2021-05-11 18:03:10 +00:00
|
|
|
width: 20,
|
|
|
|
height: 16,
|
|
|
|
body: '<path d="..." />',
|
2022-06-19 14:12:26 +00:00
|
|
|
};
|
2021-05-11 18:03:10 +00:00
|
|
|
const expected: IconifyIconBuildResult = {
|
|
|
|
attributes: {
|
|
|
|
width: '32px',
|
|
|
|
height: '40px',
|
|
|
|
viewBox: '0 0 16 20',
|
|
|
|
},
|
|
|
|
body: '<g transform="rotate(-90 10 10)"><path d="..." /></g>',
|
|
|
|
};
|
|
|
|
|
|
|
|
const result = iconToSVG(icon, custom);
|
2021-09-20 20:53:49 +00:00
|
|
|
expect(result).toEqual(expected);
|
2021-05-11 18:03:10 +00:00
|
|
|
});
|
|
|
|
|
2022-04-30 20:12:34 +00:00
|
|
|
test('Flip', () => {
|
2022-07-01 19:12:18 +00:00
|
|
|
const custom: IconifyIconCustomisations = {
|
2022-06-20 20:43:01 +00:00
|
|
|
height: '32',
|
|
|
|
hFlip: true,
|
|
|
|
};
|
2022-07-01 19:12:18 +00:00
|
|
|
const icon: IconifyIcon = {
|
2020-04-28 09:47:35 +00:00
|
|
|
width: 20,
|
|
|
|
height: 16,
|
|
|
|
body: '<path d="..." />',
|
2022-06-19 14:12:26 +00:00
|
|
|
};
|
2020-04-28 09:47:35 +00:00
|
|
|
const expected: IconifyIconBuildResult = {
|
|
|
|
attributes: {
|
|
|
|
width: '40',
|
|
|
|
height: '32',
|
|
|
|
viewBox: '0 0 20 16',
|
|
|
|
},
|
2021-05-11 18:03:10 +00:00
|
|
|
body: '<g transform="translate(20 0) scale(-1 1)"><path d="..." /></g>',
|
2020-04-28 09:47:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const result = iconToSVG(icon, custom);
|
2021-09-20 20:53:49 +00:00
|
|
|
expect(result).toEqual(expected);
|
2020-04-28 09:47:35 +00:00
|
|
|
});
|
|
|
|
|
2021-09-20 20:53:49 +00:00
|
|
|
test('Flip, rotation', () => {
|
2022-07-01 19:12:18 +00:00
|
|
|
const custom: IconifyIconCustomisations = {
|
2022-06-20 20:43:01 +00:00
|
|
|
hFlip: true,
|
|
|
|
rotate: 1,
|
|
|
|
};
|
2022-07-01 19:12:18 +00:00
|
|
|
const icon: IconifyIcon = {
|
2020-04-28 09:47:35 +00:00
|
|
|
width: 20,
|
|
|
|
height: 16,
|
|
|
|
body: '<path d="..." />',
|
2022-06-19 14:12:26 +00:00
|
|
|
};
|
2020-04-28 09:47:35 +00:00
|
|
|
const expected: IconifyIconBuildResult = {
|
|
|
|
attributes: {
|
|
|
|
width: '0.8em',
|
|
|
|
height: '1em',
|
|
|
|
viewBox: '0 0 16 20',
|
|
|
|
},
|
2021-05-11 18:03:10 +00:00
|
|
|
body: '<g transform="rotate(90 8 8) translate(20 0) scale(-1 1)"><path d="..." /></g>',
|
|
|
|
};
|
|
|
|
|
|
|
|
const result = iconToSVG(icon, custom);
|
2021-09-20 20:53:49 +00:00
|
|
|
expect(result).toEqual(expected);
|
2021-05-11 18:03:10 +00:00
|
|
|
});
|
|
|
|
|
2021-09-20 20:53:49 +00:00
|
|
|
test('Flip icon that is rotated by default', () => {
|
2022-07-01 19:12:18 +00:00
|
|
|
const custom: IconifyIconCustomisations = {
|
2022-06-20 20:43:01 +00:00
|
|
|
hFlip: true,
|
|
|
|
};
|
2022-07-01 19:12:18 +00:00
|
|
|
const icon: IconifyIcon = {
|
2021-05-11 18:03:10 +00:00
|
|
|
width: 20,
|
|
|
|
height: 16,
|
|
|
|
body: '<path d="..." />',
|
|
|
|
rotate: 1,
|
2022-06-19 14:12:26 +00:00
|
|
|
};
|
2021-05-11 18:03:10 +00:00
|
|
|
|
|
|
|
// Horizontally flipping icon that has 90 or 270 degrees rotation will result in vertical flip.
|
|
|
|
// Therefore result should be rotation + vertical flip to visually match horizontal flip on normal icon.
|
|
|
|
const expected: IconifyIconBuildResult = {
|
|
|
|
attributes: {
|
|
|
|
width: '0.8em',
|
|
|
|
height: '1em',
|
|
|
|
viewBox: '0 0 16 20',
|
|
|
|
},
|
|
|
|
body: '<g transform="translate(16 0) scale(-1 1)"><g transform="rotate(90 8 8)"><path d="..." /></g></g>',
|
2020-04-28 09:47:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const result = iconToSVG(icon, custom);
|
2021-09-20 20:53:49 +00:00
|
|
|
expect(result).toEqual(expected);
|
2020-04-28 09:47:35 +00:00
|
|
|
});
|
|
|
|
|
2021-09-20 20:53:49 +00:00
|
|
|
test('Flip and rotation canceling eachother', () => {
|
2022-07-01 19:12:18 +00:00
|
|
|
const custom: IconifyIconCustomisations = {
|
2022-06-20 20:43:01 +00:00
|
|
|
width: '1em',
|
|
|
|
height: 'auto',
|
|
|
|
hFlip: true,
|
|
|
|
vFlip: true,
|
|
|
|
rotate: 2,
|
|
|
|
};
|
2022-07-01 19:12:18 +00:00
|
|
|
const icon: IconifyIcon = {
|
2020-04-28 09:47:35 +00:00
|
|
|
width: 20,
|
|
|
|
height: 16,
|
|
|
|
body: '<path d="..." />',
|
2022-06-19 14:12:26 +00:00
|
|
|
};
|
2020-04-28 09:47:35 +00:00
|
|
|
const expected: IconifyIconBuildResult = {
|
|
|
|
attributes: {
|
|
|
|
width: '1em',
|
|
|
|
height: '16',
|
|
|
|
viewBox: '0 0 20 16',
|
|
|
|
},
|
|
|
|
body: '<path d="..." />',
|
|
|
|
};
|
|
|
|
|
|
|
|
const result = iconToSVG(icon, custom);
|
2021-09-20 20:53:49 +00:00
|
|
|
expect(result).toEqual(expected);
|
2020-04-28 09:47:35 +00:00
|
|
|
});
|
2020-04-29 08:15:37 +00:00
|
|
|
|
2022-07-01 19:12:18 +00:00
|
|
|
test('Flip with icon, no customisations', () => {
|
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>';
|
|
|
|
|
2022-07-01 19:12:18 +00:00
|
|
|
const icon: IconifyIcon = {
|
2020-04-29 08:15:37 +00:00
|
|
|
body: iconBody,
|
|
|
|
width: 128,
|
|
|
|
height: 128,
|
|
|
|
hFlip: true,
|
2022-06-19 14:12:26 +00:00
|
|
|
};
|
2020-04-29 08:15:37 +00:00
|
|
|
const expected: IconifyIconBuildResult = {
|
|
|
|
attributes: {
|
|
|
|
width: '1em',
|
|
|
|
height: '1em',
|
|
|
|
viewBox: '0 0 128 128',
|
|
|
|
},
|
|
|
|
body:
|
|
|
|
'<g transform="translate(128 0) scale(-1 1)">' +
|
|
|
|
iconBody +
|
|
|
|
'</g>',
|
|
|
|
};
|
|
|
|
|
2022-07-01 19:12:18 +00:00
|
|
|
const result = iconToSVG(icon);
|
2021-09-20 20:53:49 +00:00
|
|
|
expect(result).toEqual(expected);
|
2020-04-29 08:15:37 +00:00
|
|
|
});
|
2020-04-28 09:47:35 +00:00
|
|
|
});
|