MCPcopy
hub / github.com/thebuilder/react-intersection-observer / setupIntersectionMocking

Function setupIntersectionMocking

src/test-utils.ts:113–146  ·  view source on GitHub ↗
(mockFn: typeof vi.fn)

Source from the content-addressed store, hash-verified

111 * @param mockFn The mock function to use. Defaults to `vi.fn`.
112 */
113export function setupIntersectionMocking(mockFn: typeof vi.fn) {
114 window.IntersectionObserver = mockFn(function IntersectionObserverMock(
115 this: IntersectionObserver,
116 cb,
117 options = {},
118 ) {
119 const item = {
120 callback: cb,
121 elements: new Set<Element>(),
122 created: Date.now(),
123 };
124 const instance: IntersectionObserver = {
125 thresholds: Array.isArray(options.threshold)
126 ? options.threshold
127 : [options.threshold ?? 0],
128 root: options.root ?? null,
129 rootMargin: options.rootMargin ?? "",
130 observe: mockFn((element: Element) => {
131 item.elements.add(element);
132 }),
133 unobserve: mockFn((element: Element) => {
134 item.elements.delete(element);
135 }),
136 disconnect: mockFn(() => {
137 observers.delete(instance);
138 }),
139 takeRecords: mockFn(),
140 };
141
142 observers.set(instance, item);
143
144 return instance;
145 });
146}
147
148/**
149 * Reset the IntersectionObserver mock to its initial state, and clear all the elements being observed.

Callers 2

setup.test.tsFile · 0.90
test-utils.tsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…