UNPKG

@data-client/test

Version:
105 lines (100 loc) 2.86 kB
'use client'; 'use strict'; var react$1 = require('@data-client/react'); var react = require('react'); var jsxRuntime = require('react/jsx-runtime'); var reactNative = require('@testing-library/react-native'); /** * Provides an abstraction over react 17 and 18 compatible libraries */ function render18Wrapper(render, options) { var _SetUndefined; let renderProps = options?.initialProps; let error; const setError = e => { error = e; }; let resetErrorBoundary = () => {}; const ErrorFallback = ({ error, resetErrorBoundary: reset }) => { resetErrorBoundary = () => { resetErrorBoundary = () => {}; setTimeout(reset, 0); }; setError(error); return null; }; const SetUndefined = () => { return null; }; const WithErrorWrap = props => { resetErrorBoundary(); return /*#__PURE__*/jsxRuntime.jsx(react.Suspense, { fallback: _SetUndefined || (_SetUndefined = /*#__PURE__*/jsxRuntime.jsx(SetUndefined, {})), children: /*#__PURE__*/jsxRuntime.jsx(react$1.ErrorBoundary, { fallbackComponent: ErrorFallback, children: props.children }) }); }; // TODO: add error and waitforupdate const Wrapper = options?.wrapper; const wrapper = Wrapper ? function ProviderWrapped(props) { return /*#__PURE__*/jsxRuntime.jsx(Wrapper, { ...renderProps, ...props, children: /*#__PURE__*/jsxRuntime.jsx(WithErrorWrap, { children: props.children }) }); } : WithErrorWrap; const render18Result = reactNative.renderHook(render, { ...options, wrapper }); const ret = Object.create(render18Result, { result: { value: { get current() { return render18Result.result.current === null ? undefined : render18Result.result.current; }, get error() { return error; } } }, rerender: { value: props => { renderProps = props; render18Result.rerender(props); } }, waitFor: { value: reactNative.waitFor }, waitForNextUpdate: { value: async options => { const previousCurrent = ret.result.current; const isMockTimers = typeof jest !== 'undefined' && setTimeout.clock != null && typeof setTimeout.clock.Date === 'function'; if (isMockTimers) { jest.runOnlyPendingTimers(); jest.useRealTimers(); } await reactNative.waitFor(() => { if (!error && ret.result.current === previousCurrent) { throw new Error('timeout in waitForNextUpdate'); } }, options); if (isMockTimers) jest.useFakeTimers(); } } }); return ret; } Object.defineProperty(exports, "act", { enumerable: true, get: function () { return reactNative.act; } }); exports.render18Wrapper = render18Wrapper;