2
0
mirror of https://github.com/iconify/iconify.git synced 2024-11-15 01:04:04 +00:00
iconify/iconify-icon/icon/tests/icon-load-api-test.ts

228 lines
4.1 KiB
TypeScript
Raw Normal View History

import { fakeAPI, nextPrefix, mockAPIData } from '../src/tests/helpers';
2022-04-29 20:19:22 +00:00
import { addCollection } from '@iconify/core/lib/storage/functions';
import { parseIconValue } from '../src/attributes/icon/index';
describe('Testing parseIconValue with API', () => {
it('Loading icon from API', () => {
return new Promise((fulfill) => {
// Set config
const provider = nextPrefix();
const prefix = nextPrefix();
fakeAPI(provider);
// Mock data
const name = 'mock-test';
const iconName = `@${provider}:${prefix}:${name}`;
mockAPIData({
type: 'icons',
provider,
2022-04-29 20:19:22 +00:00
prefix,
response: {
prefix,
icons: {
[name]: {
body: '<g />',
},
2022-04-29 20:19:22 +00:00
},
},
});
2022-04-29 20:19:22 +00:00
// Test
let callbackCalled = false;
const result = parseIconValue(iconName, (value, icon, data) => {
expect(callbackCalled).toBe(false);
callbackCalled = true;
expect(value).toBe(iconName);
expect(icon).toEqual({
provider,
prefix,
name,
});
expect(data).toEqual({
body: '<g />',
});
fulfill(true);
2022-04-29 20:19:22 +00:00
});
expect(result.loading).toBeDefined();
expect(result).toEqual({
value: iconName,
name: {
provider,
prefix,
name,
},
loading: result.loading,
2022-04-29 20:19:22 +00:00
});
expect(callbackCalled).toBe(false);
2022-04-29 20:19:22 +00:00
});
});
it('Already exists', () => {
return new Promise((fulfill, reject) => {
// Set config
const provider = nextPrefix();
const prefix = nextPrefix();
fakeAPI(provider);
const name = 'mock-test';
const iconName = `@${provider}:${prefix}:${name}`;
addCollection(
{
prefix,
icons: {
[name]: {
body: '<g id="test" />',
},
2022-04-29 20:19:22 +00:00
},
},
provider
);
// Mock data
mockAPIData({
type: 'icons',
provider,
2022-04-29 20:19:22 +00:00
prefix,
response: {
prefix,
icons: {
[name]: {
body: '<g />',
},
2022-04-29 20:19:22 +00:00
},
},
delay: () => {
reject('This function should not have been called');
},
});
2022-04-29 20:19:22 +00:00
// Test
const result = parseIconValue(iconName, () => {
reject('Callback should not have been called');
});
expect(result).toEqual({
value: iconName,
name: {
provider,
prefix,
name,
},
data: {
body: '<g id="test" />',
},
});
fulfill(true);
2022-04-29 20:19:22 +00:00
});
});
it('Failing to load', () => {
return new Promise((fulfill) => {
// Set config
const provider = nextPrefix();
const prefix = nextPrefix();
fakeAPI(provider);
2022-04-29 20:19:22 +00:00
// Mock data
const name = 'mock-test';
const iconName = `@${provider}:${prefix}:${name}`;
2022-04-29 20:19:22 +00:00
mockAPIData({
type: 'icons',
2022-04-29 20:19:22 +00:00
provider,
prefix,
response: {
prefix,
icons: {},
not_found: [name],
},
2022-04-29 20:19:22 +00:00
});
// Test
let callbackCalled = false;
const result = parseIconValue(iconName, (value, icon, data) => {
expect(callbackCalled).toBe(false);
callbackCalled = true;
expect(value).toBe(iconName);
expect(icon).toEqual({
provider,
prefix,
name,
});
expect(data).toBeFalsy();
fulfill(true);
});
expect(result.loading).toBeDefined();
expect(result).toEqual({
value: iconName,
name: {
provider,
prefix,
name,
},
loading: result.loading,
});
expect(callbackCalled).toBe(false);
2022-04-29 20:19:22 +00:00
});
});
it('Already marked as missing', () => {
return new Promise((fulfill, reject) => {
// Set config
const provider = nextPrefix();
const prefix = nextPrefix();
fakeAPI(provider);
const name = 'mock-test';
const iconName = `@${provider}:${prefix}:${name}`;
addCollection(
{
prefix,
icons: {},
not_found: [name],
},
provider
);
2022-04-29 20:19:22 +00:00
// Mock data
mockAPIData({
type: 'icons',
provider,
2022-04-29 20:19:22 +00:00
prefix,
response: {
prefix,
icons: {
[name]: {
body: '<g />',
},
2022-04-29 20:19:22 +00:00
},
},
delay: () => {
reject('This function should not have been called');
},
});
2022-04-29 20:19:22 +00:00
// Test
const result = parseIconValue(iconName, () => {
reject('Callback should not have been called');
});
expect(result).toEqual({
value: iconName,
name: {
provider,
prefix,
name,
},
data: null,
});
fulfill(true);
2022-04-29 20:19:22 +00:00
});
});
});