Skip to content

Commit

Permalink
fix: use isMockFunction to detect if the IntersectionObserver is be…
Browse files Browse the repository at this point in the history
…ing mocked
  • Loading branch information
thebuilder committed Jan 14, 2025
1 parent f098c1e commit 189133f
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,23 @@ type Item = {
created: number;
};

let isMocking = false;

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

/**
* Check if the IntersectionObserver is currently being mocked.
* @return boolean
*/
const isMocking = () => {
// @ts-ignore
if (typeof jest !== "undefined") {
// @ts-ignore
return jest.isMockFunction(window.IntersectionObserver);
}
if (typeof vi !== "undefined") {
return vi.isMockFunction(window.IntersectionObserver);
}
};

// Store a reference to the original `IntersectionObserver` so we can restore it later.
// This can be relevant if testing in a browser environment, where you actually have a native `IntersectionObserver`.
const originalIntersectionObserver =
Expand Down Expand Up @@ -57,7 +70,7 @@ function getActFn() {
}

function warnOnMissingSetup() {
if (isMocking) return;
if (isMocking()) return;
console.error(
`React Intersection Observer was not configured to handle mocking.
Outside Jest and Vitest, you might need to manually configure it by calling setupIntersectionMocking() and resetIntersectionMocking() in your test setup file.
Expand All @@ -82,7 +95,7 @@ afterEach(() => {
* @param mockFn The mock function to use. Defaults to `vi.fn`.
*/
export function setupIntersectionMocking(mockFn: typeof vi.fn) {
if (isMocking) return;
if (isMocking()) return;
window.IntersectionObserver = mockFn((cb, options = {}) => {
const item = {
callback: cb,
Expand Down Expand Up @@ -111,8 +124,6 @@ export function setupIntersectionMocking(mockFn: typeof vi.fn) {

return instance;
});

isMocking = true;
}

/**
Expand All @@ -137,7 +148,6 @@ export function destroyIntersectionMocking() {
resetIntersectionMocking();
// @ts-ignore
window.IntersectionObserver = originalIntersectionObserver;
isMocking = false;
}

function triggerIntersection(
Expand Down

0 comments on commit 189133f

Please sign in to comment.