@mittwald/react-use-promise
Version:
Simple and declarative use of Promises in your React components. Observe their state and refresh them in various advanced ways.
18 lines (17 loc) • 889 B
JavaScript
import { jsx as _jsx } from "react/jsx-runtime";
import { act, render as testingLibRender } from "@testing-library/react";
import { createElement, Suspense } from "react";
import { ErrorBoundary } from "react-error-boundary";
export const sleep = (ms) => new Promise((res) => setTimeout(res, ms));
export const squareAsync = async (value, sleepTimeMs) => {
await sleep(sleepTimeMs);
return value * value;
};
export const squareSync = async (value, ignoredSleepMs) => {
return value * value;
};
export const render = async (ui) => {
return await act(async () => testingLibRender(ui));
};
const loadingView = _jsx("span", { "data-testid": "loading-view", children: "Loading" });
export const RenderWithLoadingView = (props) => (_jsx(ErrorBoundary, { fallback: "ErrorFallback", children: _jsx(Suspense, { fallback: loadingView, children: createElement(props.children) }) }));