2021-05-24 14:02:00 +00:00
|
|
|
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({
|
2020-05-29 19:08:45 +00:00
|
|
|
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({
|
2020-05-29 19:08:45 +00:00
|
|
|
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({
|
2020-05-29 19:08:45 +00:00
|
|
|
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
|
|
|
|
2021-04-23 20:31:36 +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);
|
2021-04-23 20:31:36 +00:00
|
|
|
|
|
|
|
// Same as above, but with empty names enabled
|
|
|
|
icon = stringToIcon('home', false, true);
|
2021-09-20 20:53:49 +00:00
|
|
|
expect(icon).toEqual({
|
2021-04-23 20:31:36 +00:00
|
|
|
provider: '',
|
|
|
|
prefix: '',
|
|
|
|
name: 'home',
|
|
|
|
});
|
2022-07-01 19:01:49 +00:00
|
|
|
expect(validateIconName(icon)).toBe(false);
|
|
|
|
expect(validateIconName(icon, true)).toBe(true);
|
2021-04-23 20:31:36 +00:00
|
|
|
|
2020-05-29 19:08:45 +00:00
|
|
|
// 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);
|
2020-05-29 19:08:45 +00:00
|
|
|
|
2021-04-23 20:31:36 +00:00
|
|
|
// 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);
|
2021-04-23 20:31:36 +00:00
|
|
|
|
2024-11-02 07:02:07 +00:00
|
|
|
// 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
|
|
|
|
2021-04-23 20:31:36 +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({
|
2021-04-23 20:31:36 +00:00
|
|
|
provider: '',
|
|
|
|
prefix: '',
|
|
|
|
name: 'fa_home',
|
|
|
|
});
|
2022-07-01 19:01:49 +00:00
|
|
|
expect(validateIconName(icon)).toBe(false);
|
2024-11-02 07:02:07 +00:00
|
|
|
expect(validateIconName(icon, true)).toBe(true);
|
2021-04-23 20:31:36 +00:00
|
|
|
|
2024-11-02 07:02:07 +00:00
|
|
|
// 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({
|
2020-05-29 19:08:45 +00:00
|
|
|
provider: '',
|
2020-04-28 09:47:35 +00:00
|
|
|
prefix: 'fa',
|
|
|
|
name: 'home_outline',
|
|
|
|
});
|
2024-11-02 07:02:07 +00:00
|
|
|
expect(validateIconName(icon)).toBe(true);
|
2020-04-28 09:47:35 +00:00
|
|
|
|
|
|
|
// Too many colons: fail stringToIcon
|
2020-05-29 19:08:45 +00:00
|
|
|
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
|
|
|
|
2024-11-02 07:02:07 +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({
|
2020-05-29 19:08:45 +00:00
|
|
|
provider: '',
|
2020-04-28 09:47:35 +00:00
|
|
|
prefix: 'MD',
|
|
|
|
name: 'Home',
|
|
|
|
});
|
2024-11-02 07:02:07 +00:00
|
|
|
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({
|
2020-05-29 19:08:45 +00:00
|
|
|
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
|
|
|
|
2024-11-02 07:02:07 +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({
|
2020-05-29 19:08:45 +00:00
|
|
|
provider: '',
|
2020-04-28 09:47:35 +00:00
|
|
|
prefix: 'md',
|
|
|
|
name: 'fõö',
|
|
|
|
});
|
2024-11-02 07:02:07 +00:00
|
|
|
expect(validateIconName(icon)).toBe(true);
|
2020-04-28 09:47:35 +00:00
|
|
|
});
|
2020-05-29 19:08:45 +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({
|
2020-05-29 19:08:45 +00:00
|
|
|
provider: 'iconify',
|
|
|
|
prefix: 'fa',
|
|
|
|
name: 'home',
|
|
|
|
});
|
2022-07-01 19:01:49 +00:00
|
|
|
expect(validateIconName(icon)).toBe(true);
|
2020-05-29 19:08:45 +00:00
|
|
|
|
|
|
|
// Simple @provider:prefix:name
|
|
|
|
icon = stringToIcon('@iconify:fa:arrow-left') as IconifyIconName;
|
2021-09-20 20:53:49 +00:00
|
|
|
expect(icon).toEqual({
|
2020-05-29 19:08:45 +00:00
|
|
|
provider: 'iconify',
|
|
|
|
prefix: 'fa',
|
|
|
|
name: 'arrow-left',
|
|
|
|
});
|
2022-07-01 19:01:49 +00:00
|
|
|
expect(validateIconName(icon)).toBe(true);
|
2020-05-29 19:08:45 +00:00
|
|
|
|
|
|
|
// 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({
|
2020-05-29 19:08:45 +00:00
|
|
|
provider: 'iconify-backup',
|
|
|
|
prefix: 'mdi-light',
|
|
|
|
name: 'home-outline',
|
|
|
|
});
|
2022-07-01 19:01:49 +00:00
|
|
|
expect(validateIconName(icon)).toBe(true);
|
2020-05-29 19:08:45 +00:00
|
|
|
|
|
|
|
// Missing @ for provider
|
|
|
|
icon = stringToIcon(
|
|
|
|
'iconify-backup:mdi-light:home-outline'
|
|
|
|
) as IconifyIconName;
|
2021-09-20 20:53:49 +00:00
|
|
|
expect(icon).toEqual({
|
2020-05-29 19:08:45 +00:00
|
|
|
provider: 'iconify-backup',
|
|
|
|
prefix: 'mdi-light',
|
|
|
|
name: 'home-outline',
|
|
|
|
});
|
2022-07-01 19:01:49 +00:00
|
|
|
expect(validateIconName(icon)).toBe(true);
|
2020-05-29 19:08:45 +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-05-29 19:08:45 +00:00
|
|
|
|
2021-04-23 20:31:36 +00:00
|
|
|
// 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);
|
2021-04-23 20:31:36 +00:00
|
|
|
|
2024-11-02 07:02:07 +00:00
|
|
|
// Upper case: allowed now
|
2020-05-29 19:08:45 +00:00
|
|
|
icon = stringToIcon('@MD:home-outline') as IconifyIconName;
|
2021-09-20 20:53:49 +00:00
|
|
|
expect(icon).toEqual({
|
2020-05-29 19:08:45 +00:00
|
|
|
provider: 'MD',
|
|
|
|
prefix: 'home',
|
|
|
|
name: 'outline',
|
|
|
|
});
|
2024-11-02 07:02:07 +00:00
|
|
|
expect(validateIconName(icon)).toBe(true);
|
2020-05-29 19:08:45 +00:00
|
|
|
});
|
2020-04-28 09:47:35 +00:00
|
|
|
});
|