2
0
mirror of https://github.com/iconify/iconify.git synced 2024-11-10 07:11:00 +00:00
iconify/packages/browser-tests/tests/fake-api.ts

135 lines
2.8 KiB
TypeScript
Raw Normal View History

2020-12-16 09:48:50 +00:00
import { PendingQueryItem } from '@cyberalien/redundancy';
2020-04-28 09:47:35 +00:00
import {
IconifyAPIIconsQueryParams,
IconifyAPIQueryParams,
IconifyAPIPrepareIconsQuery,
2020-04-28 09:47:35 +00:00
IconifyAPISendQuery,
} from '@iconify/core/lib/api/modules';
import { IconifyJSON } from '@iconify/types';
/**
* Fake data entry
*/
export interface FakeData {
icons: string[];
host?: string; // host to respond to
delay?: number; // 0 = instant reply
data: IconifyJSON; // data to send
}
/**
* Fake data storage
*/
const fakeData: Record<string, Record<string, FakeData[]>> = Object.create(
null
);
2020-04-28 09:47:35 +00:00
export function setFakeData(
provider: string,
prefix: string,
item: FakeData
): void {
if (fakeData[provider] === void 0) {
fakeData[provider] = Object.create(null);
}
const providerFakeData = fakeData[provider];
if (providerFakeData[prefix] === void 0) {
providerFakeData[prefix] = [];
2020-04-28 09:47:35 +00:00
}
providerFakeData[prefix].push(item);
2020-04-28 09:47:35 +00:00
}
interface FakeAPIQueryParams extends IconifyAPIIconsQueryParams {
2020-04-28 09:47:35 +00:00
data: FakeData;
}
/**
* Prepare params
*/
export const prepareQuery: IconifyAPIPrepareIconsQuery = (
provider: string,
2020-04-28 09:47:35 +00:00
prefix: string,
icons: string[]
): IconifyAPIIconsQueryParams[] => {
2020-04-28 09:47:35 +00:00
// Find items that have query
const items: IconifyAPIIconsQueryParams[] = [];
2020-04-28 09:47:35 +00:00
let missing = icons.slice(0);
if (fakeData[provider] === void 0) {
fakeData[provider] = Object.create(null);
}
const providerFakeData = fakeData[provider];
const type = 'icons';
if (providerFakeData[prefix] !== void 0) {
providerFakeData[prefix].forEach((item) => {
2020-04-28 09:47:35 +00:00
const matches = item.icons.filter(
(icon) => missing.indexOf(icon) !== -1
2020-04-28 09:47:35 +00:00
);
if (!matches.length) {
// No match
return;
}
// Contains at least one matching icon
missing = missing.filter((icon) => matches.indexOf(icon) === -1);
2020-04-28 09:47:35 +00:00
const query: FakeAPIQueryParams = {
type,
provider,
2020-04-28 09:47:35 +00:00
prefix,
icons: matches,
data: item,
};
items.push(query);
});
}
return items;
};
/**
* Load icons
*/
export const sendQuery: IconifyAPISendQuery = (
host: string,
params: IconifyAPIQueryParams,
status: PendingQueryItem
2020-04-28 09:47:35 +00:00
): void => {
if (params.type !== 'icons') {
// Fake API supports only icons
status.done(void 0, 400);
return;
}
const provider = params.provider;
2020-04-28 09:47:35 +00:00
const prefix = params.prefix;
const icons = params.icons;
const data = (params as FakeAPIQueryParams).data;
if (!data) {
throw new Error('Fake data is missing in query params');
}
if (typeof data.host === 'string' && data.host !== host) {
2020-12-16 09:48:50 +00:00
// Host mismatch - send error (first parameter = undefined)
status.done(void 0, 404);
2020-04-28 09:47:35 +00:00
return;
}
const sendResponse = () => {
console.log(
'Sending data for prefix "' +
(provider === '' ? '' : '@' + provider + ':') +
prefix +
'", icons:',
icons
);
status.done(data.data);
2020-04-28 09:47:35 +00:00
};
if (!data.delay) {
sendResponse();
} else {
setTimeout(sendResponse, data.delay);
}
};