Skip to content

Commit

Permalink
fix: test utils, only use act when in an act environment
Browse files Browse the repository at this point in the history
  • Loading branch information
brantphoto committed Apr 7, 2024
1 parent e1391be commit 3c515e7
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 3c515e7

Please sign in to comment.