2
0
mirror of https://github.com/iconify/iconify.git synced 2024-11-14 16:54:03 +00:00
iconify/packages/core/tests/api/modules-test.ts

72 lines
1.7 KiB
TypeScript
Raw Normal View History

2020-12-25 19:03:15 +00:00
import type { IconifyAPIConfig } from '../../lib/api/config';
import { addAPIProvider, getAPIConfig } from '../../lib/api/config';
import type {
IconifyAPIIconsQueryParams,
IconifyAPIModule,
} from '../../lib/api/modules';
2020-12-25 19:03:15 +00:00
import { setAPIModule, getAPIModule } from '../../lib/api/modules';
2020-04-28 09:47:35 +00:00
describe('Testing API modules', () => {
let prefixCounter = 0;
function nextPrefix(): string {
prefixCounter++;
return (
2022-03-16 21:30:16 +00:00
'api-mod-test-' +
(prefixCounter < 10 ? '0' : '') +
prefixCounter.toString()
2020-04-28 09:47:35 +00:00
);
}
const prepareQuery = (
provider: string,
2020-04-28 09:47:35 +00:00
prefix: string,
icons: string[]
): IconifyAPIIconsQueryParams[] => {
const item: IconifyAPIIconsQueryParams = {
type: 'icons',
provider,
2020-04-28 09:47:35 +00:00
prefix,
icons,
};
return [item];
};
const sendQuery = (): void => {
2020-04-28 09:47:35 +00:00
throw new Error('Unexpected API call');
};
it('Empty module', () => {
const provider = nextPrefix();
2020-04-28 09:47:35 +00:00
// Set config
addAPIProvider(provider, {
resources: ['https://localhost:3000'],
maxURL: 500,
});
2020-04-28 09:47:35 +00:00
// Set fake module
setAPIModule(provider, {
prepare: prepareQuery,
send: sendQuery,
});
2020-04-28 09:47:35 +00:00
// Get config
const config = getAPIConfig(provider) as IconifyAPIConfig;
expect(config).not.toBeUndefined();
2020-04-28 09:47:35 +00:00
// Check setAPIConfig
expect(config.resources).toEqual(['https://localhost:3000']);
2020-04-28 09:47:35 +00:00
// Check getAPIModule()
const item = getAPIModule(provider) as IconifyAPIModule;
expect(item).not.toBeUndefined();
expect(item.prepare).toBe(prepareQuery);
expect(item.send).toBe(sendQuery);
2020-04-28 09:47:35 +00:00
// Get module for different provider to make sure it is different
const provider2 = nextPrefix();
const item2 = getAPIModule(provider2);
expect(item2).not.toBe(item);
2020-04-28 09:47:35 +00:00
});
});