From 3c515e73189f814546617e60c6030d3b11e1cb59 Mon Sep 17 00:00:00 2001 From: Brant Barger Date: Sun, 7 Apr 2024 21:13:25 +0000 Subject: [PATCH] fix: test utils, only use act when in an act environment --- src/test-utils.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/test-utils.ts b/src/test-utils.ts index 3acf9a20..accd4ed0 100644 --- a/src/test-utils.ts +++ b/src/test-utils.ts @@ -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; @@ -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, @@ -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); } /**