Skip to main content

@cuple/react/testing

Helpers for testing components that use @cuple/react.

Functions​

mockCuple()​

mockCuple<TApi>(handlers): object

Defined in: testing.tsx:92

A fake server for frontend tests, typed from the server's routes — for when the tests can't (or shouldn't) run the real one.

const mock = mockCuple<typeof routes>({
getTodos: { get: () => ({ result: "success", statusCode: 200, todos: [] }) },
});
vi.stubGlobal("fetch", mock.fetch);

A request with no handler fails with the endpoint's name, so a missing mock is obvious. calls records every request, in order. Streams (SSE) are not supported.

Type Parameters​

TApi​

TApi

Parameters​

handlers​

MockHandlers<TApi>

Returns​

calls​

calls: MockCall[]

fetch​

fetch: {(input, init?): Promise<Response>; (input, init?): Promise<Response>; }

Call Signature​

(input, init?): Promise<Response>

MDN Reference

Parameters​
input​

RequestInfo | URL

init?​

RequestInit

Returns​

Promise<Response>

Call Signature​

(input, init?): Promise<Response>

MDN Reference

Parameters​
input​

string | Request | URL

init?​

RequestInit

Returns​

Promise<Response>


renderWithCuple()​

renderWithCuple(ui, options?): Promise<object & object & object>

Defined in: testing.tsx:30

Testing Library's render, set up for Cuple: under a <CupleProvider> with a fresh store, and inside an awaited act.

The act matters: React 19 drops the retry of a component that suspends inside a synchronous act, which plain render is, so a Suspense tree rendered without it never leaves its fallback.

await renderWithCuple(<Boundary fallback="loading"><Todos /></Boundary>);
expect(await screen.findByText("milk")).toBeDefined();

Parameters​

ui​

ReactElement

options?​

RenderOptions<__module, HTMLElement, HTMLElement> & object = {}

Returns​

Promise<object & object & object>

Type Aliases​

MockCall​

MockCall = object

Defined in: testing.tsx:70

Properties​

endpoint​

endpoint: string

Defined in: testing.tsx:72

The endpoint as the client names it, e.g. "admin.stats.get".

input​

input: Record<string, unknown>

Defined in: testing.tsx:74

What the handler received: params, query, body, headers.


MockHandlers​

MockHandlers<TApi> = { [K in keyof TApi]?: TApi[K] extends { tInput: infer TInput; tMethod: infer TMethod extends string; tOutput: infer TOutput } ? { [M in TMethod]?: (input: TInput) => TOutput | Promise<TOutput> } : MockHandlers<TApi[K]> }

Defined in: testing.tsx:60

A handler for every endpoint you mock, shaped like the client: { getTodo: { get: ({ params }) => ({ result: "success", statusCode: 200, … }) } }. Inputs and results are typed from the routes, so a mock can't drift from the API without a type error.

Type Parameters​

TApi​

TApi