2
0
mirror of https://github.com/iconify/iconify.git synced 2024-11-10 07:11:00 +00:00
iconify/packages/utils/tests/icon-name-test.ts

180 lines
4.6 KiB
TypeScript
Raw Normal View History

import type { IconifyIconName } from '../lib/icon/name';
import { stringToIcon, validateIcon } from '../lib/icon/name';
2020-04-28 09:47:35 +00:00
describe('Testing icon name', () => {
2021-09-20 20:53:49 +00:00
test('Simple icon names', () => {
2020-04-28 09:47:35 +00:00
let icon;
// Simple prefix-name
icon = stringToIcon('fa-home') as IconifyIconName;
2021-09-20 20:53:49 +00:00
expect(icon).toEqual({
provider: '',
2020-04-28 09:47:35 +00:00
prefix: 'fa',
name: 'home',
});
2021-09-20 20:53:49 +00:00
expect(validateIcon(icon)).toBe(true);
2020-04-28 09:47:35 +00:00
// Simple prefix:name
icon = stringToIcon('fa:arrow-left') as IconifyIconName;
2021-09-20 20:53:49 +00:00
expect(icon).toEqual({
provider: '',
2020-04-28 09:47:35 +00:00
prefix: 'fa',
name: 'arrow-left',
});
2021-09-20 20:53:49 +00:00
expect(validateIcon(icon)).toBe(true);
2020-04-28 09:47:35 +00:00
// Longer prefix:name
icon = stringToIcon('mdi-light:home-outline') as IconifyIconName;
2021-09-20 20:53:49 +00:00
expect(icon).toEqual({
provider: '',
2020-04-28 09:47:35 +00:00
prefix: 'mdi-light',
name: 'home-outline',
});
2021-09-20 20:53:49 +00:00
expect(validateIcon(icon)).toBe(true);
2020-04-28 09:47:35 +00:00
// Simple word without prefix
icon = stringToIcon('home');
2021-09-20 20:53:49 +00:00
expect(icon).toEqual(null);
expect(validateIcon(icon)).toBe(false);
// Same as above, but with empty names enabled
icon = stringToIcon('home', false, true);
2021-09-20 20:53:49 +00:00
expect(icon).toEqual({
provider: '',
prefix: '',
name: 'home',
});
2021-09-20 20:53:49 +00:00
expect(validateIcon(icon)).toBe(false);
expect(validateIcon(icon, true)).toBe(true);
// Missing icon name
icon = stringToIcon('@iconify-home-icon');
2021-09-20 20:53:49 +00:00
expect(icon).toEqual(null);
expect(validateIcon(icon)).toBe(false);
// Same as above, but with empty names enabled
icon = stringToIcon('@iconify-home-icon', false, true);
2021-09-20 20:53:49 +00:00
expect(icon).toEqual(null);
expect(validateIcon(icon)).toBe(false);
expect(validateIcon(icon, true)).toBe(false);
2020-04-28 09:47:35 +00:00
// Underscore is not an acceptable separator
icon = stringToIcon('fa_home');
2021-09-20 20:53:49 +00:00
expect(icon).toEqual(null);
expect(validateIcon(icon)).toBe(false);
2020-04-28 09:47:35 +00:00
// Same as above, but with empty names enabled
icon = stringToIcon('fa_home', false, true);
2021-09-20 20:53:49 +00:00
expect(icon).toEqual({
provider: '',
prefix: '',
name: 'fa_home',
});
2021-09-20 20:53:49 +00:00
expect(validateIcon(icon)).toBe(false);
expect(validateIcon(icon, true)).toBe(false);
2020-04-28 09:47:35 +00:00
// Invalid character '_': fail validateIcon
icon = stringToIcon('fa:home_outline') as IconifyIconName;
2021-09-20 20:53:49 +00:00
expect(icon).toEqual({
provider: '',
2020-04-28 09:47:35 +00:00
prefix: 'fa',
name: 'home_outline',
});
2021-09-20 20:53:49 +00:00
expect(validateIcon(icon)).toBe(false);
2020-04-28 09:47:35 +00:00
// Too many colons: fail stringToIcon
icon = stringToIcon('mdi:light:home:outline');
2021-09-20 20:53:49 +00:00
expect(icon).toEqual(null);
expect(validateIcon(icon)).toBe(false);
2020-04-28 09:47:35 +00:00
// Upper case: fail validateIcon
icon = stringToIcon('MD:Home') as IconifyIconName;
2021-09-20 20:53:49 +00:00
expect(icon).toEqual({
provider: '',
2020-04-28 09:47:35 +00:00
prefix: 'MD',
name: 'Home',
});
2021-09-20 20:53:49 +00:00
expect(validateIcon(icon)).toBe(false);
2020-04-28 09:47:35 +00:00
// Numbers: pass
icon = stringToIcon('1:foo') as IconifyIconName;
2021-09-20 20:53:49 +00:00
expect(icon).toEqual({
provider: '',
2020-04-28 09:47:35 +00:00
prefix: '1',
name: 'foo',
});
2021-09-20 20:53:49 +00:00
expect(validateIcon(icon)).toBe(true);
2020-04-28 09:47:35 +00:00
// Accented letters: fail validateIcon
icon = stringToIcon('md-fõö') as IconifyIconName;
2021-09-20 20:53:49 +00:00
expect(icon).toEqual({
provider: '',
2020-04-28 09:47:35 +00:00
prefix: 'md',
name: 'fõö',
});
2021-09-20 20:53:49 +00:00
expect(validateIcon(icon)).toBe(false);
2020-04-28 09:47:35 +00:00
});
it('Providers', () => {
let icon;
// Simple @provider:prefix-name
icon = stringToIcon('@iconify:fa-home') as IconifyIconName;
2021-09-20 20:53:49 +00:00
expect(icon).toEqual({
provider: 'iconify',
prefix: 'fa',
name: 'home',
});
2021-09-20 20:53:49 +00:00
expect(validateIcon(icon)).toBe(true);
// Simple @provider:prefix:name
icon = stringToIcon('@iconify:fa:arrow-left') as IconifyIconName;
2021-09-20 20:53:49 +00:00
expect(icon).toEqual({
provider: 'iconify',
prefix: 'fa',
name: 'arrow-left',
});
2021-09-20 20:53:49 +00:00
expect(validateIcon(icon)).toBe(true);
// Longer @provider:prefix:name
icon = stringToIcon(
'@iconify-backup:mdi-light:home-outline'
) as IconifyIconName;
2021-09-20 20:53:49 +00:00
expect(icon).toEqual({
provider: 'iconify-backup',
prefix: 'mdi-light',
name: 'home-outline',
});
2021-09-20 20:53:49 +00:00
expect(validateIcon(icon)).toBe(true);
// Missing @ for provider
icon = stringToIcon(
'iconify-backup:mdi-light:home-outline'
) as IconifyIconName;
2021-09-20 20:53:49 +00:00
expect(icon).toEqual({
provider: 'iconify-backup',
prefix: 'mdi-light',
name: 'home-outline',
});
2021-09-20 20:53:49 +00:00
expect(validateIcon(icon)).toBe(true);
// Too many colons: fail stringToIcon
icon = stringToIcon('@mdi:light:home:outline');
2021-09-20 20:53:49 +00:00
expect(icon).toEqual(null);
expect(validateIcon(icon)).toBe(false);
// Same as above, empty names allowed
icon = stringToIcon('@mdi:light:home:outline', false, true);
2021-09-20 20:53:49 +00:00
expect(icon).toEqual(null);
// Upper case: fail validateIcon
icon = stringToIcon('@MD:home-outline') as IconifyIconName;
2021-09-20 20:53:49 +00:00
expect(icon).toEqual({
provider: 'MD',
prefix: 'home',
name: 'outline',
});
2021-09-20 20:53:49 +00:00
expect(validateIcon(icon)).toBe(false);
});
2020-04-28 09:47:35 +00:00
});