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

126 lines
2.6 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 {
APIQueryParams,
IconifyAPIPrepareQuery,
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 APIQueryParams {
data: FakeData;
}
/**
* Prepare params
*/
export const prepareQuery: IconifyAPIPrepareQuery = (
provider: string,
2020-04-28 09:47:35 +00:00
prefix: string,
icons: string[]
): APIQueryParams[] => {
// Find items that have query
const items: APIQueryParams[] = [];
let missing = icons.slice(0);
if (fakeData[provider] === void 0) {
fakeData[provider] = Object.create(null);
}
const providerFakeData = fakeData[provider];
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 = {
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: APIQueryParams,
status: PendingQueryItem
2020-04-28 09:47:35 +00:00
): void => {
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);
}
};