Utilização

Return a fake network response.

tsx
// Return a fake network response.
import { Text, resource } from 'kuito';
import { assert, render, screen, test, type FetchAnswer } from 'kuito/tests';

function Weather() {
  const forecast = resource(async () => {
    const response = await fetch('https://api.example.com/forecast');

    return response.ok ? 'Sunny' : `Unavailable (${response.status})`;
  });

  return <Text>{forecast.data.value ?? 'Loading'}</Text>;
}

const outage: FetchAnswer = { status: 503, body: 'Service unavailable' };

test('an outage shows as unavailable', async () => {
  await render(<Weather />, { fetch: () => outage });

  assert.ok(screen.getByText('Unavailable (503)'));
});

Declaração do tipo

ts
export interface FetchAnswer {
  status?: number;
  headers?: Record<string, string>;
  body?: string | Uint8Array;
}

Membros 3

NomeTipoObrigatórioDescrição
statusnumberNão—
headersRecord<string, string>Não—
bodystring | Uint8Array<ArrayBufferLike>Não—
Gerado a partir das declarações TypeScript públicas: assinaturas, props e predefinições vêm da fonte verificada, as descrições da sua JSDoc. Nenhuma predefinição é inventada.