Skip to content

Commit

Permalink
chore: properly ignore React.act type
Browse files Browse the repository at this point in the history
  • Loading branch information
thebuilder committed Dec 30, 2024
1 parent 6e459f2 commit d4b62fd
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions src/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ let isMocking = false;

const observers = new Map<IntersectionObserver, Item>();

// If we are running in a valid testing environment, we can mock the IntersectionObserver.
/*
** If we are running in a valid testing environment, we can automate mocking the IntersectionObserver.
*/
if (
typeof window !== "undefined" &&
typeof beforeAll !== "undefined" &&
Expand All @@ -31,6 +33,23 @@ if (
});
}

function getActFn() {
if (
!(
typeof window !== "undefined" &&
// @ts-ignore
window.IS_REACT_ACT_ENVIRONMENT
)
) {
return undefined;
}

return typeof React.act === "function"
? // @ts-ignore - Older versions of React don't have the `act` method, so TypeScript will complain about it
React.act
: DeprecatedReactTestUtils.act;
}

function warnOnMissingSetup() {
if (isMocking) return;
console.error(
Expand Down Expand Up @@ -103,23 +122,6 @@ export function resetIntersectionMocking() {
observers.clear();
}

function getActFn() {
if (
!(
typeof window !== "undefined" &&
// @ts-ignore
window.IS_REACT_ACT_ENVIRONMENT
)
) {
return undefined;
}

// @ts-ignore - Older versions of React don't have the `act` method, so TypeScript will complain about it
return typeof React.act === "function"
? React.act
: DeprecatedReactTestUtils.act;
}

function triggerIntersection(
elements: Element[],
trigger: boolean | number,
Expand Down

0 comments on commit d4b62fd

Please sign in to comment.