import React from 'react' import type { RenderResult as TLRenderResult } from '@testing-library/react' export type HttpMethod = UpperCaseHttpMethod | Lowercase type UpperCaseHttpMethod = | 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS' | 'HEAD' | 'CONNECT' | 'TRACE' export interface WrapRequest extends Partial { _bodyInit?: string url: string } export interface RequestOptions { host?: string body?: object | string method?: HttpMethod headers?: Record } export interface WrapResponse extends Partial { /** The call's path we want to mock */ path: string /** To which host the call corresponds to */ host?: string /** The HTTP method we should intercept, defaults to GET */ method?: HttpMethod /** The response for this call */ responseBody?: object | string /** The request to match with the responseBody for this call */ requestBody?: object /** Allows to return multiple response for a call */ multipleResponses?: Array> catchParams?: boolean delay?: number hasBeenReturned?: boolean streamBody?: ReadableStream> } export type NetworkResponses = WrapResponse | WrapResponse[] export type StreamChunk = string | { text: string; delay?: number } export interface StreamingNetworkConfig { path: string host?: string method?: HttpMethod chunks?: StreamChunk[] delayBetweenChunks?: number keepOpen?: boolean stream?: ReadableStream> } export type DefaultUserLib = unknown export type DefaultUserInstance = unknown export type DefaultUserSetupOptions = unknown export interface InteractionDescriptor< UserLib = DefaultUserLib, UserInstance = DefaultUserInstance, UserSetupOptions = DefaultUserSetupOptions, > { UserLib: UserLib UserInstance: UserInstance UserSetupOptions: UserSetupOptions } export interface Wrap< UserInteraction extends InteractionDescriptor = InteractionDescriptor, > { withNetwork: (responses?: NetworkResponses) => Wrap withStreamingNetwork: ( config: StreamingNetworkConfig | StreamingNetworkConfig[], ) => Wrap atPath: (path: string, historyState?: object) => Wrap withProps: (props: object) => Wrap withInteraction: ( config: UserInteraction['UserSetupOptions'], ) => Wrap debugRequests: () => Wrap mount: () => RenderResult & { user: UserInteraction['UserInstance'] } } export interface WrapOptions { Component: unknown responses: WrapResponse[] props: object path: string historyState?: object hasPath: boolean debug: boolean interactionConfig?: SetupOptions } export interface WrapExtensionAPI { addResponses: (responses: Array) => unknown } type Extension< Args extends unknown[] = unknown[], UserInteraction extends InteractionDescriptor = InteractionDescriptor, > = (extensionAPI: WrapExtensionAPI, args: Args) => Wrap | void type Extensions< UserInteraction extends InteractionDescriptor = InteractionDescriptor, > = { [key: string]: Extension } type Component = React.ReactElement export type RenderResult = TLRenderResult export type Mount = (component: Component) => RenderResult export interface InteractionOptions< UserInteraction extends InteractionDescriptor = InteractionDescriptor, > { userLib: UserInteraction['UserLib'] setup?: ( userLib: UserInteraction['UserLib'], options?: UserInteraction['UserSetupOptions'], ) => UserInteraction['UserInstance'] } export interface Config< UserInteraction extends InteractionDescriptor = InteractionDescriptor, > { defaultHost: string mount: Mount extend: Extensions changeRoute: (path: string) => void history?: BrowserHistory portal?: string portals?: string[] handleQueryParams?: boolean interaction?: InteractionOptions warnOnPendingRequests?: boolean defaultResponses?: WrapResponse[] } interface BrowserHistory extends History { push: (path: string, historyState?: object) => void } export { WrapResponse as Response, Component, BrowserHistory, Extension, Extensions, }