2022-03-03 23:36:30 +00:00
|
|
|
import { loadNodeIcon } from '../lib/loader/node-loader';
|
2022-03-02 17:55:03 +00:00
|
|
|
|
2024-07-29 08:34:08 +00:00
|
|
|
describe('Testing loadNodeIcon', () => {
|
2022-03-02 17:55:03 +00:00
|
|
|
test('loadIcon works', async () => {
|
2022-03-03 23:36:30 +00:00
|
|
|
const result = await loadNodeIcon('flat-color-icons', 'up-right');
|
2022-03-02 17:55:03 +00:00
|
|
|
expect(result).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
2023-02-13 22:06:13 +00:00
|
|
|
test('loadIcon should not add xmlns:xlink', async () => {
|
2022-03-14 14:39:40 +00:00
|
|
|
const result = await loadNodeIcon('flat-color-icons', 'up-right', {
|
|
|
|
addXmlNs: true,
|
|
|
|
});
|
2022-03-02 17:55:03 +00:00
|
|
|
expect(result).toBeTruthy();
|
2024-07-29 08:08:43 +00:00
|
|
|
expect(result && !result.includes('xmlns:xlink=')).toBeTruthy();
|
2022-03-02 17:55:03 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('loadIcon with customize with default style and class', async () => {
|
2022-03-03 23:36:30 +00:00
|
|
|
const result = await loadNodeIcon('flat-color-icons', 'up-right', {
|
2022-03-02 17:55:03 +00:00
|
|
|
defaultStyle: 'margin-top: 1rem;',
|
|
|
|
defaultClass: 'clazz',
|
|
|
|
customizations: {
|
2024-07-29 08:08:43 +00:00
|
|
|
customize(props, data, name) {
|
|
|
|
// Check props
|
|
|
|
expect(props.width).toBeNull();
|
|
|
|
expect(data.width).toBe(48);
|
|
|
|
expect(data.height).toBe(48);
|
|
|
|
expect(name).toBe('flat-color-icons:up-right');
|
|
|
|
|
|
|
|
// Change props
|
2022-03-02 17:55:03 +00:00
|
|
|
props.width = '2em';
|
|
|
|
props.height = '2em';
|
|
|
|
return props;
|
|
|
|
},
|
2022-03-04 16:37:21 +00:00
|
|
|
},
|
2022-03-02 17:55:03 +00:00
|
|
|
});
|
|
|
|
expect(result).toBeTruthy();
|
2024-07-29 08:08:43 +00:00
|
|
|
expect(result && result.includes('margin-top: 1rem;')).toBeTruthy();
|
|
|
|
expect(result && result.includes('class="clazz"')).toBeTruthy();
|
|
|
|
expect(result && result.includes('width="2em"')).toBeTruthy();
|
|
|
|
expect(result && result.includes('height="2em"')).toBeTruthy();
|
2022-03-02 17:55:03 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('loadIcon preserves customizations order', async () => {
|
2022-03-03 23:36:30 +00:00
|
|
|
const result = await loadNodeIcon('flat-color-icons', 'up-right', {
|
2022-03-02 17:55:03 +00:00
|
|
|
scale: 1,
|
|
|
|
defaultStyle: 'color: red;',
|
|
|
|
defaultClass: 'clazz1',
|
|
|
|
customizations: {
|
|
|
|
additionalProps: {
|
2022-03-04 16:37:21 +00:00
|
|
|
width: '2em',
|
|
|
|
height: '2em',
|
|
|
|
style: 'color: blue;',
|
|
|
|
class: 'clazz2',
|
2022-03-02 17:55:03 +00:00
|
|
|
},
|
|
|
|
// it will never be called, it is not a custom icon
|
2022-03-24 20:16:13 +00:00
|
|
|
transform(svg) {
|
|
|
|
return svg.replace(
|
2022-03-04 16:37:21 +00:00
|
|
|
'<svg ',
|
|
|
|
'<svg width="4em" height="4em" '
|
|
|
|
);
|
2022-03-02 17:55:03 +00:00
|
|
|
},
|
2022-03-04 16:37:21 +00:00
|
|
|
},
|
2022-03-02 17:55:03 +00:00
|
|
|
});
|
|
|
|
expect(result).toBeTruthy();
|
|
|
|
expect(result && result.includes('style="color: blue;"')).toBeTruthy();
|
|
|
|
expect(result && result.includes('class="clazz2"')).toBeTruthy();
|
|
|
|
expect(result && result.includes('width="2em"')).toBeTruthy();
|
|
|
|
expect(result && result.includes('height="2em"')).toBeTruthy();
|
|
|
|
});
|
2022-03-22 18:08:38 +00:00
|
|
|
|
|
|
|
test('loadIcon apply scale', async () => {
|
|
|
|
const result = await loadNodeIcon('flat-color-icons', 'up-right', {
|
|
|
|
scale: 1.2,
|
|
|
|
defaultStyle: 'color: red;',
|
|
|
|
defaultClass: 'clazz1',
|
|
|
|
customizations: {
|
|
|
|
additionalProps: {
|
|
|
|
style: 'color: blue;',
|
|
|
|
class: 'clazz2',
|
|
|
|
},
|
|
|
|
// it will never be called, it is not a custom icon
|
2022-03-24 20:16:13 +00:00
|
|
|
transform(svg) {
|
|
|
|
return svg.replace(
|
2022-03-22 18:08:38 +00:00
|
|
|
'<svg ',
|
|
|
|
'<svg width="4em" height="4em" '
|
|
|
|
);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
expect(result).toBeTruthy();
|
|
|
|
expect(result && result.includes('style="color: blue;"')).toBeTruthy();
|
|
|
|
expect(result && result.includes('class="clazz2"')).toBeTruthy();
|
|
|
|
expect(result && result.includes('width="1.2em"')).toBeTruthy();
|
|
|
|
expect(result && result.includes('height="1.2em"')).toBeTruthy();
|
|
|
|
});
|
2023-01-27 08:13:39 +00:00
|
|
|
|
|
|
|
test('loadIcon with unset width', async () => {
|
|
|
|
const result = await loadNodeIcon('flat-color-icons', 'up-right', {
|
|
|
|
customizations: {
|
|
|
|
additionalProps: {
|
|
|
|
width: 'unset',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
expect(result).toBeTruthy();
|
|
|
|
expect(result && result.includes('width="')).toBeFalsy();
|
|
|
|
expect(result && result.includes('height="1em"')).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
2024-07-29 08:08:43 +00:00
|
|
|
test('loadIcon with custom width/height', async () => {
|
2024-07-29 07:23:51 +00:00
|
|
|
const result = await loadNodeIcon('flat-color-icons', 'up-right', {
|
|
|
|
customizations: {
|
|
|
|
customize(props) {
|
|
|
|
props.width = '2em';
|
|
|
|
props.height = '1em';
|
|
|
|
return props;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
expect(result).toBeTruthy();
|
|
|
|
expect(result && result.includes('width="2em"')).toBeTruthy();
|
|
|
|
expect(result && result.includes('height="1em"')).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
2024-07-29 07:42:23 +00:00
|
|
|
test('loadIcon with custom width/height and scale', async () => {
|
|
|
|
const result = await loadNodeIcon('flat-color-icons', 'up-right', {
|
|
|
|
customizations: {
|
|
|
|
customize(props) {
|
|
|
|
props.width = '3em';
|
|
|
|
props.height = '2em';
|
|
|
|
return props;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
scale: 1.5,
|
|
|
|
});
|
|
|
|
expect(result).toBeTruthy();
|
|
|
|
expect(result && result.includes('width="4.5em"')).toBeTruthy();
|
|
|
|
expect(result && result.includes('height="3em"')).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
2023-01-27 08:13:39 +00:00
|
|
|
test('loadIcon with 0 scale', async () => {
|
|
|
|
const result = await loadNodeIcon('flat-color-icons', 'up-right', {
|
|
|
|
scale: 0,
|
|
|
|
});
|
|
|
|
expect(result).toBeTruthy();
|
|
|
|
expect(result && result.includes('width="')).toBeFalsy();
|
|
|
|
expect(result && result.includes('height="')).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('loadIcon with 0 scale and custom height', async () => {
|
|
|
|
const result = await loadNodeIcon('flat-color-icons', 'up-right', {
|
|
|
|
scale: 0,
|
|
|
|
customizations: {
|
|
|
|
additionalProps: {
|
|
|
|
height: '1em',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
expect(result).toBeTruthy();
|
|
|
|
expect(result && result.includes('width="')).toBeFalsy();
|
|
|
|
expect(result && result.includes('height="1em"')).toBeTruthy();
|
|
|
|
});
|
2024-07-28 19:57:37 +00:00
|
|
|
|
|
|
|
test('loadIcon with bad cwd', async () => {
|
|
|
|
const result = await loadNodeIcon('flat-color-icons', 'up-left', {
|
|
|
|
cwd: './tests',
|
|
|
|
});
|
|
|
|
expect(result).toBeUndefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('loadIcon with multiple cwd', async () => {
|
|
|
|
const result = await loadNodeIcon('flat-color-icons', 'up-left', {
|
|
|
|
cwd: ['./tests', process.cwd()],
|
|
|
|
});
|
|
|
|
expect(result).toBeTruthy();
|
|
|
|
});
|
2024-07-29 08:34:08 +00:00
|
|
|
|
|
|
|
test('loadIcon with non-square icon', async () => {
|
|
|
|
const result = await loadNodeIcon('fa6-regular', 'bookmark');
|
|
|
|
expect(result).toBeTruthy();
|
|
|
|
expect(result && result.includes('width="0.75em"')).toBeTruthy();
|
|
|
|
expect(result && result.includes('height="1em"')).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('loadIcon with non-square icon with scale', async () => {
|
|
|
|
const result = await loadNodeIcon('fa6-regular', 'bookmark', {
|
|
|
|
scale: 1,
|
|
|
|
});
|
|
|
|
expect(result).toBeTruthy();
|
|
|
|
expect(result && result.includes('width="0.75em"')).toBeTruthy();
|
|
|
|
expect(result && result.includes('height="1em"')).toBeTruthy();
|
|
|
|
});
|
2022-03-02 17:55:03 +00:00
|
|
|
});
|