From 30c4ddf3f1dc01566a4cbee946c03b080cbab932 Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Sun, 9 Jan 2022 23:23:02 +0200 Subject: [PATCH] Add annotations, clean up getCustomIcon and test loading bad icon --- packages/utils/src/loader/custom.ts | 3 +++ packages/utils/src/loader/types.ts | 11 +++++++++ packages/utils/tests/fixtures/1f3eb.svg | 1 + packages/utils/tests/get-custom-icon-test.ts | 25 ++++++++++++++++++-- 4 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 packages/utils/tests/fixtures/1f3eb.svg diff --git a/packages/utils/src/loader/custom.ts b/packages/utils/src/loader/custom.ts index efef755..26137c8 100644 --- a/packages/utils/src/loader/custom.ts +++ b/packages/utils/src/loader/custom.ts @@ -4,6 +4,9 @@ import type { CustomIconLoader, InlineCollection } from './types'; const debug = createDebugger('@iconify-loader:custom'); +/** + * Get custom icon from inline collection or using loader + */ export async function getCustomIcon( custom: CustomIconLoader | InlineCollection, collection: string, diff --git a/packages/utils/src/loader/types.ts b/packages/utils/src/loader/types.ts index 9a70a27..90fe59c 100644 --- a/packages/utils/src/loader/types.ts +++ b/packages/utils/src/loader/types.ts @@ -1,10 +1,21 @@ import type { Awaitable } from '@antfu/utils'; +/** + * Custom icon loader, used by getCustomIcon() + */ export type CustomIconLoader = (name: string) => Awaitable; + +/** + * List of icons as object. Key is icon name, value is icon data or callback (can be async) to get icon data + */ export type InlineCollection = Record< string, string | (() => Awaitable) >; + +/** + * Collection of custom icons. Key is collection name, value is loader or InlineCollection object + */ export type CustomCollections = Record< string, CustomIconLoader | InlineCollection diff --git a/packages/utils/tests/fixtures/1f3eb.svg b/packages/utils/tests/fixtures/1f3eb.svg new file mode 100644 index 0000000..cc6b9c9 --- /dev/null +++ b/packages/utils/tests/fixtures/1f3eb.svg @@ -0,0 +1 @@ +image/svg+xml diff --git a/packages/utils/tests/get-custom-icon-test.ts b/packages/utils/tests/get-custom-icon-test.ts index 924b05a..e505820 100644 --- a/packages/utils/tests/get-custom-icon-test.ts +++ b/packages/utils/tests/get-custom-icon-test.ts @@ -1,15 +1,17 @@ +import { promises as fs } from 'fs'; import { getCustomIcon } from '../lib'; -const svg = - ''; +const fixturesDir = __dirname + '/fixtures'; describe('Testing getCustomIcon', () => { test('CustomIconLoader', async () => { + const svg = await fs.readFile(fixturesDir + '/circle.svg', 'utf8'); const result = await getCustomIcon(() => svg, 'a', 'b'); expect(svg).toEqual(result); }); test('CustomIconLoader with transform', async () => { + const svg = await fs.readFile(fixturesDir + '/circle.svg', 'utf8'); const result = await getCustomIcon( () => svg, 'a', @@ -21,4 +23,23 @@ describe('Testing getCustomIcon', () => { expect(result && result.indexOf('width="1em"') > -1).toBeTruthy(); expect(result && result.indexOf('height="1em"') > -1).toBeTruthy(); }); + + test('Icon with XML heading', async () => { + // 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; + + expect(svg).toEqual(result); + expect(warned).toEqual(true); + }); });