Skip to content

Commit

Permalink
Merge pull request #668 from brantphoto/only-use-act-in-env
Browse files Browse the repository at this point in the history
  • Loading branch information
thebuilder authored Apr 8, 2024
2 parents e1391be + 3c515e7 commit 5b7c9ff
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/test-utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { act } from "react-dom/test-utils";

declare global {
// biome-ignore lint/style/noVar: Needs to be `var`, not `let` or `const`, for typing to work
var IS_REACT_ACT_ENVIRONMENT: boolean;
}
type Item = {
callback: IntersectionObserverCallback;
elements: Set<Element>;
Expand Down Expand Up @@ -93,6 +96,10 @@ export function resetIntersectionMocking() {
observers.clear();
}

function getIsReactActEnvironment() {
return Boolean(global.IS_REACT_ACT_ENVIRONMENT);
}

function triggerIntersection(
elements: Element[],
trigger: boolean | number,
Expand Down Expand Up @@ -148,7 +155,8 @@ function triggerIntersection(
}

// Trigger the IntersectionObserver callback with all the entries
if (act) act(() => item.callback(entries, observer));
if (act && getIsReactActEnvironment())
act(() => item.callback(entries, observer));
else item.callback(entries, observer);
}
/**
Expand Down

0 comments on commit 5b7c9ff

Please sign in to comment.