Skip to content

Commit

Permalink
parse src
Browse files Browse the repository at this point in the history
  • Loading branch information
danielleroux committed Jun 5, 2023
1 parent d8abb9d commit dcd7d31
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/components/icon/resolveIcon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ async function fetchSVG(url: string) {
return '';
}

return responseText;
return parseSVGDataContent(responseText);
}

export async function resolveIcon(icon: Icon) {
Expand Down
84 changes: 84 additions & 0 deletions src/components/icon/test/resolveIcon.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* COPYRIGHT (c) Siemens AG 2018-2023 ALL RIGHTS RESERVED.
*/
import { Icon } from '../icon';
import { resolveIcon } from '../resolveIcon';
import { rocket } from './rocker-example';

const exampleSvg = `
<?xml version="1.0" encoding="UTF-8"?>
<svg width="512px" height="512px" viewBox="0 0 512 512" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<title>add</title>
<g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
<g id="Shape" fill="#000000" transform="translate(65.929697, 65.929697)">
<polygon points="211.189225 2.36847579e-14 211.189225 168.95138 380.140606 168.95138 380.140606 211.189225 211.189225 211.189225 211.189225 380.140606 168.95138 380.140606 168.95138 211.189225 -1.42108547e-14 211.189225 -1.42108547e-14 168.95138 168.95138 168.95138 168.95138 -1.42108547e-14"></polygon>
</g>
</g>
</svg>
`;

const invalidexampleSvg = `
<?xml version="1.0" encoding="UTF-8"?>
<script>
alert('Test!');
</script>
`;

let fetch = (global.fetch = jest.fn(() =>
Promise.resolve({
text: () => Promise.resolve(exampleSvg),
ok: true,
}),
) as jest.Mock);

describe('resolve icon', () => {
beforeEach(() => {
fetch.mockClear();
});

it('should resolve font name from name', async () => {
const icon = new Icon();
icon.name = 'rocket';
icon.src = 'http://localhost/test.svg';

const expectedName = await resolveIcon(icon);

expect(expectedName).toBeUndefined();
});

it('should resolve svg from name', async () => {
const icon = new Icon();
icon.name = rocket;
icon.src = 'http://localhost/test.svg';

const expectedName: string = await resolveIcon(icon);

expect(expectedName.startsWith('<svg')).toBeTruthy();
expect(expectedName.endsWith('</svg>')).toBeTruthy();
});

it('should resolve svg from src', async () => {
const icon = new Icon();
icon.src = 'http://localhost/test.svg';

const expectedName = await resolveIcon(icon);

expect(expectedName).toEqual(
`<svg width=\"512px\" height=\"512px\" viewBox=\"0 0 512 512\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"> <title>add</title> <g id=\"Page-1\" stroke=\"none\" stroke-width=\"1\" fill=\"none\" fill-rule=\"evenodd\"> <g id=\"Shape\" fill=\"#000000\" transform=\"translate(65.929697, 65.929697)\"> <polygon points=\"211.189225 2.36847579e-14 211.189225 168.95138 380.140606 168.95138 380.140606 211.189225 211.189225 211.189225 211.189225 380.140606 168.95138 380.140606 168.95138 211.189225 -1.42108547e-14 211.189225 -1.42108547e-14 168.95138 168.95138 168.95138 168.95138 -1.42108547e-14\"></polygon> </g> </g> </svg>`,
);
});

it('should not resolve invalid svg from src', async () => {
fetch = global.fetch = jest.fn(() =>
Promise.resolve({
text: () => Promise.resolve(invalidexampleSvg),
ok: true,
}),
) as jest.Mock;

const icon = new Icon();
icon.src = 'http://localhost/test.svg';

await expect(resolveIcon(icon)).rejects.toThrow('No valid svg data provided');
});
});

0 comments on commit dcd7d31

Please sign in to comment.