import { getQueriesForElement, prettyDOM, fireEvent as dtlFireEvent } from "@testing-library/dom"; import { tick } from "svelte"; import type { SvelteComponent } from "svelte"; import type { queries, Queries, BoundFunction, EventType, FireObject } from "@testing-library/dom"; import { spy, type Spy } from "tinyspy"; import { Gradio } from "../../app/src/gradio_helper"; const containerCache = new Map(); const componentCache = new Set(); type ComponentType = new (args: { target: any; props?: Props; }) => T; export type RenderResult< C extends SvelteComponent, Q extends Queries = typeof queries > = { container: HTMLElement; component: C; debug: (el?: HTMLElement | DocumentFragment) => void; unmount: () => void; } & { [P in keyof Q]: BoundFunction }; export interface RenderOptions { container?: HTMLElement; queries?: Q; } export async function render< Events extends Record, Props extends Record, T extends SvelteComponent, X extends Record >( Component: ComponentType | { default: ComponentType }, props?: Omit ): Promise< RenderResult & { listen: typeof listen; wait_for_event: typeof wait_for_event; } > { const container = document.body; const target = container.appendChild(document.createElement("div")); const ComponentConstructor: ComponentType< T, Props & { gradio: typeof Gradio } > = //@ts-ignore Component.default || Component; const id = Math.floor(Math.random() * 1000000); const component = new ComponentConstructor({ target, //@ts-ignore props: { ...(props || {}), gradio: new Gradio(id, target, "light", "2.0.0", "http://localhost:8000") } }); containerCache.set(container, { target, component }); componentCache.add(component); component.$$.on_destroy.push(() => { componentCache.delete(component); }); await tick(); type extractGeneric = Type extends Gradio ? X : null; type event_name = keyof extractGeneric; function listen(event: event_name): Spy { const mock = spy(); target.addEventListener("gradio", (e: Event) => { if (isCustomEvent(e)) { if (e.detail.event === event && e.detail.id === id) { mock(e); } } }); return mock; } async function wait_for_event(event: event_name): Promise { return new Promise((res) => { const mock = spy(); target.addEventListener("gradio", (e: Event) => { if (isCustomEvent(e)) { if (e.detail.event === event && e.detail.id === id) { mock(e); res(mock); } } }); }); } return { container, component, //@ts-ignore debug: (el = container): void => console.warn(prettyDOM(el)), unmount: (): void => { if (componentCache.has(component)) component.$destroy(); }, ...getQueriesForElement(container), listen, wait_for_event }; } const cleanupAtContainer = (container: HTMLElement): void => { const { target, component } = containerCache.get(container); if (componentCache.has(component)) component.$destroy(); if (target.parentNode === document.body) { document.body.removeChild(target); } containerCache.delete(container); }; export function cleanup(): void { Array.from(containerCache.keys()).forEach(cleanupAtContainer); } export const fireEvent = Object.keys(dtlFireEvent).reduce((acc, key) => { const _key = key as EventType; return { ...acc, [_key]: async ( element: Document | Element | Window, options: object = {} ): Promise => { const event = dtlFireEvent[_key](element, options); await tick(); return event; } }; }, {} as FireObject); export type FireFunction = ( element: Document | Element | Window, event: Event ) => Promise; export * from "@testing-library/dom"; function isCustomEvent(event: Event): event is CustomEvent { return "detail" in event; }