1
0
mirror of synced 2024-11-29 16:16:02 +03:00
select2/control/test/preact-util.ts
2019-06-25 15:23:14 -07:00

37 lines
1.2 KiB
TypeScript

import { VNode } from 'preact';
import { render as renderToString, shallowRender as shallowRenderToString } from 'preact-render-to-string';
import * as pretty from 'pretty';
import { QueryFunction } from '../src/abstract-select';
import { countries } from './countries';
export function shallow(component: VNode): string {
return pretty(shallowRenderToString(component), { ocd: true });
}
export function deep(component: VNode): string {
return pretty(renderToString(component), { ocd: true });
}
export const query: QueryFunction = (search, page, token) =>
new Promise((resolve, reject) => {
const results: any[] = [];
let count = 0;
const limit = 10;
const offset = page * limit;
for (const country of countries) {
if (country.name.toLowerCase().indexOf(search.toLowerCase()) >= 0) {
if (count >= offset) {
results.push(country);
}
count++;
if (count >= offset + limit) {
break;
}
}
}
resolve({
more: results.length >= limit,
token,
values: results
});
});