wix-style-react
Version:
wix-style-react
76 lines (74 loc) • 3.28 kB
JavaScript
;
exports.__esModule = true;
exports.act = void 0;
var testUtils = _interopRequireWildcard(require("react-dom/test-utils"));
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
/* This file is taken from @testing-library/react in order to solve act compatibility issues
resulting in following warning: The current testing environment is not configured to support act.
It's needed because we cannot use '@testing-library/react' in our testkits, as not all of our consumers use this library.
It's meant to be used only in ReactBase.js and should be removed once ReactBase is removed.
*/
var domAct = testUtils.act;
function getGlobalThis() {
if (typeof globalThis !== 'undefined') {
// eslint-disable-next-line no-undef
return globalThis;
}
if (typeof self !== 'undefined') {
return self;
}
if (typeof window !== 'undefined') {
return window;
}
if (typeof global !== 'undefined') {
return global;
}
throw new Error('unable to locate global object');
}
function setIsReactActEnvironment(isReactActEnvironment) {
getGlobalThis().IS_REACT_ACT_ENVIRONMENT = isReactActEnvironment;
}
function getIsReactActEnvironment() {
return getGlobalThis().IS_REACT_ACT_ENVIRONMENT;
}
function withGlobalActEnvironment(actImplementation) {
return callback => {
var previousActEnvironment = getIsReactActEnvironment();
setIsReactActEnvironment(true);
try {
// The return value of `act` is always a thenable.
var callbackNeedsToBeAwaited = false;
var actResult = actImplementation(() => {
var result = callback();
if (result !== null && typeof result === 'object' && typeof result.then === 'function') {
callbackNeedsToBeAwaited = true;
}
return result;
});
if (callbackNeedsToBeAwaited) {
var thenable = actResult;
return {
then: (resolve, reject) => {
thenable.then(returnValue => {
setIsReactActEnvironment(previousActEnvironment);
resolve(returnValue);
}, error => {
setIsReactActEnvironment(previousActEnvironment);
reject(error);
});
}
};
} else {
setIsReactActEnvironment(previousActEnvironment);
return actResult;
}
} catch (error) {
// Can't be a `finally {}` block since we don't know if we have to immediately restore IS_REACT_ACT_ENVIRONMENT
// or if we have to await the callback first.
setIsReactActEnvironment(previousActEnvironment);
throw error;
}
};
}
var act = exports.act = withGlobalActEnvironment(domAct);
//# sourceMappingURL=actCompat.js.map