2020-12-25 19:03:15 +00:00
|
|
|
import type { IconifyAPIConfig } from '../../lib/api/config';
|
2021-09-24 15:38:52 +00:00
|
|
|
import { addAPIProvider, getAPIConfig } from '../../lib/api/config';
|
2021-08-30 07:22:53 +00:00
|
|
|
import type {
|
2021-09-05 16:09:25 +00:00
|
|
|
IconifyAPIIconsQueryParams,
|
2021-08-30 07:22:53 +00:00
|
|
|
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 = (
|
2020-05-29 19:08:45 +00:00
|
|
|
provider: string,
|
2020-04-28 09:47:35 +00:00
|
|
|
prefix: string,
|
|
|
|
icons: string[]
|
2021-09-05 16:09:25 +00:00
|
|
|
): IconifyAPIIconsQueryParams[] => {
|
|
|
|
const item: IconifyAPIIconsQueryParams = {
|
2021-08-30 07:22:53 +00:00
|
|
|
type: 'icons',
|
2020-05-29 19:08:45 +00:00
|
|
|
provider,
|
2020-04-28 09:47:35 +00:00
|
|
|
prefix,
|
|
|
|
icons,
|
|
|
|
};
|
|
|
|
return [item];
|
|
|
|
};
|
|
|
|
|
2021-09-23 21:27:16 +00:00
|
|
|
const sendQuery = (): void => {
|
2020-04-28 09:47:35 +00:00
|
|
|
throw new Error('Unexpected API call');
|
|
|
|
};
|
|
|
|
|
|
|
|
it('Empty module', () => {
|
2020-05-29 19:08:45 +00:00
|
|
|
const provider = nextPrefix();
|
2020-04-28 09:47:35 +00:00
|
|
|
|
|
|
|
// Set config
|
2021-09-24 15:38:52 +00:00
|
|
|
addAPIProvider(provider, {
|
2020-05-29 19:08:45 +00:00
|
|
|
resources: ['https://localhost:3000'],
|
|
|
|
maxURL: 500,
|
|
|
|
});
|
2020-04-28 09:47:35 +00:00
|
|
|
|
|
|
|
// Set fake module
|
2020-05-31 06:44:24 +00:00
|
|
|
setAPIModule(provider, {
|
2020-05-29 19:08:45 +00:00
|
|
|
prepare: prepareQuery,
|
|
|
|
send: sendQuery,
|
|
|
|
});
|
2020-04-28 09:47:35 +00:00
|
|
|
|
|
|
|
// Get config
|
2020-05-29 19:08:45 +00:00
|
|
|
const config = getAPIConfig(provider) as IconifyAPIConfig;
|
2021-09-23 21:27:16 +00:00
|
|
|
expect(config).not.toBeUndefined();
|
2020-04-28 09:47:35 +00:00
|
|
|
|
|
|
|
// Check setAPIConfig
|
2021-09-23 21:27:16 +00:00
|
|
|
expect(config.resources).toEqual(['https://localhost:3000']);
|
2020-04-28 09:47:35 +00:00
|
|
|
|
|
|
|
// Check getAPIModule()
|
2020-05-29 19:08:45 +00:00
|
|
|
const item = getAPIModule(provider) as IconifyAPIModule;
|
2021-09-23 21:27:16 +00:00
|
|
|
expect(item).not.toBeUndefined();
|
|
|
|
expect(item.prepare).toBe(prepareQuery);
|
|
|
|
expect(item.send).toBe(sendQuery);
|
2020-04-28 09:47:35 +00:00
|
|
|
|
2021-09-23 21:27:16 +00:00
|
|
|
// Get module for different provider to make sure it is different
|
2020-05-29 19:08:45 +00:00
|
|
|
const provider2 = nextPrefix();
|
|
|
|
const item2 = getAPIModule(provider2);
|
2021-09-23 21:27:16 +00:00
|
|
|
expect(item2).not.toBe(item);
|
2020-04-28 09:47:35 +00:00
|
|
|
});
|
|
|
|
});
|