2
0
mirror of https://github.com/iconify/iconify.git synced 2025-01-12 01:45:41 +00:00

Adjust SVG framework for changes in API code

This commit is contained in:
Vjacheslav Trushkin 2021-04-28 00:06:58 +03:00
parent b12de93aaa
commit 07f2dc5608
2 changed files with 60 additions and 15 deletions

View File

@ -54,7 +54,7 @@ import {
import { getAPIModule as getJSONPAPIModule } from '@iconify/core/lib/api/modules/jsonp';
import {
getAPIModule as getFetchAPIModule,
getFetch,
setFetch,
} from '@iconify/core/lib/api/modules/fetch';
import {
IconifyIconLoaderCallback,
@ -113,6 +113,7 @@ export interface IconifyGlobal
IconifyBrowserCacheFunctions,
IconifyAPIFunctions {
_api: IconifyAPIInternalFunctions;
setNodeFetch: (nodeFetch: typeof fetch) => void;
}
/**
@ -150,23 +151,38 @@ const Iconify = ({
/**
* Initialise stuff
*/
// Check for Fetch API
const fetchModule = getFetch();
// Set API
coreModules.api = API;
let getAPIModule: GetIconifyAPIModule;
// Check for Fetch API
let getAPIModule: GetIconifyAPIModule = getFetchAPIModule;
try {
getAPIModule =
typeof fetchModule === 'function' && typeof Promise === 'function'
? getFetchAPIModule
: getJSONPAPIModule;
if (typeof document !== 'undefined' && typeof window !== 'undefined') {
// If window and document exist, attempt to load whatever module is available, otherwise use Fetch API
getAPIModule =
typeof fetch === 'function' && typeof Promise === 'function'
? getFetchAPIModule
: getJSONPAPIModule;
}
} catch (err) {
getAPIModule = getJSONPAPIModule;
//
}
setAPIModule('', getAPIModule(getAPIConfig));
/**
* Function to enable node-fetch for getting icons on server side
*/
Iconify.setNodeFetch = (nodeFetch: typeof fetch) => {
setFetch(nodeFetch);
if (getAPIModule !== getFetchAPIModule) {
getAPIModule = getFetchAPIModule;
setAPIModule('', getAPIModule(getAPIConfig));
}
};
/**
* Browser stuff
*/
if (typeof document !== 'undefined' && typeof window !== 'undefined') {
// Set cache and load existing cache
coreModules.cache = storeCache;

View File

@ -1,24 +1,53 @@
import 'mocha';
import { expect } from 'chai';
import Iconify, { IconifyIconName } from '../dist/iconify';
import { mockAPIModule, mockAPIData } from '@iconify/core/lib/api/modules/mock';
// API provider and prefix for test
const provider = 'mock-api';
const prefix = 'prefix';
// Set API module for provider
Iconify.addAPIProvider(provider, {
resources: ['http://localhost'],
});
Iconify._api.setAPIModule(provider, mockAPIModule);
// Set data
mockAPIData({
provider,
prefix,
response: {
prefix,
icons: {
home: {
body:
'<path d="M10 20v-6h4v6h5v-8h3L12 3L2 12h3v8h5z" fill="currentColor"/>',
},
},
width: 24,
height: 24,
},
});
// Test
describe('Testing loadIcons() with Node.js', () => {
it('Load icons from API (requires internet access and cross-fetch installed as dependency)', (done) => {
const prefix = 'mdi';
it('Load icons from API', (done) => {
const name = 'home';
const fullName = '@' + provider + ':' + prefix + ':' + name;
Iconify.loadIcons([prefix + ':' + name], (loaded, missing) => {
Iconify.loadIcons([fullName], (loaded, missing) => {
// Check callback data
expect(missing).to.be.eql([]);
const icon: IconifyIconName = {
provider: '',
provider,
prefix,
name,
};
expect(loaded).to.be.eql([icon]);
// Check if icon exists
expect(Iconify.iconExists(prefix + ':' + name)).to.be.equal(true);
expect(Iconify.iconExists(fullName)).to.be.equal(true);
done();
});