@data-client/test
Version:
Testing utilities for Data Client
105 lines (100 loc) • 2.88 kB
JavaScript
'use client';
;
var react$2 = require('@data-client/react');
var react$1 = require('react');
var jsxRuntime = require('react/jsx-runtime');
var react = require('@testing-library/react');
/**
* Provides an abstraction over react 17 and 18 compatible libraries
*/
function render18Wrapper(render, options) {
var _SetUndefined;
let renderProps = options == null ? void 0 : 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$1.Suspense, {
fallback: _SetUndefined || (_SetUndefined = /*#__PURE__*/jsxRuntime.jsx(SetUndefined, {})),
children: /*#__PURE__*/jsxRuntime.jsx(react$2.ErrorBoundary, {
fallbackComponent: ErrorFallback,
children: props.children
})
});
};
// TODO: add error and waitforupdate
const Wrapper = options == null ? void 0 : 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 = react.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: react.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 react.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 react.act; }
});
exports.render18Wrapper = render18Wrapper;