import { Options, Rule } from './commands/mockBridge'; import { AnyAssertion, ArgsFor, TWDElemAPI, WaitForOptions } from './twd-types'; import { URLCommandAPI } from './commands/url'; interface TWDAPI { /** * Finds an element by selector and returns the TWD API for it. * @param selector CSS selector * @returns {Promise} The TWD API for the element * * @example * ```ts * const btn = await twd.get("button"); * * ``` * */ get: (selector: string) => Promise; /** * Sets the value of an input element and dispatches an input event. We recommend using this only for range, color, time inputs. * @param el The input element * @param value The value to set * * @example * ```ts * const input = await twd.get("input[type='time']"); * twd.setInputValue(input.el, "13:30"); * * ``` */ setInputValue: (el: Element, value: string) => void; /** * Finds multiple elements by selector and returns an array of TWD APIs for them. * @param selector CSS selector * @returns {Promise} Array of TWD APIs for the elements * * @example * ```ts * const items = await twd.getAll(".item"); * items.at(0).should("be.visible"); * items.at(1).should("contain.text", "Hello"); * expect(items).to.have.length(3); * ``` */ getAll: (selector: string) => Promise; /** * Simulates visiting a URL (SPA navigation). * @param url The URL to visit * @param [reload] Whether to force a reload even if already on the URL (optional) * * @example * ```ts * twd.visit("/contact"); * // visit with reload * twd.visit("/contact", true); * ``` */ visit: (url: string, reload?: boolean) => Promise; /** * Mock a network request. * * @param alias Identifier for the mock rule. Useful for `waitForRequest()`. * @param options Options to configure the mock: * - `method`: HTTP method ("GET", "POST", …) * - `url`: URL string or RegExp to match * - `response`: Body of the mocked response * - `status`: (optional) HTTP status code (default: 200) * - `responseHeaders`: (optional) Response headers * - `delay`: (optional) Delay in ms before returning the response * * @example * ```ts * mockRequest("getUser", { * method: "GET", * url: /\/api\/user\/\d+/, * response: { id: 1, name: "Kevin" }, * status: 200, * responseHeaders: { "x-mock": "true" } * }); * ``` */ mockRequest: (alias: string, options: Options) => Promise; /** * Wait for a mocked request to be made. * @param alias The alias of the mock rule to wait for * @param retries The number of retries to make * @param retryDelay The delay between retries * @return The matched rule (with body if applicable) * * @example * ```ts * const rule = await twd.waitForRequest("aliasId"); * console.log(rule.body); * const rule = await twd.waitForRequest("aliasId", 5, 100); * console.log(rule.body); * * ``` */ waitForRequest: (alias: string, retries?: number, retryDelay?: number) => Promise; /** * wait for a list of mocked requests to be made. * @param aliases The aliases of the mock rules to wait for * @returns The matched rules (with body if applicable) * @example * ```ts * const rules = await waitForRequests(["getUser", "postComment"]); * ``` */ waitForRequests: (aliases: string[]) => Promise; /** * URL-related assertions. * * @example * ```ts * twd.url().should("eq", "http://localhost:3000/contact"); * twd.url().should("contain.url", "/contact"); * * ``` */ url: () => URLCommandAPI; /** * Initializes request mocking (registers the service worker). * @param [path] service worker absolute path (optional) * * @example * ```ts * await twd.initRequestMocking(); * // init with custom service worker path * await twd.initRequestMocking('/test-path/mock-sw.js'); * ``` */ initRequestMocking: (path?: string) => Promise; /** * Clears all request mock rules. * * @example * ```ts * twd.clearRequestMockRules(); * * ``` */ clearRequestMockRules: () => void; /** * Gets all current request mock rules. * * @example * ```ts * const rules = twd.getRequestMockRules(); * console.log(rules); * ``` */ getRequestMockRules: () => Rule[]; /** * Gets the number of times a specific mock rule was hit. * @param alias The alias of the mock rule * @returns The number of times the rule was matched * * @example * ```ts * const count = twd.getRequestCount("getUser"); * expect(count).to.equal(2); * ``` */ getRequestCount: (alias: string) => number; /** * Gets a snapshot of all mock rule hit counts. * @returns An object mapping rule aliases to their hit counts * * @example * ```ts * const counts = twd.getRequestCounts(); * expect(counts).to.deep.equal({ getUser: 2, listPosts: 1 }); * ``` */ getRequestCounts: () => Record; /** * Waits for a specified time. * @param time Time in milliseconds to wait * @returns A promise that resolves after the specified time * @example * ```ts * await twd.wait(500); // wait for 500ms * ``` */ wait: (time: number) => Promise; /** * Retries a callback until it stops throwing or the timeout expires. * Use this instead of `twd.wait(ms)` to wait for conditions rather than fixed delays. * * @param callback Function to retry — can be sync or async. Should throw if the condition is not yet met. * @param options Optional timeout, interval, and message settings * @returns A promise that resolves with the callback's return value when it succeeds * * @example * ```ts * // Wait for an analytics event and return it * const event = await twd.waitFor(() => { * const ev = findEvent("purchase"); * expect(ev).to.exist; * return ev; * }, { message: "purchase event to fire" }); * * // Wait for an element (single expression) * const heading = await twd.waitFor(() => screenDom.getByRole("heading", { name: /checkout/i })); * * // Fire-and-forget (void) — still works as before * await twd.waitFor(() => { * expect(submitButton.disabled).to.be.false; * }); * ``` */ waitFor: (callback: () => T | Promise, options?: WaitForOptions) => Promise; /** * Asserts something about the element. * @param el The element to assert on * @param name The name of the assertion. * @param args Arguments for the assertion. * @returns The same API for chaining. * @example * ```ts * const button = await twd.get("button"); * const text = screenDom.getByText("Hello"); * twd.should(button.el, "have.text", "Hello"); * twd.should(text, "be.empty"); * twd.should(button.el, "have.class", "active"); * ``` */ should: (el: Element, name: AnyAssertion, ...args: ArgsFor) => void; /** * Mock a component. * @param name The name of the component to mock * @param component The component to mock * @returns The mocked component * @example * ```ts * twd.mockComponent("Button", Button); * ``` */ mockComponent: (name: string, component: React.ComponentType) => void; /** * Clears all component mocks. * * @example * ```ts * twd.clearComponentMocks(); * ``` */ clearComponentMocks: () => void; /** * Asserts that an element does not exist in the DOM. * @param selector CSS selector of the element to check * @returns A promise that resolves if the element does not exist, or rejects if it does * * @example * ```ts * await twd.notExists(".non-existent"); * ``` */ notExists: (selector: string) => Promise; /** * Simulates a viewport size by constraining body dimensions, overriding * `window.innerWidth`/`window.innerHeight` and `window.matchMedia()`, and * rewriting CSS `@media` rules to match the simulated dimensions. * Call with no arguments to reset to the original viewport. * * @param width Viewport width in pixels * @param height Viewport height in pixels (optional — omit to leave height unconstrained) * * @example * ```ts * twd.viewport(375, 667); // mobile * twd.viewport(768); // tablet width, height unconstrained * twd.viewport(); // reset * ``` */ viewport: (width?: number, height?: number) => void; /** * Resets the viewport to its original size (undoes a previous `twd.viewport()` call). * * @example * ```ts * twd.resetViewport(); * ``` */ resetViewport: () => void; } /** * Mini Cypress-style helpers for DOM testing. * @namespace twd */ export declare const twd: TWDAPI; export {};