Skip to content

Commit f3213dd

Browse files
authored
Merge pull request thebuilder#712 from thebuilder/fix/test-utils
* fix: ensure browser environment in test-utils
2 parents e41b9c0 + 120c483 commit f3213dd

File tree

1 file changed

+28
-19
lines changed

1 file changed

+28
-19
lines changed

src/test-utils.ts

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
import * as React from "react";
22
import * as DeprecatedReactTestUtils from "react-dom/test-utils";
33

4-
declare global {
5-
var IS_REACT_ACT_ENVIRONMENT: boolean;
6-
var jest: { fn: typeof vi.fn } | undefined;
7-
}
8-
9-
const act =
10-
// @ts-ignore - Older versions of React don't have the `act` method, so TypeScript will complain about it
11-
typeof React.act === "function" ? React.act : DeprecatedReactTestUtils.act;
12-
134
type Item = {
145
callback: IntersectionObserverCallback;
156
elements: Set<Element>;
@@ -20,10 +11,17 @@ let isMocking = false;
2011

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

23-
// If we are running in a valid testing environment, we can mock the IntersectionObserver.
24-
if (typeof beforeAll !== "undefined" && typeof afterEach !== "undefined") {
14+
/*
15+
** If we are running in a valid testing environment, we can automate mocking the IntersectionObserver.
16+
*/
17+
if (
18+
typeof window !== "undefined" &&
19+
typeof beforeAll !== "undefined" &&
20+
typeof afterEach !== "undefined"
21+
) {
2522
beforeAll(() => {
2623
// Use the exposed mock function. Currently, only supports Jest (`jest.fn`) and Vitest with globals (`vi.fn`).
24+
// @ts-ignore
2725
if (typeof jest !== "undefined") setupIntersectionMocking(jest.fn);
2826
else if (typeof vi !== "undefined") {
2927
setupIntersectionMocking(vi.fn);
@@ -35,6 +33,23 @@ if (typeof beforeAll !== "undefined" && typeof afterEach !== "undefined") {
3533
});
3634
}
3735

36+
function getActFn() {
37+
if (
38+
!(
39+
typeof window !== "undefined" &&
40+
// @ts-ignore
41+
window.IS_REACT_ACT_ENVIRONMENT
42+
)
43+
) {
44+
return undefined;
45+
}
46+
// @ts-ignore - Older versions of React don't have the `act` method, so TypeScript will complain about it
47+
return typeof React.act === "function"
48+
? // @ts-ignore
49+
React.act
50+
: DeprecatedReactTestUtils.act;
51+
}
52+
3853
function warnOnMissingSetup() {
3954
if (isMocking) return;
4055
console.error(
@@ -107,12 +122,6 @@ export function resetIntersectionMocking() {
107122
observers.clear();
108123
}
109124

110-
function getIsReactActEnvironment() {
111-
return Boolean(
112-
typeof window !== "undefined" && window.IS_REACT_ACT_ENVIRONMENT,
113-
);
114-
}
115-
116125
function triggerIntersection(
117126
elements: Element[],
118127
trigger: boolean | number,
@@ -168,8 +177,8 @@ function triggerIntersection(
168177
}
169178

170179
// Trigger the IntersectionObserver callback with all the entries
171-
if (act && getIsReactActEnvironment())
172-
act(() => item.callback(entries, observer));
180+
const act = getActFn();
181+
if (act) act(() => item.callback(entries, observer));
173182
else item.callback(entries, observer);
174183
}
175184
/**

0 commit comments

Comments
 (0)