(mockFn: typeof vi.fn)
| 111 | * @param mockFn The mock function to use. Defaults to `vi.fn`. |
| 112 | */ |
| 113 | export 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. |
no outgoing calls
no test coverage detected
searching dependent graphs…