tests.FetchHandler
Answers the app's requests. Plain data both ways: the app's realm has
Kuito's own fetch, not Node's Request and Response.
Utilisation
Handle requests in a screen test.
// Handle requests in a screen test.
import { Text, resource } from 'kuito';
import { assert, render, screen, test, type FetchHandler } from 'kuito/tests';
// A tiny fake server: answers by path, 404 for the rest.
const api: FetchHandler = (request) => {
const path = new URL(request.url).pathname;
if (path === '/me')
return { headers: { 'Content-Type': 'application/json' }, body: '{"name":"Ana"}' };
return { status: 404 };
};
function Greeting() {
const me = resource(async () => {
const response = await fetch('https://api.example.com/me');
return ((await response.json()) as { name: string }).name;
});
return <Text>{`Hi, ${me.data.value ?? '…'}`}</Text>;
}
test('greets the signed-in user', async () => {
await render(<Greeting />, { fetch: api });
assert.ok(screen.getByText('Hi, Ana'));
});Déclaration du type
export type FetchHandler = (request: FetchRequest) => FetchAnswer | Promise<FetchAnswer>;Type
(request: FetchRequest) => FetchAnswer | Promise<FetchAnswer>Généré depuis les déclarations TypeScript publiques : signatures, props et valeurs par défaut viennent de la source vérifiée, les descriptions de sa JSDoc. Aucune valeur par défaut n'est inventée.