MockEndpointAPIProviderclass
new MockEndpointAPIProvider<P, R, C>(handlers: EndpointHandlers<C>, context: C, source?: ClientAPIProvider<P, R>)
| Param | Type | |
|---|---|---|
handlers | EndpointHandlers<C> | The endpoint handlers (from Endpoint.handler()) that serve matching requests. required |
context | C | The context value passed to each handler. required |
source | ClientAPIProvider<P, R> | Optional source provider used to build requests and parse responses. |
| Return | |
|---|---|
MockEndpointAPIProvider<P, R, C> | Provider that mocks an API that calls and matches an array of EndpointHandler objects returned from Endpoint.handler() |
Provider that mocks an API that calls and matches an array of EndpointHandler objects returned from Endpoint.handler()
- Used to test server-side API code, calls against an API made up of multiple
Endpointinstances.
A MockAPIProvider that wires the mock transport directly to a real EndpointHandler array. Client code calls endpoints exactly as in production, but each call is dispatched to its handler in the same process — so you can test client and server logic together without a network.
The constructor takes the handlers array, a context value passed to every handler, and an optional source provider.
Usage
import { MockEndpointAPIProvider } from "shelving/api"
const handlers = [
getUser.handler(async ({ id }) => ({ id, name: "Alice", email: "[email protected]" })),
]
const api = new MockEndpointAPIProvider(handlers, undefined)
const user = await api.call(getUser, { id: "u_1" })
// user == { id: "u_1", name: "Alice", email: "[email protected]" }
console.log(api.requestCalls) // inspect recorded callsExamples
const endpoint = POST("/squared", INTEGER, INTEGER); // Create an endpoint designed to square its input number.
const handlers = [endpoint.handler(num => num * num)]; // Implement handlers for the endpoints.
const api = new MockEnpdointAPIProvider(handlers); // Create a new mock provider.
const result = await api.fetch(endpoint, 4); // Mock a call to the endpoint through the provider.
expect(result).toBe(16);