import { html } from "../hvniel/vite-plugin-svelte-inline-component"; /** * Test utilities for Svelte components in vitest environment. * * This module provides helpers for mounting Svelte components in tests with a clean, * composable API that eliminates boilerplate and enables easy nested component testing. */ import type { ComponentInternals, Component, Snippet, ComponentProps } from "svelte"; interface ElementFunction { (a: any, html: () => string): void; s: (html: () => string) => Snippet; c: (html: () => string) => Component; } /** * HTML rendering utility for tests. Provides three methods: * - `element(anchor, htmlFn)`: Direct HTML rendering to anchor * - `element.s(htmlFn)`: Returns a Snippet for use in component props * - `element.c(htmlFn)`: Returns a Component for use in mount functions * * @example * ```ts * // Direct rendering * element(anchor, () => "

Hello

"); * * // As Snippet for component props * m(Route, { * path: "/", * element: element.s(() => "

Home

") * }); * * // As Component for mount functions * m(element.c(() => "
Content
")); * ``` */ declare const element: ElementFunction; /** * Callback function type for mounting nested components. * * @param mount - Mount function for rendering child components. Use this to mount * nested components within the current component's children prop. * * @example * ```ts * // The `mount` parameter lets you render nested components * m(MemoryRouter, { basename: "/app" }, mount => { * // `mount` here is a Mount function for nested components * mount(Routes, mount => { * mount(Route, { path: "/", element: element.s(() => "

Home

") }); * }); * }); * ``` */ export type MountChildren = (mount: Mount) => void; /** * Mount function interface for rendering Svelte components in tests. * * Supports two calling patterns: * 1. `m(Component, props, children)` - Standard component mounting * 2. `m(Component, children)` - When no props are needed * * @template C - The component type * @param component - The Svelte component to mount * @param props - Component props OR children function when no props needed * @param children - Function that receives a mount instance for nested components * * @example * ```ts * // With props and children * m(MemoryRouter, { basename: "/app" }, m => * m(Routes, m => * m(Route, { path: "/", element: element.s(() => "

Home

") }) * ) * ) * * // Children only (no props) * m(Routes, m => * m(Route, { path: "/" }) * ) * ``` */ export interface Mount { (component: C, props?: ComponentProps | MountChildren, children?: MountChildren): void; } /** * Creates a Mount function bound to a specific ComponentInternals anchor. * * This is the core function that enables the composable mounting API. * It creates a mount function that can render components and handle * nested children through recursive mount creation. * * @param anchor - The ComponentInternals anchor to mount components to * @returns A Mount function that renders components to the given anchor * * @example * ```ts * const renderer = render(anchor => { * const m = createMount(anchor); * m(MyComponent, { prop: "value" }); * }); * ``` */ export declare function createMount(anchor: ComponentInternals): Mount; /** * Creates a Svelte Snippet from a function that operates on ComponentInternals. * * This utility converts a function that takes ComponentInternals into a proper * Svelte Snippet that can be used in component props. * * @param fn - Function that takes ComponentInternals and performs rendering * @returns A Snippet that can be used in Svelte components * * @example * ```ts * const mySnippet = snippet(a => { * element(a, () => "

Hello World

"); * }); * // Use in component: * ``` */ export declare function snippet(fn: (a: ComponentInternals) => void): Snippet; export declare namespace snippet { var c: (fn: (a: ComponentInternals) => void) => Component; } /** * Interface for the main mount function that can be used directly with render(). * * This function signature allows for the clean API: `render(m(Component, ...))` * where the result of `m()` is a function compatible with vitest's render. */ interface RenderableMount { (component: C, props?: ComponentProps | MountChildren, children?: MountChildren): (anchor: ComponentInternals) => void; } /** * The main mount function for clean test rendering. * * This provides the cleanest API for mounting components in tests: * `render(m(Component, props, children))` instead of needing to set up * `createMount` manually in every test. * * The function returns a function compatible with vitest's `render()`, * eliminating the need for wrapper functions. * * @param component - The Svelte component to mount * @param props - Component props OR children function when no props needed * @param children - Function for nested component mounting * @returns Function compatible with vitest render() that mounts the component tree * * @example * ```ts * // Clean API - no wrapper functions needed * const renderer = render( * m(MemoryRouter, { basename: "/app" }, mount => * mount(Routes, mount => * mount(Route, { * path: "/", * element: element.s(() => "

Home

") * }) * ) * ) * ); * * // Compare to the old way: * const renderer = render(anchor => { * const mount = createMount(anchor); * mount(MemoryRouter, { basename: "/app" }, mount => { * // ... same nesting * }); * }); * ``` */ export declare const m: RenderableMount; import type { Locator } from "../vitest/browser/context"; /** * Accepts a `Locator` instance and returns its executable Svelte `on:click` handler. * This is useful for testing click logic without simulating a full DOM event. * * @param locator The `Locator` instance from a query (e.g., `screen.getByText(...)`). * @returns A zero-argument function that executes the Svelte `on:click` handler. */ export declare function createClickGetter(locator: Locator): () => void | Promise; export { element, html }; export declare const delay: (ms: number) => Promise;