2
0
mirror of https://github.com/iconify/iconify.git synced 2024-12-13 14:13:06 +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';
2022-07-01 19:01:49 +00:00
import { stringToIcon, validateIconName } 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',
});
2022-07-01 19:01:49 +00:00
expect(validateIconName(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',
});
2022-07-01 19:01:49 +00:00
expect(validateIconName(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',
});
2022-07-01 19:01:49 +00:00
expect(validateIconName(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);
2022-07-01 19:01:49 +00:00
expect(validateIconName(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',
});
2022-07-01 19:01:49 +00:00
expect(validateIconName(icon)).toBe(false);
expect(validateIconName(icon, true)).toBe(true);
// Missing icon name
icon = stringToIcon('@iconify-home-icon');
2021-09-20 20:53:49 +00:00
expect(icon).toEqual(null);
2022-07-01 19:01:49 +00:00
expect(validateIconName(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);
2022-07-01 19:01:49 +00:00
expect(validateIconName(icon)).toBe(false);
expect(validateIconName(icon, true)).toBe(false);
// No prefix
2020-04-28 09:47:35 +00:00
icon = stringToIcon('fa_home');
2021-09-20 20:53:49 +00:00
expect(icon).toEqual(null);
2022-07-01 19:01:49 +00:00
expect(validateIconName(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',
});
2022-07-01 19:01:49 +00:00
expect(validateIconName(icon)).toBe(false);
expect(validateIconName(icon, true)).toBe(true);
// Underscore is allowed now
2020-04-28 09:47:35 +00:00
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',
});
expect(validateIconName(icon)).toBe(true);
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);
2022-07-01 19:01:49 +00:00
expect(validateIconName(icon)).toBe(false);
2020-04-28 09:47:35 +00:00
// Upper case: allowed now
2020-04-28 09:47:35 +00:00
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',
});
expect(validateIconName(icon)).toBe(true);
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',
});
2022-07-01 19:01:49 +00:00
expect(validateIconName(icon)).toBe(true);
2020-04-28 09:47:35 +00:00
// Accented letters: allowed now
2020-04-28 09:47:35 +00:00
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õö',
});
expect(validateIconName(icon)).toBe(true);
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',
});
2022-07-01 19:01:49 +00:00
expect(validateIconName(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',
});
2022-07-01 19:01:49 +00:00
expect(validateIconName(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',
});
2022-07-01 19:01:49 +00:00
expect(validateIconName(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',
});
2022-07-01 19:01:49 +00:00
expect(validateIconName(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);
2022-07-01 19:01:49 +00:00
expect(validateIconName(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: allowed now
icon = stringToIcon('@MD:home-outline') as IconifyIconName;
2021-09-20 20:53:49 +00:00
expect(icon).toEqual({
provider: 'MD',
prefix: 'home',
name: 'outline',
});
expect(validateIconName(icon)).toBe(true);
});
2020-04-28 09:47:35 +00:00
});